Skip to content

Commit bbdcc0b

Browse files
author
MarcoFalke
committed
Merge #15342: Suggested wallet code cleanups from #14711
aebafd0 Rename Chain getLocator -> getTipLocator (Russell Yanofsky) 2c1fbaa Drop redundant get_value_or (Russell Yanofsky) 84adb20 Fix ScanForWalletTransactions start_block comment (Russell Yanofsky) 2efa66b Document rescanblockchain returned stop_height being null (Russell Yanofsky) db2d093 Add suggested rescanblockchain comments (Russell Yanofsky) a8d645c Update ScanForWalletTransactions result comment (Russell Yanofsky) 95a812b Rename ScanResult stop_block field (Russell Yanofsky) Pull request description: This implements suggested changes from #14711 review comments that didn't make make it in before merging. There are no changes in behavior in this PR, just documentation updates, simplifications, and variable renames. Tree-SHA512: 39f1a5718195732b70b5e427c3b3e4295ea5af6328a5991763a422051212dfb95383186db0c0504ce2c2782fb61998dfd2fe9851645b7cb4e75d849049483cc8
2 parents e508535 + aebafd0 commit bbdcc0b

File tree

7 files changed

+49
-42
lines changed

7 files changed

+49
-42
lines changed

src/interfaces/chain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class LockImpl : public Chain::Lock
123123
CBlockIndex* block = LookupBlockIndex(hash);
124124
return block && block->GetAncestor(::chainActive.Height()) == ::chainActive.Tip();
125125
}
126-
CBlockLocator getLocator() override { return ::chainActive.GetLocator(); }
126+
CBlockLocator getTipLocator() override { return ::chainActive.GetLocator(); }
127127
Optional<int> findLocatorFork(const CBlockLocator& locator) override
128128
{
129129
LockAnnotation lock(::cs_main);

src/interfaces/chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class Chain
9696
virtual bool isPotentialTip(const uint256& hash) = 0;
9797

9898
//! Get locator for the current chain tip.
99-
virtual CBlockLocator getLocator() = 0;
99+
virtual CBlockLocator getTipLocator() = 0;
100100

101101
//! Return height of the latest block common to locator and chain, which
102102
//! is guaranteed to be an ancestor of the block used to create the

src/qt/test/wallettests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ void TestGUI()
149149
reserver.reserve();
150150
CWallet::ScanResult result = wallet->ScanForWalletTransactions(locked_chain->getBlockHash(0), {} /* stop_block */, reserver, true /* fUpdate */);
151151
QCOMPARE(result.status, CWallet::ScanResult::SUCCESS);
152-
QCOMPARE(result.stop_block, chainActive.Tip()->GetBlockHash());
153-
QVERIFY(result.failed_block.IsNull());
152+
QCOMPARE(result.last_scanned_block, chainActive.Tip()->GetBlockHash());
153+
QVERIFY(result.last_failed_block.IsNull());
154154
}
155155
wallet->SetBroadcastTransactions(true);
156156

src/wallet/rpcwallet.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,8 +3412,8 @@ UniValue rescanblockchain(const JSONRPCRequest& request)
34123412
},
34133413
RPCResult{
34143414
"{\n"
3415-
" \"start_height\" (numeric) The block height where the rescan has started.\n"
3416-
" \"stop_height\" (numeric) The height of the last rescanned block.\n"
3415+
" \"start_height\" (numeric) The block height where the rescan started (the requested height or 0)\n"
3416+
" \"stop_height\" (numeric) The height of the last rescanned block. May be null in rare cases if there was a reorg and the call didn't scan any blocks because they were already scanned in the background.\n"
34173417
"}\n"
34183418
},
34193419
RPCExamples{
@@ -3459,6 +3459,11 @@ UniValue rescanblockchain(const JSONRPCRequest& request)
34593459

34603460
if (tip_height) {
34613461
start_block = locked_chain->getBlockHash(start_height);
3462+
// If called with a stop_height, set the stop_height here to
3463+
// trigger a rescan to that height.
3464+
// If called without a stop height, leave stop_height as null here
3465+
// so rescan continues to the tip (even if the tip advances during
3466+
// rescan).
34623467
if (stop_height) {
34633468
stop_block = locked_chain->getBlockHash(*stop_height);
34643469
}
@@ -3478,7 +3483,7 @@ UniValue rescanblockchain(const JSONRPCRequest& request)
34783483
}
34793484
UniValue response(UniValue::VOBJ);
34803485
response.pushKV("start_height", start_height);
3481-
response.pushKV("stop_height", result.stop_height ? *result.stop_height : UniValue());
3486+
response.pushKV("stop_height", result.last_scanned_height ? *result.last_scanned_height : UniValue());
34823487
return response;
34833488
}
34843489

