Skip to content

Commit 8d4a058

Browse files
committed
Merge bitcoin/bitcoin#23997: wallet: avoid rescans under assumed-valid blocks
817326a wallet: avoid rescans if under the snapshot (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11) (parent PR: #15606) --- Refuse to load a wallet if it requires a rescan lower than the height of assumed-valid blocks. Of course in live code right now, `BLOCK_ASSUMED_VALID` block index entries don't exist since they're a unique flag introduced by the use of UTXO snapshots, so this is prophylactic code exercised only by unittests. ACKs for top commit: achow101: ACK 817326a ryanofsky: Code review ACK 817326a. This seems like the simplest change we can make to avoid wallet problems when an assumeutxo snapshot is loaded. Tree-SHA512: cfa44b2eb33d1818d30df45210d0dde1e9b78cc9b7c88cb985054dc28427bba9e0905debe4196065d1d3a5ce7bca7e605e629d5ce5f0225b25395746e6d3d596
2 parents 4aaa3b5 + 817326a commit 8d4a058

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/interfaces/chain.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ class Chain
287287
//! to be prepared to handle this by ignoring notifications about unknown
288288
//! removed transactions and already added new transactions.
289289
virtual void requestMempoolTransactions(Notifications& notifications) = 0;
290+
291+
//! Return true if an assumed-valid chain is in use.
292+
virtual bool hasAssumedValidChain() = 0;
290293
};
291294

292295
//! Interface to let node manage chain clients (wallets, or maybe tools for

src/node/interfaces.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,11 @@ class ChainImpl : public Chain
775775
notifications.transactionAddedToMempool(entry.GetSharedTx(), 0 /* mempool_sequence */);
776776
}
777777
}
778+
bool hasAssumedValidChain() override
779+
{
780+
return Assert(m_node.chainman)->IsSnapshotActive();
781+
}
782+
778783
NodeContext& m_node;
779784
};
780785
} // namespace

src/wallet/wallet.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3049,20 +3049,31 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
30493049

30503050
if (tip_height && *tip_height != rescan_height)
30513051
{
3052-
if (chain.havePruned()) {
3052+
// Technically we could execute the code below in any case, but performing the
3053+
// `while` loop below can make startup very slow, so only check blocks on disk
3054+
// if necessary.
3055+
if (chain.havePruned() || chain.hasAssumedValidChain()) {
30533056
int block_height = *tip_height;
30543057
while (block_height > 0 && chain.haveBlockOnDisk(block_height - 1) && rescan_height != block_height) {
30553058
--block_height;
30563059
}
30573060

30583061
if (rescan_height != block_height) {
3059-
// We can't rescan beyond non-pruned blocks, stop and throw an error.
3062+
// We can't rescan beyond blocks we don't have data for, stop and throw an error.
30603063
// This might happen if a user uses an old wallet within a pruned node
30613064
// or if they ran -disablewallet for a longer time, then decided to re-enable
30623065
// Exit early and print an error.
3066+
// It also may happen if an assumed-valid chain is in use and therefore not
3067+
// all block data is available.
30633068
// If a block is pruned after this check, we will load the wallet,
30643069
// but fail the rescan with a generic error.
3065-
error = _("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)");
3070+
3071+
error = chain.hasAssumedValidChain() ?
3072+
_(
3073+
"Assumed-valid: last wallet synchronisation goes beyond "
3074+
"available block data. You need to wait for the background "
3075+
"validation chain to download more blocks.") :
3076+
_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)");
30663077
return false;
30673078
}
30683079
}

0 commit comments

Comments
 (0)