Skip to content

Commit dae7299

Browse files
author
MarcoFalke
committed
Merge #15779: test: Add wallet_balance benchmark
fad7c33 refactor: Add handleNotifications method to wallet (MarcoFalke) fa46ac3 bench: Add wallet_balance benchmarks (MarcoFalke) Pull request description: ACKs for commit fad7c3: ryanofsky: utACK fad7c33. I might squash or rearrange the commits to avoid adding code in one commit that just gets deleted in the next one. But overall this looks good and the cleanup is nice. Tree-SHA512: 231faac168cbe9bb0ab4bf10ac1d5b042c610364406d75061fba27f1e9d16c71867e74cc4606e9f42659aa980d7133c00e29fcc18bbba7da2fa7a80178b3246c
2 parents 429a7cf + fad7c33 commit dae7299

File tree

8 files changed

+85
-15
lines changed

8 files changed

+85
-15
lines changed

build_msvc/bench_bitcoin/bench_bitcoin.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<ClCompile Include="..\..\src\bench\rpc_mempool.cpp" />
4040
<ClCompile Include="..\..\src\bench\merkle_root.cpp" />
4141
<ClCompile Include="..\..\src\bench\rollingbloom.cpp" />
42+
<ClCompile Include="..\..\src\bench\wallet_balance.cpp" />
4243
<ClCompile Include="..\..\src\bench\verify_script.cpp" />
4344
</ItemGroup>
4445
<ItemGroup>

src/Makefile.bench.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ endif
6464

6565
if ENABLE_WALLET
6666
bench_bench_bitcoin_SOURCES += bench/coin_selection.cpp
67+
bench_bench_bitcoin_SOURCES += bench/wallet_balance.cpp
6768
endif
6869

6970
bench_bench_bitcoin_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(CRYPTO_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(MINIUPNPC_LIBS)

src/bench/wallet_balance.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2012-2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <bench/bench.h>
6+
#include <interfaces/chain.h>
7+
#include <key_io.h>
8+
#include <optional.h>
9+
#include <test/util.h>
10+
#include <validationinterface.h>
11+
#include <wallet/wallet.h>
12+
13+
static void WalletBalance(benchmark::State& state, const bool set_dirty, const bool add_watchonly, const bool add_mine)
14+
{
15+
const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
16+
17+
std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain();
18+
CWallet wallet{chain.get(), WalletLocation(), WalletDatabase::CreateMock()};
19+
{
20+
bool first_run;
21+
if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false);
22+
wallet.handleNotifications();
23+
}
24+
25+
26+
const Optional<std::string> address_mine{add_mine ? Optional<std::string>{getnewaddress(wallet)} : nullopt};
27+
if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY);
28+
29+
for (int i = 0; i < 100; ++i) {
30+
generatetoaddress(address_mine.get_value_or(ADDRESS_WATCHONLY));
31+
generatetoaddress(ADDRESS_WATCHONLY);
32+
}
33+
SyncWithValidationInterfaceQueue();
34+
35+
auto bal = wallet.GetBalance(); // Cache
36+
37+
while (state.KeepRunning()) {
38+
if (set_dirty) wallet.MarkDirty();
39+
bal = wallet.GetBalance();
40+
if (add_mine) assert(bal.m_mine_trusted > 0);
41+
if (add_watchonly) assert(bal.m_watchonly_trusted > 0);
42+
}
43+
}
44+
45+
static void WalletBalanceDirty(benchmark::State& state) { WalletBalance(state, /* set_dirty */ true, /* add_watchonly */ true, /* add_mine */ true); }
46+
static void WalletBalanceClean(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ true); }
47+
static void WalletBalanceMine(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ false, /* add_mine */ true); }
48+
static void WalletBalanceWatch(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ false); }
49+
50+
BENCHMARK(WalletBalanceDirty, 2500);
51+
BENCHMARK(WalletBalanceClean, 8000);
52+
BENCHMARK(WalletBalanceMine, 16000);
53+
BENCHMARK(WalletBalanceWatch, 8000);

src/test/util.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include <boost/thread.hpp>
2424

25+
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
26+
2527
#ifdef ENABLE_WALLET
2628
std::string getnewaddress(CWallet& w)
2729
{
@@ -75,7 +77,6 @@ CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
7577
return CTxIn{block->vtx[0]->GetHash(), 0};
7678
}
7779

