Skip to content

Commit d5723b8

Browse files
MarcoFalkePastaPastaPasta
MarcoFalke
authored andcommitted
Merge bitcoin#15342: Suggested wallet code cleanups from bitcoin#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 bitcoin#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 # Conflicts: # src/interfaces/chain.cpp # src/qt/test/wallettests.cpp # src/wallet/test/wallet_tests.cpp # src/wallet/wallet.cpp
1 parent ff82953 commit d5723b8

File tree

7 files changed

+49
-43
lines changed

7 files changed

+49
-43
lines changed

src/interfaces/chain.cpp

+1-1
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

+1-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ void TestGUI()
124124
reserver.reserve();
125125
CWallet::ScanResult result = wallet->ScanForWalletTransactions(locked_chain->getBlockHash(0), {} /* stop_block */, reserver, true /* fUpdate */);
126126
QCOMPARE(result.status, CWallet::ScanResult::SUCCESS);
127-
QCOMPARE(result.stop_block, ::ChainActive().Tip()->GetBlockHash());
128-
QVERIFY(result.failed_block.IsNull());
127+
QCOMPARE(result.last_scanned_block, ::ChainActive().Tip()->GetBlockHash());
128+
QVERIFY(result.last_failed_block.IsNull());
129129
}
130130
wallet->SetBroadcastTransactions(true);
131131

src/wallet/rpcwallet.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -3749,8 +3749,8 @@ static UniValue rescanblockchain(const JSONRPCRequest& request)
37493749
"2. \"stop_height\" (numeric, optional) the last block height that should be scanned. If none is provided it will rescan up to the tip at return time of this call.\n"
37503750
"\nResult:\n"
37513751
"{\n"
3752-
" \"start_height\" (numeric) The block height where the rescan has started.\n"
3753-
" \"stop_height\" (numeric) The height of the last rescanned block.\n"
3752+
" \"start_height\" (numeric) The block height where the rescan started (the requested height or 0)\n"
3753+
" \"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"
37543754
"}\n"
37553755
"\nExamples:\n"
37563756
+ HelpExampleCli("rescanblockchain", "100000 120000")
@@ -3794,6 +3794,11 @@ static UniValue rescanblockchain(const JSONRPCRequest& request)
37943794

37953795
if (tip_height) {
37963796
start_block = locked_chain->getBlockHash(start_height);
3797+
// If called with a stop_height, set the stop_height here to
3798+
// trigger a rescan to that height.
3799+
// If called without a stop height, leave stop_height as null here
3800+
// so rescan continues to the tip (even if the tip advances during
3801+
// rescan).
37973802
if (stop_height) {
37983803
stop_block = locked_chain->getBlockHash(*stop_height);
37993804
}
@@ -3813,7 +3818,7 @@ static UniValue rescanblockchain(const JSONRPCRequest& request)
38133818
}
38143819
UniValue response(UniValue::VOBJ);
38153820
response.pushKV("start_height", start_height);
3816-
response.pushKV("stop_height", result.stop_height ? *result.stop_height : UniValue());
3821+
response.pushKV("stop_height", result.last_scanned_height ? *result.last_scanned_height : UniValue());
38173822
return response;
38183823
}
38193824

src/wallet/test/wallet_tests.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
5757
reserver.reserve();
5858
CWallet::ScanResult result = wallet.ScanForWalletTransactions({} /* start_block */, {} /* stop_block */, reserver, false /* update */);
5959
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
60-
BOOST_CHECK(result.failed_block.IsNull());
61-
BOOST_CHECK(result.stop_block.IsNull());
62-
BOOST_CHECK(!result.stop_height);
60+
BOOST_CHECK(result.last_failed_block.IsNull());
61+
BOOST_CHECK(result.last_scanned_block.IsNull());
62+
BOOST_CHECK(!result.last_scanned_height);
6363
BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 0);
6464
}
6565