src/wallet/test/wallet_tests.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
5454
reserver.reserve();
5555
CWallet::ScanResult result = wallet.ScanForWalletTransactions({} /* start_block */, {} /* stop_block */, reserver, false /* update */);
5656
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
57-
BOOST_CHECK(result.failed_block.IsNull());
58-
BOOST_CHECK(result.stop_block.IsNull());
59-
BOOST_CHECK(!result.stop_height);
57+
BOOST_CHECK(result.last_failed_block.IsNull());
58+
BOOST_CHECK(result.last_scanned_block.IsNull());
59+
BOOST_CHECK(!result.last_scanned_height);
6060
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 0);
6161
}
6262

@@ -69,9 +69,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
6969
reserver.reserve();
7070
CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
7171
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
72-
BOOST_CHECK(result.failed_block.IsNull());
73-
BOOST_CHECK_EQUAL(result.stop_block, newTip->GetBlockHash());
74-
BOOST_CHECK_EQUAL(*result.stop_height, newTip->nHeight);
72+
BOOST_CHECK(result.last_failed_block.IsNull());
73+
BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
74+
BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
7575
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 100 * COIN);
7676
}
7777

@@ -88,9 +88,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
8888
reserver.reserve();
8989
CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
9090
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
91-
BOOST_CHECK_EQUAL(result.failed_block, oldTip->GetBlockHash());
92-
BOOST_CHECK_EQUAL(result.stop_block, newTip->GetBlockHash());
93-
BOOST_CHECK_EQUAL(*result.stop_height, newTip->nHeight);
91+
BOOST_CHECK_EQUAL(result.last_failed_block, oldTip->GetBlockHash());
92+
BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
93+
BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
9494
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 50 * COIN);
9595
}
9696

@@ -106,9 +106,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
106106
reserver.reserve();
107107
CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
108108
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
109-
BOOST_CHECK_EQUAL(result.failed_block, newTip->GetBlockHash());
110-
BOOST_CHECK(result.stop_block.IsNull());
111-
BOOST_CHECK(!result.stop_height);
109+
BOOST_CHECK_EQUAL(result.last_failed_block, newTip->GetBlockHash());
110+
BOOST_CHECK(result.last_scanned_block.IsNull());
111+
BOOST_CHECK(!result.last_scanned_height);
112112
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 0);
113113
}
114114
}
@@ -345,9 +345,9 @@ class ListCoinsTestingSetup : public TestChain100Setup
345345
reserver.reserve();
346346
CWallet::ScanResult result = wallet->ScanForWalletTransactions(chainActive.Genesis()->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
347347
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
348-
BOOST_CHECK_EQUAL(result.stop_block, chainActive.Tip()->GetBlockHash());
349-
BOOST_CHECK_EQUAL(*result.stop_height, chainActive.Height());
350-
BOOST_CHECK(result.failed_block.IsNull());
348+
BOOST_CHECK_EQUAL(result.last_scanned_block, chainActive.Tip()->GetBlockHash());
349+
BOOST_CHECK_EQUAL(*result.last_scanned_height, chainActive.Height());
350+
BOOST_CHECK(result.last_failed_block.IsNull());
351351
}
352352

353353
~ListCoinsTestingSetup()

