Skip to content

Commit 4586ae2

Browse files
committed
Merge bitcoin/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 #26655 ACKs for top commit: ishaanam: re-utACK 3784009 w0xlt: utACK bitcoin/bitcoin@3784009 furszy: Code review ACK 3784009 Tree-SHA512: f0d90b62940d97d50f21e1e01fa6dcb54409fad819cea4283612825c4d93d733df323cd92787fed43956b0a8e386a5bf88218f1f5749c913398667a5c8f54470
2 parents 68f88bc + 3784009 commit 4586ae2

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/wallet/wallet.cpp

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

31843184
if (tip_height && *tip_height != rescan_height)
31853185
{
3186+
// No need to read and scan block if block was created before
3187+
// our wallet birthday (as adjusted for block time variability)
3188+
std::optional<int64_t> time_first_key;
3189+
for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) {
3190+
int64_t time = spk_man->GetTimeFirstKey();
3191+
if (!time_first_key || time < *time_first_key) time_first_key = time;
3192+
}
3193+
if (time_first_key) {
3194+
FoundBlock found = FoundBlock().height(rescan_height);
3195+
chain.findFirstBlockWithTimeAndHeight(*time_first_key - TIMESTAMP_WINDOW, rescan_height, found);
3196+
if (!found.found) {
3197+
// We were unable to find a block that had a time more recent than our earliest timestamp
3198+
// or a height higher than the wallet was synced to, indicating that the wallet is newer than the
3199+
// current chain tip. Skip rescanning in this case.
3200+
rescan_height = *tip_height;
3201+
}
3202+
}
3203+
31863204
// Technically we could execute the code below in any case, but performing the
31873205
// `while` loop below can make startup very slow, so only check blocks on disk
31883206
// if necessary.
@@ -3217,17 +3235,6 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
32173235
chain.initMessage(_("Rescanning…").translated);
32183236
walletInstance->WalletLogPrintf("Rescanning last %i blocks (from block %i)...\n", *tip_height - rescan_height, rescan_height);
32193237

3220-
// No need to read and scan block if block was created before
3221-
// our wallet birthday (as adjusted for block time variability)
3222-
std::optional<int64_t> time_first_key;
3223-
for (auto spk_man : walletInstance->GetAllScriptPubKeyMans()) {
3224-
int64_t time = spk_man->GetTimeFirstKey();
3225-
if (!time_first_key || time < *time_first_key) time_first_key = time;
3226-
}
3227-
if (time_first_key) {
3228-
chain.findFirstBlockWithTimeAndHeight(*time_first_key - TIMESTAMP_WINDOW, rescan_height, FoundBlock().height(rescan_height));
3229-
}
3230-
32313238
{
32323239
WalletRescanReserver reserver(*walletInstance);
32333240
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)