Skip to content

Commit 0bed3e3

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 f00dec1 commit 0bed3e3

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

src/wallet/wallet.cpp

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

32823282
if (tip_height && *tip_height != rescan_height)
32833283
{
3284+
// No need to read and scan block if block was created before
3285+
// our wallet birthday (as adjusted for block time variability)
3286+
std::optional<int64_t> time_first_key;
3287+
for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) {
3288+
int64_t time = spk_man->GetTimeFirstKey();
3289+
if (!time_first_key || time < *time_first_key) time_first_key = time;
3290+
}
3291+
if (time_first_key) {
3292+
FoundBlock found = FoundBlock().height(rescan_height);
3293+
chain.findFirstBlockWithTimeAndHeight(*time_first_key - TIMESTAMP_WINDOW, rescan_height, found);
3294+
if (!found.found) {
3295+
// We were unable to find a block that had a time more recent than our earliest timestamp
3296+
// or a height higher than the wallet was synced to, indicating that the wallet is newer than the
3297+
// current chain tip. Skip rescanning in this case.
3298+
rescan_height = *tip_height;
3299+
}
3300+
}
3301+
32843302
// Technically we could execute the code below in any case, but performing the
32853303
// `while` loop below can make startup very slow, so only check blocks on disk
32863304
// if necessary.
@@ -3315,20 +3333,6 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
33153333
chain.initMessage(_("Rescanning…").translated);
33163334
walletInstance->WalletLogPrintf("Rescanning last %i blocks (from block %i)...\n", *tip_height - rescan_height, rescan_height);
33173335

3318-
// No need to read and scan block if block was created before
3319-
// our wallet birthday (as adjusted for block time variability)
3320-
// unless a full rescan was requested
3321-
if (gArgs.GetIntArg("-rescan", 0) != 2) {
3322-
std::optional<int64_t> time_first_key;
3323-
for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) {
3324-
int64_t time = spk_man->GetTimeFirstKey();
3325-
if (!time_first_key || time < *time_first_key) time_first_key = time;
3326-
}
3327-
if (time_first_key) {
3328-
chain.findFirstBlockWithTimeAndHeight(*time_first_key - TIMESTAMP_WINDOW, rescan_height, FoundBlock().height(rescan_height));
3329-
}
3330-
}
3331-
33323336
{
33333337
WalletRescanReserver reserver(*walletInstance);
33343338
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)