Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a941bfc

Browse files
MarcoFalkePastaPastaPasta
MarcoFalke
authored andcommittedNov 1, 2021
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 75fda78 commit a941bfc

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
@@ -126,8 +126,8 @@ void TestGUI()
126126
reserver.reserve();
127127
CWallet::ScanResult result = wallet->ScanForWalletTransactions(locked_chain->getBlockHash(0), {} /* stop_block */, reserver, true /* fUpdate */);
128128
QCOMPARE(result.status, CWallet::ScanResult::SUCCESS);
129-
QCOMPARE(result.stop_block, ::ChainActive().Tip()->GetBlockHash());
130-
QVERIFY(result.failed_block.IsNull());
129+
QCOMPARE(result.last_scanned_block, ::ChainActive().Tip()->GetBlockHash());
130+
QVERIFY(result.last_failed_block.IsNull());
131131
}
132132
wallet->SetBroadcastTransactions(true);
133133

‎src/wallet/rpcwallet.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -3566,8 +3566,8 @@ static UniValue rescanblockchain(const JSONRPCRequest& request)
35663566
.ToString() +
35673567
"\nResult:\n"
35683568
"{\n"
3569-
" \"start_height\" (numeric) The block height where the rescan has started.\n"
3570-
" \"stop_height\" (numeric) The height of the last rescanned block.\n"
3569+
" \"start_height\" (numeric) The block height where the rescan started (the requested height or 0)\n"
3570+
" \"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"
35713571
"}\n"
35723572
"\nExamples:\n"
35733573
+ HelpExampleCli("rescanblockchain", "100000 120000")
@@ -3611,6 +3611,11 @@ static UniValue rescanblockchain(const JSONRPCRequest& request)
36113611

36123612
if (tip_height) {
36133613
start_block = locked_chain->getBlockHash(start_height);
3614+
// If called with a stop_height, set the stop_height here to
3615+
// trigger a rescan to that height.
3616+
// If called without a stop height, leave stop_height as null here
3617+
// so rescan continues to the tip (even if the tip advances during
3618+
// rescan).
36143619
if (stop_height) {
36153620
stop_block = locked_chain->getBlockHash(*stop_height);
36163621
}
@@ -3630,7 +3635,7 @@ static UniValue rescanblockchain(const JSONRPCRequest& request)
36303635
}
36313636
UniValue response(UniValue::VOBJ);
36323637
response.pushKV("start_height", start_height);
3633-
response.pushKV("stop_height", result.stop_height ? *result.stop_height : UniValue());
3638+
response.pushKV("stop_height", result.last_scanned_height ? *result.last_scanned_height : UniValue());
36343639
return response;
36353640
}
36363641

‎src/wallet/test/wallet_tests.cpp

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

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

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

@@ -108,9 +108,9 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
108108
reserver.reserve();
109109
CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
110110
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
111-
BOOST_CHECK_EQUAL(result.failed_block, newTip->GetBlockHash());
112-
BOOST_CHECK(result.stop_block.IsNull());
113-
BOOST_CHECK(!result.stop_height);
111+
BOOST_CHECK_EQUAL(result.last_failed_block, newTip->GetBlockHash());
112+
BOOST_CHECK(result.last_scanned_block.IsNull());
113+
BOOST_CHECK(!result.last_scanned_height);
114114
BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 0);
115115
}
116116
}
@@ -352,9 +352,9 @@ class ListCoinsTestingSetup : public TestChain100Setup
352352
reserver.reserve();
353353
CWallet::ScanResult result = wallet->ScanForWalletTransactions(::ChainActive().Genesis()->GetBlockHash(), {} /* stop_block */, reserver, false /* update */);
354354
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
355-
BOOST_CHECK_EQUAL(result.stop_block, ::ChainActive().Tip()->GetBlockHash());
356-
BOOST_CHECK_EQUAL(*result.stop_height, ::ChainActive().Height());
357-
BOOST_CHECK(result.failed_block.IsNull());
355+
BOOST_CHECK_EQUAL(result.last_scanned_block, ::ChainActive().Tip()->GetBlockHash());
356+
BOOST_CHECK_EQUAL(*result.last_scanned_height, ::ChainActive().Height());
357+
BOOST_CHECK(result.last_failed_block.IsNull());
358358
}
359359

360360
~ListCoinsTestingSetup()

