Skip to content

Commit 27f5a29

Browse files
committed
Merge #14957: wallet: Initialize stop_block in CWallet::ScanForWalletTransactions
8b9171c wallet: Initialize stop_block to nullptr in CWallet::ScanForWalletTransactions (Ben Woosley) Pull request description: Previously the argument would be untouched if the first block scan failed. This makes the behavior predictable, and consistent with the documentation. Tree-SHA512: 3efadf9fd5e25ecd9450f32545f58e61a123ad883e921ef427b13e4782ffdd8ffe905c9ad3edc7e8f9e4953342cd72247bb4cc9eeaf9e5fd04291ac5c1bb5eec
2 parents b545a6e + 8b9171c commit 27f5a29

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void AddKey(CWallet& wallet, const CKey& key)
3434
wallet.AddKeyPubKey(key, key.GetPubKey());
3535
}
3636

37-
BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
37+
BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
3838
{
3939
auto chain = interfaces::MakeChain();
4040

@@ -47,14 +47,27 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
4747

4848
auto locked_chain = chain->lock();
4949

50+
// Verify ScanForWalletTransactions accomodates a null start block.
51+
{
52+
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
53+
AddKey(wallet, coinbaseKey);
54+
WalletRescanReserver reserver(&wallet);
55+
reserver.reserve();
56+
const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
57+
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(nullptr, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS);
58+
BOOST_CHECK_EQUAL(failed_block, null_block);
59+
BOOST_CHECK_EQUAL(stop_block, null_block);
60+
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 0);
61+
}
62+
5063
// Verify ScanForWalletTransactions picks up transactions in both the old
5164
// and new block files.
5265
{
5366
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
5467
AddKey(wallet, coinbaseKey);
5568
WalletRescanReserver reserver(&wallet);
5669
reserver.reserve();
57-
const CBlockIndex *stop_block, *failed_block;
70+
const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
5871
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS);
5972
BOOST_CHECK_EQUAL(failed_block, null_block);
6073
BOOST_CHECK_EQUAL(stop_block, newTip);
@@ -72,13 +85,47 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
7285
AddKey(wallet, coinbaseKey);
7386
WalletRescanReserver reserver(&wallet);
7487
reserver.reserve();
75-
const CBlockIndex *stop_block, *failed_block;
88+
const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
7689
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::FAILURE);
7790
BOOST_CHECK_EQUAL(failed_block, oldTip);
7891
BOOST_CHECK_EQUAL(stop_block, newTip);
7992
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 50 * COIN);
8093
}
8194

95+
// Prune the remaining block file.
96+
PruneOneBlockFile(newTip->GetBlockPos().nFile);
97+
UnlinkPrunedFiles({newTip->GetBlockPos().nFile});
98+
99+
// Verify ScanForWalletTransactions scans no blocks.
100+
{
101+
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
102+
AddKey(wallet, coinbaseKey);
103+
WalletRescanReserver reserver(&wallet);
104+
reserver.reserve();
105+
const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
106+
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::FAILURE);
107+
BOOST_CHECK_EQUAL(failed_block, newTip);
108+
BOOST_CHECK_EQUAL(stop_block, null_block);
109+
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 0);
110+
}
111+
}
112+
113+
BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
114+
{
115+
auto chain = interfaces::MakeChain();
116+
117+
// Cap last block file size, and mine new block in a new block file.
118+
CBlockIndex* oldTip = chainActive.Tip();
119+
GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE;
120+
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
121+
CBlockIndex* newTip = chainActive.Tip();
122+
123+
auto locked_chain = chain->lock();
124+
125+
// Prune the older block file.
126+
PruneOneBlockFile(oldTip->GetBlockPos().nFile);
127+
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
128+
82129
// Verify importmulti RPC returns failure for a key whose creation time is
83130
// before the missing block, and success for a key whose creation time is
84131
// after.
@@ -294,7 +341,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
294341
WalletRescanReserver reserver(wallet.get());
295342
reserver.reserve();
296343
const CBlockIndex* const null_block = nullptr;
297-
const CBlockIndex *stop_block, *failed_block;
344+
const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
298345
BOOST_CHECK_EQUAL(wallet->ScanForWalletTransactions(chainActive.Genesis(), nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS);
299346
BOOST_CHECK_EQUAL(stop_block, chainActive.Tip());
300347
BOOST_CHECK_EQUAL(failed_block, null_block);

src/wallet/wallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const CBlockIndex* const
16011601

16021602
const CBlockIndex* pindex = pindexStart;
16031603
failed_block = nullptr;
1604+
stop_block = nullptr;
16041605

16051606
if (pindex) WalletLogPrintf("Rescan started from block %d...\n", pindex->nHeight);
16061607

0 commit comments

Comments
 (0)