78-
7980
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
8081
{
8182
auto block = std::make_shared<CBlock>(

src/test/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class CScript;
1313
class CTxIn;
1414
class CWallet;
1515

16+
// Constants //
17+
18+
extern const std::string ADDRESS_BCRT1_UNSPENDABLE;
19+
1620
// Lower-level utils //
1721

1822
/** Returns the generated coin */

src/wallet/test/wallet_test_fixture.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
#include <wallet/db.h>
99
#include <wallet/rpcwallet.h>
1010

11-
WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
12-
TestingSetup(chainName), m_wallet(m_chain.get(), WalletLocation(), WalletDatabase::CreateMock())
11+
WalletTestingSetup::WalletTestingSetup(const std::string& chainName)
12+
: TestingSetup(chainName),
13+
m_wallet(m_chain.get(), WalletLocation(), WalletDatabase::CreateMock())
1314
{
1415
bool fFirstRun;
1516
m_wallet.LoadWallet(fFirstRun);
16-
m_wallet.m_chain_notifications_handler = m_chain->handleNotifications(m_wallet);
17+
m_wallet.handleNotifications();
1718

1819
m_chain_client->registerRpcs();
1920
}

src/wallet/wallet.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
#include <wallet/wallet.h>
77

8-
#include <checkpoints.h>
98
#include <chain.h>
10-
#include <wallet/coincontrol.h>
9+
#include <checkpoints.h>
1110
#include <consensus/consensus.h>
1211
#include <consensus/validation.h>
1312
#include <fs.h>
@@ -16,7 +15,6 @@
1615
#include <key.h>
1716
#include <key_io.h>
1817
#include <keystore.h>
19-
#include <validation.h>
2018
#include <net.h>
2119
#include <policy/fees.h>
2220
#include <policy/policy.h>
@@ -34,6 +32,8 @@
3432
#include <util/moneystr.h>
3533
#include <util/rbf.h>
3634
#include <util/validation.h>
35+
#include <validation.h>
36+
#include <wallet/coincontrol.h>
3737
#include <wallet/fees.h>
3838

3939
#include <algorithm>
@@ -4303,7 +4303,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
43034303
chain.loadWallet(interfaces::MakeWallet(walletInstance));
43044304

43054305
// Register with the validation interface. It's ok to do this after rescan since we're still holding locked_chain.
4306-
walletInstance->m_chain_notifications_handler = chain.handleNotifications(*walletInstance);
4306+
walletInstance->handleNotifications();
43074307

43084308
walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
43094309

@@ -4316,6 +4316,11 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
43164316
return walletInstance;
43174317
}
43184318

4319+
void CWallet::handleNotifications()
4320+
{
4321+
m_chain_notifications_handler = m_chain->handleNotifications(*this);
4322+
}
4323+
43194324
void CWallet::postInitProcess()
43204325
{
43214326
auto locked_chain = chain().lock();

src/wallet/wallet.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
#include <interfaces/handler.h>
1212
#include <outputtype.h>
1313
#include <policy/feerate.h>
14+
#include <script/ismine.h>
15+
#include <script/sign.h>
1416
#include <streams.h>
1517
#include <tinyformat.h>
1618
#include <ui_interface.h>
1719
#include <util/strencodings.h>
18-
#include <validationinterface.h>
19-
#include <script/ismine.h>
20-
#include <script/sign.h>
2120
#include <util/system.h>
22-
#include <wallet/crypter.h>
21+
#include <validationinterface.h>
2322
#include <wallet/coinselection.h>
23+
#include <wallet/crypter.h>
2424
#include <wallet/walletdb.h>
2525
#include <wallet/walletutil.h>
2626

@@ -767,7 +767,10 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
767767
unsigned int nMasterKeyMaxID = 0;
768768

769769
/** Construct wallet with specified name and database implementation. */
770-
CWallet(interfaces::Chain* chain, const WalletLocation& location, std::unique_ptr<WalletDatabase> database) : m_chain(chain), m_location(location), database(std::move(database))
770+
CWallet(interfaces::Chain* chain, const WalletLocation& location, std::unique_ptr<WalletDatabase> database)
771+
: m_chain(chain),
772+
m_location(location),
773+
database(std::move(database))
771774
{
772775
}
773776

@@ -794,6 +797,9 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
794797
/** Registered interfaces::Chain::Notifications handler. */
795798
std::unique_ptr<interfaces::Handler> m_chain_notifications_handler;
796799

800+
/** Register the wallet for chain notifications */
801+
void handleNotifications();
802+
797803
/** Interface for accessing chain state. */
798804
interfaces::Chain& chain() const { assert(m_chain); return *m_chain; }
799805

@@ -1208,8 +1214,6 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
12081214

12091215
/** Add a KeyOriginInfo to the wallet */
12101216
bool AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info);
1211-
1212-
friend struct WalletTestingSetup;
12131217
};
12141218

12151219
/**

0 commit comments

Comments
 (0)