Skip to content

Commit de72b9a

Browse files
achow101vijaydasmp
authored andcommitted
Merge bitcoin#26679: wallet: Skip rescanning if wallet is more recent than tip
3784009 wallet: Skip rescanning if wallet is more recent than tip (Andrew Chow) Pull request description: If a wallet has key birthdates that are more recent than the currrent chain tip, or a bestblock height higher than the current tip, we should not attempt to rescan as there is nothing to scan for. Fixes bitcoin#26655 ACKs for top commit: ishaanam: re-utACK 3784009 w0xlt: utACK bitcoin@3784009 furszy: Code review ACK 3784009 Tree-SHA512: f0d90b62940d97d50f21e1e01fa6dcb54409fad819cea4283612825c4d93d733df323cd92787fed43956b0a8e386a5bf88218f1f5749c913398667a5c8f54470
1 parent 402c903 commit de72b9a

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

src/wallet/wallet.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,6 +3363,24 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
33633363

33643364
if (tip_height && *tip_height != rescan_height)
33653365
{
3366+
// No need to read and scan block if block was created before
3367+
// our wallet birthday (as adjusted for block time variability)
3368+
std::optional<int64_t> time_first_key;
3369+
for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) {
3370+
int64_t time = spk_man->GetTimeFirstKey();
3371+
if (!time_first_key || time < *time_first_key) time_first_key = time;
3372+
}
3373+
if (time_first_key) {
3374+
FoundBlock found = FoundBlock().height(rescan_height);
3375+
chain.findFirstBlockWithTimeAndHeight(*time_first_key - TIMESTAMP_WINDOW, rescan_height, found);
3376+
if (!found.found) {
3377+
// We were unable to find a block that had a time more recent than our earliest timestamp
3378+
// or a height higher than the wallet was synced to, indicating that the wallet is newer than the
3379+
// current chain tip. Skip rescanning in this case.
3380+
rescan_height = *tip_height;
3381+
}
3382+
}
3383+
33663384
// Technically we could execute the code below in any case, but performing the
33673385
// `while` loop below can make startup very slow, so only check blocks on disk
33683386
// if necessary.
@@ -3396,21 +3414,9 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
33963414

33973415
chain.initMessage(_("Rescanning…").translated);
33983416
walletInstance->WalletLogPrintf("Rescanning last %i blocks (from block %i)...\n", *tip_height - rescan_height, rescan_height);
3399-
3400-
// No need to read and scan block if block was created before
3401-
// our wallet birthday (as adjusted for block time variability)
34023417
// unless a full rescan was requested
34033418
if (gArgs.GetIntArg("-rescan", 0) != 2) {
3404-
std::optional<int64_t> time_first_key;
3405-
for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) {
3406-
int64_t time = spk_man->GetTimeFirstKey();
3407-
if (!time_first_key || time < *time_first_key) time_first_key = time;
3408-
}
3409-
if (time_first_key) {
3410-
chain.findFirstBlockWithTimeAndHeight(*time_first_key - TIMESTAMP_WINDOW, rescan_height, FoundBlock().height(rescan_height));
3411-
}
34123419
}
3413-
34143420
{
34153421
WalletRescanReserver reserver(*walletInstance);
34163422
if (!reserver.reserve() || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions(chain.getBlockHash(rescan_height), rescan_height, /*max_height=*/{}, reserver, /*fUpdate=*/true, /*save_progress=*/true).status)) {

0 commit comments

Comments
 (0)