Skip to content

Commit

Permalink
Wallet scan optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
levoncrypto committed Aug 5, 2024
1 parent c0ddba2 commit ee6456a
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 21 deletions.
12 changes: 11 additions & 1 deletion src/qt/forms/notifymnemonic.ui
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,21 @@
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="walletBirthDate">
<property name="text">
<string></string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="textLabel2">
<property name="text">
<string>Please write down these recovery seed phrase and keep them in a secure location.</string>
<string>Please write down these recovery seed phrase and wallet birth date and keep them in a secure location. Specifying the wallet's birth date when restoring your wallet speeds up and optimizes the scanning of your wallet.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
Expand Down
67 changes: 50 additions & 17 deletions src/qt/forms/recover.ui
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ You can also choose to further encrypt your recovery seed phrase with an additio
You will need to save this passphrase as well with the recovery seed phrase. Failing to save the passphrase will lead to your funds being irrecoverable.

If you have an existing recovery seed phrase, please select "Recover existing wallet". If you have secured your recovery seed phrase with an additional passphrase, enter it too.
Also you can choose wallet birth date for more faster and optimised wallet scan.
</string>
</property>
<property name="wordWrap">
Expand Down Expand Up @@ -134,20 +135,58 @@ If you have an existing recovery seed phrase, please select "Recover existing wa
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout2">
<layout class="QVBoxLayout" name="verticalLayout2">
<item>
<widget class="QLabel" name="textLabel2">
<property name="text">
<string>Input recovery seed phrase here:</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout2">
<item>
<widget class="QLabel" name="textLabel2">
<property name="text">
<string>Input recovery seed phrase here:</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mnemonicWords"/>
</item>
</layout>
</item>
<item>
<widget class="QLineEdit" name="mnemonicWords"/>
</item>
<layout class="QHBoxLayout" name="horizontalLayoutDate">
<item>
<widget class="QLabel" name="textLabelDate">
<property name="text">
<string>Choose the wallet creation date:</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="enableDateSelection">
<property name="text">
<string>Enable Date Selection</string>
</property>
</widget>
</item>
<item>
<widget class="QDateEdit" name="dateInput">
<property name="calendarPopup">
<bool>true</bool>
</property>
<property name="displayFormat">
<string>dd-MM-yyyy</string>
</property>
<property name="date">
</property>
</widget>
</item>
</layout>
</item>

</layout>
</item>
<item>
Expand Down Expand Up @@ -234,12 +273,6 @@ If you have an existing recovery seed phrase, please select "Recover existing wa
</item>
<item>
<widget class="QLabel" name="errorMessage">
<property name="text">
<string/>
</property>
<property name="styleSheet">
<string notr="true">color:red;</string>
</property>
<property name="text">
<string/>
</property>
Expand Down
6 changes: 6 additions & 0 deletions src/qt/notifymnemonic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QSettings>
#include <QMessageBox>
#include <QAbstractButton>
#include <QDate>

NotifyMnemonic::NotifyMnemonic(QWidget *parent) :
QWizard(parent),
Expand All @@ -36,6 +37,10 @@ void NotifyMnemonic::cancelEvent()
}
}

QString getCurrentDate() {
return QDate::currentDate().toString("dd-MM-yyyy");
}

void NotifyMnemonic::notify()
{
#ifdef ENABLE_WALLET
Expand All @@ -44,6 +49,7 @@ void NotifyMnemonic::notify()
NotifyMnemonic notify;
notify.setWindowIcon(QIcon(":icons/firo"));
notify.show();
notify.ui->walletBirthDate->setText("Wallet creation date: " + getCurrentDate());
notify.ui->mnemonic->setText(mnemonic.c_str());
notify.restart();
while(true)
Expand Down
20 changes: 20 additions & 0 deletions src/qt/recover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Recover::Recover(QWidget *parent) :

connect(this, &Recover::stopThread, thread, &QThread::quit);
thread->start();

connect(ui->enableDateSelection, &QCheckBox::toggled, this, &Recover::updateDateInputState);
}

