2022-04-18 15:52:26 -04:00
|
|
|
// Copyright (c) 2022 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 <bench/bench.h>
|
|
|
|
#include <interfaces/chain.h>
|
|
|
|
#include <node/context.h>
|
|
|
|
#include <test/util/mining.h>
|
|
|
|
#include <test/util/setup_common.h>
|
2022-08-19 11:13:41 -03:00
|
|
|
#include <wallet/test/util.h>
|
2022-04-18 15:52:26 -04:00
|
|
|
#include <util/translation.h>
|
|
|
|
#include <validationinterface.h>
|
|
|
|
#include <wallet/context.h>
|
|
|
|
#include <wallet/receive.h>
|
|
|
|
#include <wallet/wallet.h>
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
using wallet::CWallet;
|
2022-12-12 16:43:49 -05:00
|
|
|
using wallet::CreateMockableWalletDatabase;
|
2022-04-20 13:03:45 -04:00
|
|
|
using wallet::DatabaseFormat;
|
2022-04-18 15:52:26 -04:00
|
|
|
using wallet::DatabaseOptions;
|
2022-04-20 13:25:06 -04:00
|
|
|
using wallet::TxStateInactive;
|
2022-04-18 15:52:26 -04:00
|
|
|
using wallet::WALLET_FLAG_DESCRIPTORS;
|
|
|
|
using wallet::WalletContext;
|
2022-04-20 13:03:45 -04:00
|
|
|
using wallet::WalletDatabase;
|
2022-04-18 15:52:26 -04:00
|
|
|
|
2023-01-20 16:25:14 +01:00
|
|
|
static std::shared_ptr<CWallet> BenchLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, DatabaseOptions& options)
|
2022-04-18 15:52:26 -04:00
|
|
|
{
|
|
|
|
bilingual_str error;
|
|
|
|
std::vector<bilingual_str> warnings;
|
|
|
|
auto wallet = CWallet::Create(context, "", std::move(database), options.create_flags, error, warnings);
|
|
|
|
NotifyWalletLoaded(context, wallet);
|
|
|
|
if (context.chain) {
|
|
|
|
wallet->postInitProcess();
|
|
|
|
}
|
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BenchUnloadWallet(std::shared_ptr<CWallet>&& wallet)
|
|
|
|
{
|
|
|
|
SyncWithValidationInterfaceQueue();
|
|
|
|
wallet->m_chain_notifications_handler.reset();
|
|
|
|
UnloadWallet(std::move(wallet));
|
|
|
|
}
|
|
|
|
|
2022-04-20 13:25:06 -04:00
|
|
|
static void AddTx(CWallet& wallet)
|
|
|
|
{
|
|
|
|
CMutableTransaction mtx;
|
2022-07-21 08:30:39 -04:00
|
|
|
mtx.vout.push_back({COIN, GetScriptForDestination(*Assert(wallet.GetNewDestination(OutputType::BECH32, "")))});
|
2022-04-20 13:25:06 -04:00
|
|
|
mtx.vin.push_back(CTxIn());
|
|
|
|
|
|
|
|
wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});
|
|
|
|
}
|
|
|
|
|
2022-04-18 15:52:26 -04:00
|
|
|
static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
|
|
|
|
{
|
|
|
|
const auto test_setup = MakeNoLogFileContext<TestingSetup>();
|
2022-04-20 12:58:29 -04:00
|
|
|
test_setup->m_args.ForceSetArg("-unsafesqlitesync", "1");
|
2022-04-18 15:52:26 -04:00
|
|
|
|
|
|
|
WalletContext context;
|
|
|
|
context.args = &test_setup->m_args;
|
|
|
|
context.chain = test_setup->m_node.chain.get();
|
|
|
|
|
|
|
|
// Setup the wallet
|
|
|
|
// Loading the wallet will also create it
|
|
|
|
DatabaseOptions options;
|
2022-04-20 13:03:45 -04:00
|
|
|
if (legacy_wallet) {
|
|
|
|
options.require_format = DatabaseFormat::BERKELEY;
|
|
|
|
} else {
|
|
|
|
options.create_flags = WALLET_FLAG_DESCRIPTORS;
|
|
|
|
options.require_format = DatabaseFormat::SQLITE;
|
|
|
|
}
|
|
|
|
auto database = CreateMockWalletDatabase(options);
|
|
|
|
auto wallet = BenchLoadWallet(std::move(database), context, options);
|
2022-04-18 15:52:26 -04:00
|
|
|
|
|
|
|
// Generate a bunch of transactions and addresses to put into the wallet
|
2022-04-20 13:52:27 -04:00
|
|
|
for (int i = 0; i < 1000; ++i) {
|
2022-04-20 13:25:06 -04:00
|
|
|
AddTx(*wallet);
|
2022-04-18 15:52:26 -04:00
|
|
|
}
|
|
|
|
|
2022-04-20 13:03:45 -04:00
|
|
|
database = DuplicateMockDatabase(wallet->GetDatabase(), options);
|
|
|
|
|
2022-04-18 15:52:26 -04:00
|
|
|
// reload the wallet for the actual benchmark
|
|
|
|
BenchUnloadWallet(std::move(wallet));
|
|
|
|
|
2022-04-20 13:52:45 -04:00
|
|
|
bench.epochs(5).run([&] {
|
2022-04-20 13:03:45 -04:00
|
|
|
wallet = BenchLoadWallet(std::move(database), context, options);
|
2022-04-18 15:52:26 -04:00
|
|
|
|
|
|
|
// Cleanup
|
2022-04-20 13:03:45 -04:00
|
|
|
database = DuplicateMockDatabase(wallet->GetDatabase(), options);
|
2022-04-18 15:52:26 -04:00
|
|
|
BenchUnloadWallet(std::move(wallet));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-20 12:53:42 -04:00
|
|
|
#ifdef USE_BDB
|
2022-04-18 15:52:26 -04:00
|
|
|
static void WalletLoadingLegacy(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/true); }
|
2022-09-25 12:25:16 -03:00
|
|
|
BENCHMARK(WalletLoadingLegacy, benchmark::PriorityLevel::HIGH);
|
2022-04-20 12:53:42 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_SQLITE
|
|
|
|
static void WalletLoadingDescriptors(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/false); }
|
2022-09-25 12:25:16 -03:00
|
|
|
BENCHMARK(WalletLoadingDescriptors, benchmark::PriorityLevel::HIGH);
|
2022-04-20 12:53:42 -04:00
|
|
|
#endif
|