17
17
#include < optional>
18
18
19
19
using wallet::CWallet;
20
+ using wallet::DatabaseFormat;
20
21
using wallet::DatabaseOptions;
21
- using wallet::DatabaseStatus;
22
22
using wallet::ISMINE_SPENDABLE;
23
23
using wallet::MakeWalletDatabase;
24
24
using wallet::TxStateInactive;
25
25
using wallet::WALLET_FLAG_DESCRIPTORS;
26
26
using wallet::WalletContext;
27
+ using wallet::WalletDatabase;
27
28
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)
29
30
{
30
- DatabaseStatus status;
31
31
bilingual_str error;
32
32
std::vector<bilingual_str> warnings;
33
- auto database = MakeWalletDatabase (" " , options, status, error);
34
- assert (database);
35
33
auto wallet = CWallet::Create (context, " " , std::move (database), options.create_flags , error, warnings);
36
34
NotifyWalletLoaded (context, wallet);
37
35
if (context.chain ) {
@@ -60,6 +58,30 @@ static void AddTx(CWallet& wallet)
60
58
wallet.AddToWallet (MakeTransactionRef (mtx), TxStateInactive{});
61
59
}
62
60
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
+
63
85
static void WalletLoading (benchmark::Bench& bench, bool legacy_wallet)
64
86
{
65
87
const auto test_setup = MakeNoLogFileContext<TestingSetup>();
@@ -72,21 +94,30 @@ static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
72
94
// Setup the wallet
73
95
// Loading the wallet will also create it
74
96
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);
77
105
78
106
// Generate a bunch of transactions and addresses to put into the wallet
79
107
for (int i = 0 ; i < 1000 ; ++i) {
80
108
AddTx (*wallet);
81
109
}
82
110
111
+ database = DuplicateMockDatabase (wallet->GetDatabase (), options);
112
+
83
113
// reload the wallet for the actual benchmark
84
114
BenchUnloadWallet (std::move (wallet));
85
115
86
116
bench.epochs (5 ).run ([&] {
87
- wallet = BenchLoadWallet (context, options);
117
+ wallet = BenchLoadWallet (std::move (database), context, options);
88
118
89
119
// Cleanup
120
+ database = DuplicateMockDatabase (wallet->GetDatabase (), options);
90
121
BenchUnloadWallet (std::move (wallet));
91
122
});
92
123
}
0 commit comments