/home/zip/work/bitcoin/src/wallet/test/util.cpp
Line | Count | Source |
1 | | // Copyright (c) 2021-present 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 <wallet/test/util.h> |
6 | | |
7 | | #include <chain.h> |
8 | | #include <key.h> |
9 | | #include <key_io.h> |
10 | | #include <test/util/setup_common.h> |
11 | | #include <validationinterface.h> |
12 | | #include <wallet/context.h> |
13 | | #include <wallet/wallet.h> |
14 | | #include <wallet/walletdb.h> |
15 | | |
16 | | #include <sqlite3.h> |
17 | | |
18 | | #include <memory> |
19 | | |
20 | | namespace wallet { |
21 | | std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key) |
22 | 0 | { |
23 | 0 | auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase()); |
24 | 0 | { |
25 | 0 | LOCK2(wallet->cs_wallet, ::cs_main); Line | Count | Source | 270 | 0 | UniqueLock criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \ | 271 | 0 | UniqueLock criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__) |
|
26 | 0 | wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash()); |
27 | 0 | } |
28 | 0 | { |
29 | 0 | LOCK(wallet->cs_wallet); Line | Count | Source | 268 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
30 | 0 | wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS); |
31 | 0 | wallet->SetupDescriptorScriptPubKeyMans(); |
32 | |
|
33 | 0 | FlatSigningProvider provider; |
34 | 0 | std::string error; |
35 | 0 | auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false); |
36 | 0 | assert(descs.size() == 1); |
37 | 0 | auto& desc = descs.at(0); |
38 | 0 | WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1); |
39 | 0 | Assert(wallet->AddWalletDescriptor(w_desc, provider, "", false)); Line | Count | Source | 116 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
40 | 0 | } |
41 | 0 | WalletRescanReserver reserver(*wallet); |
42 | 0 | reserver.reserve(); |
43 | 0 | CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false); |
44 | 0 | assert(result.status == CWallet::ScanResult::SUCCESS); |
45 | 0 | assert(result.last_scanned_block == cchain.Tip()->GetBlockHash()); |
46 | 0 | assert(*result.last_scanned_height == cchain.Height()); |
47 | 0 | assert(result.last_failed_block.IsNull()); |
48 | 0 | return wallet; |
49 | 0 | } |
50 | | |
51 | | std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags) |
52 | 0 | { |
53 | 0 | bilingual_str _error; |
54 | 0 | std::vector<bilingual_str> _warnings; |
55 | 0 | auto wallet = CWallet::CreateNew(context, "", std::move(database), create_flags, _error, _warnings); |
56 | 0 | NotifyWalletLoaded(context, wallet); |
57 | 0 | if (context.chain) { |
58 | 0 | wallet->postInitProcess(); |
59 | 0 | } |
60 | 0 | return wallet; |
61 | 0 | } |
62 | | |
63 | | std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context) |
64 | 0 | { |
65 | 0 | DatabaseOptions options; |
66 | 0 | options.require_create = true; |
67 | 0 | options.create_flags = WALLET_FLAG_DESCRIPTORS; |
68 | 0 | DatabaseStatus status; |
69 | 0 | bilingual_str error; |
70 | 0 | std::vector<bilingual_str> warnings; |
71 | 0 | auto database = MakeWalletDatabase("", options, status, error); |
72 | 0 | return TestCreateWallet(std::move(database), context, options.create_flags); |
73 | 0 | } |
74 | | |
75 | | |
76 | | std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context) |
77 | 0 | { |
78 | 0 | bilingual_str error; |
79 | 0 | std::vector<bilingual_str> warnings; |
80 | 0 | auto wallet = CWallet::LoadExisting(context, "", std::move(database), error, warnings); |
81 | 0 | NotifyWalletLoaded(context, wallet); |
82 | 0 | if (context.chain) { |
83 | 0 | wallet->postInitProcess(); |
84 | 0 | } |
85 | 0 | return wallet; |
86 | 0 | } |
87 | | |
88 | | std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context) |
89 | 0 | { |
90 | 0 | DatabaseOptions options; |
91 | 0 | options.require_existing = true; |
92 | 0 | DatabaseStatus status; |
93 | 0 | bilingual_str error; |
94 | 0 | std::vector<bilingual_str> warnings; |
95 | 0 | auto database = MakeWalletDatabase("", options, status, error); |
96 | 0 | return TestLoadWallet(std::move(database), context); |
97 | 0 | } |
98 | | |
99 | | void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet) |
100 | 0 | { |
101 | | // Calls SyncWithValidationInterfaceQueue |
102 | 0 | wallet->chain().waitForNotificationsIfTipChanged({}); |
103 | 0 | wallet->m_chain_notifications_handler.reset(); |
104 | 0 | WaitForDeleteWallet(std::move(wallet)); |
105 | 0 | } |
106 | | |
107 | | std::string getnewaddress(CWallet& w) |
108 | 0 | { |
109 | 0 | constexpr auto output_type = OutputType::BECH32; |
110 | 0 | return EncodeDestination(getNewDestination(w, output_type)); |
111 | 0 | } |
112 | | |
113 | | CTxDestination getNewDestination(CWallet& w, OutputType output_type) |
114 | 0 | { |
115 | 0 | return *Assert(w.GetNewDestination(output_type, "")); Line | Count | Source | 116 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
116 | 0 | } |
117 | | |
118 | | MockableSQLiteDatabase::MockableSQLiteDatabase() |
119 | 0 | : SQLiteDatabase(fs::PathFromString("mock/"), fs::PathFromString("mock/wallet.dat"), DatabaseOptions(), SQLITE_OPEN_MEMORY) |
120 | 0 | {} |
121 | | |
122 | | std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase() |
123 | 0 | { |
124 | 0 | return std::make_unique<MockableSQLiteDatabase>(); |
125 | 0 | } |
126 | | |
127 | | wallet::DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success) |
128 | 0 | { |
129 | 0 | keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS); |
130 | |
|
131 | 0 | FlatSigningProvider keys; |
132 | 0 | std::string error; |
133 | 0 | auto parsed_descs = Parse(desc_str, keys, error, false); |
134 | 0 | Assert(success == (!parsed_descs.empty())); Line | Count | Source | 116 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
135 | 0 | if (!success) return nullptr; |
136 | 0 | auto& desc = parsed_descs.at(0); |
137 | |
|
138 | 0 | const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1; |
139 | |
|
140 | 0 | WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index); |
141 | |
|
142 | 0 | LOCK(keystore.cs_wallet); Line | Count | Source | 268 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
143 | 0 | auto spkm = Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false)); Line | Count | Source | 116 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
144 | 0 | return &spkm.value().get(); |
145 | 0 | }; |
146 | | } // namespace wallet |