‎src/wallet/wallet.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,7 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
21232123
ScanResult result = ScanForWalletTransactions(start_block, {} /* stop_block */, reserver, update);
21242124
if (result.status == ScanResult::FAILURE) {
21252125
int64_t time_max;
2126-
if (!chain().findBlock(result.failed_block, nullptr /* block */, nullptr /* time */, &time_max)) {
2126+
if (!chain().findBlock(result.last_failed_block, nullptr /* block */, nullptr /* time */, &time_max)) {
21272127
throw std::logic_error("ScanForWalletTransactions returned invalid block hash");
21282128
}
21292129
return time_max + TIMESTAMP_WINDOW + 1;
@@ -2137,15 +2137,17 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
21372137
* from or to us. If fUpdate is true, found transactions that already
21382138
* exist in the wallet will be updated.
21392139
*
2140-
* @param[in] start_block if not null, the scan will start at this block instead
2141-
* of the genesis block
2142-
* @param[in] stop_block if not null, the scan will stop at this block instead
2143-
* of the chain tip
2140+
* @param[in] start_block Scan starting block. If block is not on the active
2141+
* chain, the scan will return SUCCESS immediately.
2142+
* @param[in] stop_block Scan ending block. If block is not on the active
2143+
* chain, the scan will continue until it reaches the
2144+
* chain tip.
21442145
*
2145-
* @return ScanResult indicating success or failure of the scan. SUCCESS if
2146-
* scan was successful. FAILURE if a complete rescan was not possible (due to
2147-
* pruning or corruption). USER_ABORT if the rescan was aborted before it
2148-
* could complete.
2146+
* @return ScanResult returning scan information and indicating success or
2147+
* failure. Return status will be set to SUCCESS if scan was
2148+
* successful. FAILURE if a complete rescan was not possible (due to
2149+
* pruning or corruption). USER_ABORT if the rescan was aborted before
2150+
* it could complete.
21492151
*
21502152
* @pre Caller needs to make sure start_block (and the optional stop_block) are on
21512153
* the main chain after to the addition of any new keys you want to detect
@@ -2198,19 +2200,19 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
21982200
// marking transactions as coming from the wrong block.
21992201
// TODO: This should return success instead of failure, see
22002202
// https://github.com/bitcoin/bitcoin/pull/14711#issuecomment-458342518
2201-
result.failed_block = block_hash;
2203+
result.last_failed_block = block_hash;
22022204
result.status = ScanResult::FAILURE;
22032205
break;
22042206
}
22052207
for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
22062208
SyncTransaction(block.vtx[posInBlock], block_hash, posInBlock, fUpdate);
22072209
}
22082210
// scan succeeded, record block as most recent successfully scanned
2209-
result.stop_block = block_hash;
2210-
result.stop_height = *block_height;
2211+
result.last_scanned_block = block_hash;
2212+
result.last_scanned_height = *block_height;
22112213
} else {
22122214
// could not scan block, keep scanning but record this block as the most recent failure
2213-
result.failed_block = block_hash;
2215+
result.last_failed_block = block_hash;
22142216
result.status = ScanResult::FAILURE;
22152217
}
22162218
if (block_hash == stop_block) {
@@ -2240,10 +2242,10 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
22402242
}
22412243
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), 100); // hide progress dialog in GUI
22422244
if (block_height && fAbortRescan) {
2243-
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", block_height.value_or(0), progress_current);
2245+
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", *block_height, progress_current);
22442246
result.status = ScanResult::USER_ABORT;
22452247
} else if (block_height && ShutdownRequested()) {
2246-
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", block_height.value_or(0), progress_current);
2248+
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", *block_height, progress_current);
22472249
result.status = ScanResult::USER_ABORT;
22482250
}
22492251
}
@@ -4997,7 +4999,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
49974999
}
49985000

49995001
auto locked_chain = chain.assumeLocked(); // Temporary. Removed in upcoming lock cleanup
5000-
walletInstance->ChainStateFlushed(locked_chain->getLocator());
5002+
walletInstance->ChainStateFlushed(locked_chain->getTipLocator());
50015003

50025004
// Try to create wallet backup right after new wallet was created
50035005
std::string strBackupWarning;
@@ -5010,7 +5012,6 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
50105012
return error(strBackupError);
50115013
}
50125014
}
5013-
50145015
} else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
50155016
// Make it impossible to disable private keys after creation
50165017
InitError(strprintf(_("Error loading %s: Private keys can only be disabled during creation"), walletFile));
@@ -5164,7 +5165,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
51645165
}
51655166
}
51665167
walletInstance->WalletLogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nStart);
5167-
walletInstance->ChainStateFlushed(locked_chain->getLocator());
5168+
walletInstance->ChainStateFlushed(locked_chain->getTipLocator());
51685169
walletInstance->database->IncrementUpdateCounter();
51695170

51705171
// 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)
Please sign in to comment.