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
refactor: Make CWalletTx sync state type-safe #21206
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2021 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_UTIL_OVERLOADED_H | ||
#define BITCOIN_UTIL_OVERLOADED_H | ||
|
||
namespace util { | ||
//! Overloaded helper for std::visit. This helper and std::visit in general are | ||
//! useful to write code that switches on a variant type. Unlike if/else-if and | ||
//! switch/case statements, std::visit will trigger compile errors if there are | ||
//! unhandled cases. | ||
//! | ||
//! Implementation comes from and example usage can be found at | ||
//! https://en.cppreference.com/w/cpp/utility/variant/visit#Example | ||
template<class... Ts> struct Overloaded : Ts... { using Ts::operator()...; }; | ||
|
||
//! Explicit deduction guide (not needed as of C++20) | ||
template<class... Ts> Overloaded(Ts...) -> Overloaded<Ts...>; | ||
} // namespace util | ||
|
||
#endif // BITCOIN_UTIL_OVERLOADED_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -330,17 +330,14 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) | |||||
BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) | ||||||
{ | ||||||
CWallet wallet(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase()); | ||||||
CWalletTx wtx(m_coinbase_txns.back()); | ||||||
CWalletTx wtx{m_coinbase_txns.back(), TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*position_in_block=*/0}}; | ||||||
|
||||||
LOCK(wallet.cs_wallet); | ||||||
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS); | ||||||
wallet.SetupDescriptorScriptPubKeyMans(); | ||||||
|
||||||
wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash()); | ||||||
|
||||||
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash(), 0); | ||||||
wtx.m_confirm = confirm; | ||||||
|
||||||
// Call GetImmatureCredit() once before adding the key to the wallet to | ||||||
// cache the current immature credit amount, which is 0. | ||||||
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx), 0); | ||||||
|
@@ -355,7 +352,7 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) | |||||
static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime) | ||||||
{ | ||||||
CMutableTransaction tx; | ||||||
CWalletTx::Confirmation confirm; | ||||||
TxState state = TxStateInactive{}; | ||||||
tx.nLockTime = lockTime; | ||||||
SetMockTime(mockTime); | ||||||
CBlockIndex* block = nullptr; | ||||||
|
@@ -367,13 +364,13 @@ static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lock | |||||
block = inserted.first->second; | ||||||
block->nTime = blockTime; | ||||||
block->phashBlock = &hash; | ||||||
confirm = {CWalletTx::Status::CONFIRMED, block->nHeight, hash, 0}; | ||||||
state = TxStateConfirmed{hash, block->nHeight, /*position_in_block=*/0}; | ||||||
} | ||||||
|
||||||
// If transaction is already in map, to avoid inconsistencies, unconfirmation | ||||||
// is needed before confirm again with different block. | ||||||
return wallet.AddToWallet(MakeTransactionRef(tx), confirm, [&](CWalletTx& wtx, bool /* new_tx */) { | ||||||
wtx.setUnconfirmed(); | ||||||
return wallet.AddToWallet(MakeTransactionRef(tx), state, [&](CWalletTx& wtx, bool /* new_tx */) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (if need to retouch)
Suggested change
|
||||||
// Assign wtx.m_state to simplify test and avoid the need to simulate | ||||||
// reorg events. Without this, AddToWallet asserts false when the same | ||||||
// transaction is confirmed in different blocks. | ||||||
wtx.m_state = state; | ||||||
return true; | ||||||
})->nTimeSmart; | ||||||
} | ||||||
|
@@ -534,8 +531,7 @@ class ListCoinsTestingSetup : public TestChain100Setup | |||||
wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, m_node.chainman->ActiveChain().Tip()->GetBlockHash()); | ||||||
auto it = wallet->mapWallet.find(tx->GetHash()); | ||||||
BOOST_CHECK(it != wallet->mapWallet.end()); | ||||||
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash(), 1); | ||||||
it->second.m_confirm = confirm; | ||||||
it->second.m_state = TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*position_in_block=*/1}; | ||||||
return it->second; | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2021 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <wallet/transaction.h> | ||
|
||
#include <wallet/test/wallet_test_fixture.h> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
BOOST_FIXTURE_TEST_SUITE(wallet_transaction_tests, WalletTestingSetup) | ||
|
||
BOOST_AUTO_TEST_CASE(roundtrip) | ||
{ | ||
for (uint8_t hash = 0; hash < 5; ++hash) { | ||
for (int index = -2; index < 3; ++index) { | ||
TxState state = TxStateInterpretSerialized(TxStateUnrecognized{uint256{hash}, index}); | ||
BOOST_CHECK_EQUAL(TxStateSerializedBlockHash(state), uint256{hash}); | ||
BOOST_CHECK_EQUAL(TxStateSerializedIndex(state), index); | ||
} | ||
} | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As for Clang, this explicit deduction guide can be dropped for versions >=17 only, unfortunately.
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 is part of #29042.