Skip to content

Commit

Permalink
Wallet rescan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
levoncrypto committed Aug 8, 2024
1 parent 3363b32 commit 31589ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 47 deletions.
7 changes: 0 additions & 7 deletions src/qt/forms/recover.ui
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,6 @@ Also you can choose wallet birth date for more faster and optimised wallet scan.
</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">
Expand Down
22 changes: 5 additions & 17 deletions src/qt/recover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Recover::Recover(QWidget *parent) :
connect(this, &Recover::stopThread, thread, &QThread::quit);
thread->start();

connect(ui->enableDateSelection, &QCheckBox::toggled, this, &Recover::updateDateInputState);
ui->dateInput->setDisplayFormat("dd-MM-yyyy");
ui->dateInput->setMinimumDate(QDate(2019, 12, 11));
}
Expand All @@ -53,7 +52,6 @@ void Recover::setCreateNew()
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 @@ -62,11 +60,6 @@ void Recover::setCreateNew()
ui->mnemonicPassPhrase2->setEnabled(false);
}

void Recover::updateDateInputState(bool checked) {
ui->dateInput->setEnabled(checked);
ui->dateInput->setMinimumDate(QDate(2019, 12, 11));
}

void Recover::on_createNew_clicked()
{
setCreateNew();
Expand All @@ -79,8 +72,7 @@ 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);
ui->dateInput->setEnabled(true);
ui->dateInput->setDisplayFormat("dd-MM-yyyy");
ui->dateInput->setDate(ui->dateInput->minimumDate());
}
Expand Down Expand Up @@ -125,14 +117,10 @@ 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()) {
QDate date = recover.ui->dateInput->date();
QDate newDate = date.addDays(-1);
recover.ui->dateInput->setDate(newDate);
SoftSetArg("-wcdate", recover.ui->dateInput->text().toStdString());
} else {
SoftSetArg("-wcdate", "");
}
QDate date = recover.ui->dateInput->date();
QDate newDate = date.addDays(-1);
recover.ui->dateInput->setDate(newDate);
SoftSetArg("-wcdate", recover.ui->dateInput->text().toStdString());
const char* str = mnemonic.c_str();
bool space = true;
int n = 0;
Expand Down
1 change: 0 additions & 1 deletion src/qt/recover.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ 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
50 changes: 29 additions & 21 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2371,19 +2371,18 @@ static std::time_t parseDate(const std::string& dateStr) {
return std::mktime(&tm);
}

int CWallet::GetBlockHeightByDate(CBlockIndex* pindexStart, const std::string& dateStr) {
CBlockIndex* CWallet::GetBlockByDate(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;
return chainActive[pindex->nHeight - 200];
}
pindex = chainActive.Next(pindex);
}

return chainActive.Tip()->nHeight;
return chainActive[chainActive.Tip()->nHeight];
}

/**
Expand All @@ -2404,27 +2403,36 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex *pindexStart, bool f
CBlockIndex* pindex = pindexStart;
{
LOCK2(cs_main, cs_wallet);

// no need to read and scan block, if block was created before
// 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
int targetHeight = 0;
// No need to read and scan block if block was created before our wallet birthday (as adjusted for block time variability).
// If you are recovering wallet with mnemonics, start rescan from the block when mnemonics were implemented in Firo.
// If the user provides a date, start scanning from the block that corresponds to that date.
// If no date is provided, start scanning from the mnemonic start block.

std::string wcdate = GetArg("-wcdate", "");
CBlockIndex* mnemonicStartBlock = chainActive[chainParams.GetConsensus().nMnemonicBlock];
CBlockIndex* dateBlockPindex = pindexStart;
if (!wcdate.empty()) {
dateBlockPindex = GetBlockByDate(mnemonicStartBlock, wcdate);
if (dateBlockPindex->nHeight < chainParams.GetConsensus().nMnemonicBlock) {
dateBlockPindex = chainActive[chainParams.GetConsensus().nMnemonicBlock];
}
} else {
dateBlockPindex = mnemonicStartBlock;
}
pindex = dateBlockPindex;
if (fRecoverMnemonic) {
std::string wcdate = GetArg("-wcdate", "");
CBlockIndex* mnemonicStartBlock = chainActive[chainParams.GetConsensus().nMnemonicBlock];
targetHeight = GetBlockHeightByDate(mnemonicStartBlock, wcdate);
if (pindex == nullptr)
pindex = chainActive.Tip();
} else {
if (!wcdate.empty()) {
if (targetHeight < chainParams.GetConsensus().nMnemonicBlock) {
targetHeight = chainParams.GetConsensus().nMnemonicBlock;
}
pindex = chainActive[targetHeight];
pindex = dateBlockPindex;
} else {
pindex = mnemonicStartBlock;
while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200)))
pindex = chainActive.Next(pindex);
}
} else
while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200)))
pindex = chainActive.Next(pindex);
LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height(), targetHeight);
}

LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height(), pindex->nHeight);
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
double dProgressStart = GuessVerificationProgress(chainParams.TxData(), pindex);
double dProgressTip = GuessVerificationProgress(chainParams.TxData(), chainActive.Tip());
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +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);
CBlockIndex* GetBlockByDate(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 31589ba

Please sign in to comment.