Skip to content

Commit c61d3f0

Browse files
committed
tests, bench: Consolidate {Test,Bench}Un/LoadWallet helper
The wallet tests and benchmarks both had helper functions for loading and unloading the wallet for the test that were almost identical. These functions are consolidated and reused.
1 parent 25202ca commit c61d3f0

File tree

5 files changed

+51
-60
lines changed

5 files changed

+51
-60
lines changed

src/bench/wallet_balance.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@
1414

1515
#include <optional>
1616

17-
using wallet::CWallet;
18-
using wallet::CreateMockableWalletDatabase;
19-
using wallet::DBErrors;
20-
using wallet::GetBalance;
21-
using wallet::WALLET_FLAG_DESCRIPTORS;
22-
2317
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
2418

19+
namespace wallet {
2520
static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)
2621
{
2722
const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
@@ -63,3 +58,4 @@ BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH);
6358
BENCHMARK(WalletBalanceClean, benchmark::PriorityLevel::HIGH);
6459
BENCHMARK(WalletBalanceMine, benchmark::PriorityLevel::HIGH);
6560
BENCHMARK(WalletBalanceWatch, benchmark::PriorityLevel::HIGH);
61+
} // 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: 5 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
@@ -33,6 +34,10 @@ static const DatabaseFormat DATABASE_FORMATS[] = {
3334

3435
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
3536

37+
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
38+
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
39+
void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
40+
3641
// Creates a copy of the provided database
3742
std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
3843

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)