src/wallet/wallet.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
16031603
ScanResult result = ScanForWalletTransactions(start_block, {} /* stop_block */, reserver, update);
16041604
if (result.status == ScanResult::FAILURE) {
16051605
int64_t time_max;
1606-
if (!chain().findBlock(result.failed_block, nullptr /* block */, nullptr /* time */, &time_max)) {
1606+
if (!chain().findBlock(result.last_failed_block, nullptr /* block */, nullptr /* time */, &time_max)) {
16071607
throw std::logic_error("ScanForWalletTransactions returned invalid block hash");
16081608
}
16091609
return time_max + TIMESTAMP_WINDOW + 1;
@@ -1617,15 +1617,17 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
16171617
* from or to us. If fUpdate is true, found transactions that already
16181618
* exist in the wallet will be updated.
16191619
*
1620-
* @param[in] start_block if not null, the scan will start at this block instead
1621-
* of the genesis block
1622-
* @param[in] stop_block if not null, the scan will stop at this block instead
1623-
* of the chain tip
1620+
* @param[in] start_block Scan starting block. If block is not on the active
1621+
* chain, the scan will return SUCCESS immediately.
1622+
* @param[in] stop_block Scan ending block. If block is not on the active
1623+
* chain, the scan will continue until it reaches the
1624+
* chain tip.
16241625
*
1625-
* @return ScanResult indicating success or failure of the scan. SUCCESS if
1626-
* scan was successful. FAILURE if a complete rescan was not possible (due to
1627-
* pruning or corruption). USER_ABORT if the rescan was aborted before it
1628-
* could complete.
1626+
* @return ScanResult returning scan information and indicating success or
1627+
* failure. Return status will be set to SUCCESS if scan was
1628+
* successful. FAILURE if a complete rescan was not possible (due to
1629+
* pruning or corruption). USER_ABORT if the rescan was aborted before
1630+
* it could complete.
16291631
*
16301632
* @pre Caller needs to make sure start_block (and the optional stop_block) are on
16311633
* the main chain after to the addition of any new keys you want to detect
@@ -1678,19 +1680,19 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
16781680
// marking transactions as coming from the wrong block.
16791681
// TODO: This should return success instead of failure, see
16801682
// https://github.com/bitcoin/bitcoin/pull/14711#issuecomment-458342518
1681-
result.failed_block = block_hash;
1683+
result.last_failed_block = block_hash;
16821684
result.status = ScanResult::FAILURE;
16831685
break;
16841686
}
16851687
for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
16861688
SyncTransaction(block.vtx[posInBlock], block_hash, posInBlock, fUpdate);
16871689
}
16881690
// scan succeeded, record block as most recent successfully scanned
1689-
result.stop_block = block_hash;
1690-
result.stop_height = *block_height;
1691+
result.last_scanned_block = block_hash;
1692+
result.last_scanned_height = *block_height;
16911693
} else {
16921694
// could not scan block, keep scanning but record this block as the most recent failure
1693-
result.failed_block = block_hash;
1695+
result.last_failed_block = block_hash;
16941696
result.status = ScanResult::FAILURE;
16951697
}
16961698
if (block_hash == stop_block) {
@@ -1720,10 +1722,10 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
17201722
}
17211723
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), 100); // hide progress dialog in GUI
17221724
if (block_height && fAbortRescan) {
1723-
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", block_height.get_value_or(0), progress_current);
1725+
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", *block_height, progress_current);
17241726
result.status = ScanResult::USER_ABORT;
17251727
} else if (block_height && ShutdownRequested()) {
1726-
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", block_height.get_value_or(0), progress_current);
1728+
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", *block_height, progress_current);
17271729
result.status = ScanResult::USER_ABORT;
17281730
}
17291731
}
@@ -4084,7 +4086,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
40844086
}
40854087

40864088
auto locked_chain = chain.assumeLocked(); // Temporary. Removed in upcoming lock cleanup
4087-
walletInstance->ChainStateFlushed(locked_chain->getLocator());
4089+
walletInstance->ChainStateFlushed(locked_chain->getTipLocator());
40884090
} else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
40894091
// Make it impossible to disable private keys after creation
40904092
InitError(strprintf(_("Error loading %s: Private keys can only be disabled during creation"), walletFile));
@@ -4231,7 +4233,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
42314233
}
42324234
}
42334235
walletInstance->WalletLogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nStart);
4234-
walletInstance->ChainStateFlushed(locked_chain->getLocator());
4236+
walletInstance->ChainStateFlushed(locked_chain->getTipLocator());
42354237
walletInstance->database->IncrementUpdateCounter();
42364238

42374239
// Restore wallet transaction metadata after -zapwallettxes=1

src/wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -912,14 +912,14 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
912912
//! Hash and height of most recent block that was successfully scanned.
913913
//! Unset if no blocks were scanned due to read errors or the chain
914914
//! being empty.
915-
uint256 stop_block;
916-
Optional<int> stop_height;
915+
uint256 last_scanned_block;
916+
Optional<int> last_scanned_height;
917917

918918
//! Height of the most recent block that could not be scanned due to
919919
//! read errors or pruning. Will be set if status is FAILURE, unset if
920920
//! status is SUCCESS, and may or may not be set if status is
921921
//! USER_ABORT.
922-
uint256 failed_block;
922+
uint256 last_failed_block;
923923
};
924924
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
925925
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;

0 commit comments

Comments
 (0)