1717#include < optional>
1818
1919using wallet::CWallet;
20+ using wallet::DatabaseFormat;
2021using wallet::DatabaseOptions;
21- using wallet::DatabaseStatus;
2222using wallet::ISMINE_SPENDABLE;
2323using wallet::MakeWalletDatabase;
2424using wallet::TxStateInactive;
2525using wallet::WALLET_FLAG_DESCRIPTORS;
2626using wallet::WalletContext;
27+ using wallet::WalletDatabase;
2728
28- static const std::shared_ptr<CWallet> BenchLoadWallet (WalletContext& context, DatabaseOptions& options)
29+ static const std::shared_ptr<CWallet> BenchLoadWallet (std::unique_ptr<WalletDatabase> database, WalletContext& context, DatabaseOptions& options)
2930{
30- DatabaseStatus status;
3131 bilingual_str error;
3232 std::vector<bilingual_str> warnings;
33- auto database = MakeWalletDatabase (" " , options, status, error);
34- assert (database);
3533 auto wallet = CWallet::Create (context, " " , std::move (database), options.create_flags , error, warnings);
3634 NotifyWalletLoaded (context, wallet);
3735 if (context.chain ) {
@@ -60,6 +58,30 @@ static void AddTx(CWallet& wallet)
6058 wallet.AddToWallet (MakeTransactionRef (mtx), TxStateInactive{});
6159}
6260
61+ static std::unique_ptr<WalletDatabase> DuplicateMockDatabase (WalletDatabase& database, DatabaseOptions& options)
62+ {
63+ auto new_database = CreateMockWalletDatabase (options);
64+
65+ // Get a cursor to the original database
66+ auto batch = database.MakeBatch ();
67+ batch->StartCursor ();
68+
69+ // Get a batch for the new database
70+ auto new_batch = new_database->MakeBatch ();
71+
72+ // Read all records from the original database and write them to the new one
73+ while (true ) {
74+ CDataStream key (SER_DISK, CLIENT_VERSION);
75+ CDataStream value (SER_DISK, CLIENT_VERSION);
76+ bool complete;
77+ batch->ReadAtCursor (key, value, complete);
78+ if (complete) break ;
79+ new_batch->Write (key, value);
80+ }
81+
82+ return new_database;
83+ }
84+
6385static void WalletLoading (benchmark::Bench& bench, bool legacy_wallet)
6486{
6587 const auto test_setup = MakeNoLogFileContext<TestingSetup>();
@@ -72,21 +94,30 @@ static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
7294 // Setup the wallet
7395 // Loading the wallet will also create it
7496 DatabaseOptions options;
75- if (!legacy_wallet) options.create_flags = WALLET_FLAG_DESCRIPTORS;
76- auto wallet = BenchLoadWallet (context, options);
97+ if (legacy_wallet) {
98+ options.require_format = DatabaseFormat::BERKELEY;
99+ } else {
100+ options.create_flags = WALLET_FLAG_DESCRIPTORS;
101+ options.require_format = DatabaseFormat::SQLITE;
102+ }
103+ auto database = CreateMockWalletDatabase (options);
104+ auto wallet = BenchLoadWallet (std::move (database), context, options);
77105
78106 // Generate a bunch of transactions and addresses to put into the wallet
79107 for (int i = 0 ; i < 1000 ; ++i) {
80108 AddTx (*wallet);
81109 }
82110
111+ database = DuplicateMockDatabase (wallet->GetDatabase (), options);
112+
83113 // reload the wallet for the actual benchmark
84114 BenchUnloadWallet (std::move (wallet));
85115
86116 bench.epochs (5 ).run ([&] {
87- wallet = BenchLoadWallet (context, options);
117+ wallet = BenchLoadWallet (std::move (database), context, options);
88118
89119 // Cleanup
120+ database = DuplicateMockDatabase (wallet->GetDatabase (), options);
90121 BenchUnloadWallet (std::move (wallet));
91122 });
92123}
0 commit comments