Skip to content

Commit 817326a

Browse files
committed
wallet: avoid rescans if under the snapshot
Refuse to load a wallet if it requires a rescan lower than the height of an unvalidated snapshot we're running -- in more general terms, if we don't have data for the blocks.
1 parent d0bf9bb commit 817326a

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
@@ -286,6 +286,9 @@ class Chain
286286
//! to be prepared to handle this by ignoring notifications about unknown
287287
//! removed transactions and already added new transactions.
288288
virtual void requestMempoolTransactions(Notifications& notifications) = 0;
289+
290+
//! Return true if an assumed-valid chain is in use.
291+
virtual bool hasAssumedValidChain() = 0;
289292
};
290293

291294
//! 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
@@ -723,6 +723,11 @@ class ChainImpl : public Chain
723723
notifications.transactionAddedToMempool(entry.GetSharedTx(), 0 /* mempool_sequence */);
724724
}
725725
}
726+
bool hasAssumedValidChain() override
727+
{
728+
return Assert(m_node.chainman)->IsSnapshotActive();
729+
}
730+
726731
NodeContext& m_node;
727732
};
728733
} // namespace

src/wallet/wallet.cpp

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

29042904
if (tip_height && *tip_height != rescan_height)
29052905
{
2906-
if (chain.havePruned()) {
2906+
// Technically we could execute the code below in any case, but performing the
2907+
// `while` loop below can make startup very slow, so only check blocks on disk
2908+
// if necessary.
2909+
if (chain.havePruned() || chain.hasAssumedValidChain()) {
29072910
int block_height = *tip_height;
29082911
while (block_height > 0 && chain.haveBlockOnDisk(block_height - 1) && rescan_height != block_height) {
29092912
--block_height;
29102913
}
29112914

29122915
if (rescan_height != block_height) {
2913-
// We can't rescan beyond non-pruned blocks, stop and throw an error.
2916+
// We can't rescan beyond blocks we don't have data for, stop and throw an error.
29142917
// This might happen if a user uses an old wallet within a pruned node
29152918
// or if they ran -disablewallet for a longer time, then decided to re-enable
29162919
// Exit early and print an error.
2920+
// It also may happen if an assumed-valid chain is in use and therefore not
2921+
// all block data is available.
29172922
// If a block is pruned after this check, we will load the wallet,
29182923
// but fail the rescan with a generic error.
2919-
error = _("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)");
2924+
2925+
error = chain.hasAssumedValidChain() ?
2926+
_(
2927+
"Assumed-valid: last wallet synchronisation goes beyond "
2928+
"available block data. You need to wait for the background "
2929+
"validation chain to download more blocks.") :
2930+
_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)");
29202931
return false;
29212932
}
29222933
}

0 commit comments

Comments
 (0)