Skip to content

Commit 05ec664

Browse files
committed
Merge bitcoin/bitcoin#27666: wallet, bench: Move commonly used functions to their own file and fix a bug
7379a54 bench: Remove incorrect LoadWallet call in WalletBalance (Andrew Chow) 846b2fe tests: Move ADDRESS_BCRT1_UNSPENDABLE to wallet/test/util.h (Andrew Chow) c61d3f0 tests, bench: Consolidate {Test,Bench}Un/LoadWallet helper (Andrew Chow) Pull request description: I have a few PRs and branches that use these two commits, probably makes sense to split them into a separate PR to be merged sooner. The first commit contains some things that end up being commonly used in new wallet benchmarks. These are moved into `wallet_common.{h/cpp}`. The second commit contains a bugfix for the wallet_balance benchmark where it calls `LoadWallet` in the wrong place. It's unnecessary to call that function in this benchmark. Although this does not cause any issues currently, it ends up causing issues in some PRs and branches that I'm working on. ACKs for top commit: Sjors: utACK 7379a54 furszy: ACK 7379a54 Tree-SHA512: 47773887a16c69ac7121c699d3446a8c399bd792a6a31714998b7b7a19fea179c6d3b29cb898b04397b2962c1b4120d57009352b8460b8283e188d4cb480c9ba
2 parents 214f8f1 + 7379a54 commit 05ec664

File tree

5 files changed

+53
-63
lines changed

5 files changed

+53
-63
lines changed

src/bench/wallet_balance.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@
1515

1616
#include <optional>
1717

18-
using wallet::CWallet;
19-
using wallet::CreateMockableWalletDatabase;
20-
using wallet::DBErrors;
21-
using wallet::GetBalance;
22-
using wallet::WALLET_FLAG_DESCRIPTORS;
23-
24-
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
25-
18+
namespace wallet {
2619
static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)
2720
{
2821
const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
@@ -37,7 +30,6 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b
3730
LOCK(wallet.cs_wallet);
3831
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
3932
wallet.SetupDescriptorScriptPubKeyMans();
40-
if (wallet.LoadWallet() != DBErrors::LOAD_OK) assert(false);
4133
}
4234
auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
4335

@@ -67,3 +59,4 @@ BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH);
6759
BENCHMARK(WalletBalanceClean, benchmark::PriorityLevel::HIGH);
6860
BENCHMARK(WalletBalanceMine, benchmark::PriorityLevel::HIGH);
6961
BENCHMARK(WalletBalanceWatch, benchmark::PriorityLevel::HIGH);
62+
} // namespace wallet

src/bench/wallet_loading.cpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,7 @@
1616

1717
#include <optional>
1818

19-
using wallet::CWallet;
20-
using wallet::CreateMockableWalletDatabase;
21-
using wallet::TxStateInactive;
22-
using wallet::WALLET_FLAG_DESCRIPTORS;
23-
using wallet::WalletContext;
24-
using wallet::WalletDatabase;
25-
26-
static std::shared_ptr<CWallet> BenchLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
27-
{
28-
bilingual_str error;
29-
std::vector<bilingual_str> warnings;
30-
auto wallet = CWallet::Create(context, "", std::move(database), create_flags, error, warnings);
31-
NotifyWalletLoaded(context, wallet);
32-
if (context.chain) {
33-
wallet->postInitProcess();
34-
}
35-
return wallet;
36-
}
37-
38-
static void BenchUnloadWallet(std::shared_ptr<CWallet>&& wallet)
39-
{
40-
SyncWithValidationInterfaceQueue();
41-
wallet->m_chain_notifications_handler.reset();
42-
UnloadWallet(std::move(wallet));
43-
}
44-
19+
namespace wallet{
4520
static void AddTx(CWallet& wallet)
4621
{
4722
CMutableTransaction mtx;
@@ -66,7 +41,7 @@ static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
6641
create_flags = WALLET_FLAG_DESCRIPTORS;
6742
}
6843
auto database = CreateMockableWalletDatabase();
69-
auto wallet = BenchLoadWallet(std::move(database), context, create_flags);
44+
auto wallet = TestLoadWallet(std::move(database), context, create_flags);
7045

7146
// Generate a bunch of transactions and addresses to put into the wallet
7247
for (int i = 0; i < 1000; ++i) {
@@ -76,14 +51,14 @@ static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
7651
database = DuplicateMockDatabase(wallet->GetDatabase());
7752

7853
// reload the wallet for the actual benchmark
79-
BenchUnloadWallet(std::move(wallet));
54+
TestUnloadWallet(std::move(wallet));
8055

8156
bench.epochs(5).run([&] {
82-
wallet = BenchLoadWallet(std::move(database), context, create_flags);
57+
wallet = TestLoadWallet(std::move(database), context, create_flags);
8358

8459
// Cleanup
8560
database = DuplicateMockDatabase(wallet->GetDatabase());
86-
BenchUnloadWallet(std::move(wallet));
61+
TestUnloadWallet(std::move(wallet));
8762
});
8863
}
8964

@@ -96,3 +71,4 @@ BENCHMARK(WalletLoadingLegacy, benchmark::PriorityLevel::HIGH);
9671
static void WalletLoadingDescriptors(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/false); }
9772
BENCHMARK(WalletLoadingDescriptors, benchmark::PriorityLevel::HIGH);
9873
#endif
74+
} // namespace wallet

