From fa46ac3127142358116c8473741708b3bb70ca15 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 10 Apr 2019 15:06:31 -0400 Subject: [PATCH 1/2] bench: Add wallet_balance benchmarks --- .../bench_bitcoin/bench_bitcoin.vcxproj | 1 + src/Makefile.bench.include | 1 + src/bench/wallet_balance.cpp | 68 +++++++++++++++++++ src/test/util.cpp | 3 +- src/test/util.h | 4 ++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/bench/wallet_balance.cpp diff --git a/build_msvc/bench_bitcoin/bench_bitcoin.vcxproj b/build_msvc/bench_bitcoin/bench_bitcoin.vcxproj index 7e27127f1..723e230d3 100644 --- a/build_msvc/bench_bitcoin/bench_bitcoin.vcxproj +++ b/build_msvc/bench_bitcoin/bench_bitcoin.vcxproj @@ -39,6 +39,7 @@ + diff --git a/src/Makefile.bench.include b/src/Makefile.bench.include index d93161a90..ae7eb19ce 100644 --- a/src/Makefile.bench.include +++ b/src/Makefile.bench.include @@ -64,6 +64,7 @@ endif if ENABLE_WALLET bench_bench_bitcoin_SOURCES += bench/coin_selection.cpp +bench_bench_bitcoin_SOURCES += bench/wallet_balance.cpp endif bench_bench_bitcoin_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(CRYPTO_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(MINIUPNPC_LIBS) diff --git a/src/bench/wallet_balance.cpp b/src/bench/wallet_balance.cpp new file mode 100644 index 000000000..7fd279416 --- /dev/null +++ b/src/bench/wallet_balance.cpp @@ -0,0 +1,68 @@ +// Copyright (c) 2012-2019 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 +#include +#include +#include +#include +#include +#include + +struct WalletTestingSetup { + std::unique_ptr m_chain = interfaces::MakeChain(); + CWallet m_wallet; + + WalletTestingSetup() + : m_wallet{m_chain.get(), WalletLocation(), WalletDatabase::CreateMock()} + { + } + + void handleNotifications() + { + m_wallet.m_chain_notifications_handler = m_chain->handleNotifications(m_wallet); + } +}; + +static void WalletBalance(benchmark::State& state, const bool set_dirty, const bool add_watchonly, const bool add_mine) +{ + const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE; + + WalletTestingSetup wallet_t{}; + auto& wallet = wallet_t.m_wallet; + { + bool first_run; + if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false); + wallet_t.handleNotifications(); + } + + + const Optional address_mine{add_mine ? Optional{getnewaddress(wallet)} : nullopt}; + if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY); + + for (int i = 0; i < 100; ++i) { + generatetoaddress(address_mine.get_value_or(ADDRESS_WATCHONLY)); + generatetoaddress(ADDRESS_WATCHONLY); + } + SyncWithValidationInterfaceQueue(); + + auto bal = wallet.GetBalance(); // Cache + + while (state.KeepRunning()) { + if (set_dirty) wallet.MarkDirty(); + bal = wallet.GetBalance(); + if (add_mine) assert(bal.m_mine_trusted > 0); + if (add_watchonly) assert(bal.m_watchonly_trusted > 0); + } +} + +static void WalletBalanceDirty(benchmark::State& state) { WalletBalance(state, /* set_dirty */ true, /* add_watchonly */ true, /* add_mine */ true); } +static void WalletBalanceClean(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ true); } +static void WalletBalanceMine(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ false, /* add_mine */ true); } +static void WalletBalanceWatch(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ false); } + +BENCHMARK(WalletBalanceDirty, 2500); +BENCHMARK(WalletBalanceClean, 8000); +BENCHMARK(WalletBalanceMine, 16000); +BENCHMARK(WalletBalanceWatch, 8000); diff --git a/src/test/util.cpp b/src/test/util.cpp index 7c348c5b2..05d3a97a5 100644 --- a/src/test/util.cpp +++ b/src/test/util.cpp @@ -22,6 +22,8 @@ #include +const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj"; + #ifdef ENABLE_WALLET std::string getnewaddress(CWallet& w) { @@ -75,7 +77,6 @@ CTxIn MineBlock(const CScript& coinbase_scriptPubKey) return CTxIn{block->vtx[0]->GetHash(), 0}; } - std::shared_ptr PrepareBlock(const CScript& coinbase_scriptPubKey) { auto block = std::make_shared( diff --git a/src/test/util.h b/src/test/util.h index e51e924f6..8ba647ec3 100644 --- a/src/test/util.h +++ b/src/test/util.h @@ -13,6 +13,10 @@ class CScript; class CTxIn; class CWallet; +// Constants // + +extern const std::string ADDRESS_BCRT1_UNSPENDABLE; + // Lower-level utils // /** Returns the generated coin */ From fad7c33342cb51b310a7dd372bfa675df8810367 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 17 Apr 2019 13:27:02 -0400 Subject: [PATCH 2/2] refactor: Add handleNotifications method to wallet Further stylistic cleanups in touched files: * Sort the includes * Wrap long single-line constructors into multiple lines --- src/bench/wallet_balance.cpp | 21 +++------------------ src/wallet/test/wallet_test_fixture.cpp | 7 ++++--- src/wallet/wallet.cpp | 13 +++++++++---- src/wallet/wallet.h | 18 +++++++++++------- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/bench/wallet_balance.cpp b/src/bench/wallet_balance.cpp index 7fd279416..46ca12826 100644 --- a/src/bench/wallet_balance.cpp +++ b/src/bench/wallet_balance.cpp @@ -10,31 +10,16 @@ #include #include -struct WalletTestingSetup { - std::unique_ptr m_chain = interfaces::MakeChain(); - CWallet m_wallet; - - WalletTestingSetup() - : m_wallet{m_chain.get(), WalletLocation(), WalletDatabase::CreateMock()} - { - } - - void handleNotifications() - { - m_wallet.m_chain_notifications_handler = m_chain->handleNotifications(m_wallet); - } -}; - static void WalletBalance(benchmark::State& state, const bool set_dirty, const bool add_watchonly, const bool add_mine) { const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE; - WalletTestingSetup wallet_t{}; - auto& wallet = wallet_t.m_wallet; + std::unique_ptr chain = interfaces::MakeChain(); + CWallet wallet{chain.get(), WalletLocation(), WalletDatabase::CreateMock()}; { bool first_run; if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false); - wallet_t.handleNotifications(); + wallet.handleNotifications(); } diff --git a/src/wallet/test/wallet_test_fixture.cpp b/src/wallet/test/wallet_test_fixture.cpp index 7b8299453..6526e69ee 100644 --- a/src/wallet/test/wallet_test_fixture.cpp +++ b/src/wallet/test/wallet_test_fixture.cpp @@ -8,12 +8,13 @@ #include #include -WalletTestingSetup::WalletTestingSetup(const std::string& chainName): - TestingSetup(chainName), m_wallet(m_chain.get(), WalletLocation(), WalletDatabase::CreateMock()) +WalletTestingSetup::WalletTestingSetup(const std::string& chainName) + : TestingSetup(chainName), + m_wallet(m_chain.get(), WalletLocation(), WalletDatabase::CreateMock()) { bool fFirstRun; m_wallet.LoadWallet(fFirstRun); - m_wallet.m_chain_notifications_handler = m_chain->handleNotifications(m_wallet); + m_wallet.handleNotifications(); m_chain_client->registerRpcs(); } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 98ba48972..4737e2f6f 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -5,9 +5,8 @@ #include -#include #include -#include +#include #include #include #include @@ -16,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -34,6 +32,8 @@ #include #include #include +#include +#include #include #include @@ -4303,7 +4303,7 @@ std::shared_ptr CWallet::CreateWalletFromFile(interfaces::Chain& chain, chain.loadWallet(interfaces::MakeWallet(walletInstance)); // Register with the validation interface. It's ok to do this after rescan since we're still holding locked_chain. - walletInstance->m_chain_notifications_handler = chain.handleNotifications(*walletInstance); + walletInstance->handleNotifications(); walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST)); @@ -4316,6 +4316,11 @@ std::shared_ptr CWallet::CreateWalletFromFile(interfaces::Chain& chain, return walletInstance; } +void CWallet::handleNotifications() +{ + m_chain_notifications_handler = m_chain->handleNotifications(*this); +} + void CWallet::postInitProcess() { auto locked_chain = chain().lock(); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 53de1ea06..62ba0aa96 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -11,16 +11,16 @@ #include #include #include +#include