Recover::~Recover()
Expand All @@ -47,6 +49,9 @@ void Recover::setCreateNew()
ui->textLabel2->setEnabled(false);
ui->mnemonicWords->setEnabled(false);
ui->mnemonicWords->clear();
ui->dateInput->setEnabled(false);
ui->dateInput->clear();
ui->enableDateSelection->setEnabled(false);
ui->use24->setChecked(true);
ui->usePassphrase->setChecked(false);
ui->textLabel3->setEnabled(false);
Expand All @@ -55,6 +60,13 @@ void Recover::setCreateNew()
ui->mnemonicPassPhrase2->setEnabled(false);
}

void Recover::updateDateInputState(bool checked) {
ui->dateInput->setEnabled(checked);
if (checked) {
ui->dateInput->setMinimumDate(QDate(2020, 3, 23));
}
}

void Recover::on_createNew_clicked()
{
setCreateNew();
Expand All @@ -64,6 +76,9 @@ void Recover::on_recoverExisting_clicked()
{
ui->textLabel2->setEnabled(true);
ui->mnemonicWords->setEnabled(true);
ui->dateInput->setEnabled(true);
ui->dateInput->setEnabled(ui->enableDateSelection->isChecked());
ui->enableDateSelection->setEnabled(true);
}

void Recover::on_usePassphrase_clicked()
Expand Down Expand Up @@ -106,6 +121,11 @@ bool Recover::askRecover(bool& newWallet)
if(recover.ui->recoverExisting->isChecked()) {
newWallet = false;
std::string mnemonic = recover.ui->mnemonicWords->text().toStdString();
if (recover.ui->enableDateSelection->isChecked()) {
SoftSetArg("-wcdate", recover.ui->dateInput->text().toStdString());
} else {
SoftSetArg("-wcdate", "");
}
const char* str = mnemonic.c_str();
bool space = true;
int n = 0;
Expand Down
1 change: 1 addition & 0 deletions src/qt/recover.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Recover : public QDialog
void stopThread();

private Q_SLOTS:
void updateDateInputState(bool checked);
void on_createNew_clicked();
void on_recoverExisting_clicked();
void on_usePassphrase_clicked();
Expand Down
36 changes: 33 additions & 3 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,28 @@ void CWalletTx::GetAmounts(std::list<COutputEntry>& listReceived,

}

static std::time_t parseDate(const std::string& dateStr) {
std::tm tm = {};
std::istringstream ss(dateStr);
ss >> std::get_time(&tm, "%d-%m-%Y");
return std::mktime(&tm);
}

int CWallet::GetBlockHeightByDate(CBlockIndex* pindexStart, const std::string& dateStr) {
std::time_t targetTimestamp = parseDate(dateStr);

CBlockIndex* pindex = pindexStart;

while (pindex) {
if (pindex->GetBlockTime() > targetTimestamp) {
return pindex->nHeight - 200;
}
pindex = chainActive.Next(pindex);
}

return chainActive.Tip()->nHeight;
}

/**
* Scan the block chain (starting in pindexStart) for transactions
* from or to us. If fUpdate is true, found transactions that already
Expand All @@ -2387,9 +2409,17 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex *pindexStart, bool f
// our wallet birthday (as adjusted for block time variability)
// if you are recovering wallet with mnemonics start rescan from block when mnemonics implemented in Firo
if (fRecoverMnemonic) {
pindex = chainActive[chainParams.GetConsensus().nMnemonicBlock];
if (pindex == NULL)
pindex = chainActive.Tip();
std::string wcdate = GetArg("-wcdate", "");
CBlockIndex* mnemonicStartBlock = chainActive[chainParams.GetConsensus().nMnemonicBlock];
if (!wcdate.empty()) {
int targetHeight = GetBlockHeightByDate(mnemonicStartBlock, wcdate);
if (targetHeight <= 0) {
targetHeight = chainParams.GetConsensus().nMnemonicBlock;
}
pindex = chainActive[targetHeight];
} else {
pindex = mnemonicStartBlock;
}
} else
while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200)))
pindex = chainActive.Next(pindex);
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
void SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, int posInBlock) override;
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate);
CBlockIndex* ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false, bool fRecoverMnemonic = false);
int GetBlockHeightByDate(CBlockIndex* pindexStart, const std::string& dateStr);
void ReacceptWalletTransactions();
void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) override;
std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime, CConnman* connman);
Expand Down

0 comments on commit ee6456a

Please sign in to comment.