src/wallet/test/util.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <key_io.h>
1010
#include <streams.h>
1111
#include <test/util/setup_common.h>
12+
#include <wallet/context.h>
1213
#include <wallet/wallet.h>
1314
#include <wallet/walletdb.h>
1415

@@ -45,6 +46,36 @@ std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cc
4546
return wallet;
4647
}
4748

49+
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
50+
{
51+
bilingual_str error;
52+
std::vector<bilingual_str> warnings;
53+
auto wallet = CWallet::Create(context, "", std::move(database), create_flags, error, warnings);
54+
NotifyWalletLoaded(context, wallet);
55+
if (context.chain) {
56+
wallet->postInitProcess();
57+
}
58+
return wallet;
59+
}
60+
61+
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
62+
{
63+
DatabaseOptions options;
64+
options.create_flags = WALLET_FLAG_DESCRIPTORS;
65+
DatabaseStatus status;
66+
bilingual_str error;
67+
std::vector<bilingual_str> warnings;
68+
auto database = MakeWalletDatabase("", options, status, error);
69+
return TestLoadWallet(std::move(database), context, options.create_flags);
70+
}
71+
72+
void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
73+
{
74+
SyncWithValidationInterfaceQueue();
75+
wallet->m_chain_notifications_handler.reset();
76+
UnloadWallet(std::move(wallet));
77+
}
78+
4879
std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database)
4980
{
5081
return std::make_unique<MockableDatabase>(dynamic_cast<MockableDatabase&>(database).m_records);

src/wallet/test/util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Chain;
2121
namespace wallet {
2222
class CWallet;
2323
class WalletDatabase;
24+
struct WalletContext;
2425

2526
static const DatabaseFormat DATABASE_FORMATS[] = {
2627
#ifdef USE_SQLITE
@@ -31,8 +32,14 @@ static const DatabaseFormat DATABASE_FORMATS[] = {
3132
#endif
3233
};
3334

35+
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
36+
3437
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
3538

39+
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
40+
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
41+
void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
42+
3643
// Creates a copy of the provided database
3744
std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
3845

src/wallet/test/wallet_tests.cpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,6 @@ static_assert(WALLET_INCREMENTAL_RELAY_FEE >= DEFAULT_INCREMENTAL_RELAY_FEE, "wa
4242

4343
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
4444

45-
static std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
46-
{
47-
DatabaseOptions options;
48-
options.create_flags = WALLET_FLAG_DESCRIPTORS;
49-
DatabaseStatus status;
50-
bilingual_str error;
51-
std::vector<bilingual_str> warnings;
52-
auto database = MakeWalletDatabase("", options, status, error);
53-
auto wallet = CWallet::Create(context, "", std::move(database), options.create_flags, error, warnings);
54-
NotifyWalletLoaded(context, wallet);
55-
return wallet;
56-
}
57-
58-
static void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
59-
{
60-
SyncWithValidationInterfaceQueue();
61-
wallet->m_chain_notifications_handler.reset();
62-
UnloadWallet(std::move(wallet));
63-
}
64-
6545
static CMutableTransaction TestSimpleSpend(const CTransaction& from, uint32_t index, const CKey& key, const CScript& pubkey)
6646
{
6747
CMutableTransaction mtx;
@@ -845,10 +825,11 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
845825

846826
// Reload wallet and make sure new transactions are detected despite events
847827
// being blocked
828+
// Loading will also ask for current mempool transactions
848829
wallet = TestLoadWallet(context);
849830
BOOST_CHECK(rescan_completed);
850-
// AddToWallet events for block_tx and mempool_tx
851-
BOOST_CHECK_EQUAL(addtx_count, 2);
831+
// AddToWallet events for block_tx and mempool_tx (x2)
832+
BOOST_CHECK_EQUAL(addtx_count, 3);
852833
{
853834
LOCK(wallet->cs_wallet);
854835
BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_tx.GetHash()), 1U);
@@ -862,7 +843,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
862843
SyncWithValidationInterfaceQueue();
863844
// AddToWallet events for block_tx and mempool_tx events are counted a
864845
// second time as the notification queue is processed
865-
BOOST_CHECK_EQUAL(addtx_count, 4);
846+
BOOST_CHECK_EQUAL(addtx_count, 5);
866847

867848

868849
TestUnloadWallet(std::move(wallet));
@@ -885,7 +866,9 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
885866
SyncWithValidationInterfaceQueue();
886867
});
887868
wallet = TestLoadWallet(context);
888-
BOOST_CHECK_EQUAL(addtx_count, 2);
869+
// Since mempool transactions are requested at the end of loading, there will
870+
// be 2 additional AddToWallet calls, one from the previous test, and a duplicate for mempool_tx
871+
BOOST_CHECK_EQUAL(addtx_count, 2 + 2);
889872
{
890873
LOCK(wallet->cs_wallet);
891874
BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_tx.GetHash()), 1U);

0 commit comments

Comments
 (0)