-
Notifications
You must be signed in to change notification settings - Fork 37.7k
wallet: Initialize stop_block in CWallet::ScanForWalletTransactions #14957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Bug fixes should come with test coverage, so that they wouldn't break again in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK b5c20992a201c1711924cf09ab21c0a3ce6cbb93, thanks for addressing this so quickly. I agree a test would be nice, though looking more closely, maybe the bug doesn't really result in any consequences. For example, without file corruption I don't see a way to call rescanblockchain
that would make the stopBlock->nHeight
line segfault:
bitcoin/src/wallet/rpcwallet.cpp
Line 3439 in 9133227
response.pushKV("stop_height", stopBlock->nHeight); |
utACK b5c2099. I assumed it would segfault when there is a reorg during rescan, not sure how easy it is to write a test for that. |
I don't think even a reorg would be enough to cause a segfault with But alternately, it would be simple to write a c++ unit test that directly called |
Isn't the start block inclusive in the range? In that case an empty range couldn't be passed. |
Passing null pindexStart might work, or using bitcoin/src/wallet/test/wallet_tests.cpp Line 65 in 9133227
|
b5c2099
to
7b6935f
Compare
Thanks @ryanofsky, added the two cases you mention, both fail without the initialization. Looking into the importmulti test that follows immediately after... |
7b6935f
to
3412d21
Compare
Split the importmulti test into its own case for independence. |
3412d21
to
b5de045
Compare
b5de045
to
1c19a0b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs rebase to account for #14979.
ACK 1c19a0b, just a comment in the tests.
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(nullptr, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS); | ||
BOOST_CHECK_EQUAL(failed_block, null_block); | ||
BOOST_CHECK_EQUAL(stop_block, null_block); | ||
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Irrelevant for this test case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't hurt either
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::FAILURE); | ||
BOOST_CHECK_EQUAL(failed_block, newTip); | ||
BOOST_CHECK_EQUAL(stop_block, null_block); | ||
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
utACK 1c19a0b4812ba3b9e0a21985d8064801ca25e334 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 1c19a0b4812ba3b9e0a21985d8064801ca25e334, but one of the test comments appears to be wrong, so it would be good to fix that before merging.
…nsactions Previously the argument would be untouched if the first block scan failed. This makes the behavior predictable, and consistent with the documentation.
1c19a0b
to
8b9171c
Compare
Thanks, fixed the nits - many of these had been fixed in b5de045 but I accidentally overwrote it on a second machine rebasing yesterday. Here's the diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
index 28200123f..1ed1926af 100644
--- a/src/wallet/test/wallet_tests.cpp
+++ b/src/wallet/test/wallet_tests.cpp
@@ -53,7 +53,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
AddKey(wallet, coinbaseKey);
WalletRescanReserver reserver(&wallet);
reserver.reserve();
- const CBlockIndex *stop_block, *failed_block;
+ const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(nullptr, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS);
BOOST_CHECK_EQUAL(failed_block, null_block);
BOOST_CHECK_EQUAL(stop_block, null_block);
@@ -67,7 +67,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
AddKey(wallet, coinbaseKey);
WalletRescanReserver reserver(&wallet);
reserver.reserve();
- const CBlockIndex *stop_block, *failed_block;
+ const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS);
BOOST_CHECK_EQUAL(failed_block, null_block);
BOOST_CHECK_EQUAL(stop_block, newTip);
@@ -85,7 +85,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
AddKey(wallet, coinbaseKey);
WalletRescanReserver reserver(&wallet);
reserver.reserve();
- const CBlockIndex *stop_block, *failed_block;
+ const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::FAILURE);
BOOST_CHECK_EQUAL(failed_block, oldTip);
BOOST_CHECK_EQUAL(stop_block, newTip);
@@ -96,14 +96,13 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
PruneOneBlockFile(newTip->GetBlockPos().nFile);
UnlinkPrunedFiles({newTip->GetBlockPos().nFile});
- // Verify ScanForWalletTransactions only picks transactions in the new block
- // file.
+ // Verify ScanForWalletTransactions scans no blocks.
{
CWallet wallet(*chain, WalletLocation(), WalletDatabase::CreateDummy());
AddKey(wallet, coinbaseKey);
WalletRescanReserver reserver(&wallet);
reserver.reserve();
- const CBlockIndex *stop_block, *failed_block;
+ const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::FAILURE);
BOOST_CHECK_EQUAL(failed_block, newTip);
BOOST_CHECK_EQUAL(stop_block, null_block);
@@ -130,6 +129,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
// Verify importmulti RPC returns failure for a key whose creation time is
// before the missing block, and success for a key whose creation time is
// after.
+ {
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateDummy());
AddWallet(wallet);
UniValue keys;
@@ -164,6 +164,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW));
RemoveWallet(wallet);
}
+}
// Verify importwallet RPC starts rescan at earliest block with timestamp
// greater or equal than key birthday. Previously there was a bug where
@@ -340,7 +341,7 @@ public:
WalletRescanReserver reserver(wallet.get());
reserver.reserve();
const CBlockIndex* const null_block = nullptr;
- const CBlockIndex *stop_block, *failed_block;
+ const CBlockIndex *stop_block = null_block + 1, *failed_block = null_block + 1;
BOOST_CHECK_EQUAL(wallet->ScanForWalletTransactions(chainActive.Genesis(), nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS);
BOOST_CHECK_EQUAL(stop_block, chainActive.Tip());
BOOST_CHECK_EQUAL(failed_block, null_block); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 8b9171c. Only suggested changes since last review (Thanks!)
re-utACK 8b9171c |
@ryanofsky is |
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
"There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type." so the arithmetic is on a 0-initialized const CBlockIndex*, so it's regular pointer math. That said, I don't know how much less likely the value 1 is to be found in uninitialized memory, over the value 0. |
Ok, utACK 8b9171c |
…Transactions 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
@@ -47,14 +47,27 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup) | |||
|
|||
auto locked_chain = chain->lock(); | |||
|
|||
// Verify ScanForWalletTransactions accomodates a null start block. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to trigger a lint warning from test/lint/lint-all.sh
on master:
src/wallet/test/wallet_tests.cpp:50: accomodates ==> accommodates
^ Warning: codespell identified likely spelling errors. Any false positives? Add them to the list of ignored words in test/lint/lint-spelling.ignore-words.txt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be fixed in #15102.
Summary: ``` Previously the argument would be untouched if the first block scan failed. This makes the behavior predictable, and consistent with the documentation. ``` Backport of core [[bitcoin/bitcoin#14957 | PR14957]] and the remaining of [[bitcoin/bitcoin#15321 | PR15321]] (see D5238). Depends on D5563. Test Plan: With Clang: cmake -GNinja .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_WERROR=ON ninja all check check-functional Reviewers: #bitcoin_abc, deadalnix Reviewed By: #bitcoin_abc, deadalnix Differential Revision: https://reviews.bitcoinabc.org/D5564
…rWalletTransactions 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 # Conflicts: # src/wallet/test/wallet_tests.cpp # src/wallet/wallet.cpp
…rWalletTransactions 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 # Conflicts: # src/wallet/test/wallet_tests.cpp
…rWalletTransactions 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 # Conflicts: # src/wallet/test/wallet_tests.cpp # src/wallet/wallet.cpp
…rWalletTransactions (#4538) * Merge bitcoin#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 # Conflicts: # src/wallet/test/wallet_tests.cpp * Add lock annotation * fix Co-authored-by: MeshCollider <dobsonsa68@gmail.com>
…rWalletTransactions 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 # Conflicts: # src/wallet/test/wallet_tests.cpp # src/wallet/wallet.cpp
…rWalletTransactions (dashpay#4538) * Merge bitcoin#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 # Conflicts: # src/wallet/test/wallet_tests.cpp * Add lock annotation * fix Co-authored-by: MeshCollider <dobsonsa68@gmail.com>
…rWalletTransactions (dashpay#4538) * Merge bitcoin#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 # Conflicts: # src/wallet/test/wallet_tests.cpp * Add lock annotation * fix Co-authored-by: MeshCollider <dobsonsa68@gmail.com>
Previously the argument would be untouched if the first block scan failed. This
makes the behavior predictable, and consistent with the documentation.