@@ -72,9 +72,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
7272
reserver.reserve();
7373
CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
7474
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
75-
BOOST_CHECK(result.failed_block.IsNull());
76-
BOOST_CHECK_EQUAL(result.stop_block, newTip->GetBlockHash());
77-
BOOST_CHECK_EQUAL(*result.stop_height, newTip->nHeight);
75+
BOOST_CHECK(result.last_failed_block.IsNull());
76+
BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
77+
BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
7878
BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 1000 * COIN);
7979
}
8080

@@ -91,9 +91,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
9191
reserver.reserve();
9292
CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
9393
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
94-
BOOST_CHECK_EQUAL(result.failed_block, oldTip->GetBlockHash());
95-
BOOST_CHECK_EQUAL(result.stop_block, newTip->GetBlockHash());
96-
BOOST_CHECK_EQUAL(*result.stop_height, newTip->nHeight);
94+
BOOST_CHECK_EQUAL(result.last_failed_block, oldTip->GetBlockHash());
95+
BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
96+
BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
9797
BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 500 * COIN);
9898
}
9999

@@ -109,9 +109,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
109109
reserver.reserve();
110110
CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
111111
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
112-
BOOST_CHECK_EQUAL(result.failed_block, newTip->GetBlockHash());
113-
BOOST_CHECK(result.stop_block.IsNull());
114-
BOOST_CHECK(!result.stop_height);
112+
BOOST_CHECK_EQUAL(result.last_failed_block, newTip->GetBlockHash());
113+
BOOST_CHECK(result.last_scanned_block.IsNull());
114+
BOOST_CHECK(!result.last_scanned_height);
115115
BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 0);
116116
}
117117
}
@@ -349,9 +349,9 @@ class ListCoinsTestingSetup : public TestChain100Setup
349349
reserver.reserve();
350350
CWallet::ScanResult result = wallet->ScanForWalletTransactions(::ChainActive().Genesis()->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
351351
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
352-
BOOST_CHECK_EQUAL(result.stop_block, ::ChainActive().Tip()->GetBlockHash());
353-
BOOST_CHECK_EQUAL(*result.stop_height, ::ChainActive().Height());
354-
BOOST_CHECK(result.failed_block.IsNull());
352+
BOOST_CHECK_EQUAL(result.last_scanned_block, ::ChainActive().Tip()->GetBlockHash());
353+
BOOST_CHECK_EQUAL(*result.last_scanned_height, ::ChainActive().Height());
354+
BOOST_CHECK(result.last_failed_block.IsNull());
355355
}
356356

357357
~ListCoinsTestingSetup()

src/wallet/wallet.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,7 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
21162116
ScanResult result = ScanForWalletTransactions(start_block, {} /* stop_block */, reserver, update);
21172117
if (result.status == ScanResult::FAILURE) {
21182118
int64_t time_max;
2119-
if (!chain().findBlock(result.failed_block, nullptr /* block */, nullptr /* time */, &time_max)) {
2119+
if (!chain().findBlock(result.last_failed_block, nullptr /* block */, nullptr /* time */, &time_max)) {
21202120
throw std::logic_error("ScanForWalletTransactions returned invalid block hash");
21212121
}
21222122
return time_max + TIMESTAMP_WINDOW + 1;
@@ -2130,15 +2130,17 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
21302130
* from or to us. If fUpdate is true, found transactions that already
21312131
* exist in the wallet will be updated.
21322132
*
2133-
* @param[in] start_block if not null, the scan will start at this block instead
2134-
* of the genesis block
2135-
* @param[in] stop_block if not null, the scan will stop at this block instead
2136-
* of the chain tip
2133+
* @param[in] start_block Scan starting block. If block is not on the active
2134+
* chain, the scan will return SUCCESS immediately.
2135+
* @param[in] stop_block Scan ending block. If block is not on the active
2136+
* chain, the scan will continue until it reaches the
2137+
* chain tip.
21372138
*
2138-
* @return ScanResult indicating success or failure of the scan. SUCCESS if
2139-
* scan was successful. FAILURE if a complete rescan was not possible (due to
2140-
* pruning or corruption). USER_ABORT if the rescan was aborted before it
2141-
* could complete.
2139+
* @return ScanResult returning scan information and indicating success or
2140+
* failure. Return status will be set to SUCCESS if scan was
2141+
* successful. FAILURE if a complete rescan was not possible (due to
2142+
* pruning or corruption). USER_ABORT if the rescan was aborted before
2143+
* it could complete.
21422144
*
21432145
* @pre Caller needs to make sure start_block (and the optional stop_block) are on
21442146
* the main chain after to the addition of any new keys you want to detect
@@ -2191,19 +2193,19 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
21912193
// marking transactions as coming from the wrong block.
21922194
// TODO: This should return success instead of failure, see
21932195
// https://github.com/bitcoin/bitcoin/pull/14711#issuecomment-458342518
2194-
result.failed_block = block_hash;
2196+
result.last_failed_block = block_hash;
21952197
result.status = ScanResult::FAILURE;
21962198
break;
21972199
}
21982200
for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
21992201
SyncTransaction(block.vtx[posInBlock], block_hash, posInBlock, fUpdate);
22002202
}
22012203
// scan succeeded, record block as most recent successfully scanned
2202-
result.stop_block = block_hash;
2203-
result.stop_height = *block_height;
2204+
result.last_scanned_block = block_hash;
2205+
result.last_scanned_height = *block_height;
22042206
} else {
22052207
// could not scan block, keep scanning but record this block as the most recent failure
2206-
result.failed_block = block_hash;
2208+
result.last_failed_block = block_hash;
22072209
result.status = ScanResult::FAILURE;
22082210
}
22092211
if (block_hash == stop_block) {
@@ -2233,10 +2235,10 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
22332235
}
22342236
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), 100); // hide progress dialog in GUI
22352237
if (block_height && fAbortRescan) {
2236-
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", block_height.value_or(0), progress_current);
2238+
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", *block_height, progress_current);
22372239
result.status = ScanResult::USER_ABORT;
22382240
} else if (block_height && ShutdownRequested()) {
2239-
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", block_height.value_or(0), progress_current);
2241+
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", *block_height, progress_current);
22402242
result.status = ScanResult::USER_ABORT;
22412243
}
22422244
}
@@ -4987,7 +4989,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
49874989
}
49884990

49894991
auto locked_chain = chain.assumeLocked(); // Temporary. Removed in upcoming lock cleanup
4990-
walletInstance->ChainStateFlushed(locked_chain->getLocator());
4992+
walletInstance->ChainStateFlushed(locked_chain->getTipLocator());
49914993

49924994
// Try to create wallet backup right after new wallet was created
49934995
std::string strBackupWarning;
@@ -5000,7 +5002,6 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
50005002
return error(strBackupError);
50015003
}
50025004
}
5003-
50045005
} else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
50055006
// Make it impossible to disable private keys after creation
50065007
InitError(strprintf(_("Error loading %s: Private keys can only be disabled during creation"), walletFile));
@@ -5154,7 +5155,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
51545155
}
51555156
}
51565157
walletInstance->WalletLogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nStart);
5157-
walletInstance->ChainStateFlushed(locked_chain->getLocator());
5158+
walletInstance->ChainStateFlushed(locked_chain->getTipLocator());
51585159
walletInstance->database->IncrementUpdateCounter();
51595160

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

src/wallet/wallet.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1006,14 +1006,14 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
10061006
//! Hash and height of most recent block that was successfully scanned.
10071007
//! Unset if no blocks were scanned due to read errors or the chain
10081008
//! being empty.
1009-
uint256 stop_block;
1010-
Optional<int> stop_height;
1009+
uint256 last_scanned_block;
1010+
Optional<int> last_scanned_height;
10111011

10121012
//! Height of the most recent block that could not be scanned due to
10131013
//! read errors or pruning. Will be set if status is FAILURE, unset if
10141014
//! status is SUCCESS, and may or may not be set if status is
10151015
//! USER_ABORT.
1016-
uint256 failed_block;
1016+
uint256 last_failed_block;
10171017
};
10181018
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
10191019
void TransactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason) override;

0 commit comments

Comments
 (0)