/root/bitcoin/src/wallet/test/util.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2021-2022 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 <streams.h> |
11 | | #include <test/util/setup_common.h> |
12 | | #include <validationinterface.h> |
13 | | #include <wallet/context.h> |
14 | | #include <wallet/wallet.h> |
15 | | #include <wallet/walletdb.h> |
16 | | |
17 | | #include <memory> |
18 | | |
19 | | namespace wallet { |
20 | | std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key) |
21 | 0 | { |
22 | 0 | auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase()); |
23 | 0 | { |
24 | 0 | LOCK2(wallet->cs_wallet, ::cs_main); |
25 | 0 | wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash()); |
26 | 0 | } |
27 | 0 | { |
28 | 0 | LOCK(wallet->cs_wallet); |
29 | 0 | wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS); |
30 | 0 | wallet->SetupDescriptorScriptPubKeyMans(); |
31 | |
|
32 | 0 | FlatSigningProvider provider; |
33 | 0 | std::string error; |
34 | 0 | auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false); |
35 | 0 | assert(descs.size() == 1); |
36 | 0 | auto& desc = descs.at(0); |
37 | 0 | WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1); |
38 | 0 | if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false); |
39 | 0 | } |
40 | 0 | WalletRescanReserver reserver(*wallet); |
41 | 0 | reserver.reserve(); |
42 | 0 | CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false); |
43 | 0 | assert(result.status == CWallet::ScanResult::SUCCESS); |
44 | 0 | assert(result.last_scanned_block == cchain.Tip()->GetBlockHash()); |
45 | 0 | assert(*result.last_scanned_height == cchain.Height()); |
46 | 0 | assert(result.last_failed_block.IsNull()); |
47 | 0 | return wallet; |
48 | 0 | } |
49 | | |
50 | | std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags) |
51 | 0 | { |
52 | 0 | bilingual_str error; |
53 | 0 | std::vector<bilingual_str> warnings; |
54 | 0 | auto wallet = CWallet::Create(context, "", std::move(database), create_flags, error, warnings); |
55 | 0 | NotifyWalletLoaded(context, wallet); |
56 | 0 | if (context.chain) { |
57 | 0 | wallet->postInitProcess(); |
58 | 0 | } |
59 | 0 | return wallet; |
60 | 0 | } |
61 | | |
62 | | std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context) |
63 | 0 | { |
64 | 0 | DatabaseOptions options; |
65 | 0 | options.create_flags = WALLET_FLAG_DESCRIPTORS; |
66 | 0 | DatabaseStatus status; |
67 | 0 | bilingual_str error; |
68 | 0 | std::vector<bilingual_str> warnings; |
69 | 0 | auto database = MakeWalletDatabase("", options, status, error); |
70 | 0 | return TestLoadWallet(std::move(database), context, options.create_flags); |
71 | 0 | } |
72 | | |
73 | | void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet) |
74 | 0 | { |
75 | | // Calls SyncWithValidationInterfaceQueue |
76 | 0 | wallet->chain().waitForNotificationsIfTipChanged({}); |
77 | 0 | wallet->m_chain_notifications_handler.reset(); |
78 | 0 | WaitForDeleteWallet(std::move(wallet)); |
79 | 0 | } |
80 | | |
81 | | std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database) |
82 | 0 | { |
83 | 0 | return std::make_unique<MockableDatabase>(dynamic_cast<MockableDatabase&>(database).m_records); |
84 | 0 | } |
85 | | |
86 | | std::string getnewaddress(CWallet& w) |
87 | 0 | { |
88 | 0 | constexpr auto output_type = OutputType::BECH32; |
89 | 0 | return EncodeDestination(getNewDestination(w, output_type)); |
90 | 0 | } |
91 | | |
92 | | CTxDestination getNewDestination(CWallet& w, OutputType output_type) |
93 | 0 | { |
94 | 0 | return *Assert(w.GetNewDestination(output_type, "")); |
95 | 0 | } |
96 | | |
97 | | MockableCursor::MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix) |
98 | 0 | { |
99 | 0 | m_pass = pass; |
100 | 0 | std::tie(m_cursor, m_cursor_end) = records.equal_range(BytePrefix{prefix}); |
101 | 0 | } |
102 | | |
103 | | DatabaseCursor::Status MockableCursor::Next(DataStream& key, DataStream& value) |
104 | 0 | { |
105 | 0 | if (!m_pass) { |
106 | 0 | return Status::FAIL; |
107 | 0 | } |
108 | 0 | if (m_cursor == m_cursor_end) { |
109 | 0 | return Status::DONE; |
110 | 0 | } |
111 | 0 | key.clear(); |
112 | 0 | value.clear(); |
113 | 0 | const auto& [key_data, value_data] = *m_cursor; |
114 | 0 | key.write(key_data); |
115 | 0 | value.write(value_data); |
116 | 0 | m_cursor++; |
117 | 0 | return Status::MORE; |
118 | 0 | } |
119 | | |
120 | | bool MockableBatch::ReadKey(DataStream&& key, DataStream& value) |
121 | 0 | { |
122 | 0 | if (!m_pass) { |
123 | 0 | return false; |
124 | 0 | } |
125 | 0 | SerializeData key_data{key.begin(), key.end()}; |
126 | 0 | const auto& it = m_records.find(key_data); |
127 | 0 | if (it == m_records.end()) { |
128 | 0 | return false; |
129 | 0 | } |
130 | 0 | value.clear(); |
131 | 0 | value.write(it->second); |
132 | 0 | return true; |
133 | 0 | } |
134 | | |
135 | | bool MockableBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite) |
136 | 0 | { |
137 | 0 | if (!m_pass) { |
138 | 0 | return false; |
139 | 0 | } |
140 | 0 | SerializeData key_data{key.begin(), key.end()}; |
141 | 0 | SerializeData value_data{value.begin(), value.end()}; |
142 | 0 | auto [it, inserted] = m_records.emplace(key_data, value_data); |
143 | 0 | if (!inserted && overwrite) { // Overwrite if requested |
144 | 0 | it->second = value_data; |
145 | 0 | inserted = true; |
146 | 0 | } |
147 | 0 | return inserted; |
148 | 0 | } |
149 | | |
150 | | bool MockableBatch::EraseKey(DataStream&& key) |
151 | 0 | { |
152 | 0 | if (!m_pass) { |
153 | 0 | return false; |
154 | 0 | } |
155 | 0 | SerializeData key_data{key.begin(), key.end()}; |
156 | 0 | m_records.erase(key_data); |
157 | 0 | return true; |
158 | 0 | } |
159 | | |
160 | | bool MockableBatch::HasKey(DataStream&& key) |
161 | 0 | { |
162 | 0 | if (!m_pass) { |
163 | 0 | return false; |
164 | 0 | } |
165 | 0 | SerializeData key_data{key.begin(), key.end()}; |
166 | 0 | return m_records.count(key_data) > 0; |
167 | 0 | } |
168 | | |
169 | | bool MockableBatch::ErasePrefix(Span<const std::byte> prefix) |
170 | 0 | { |
171 | 0 | if (!m_pass) { |
172 | 0 | return false; |
173 | 0 | } |
174 | 0 | auto it = m_records.begin(); |
175 | 0 | while (it != m_records.end()) { |
176 | 0 | auto& key = it->first; |
177 | 0 | if (key.size() < prefix.size() || std::search(key.begin(), key.end(), prefix.begin(), prefix.end()) != key.begin()) { |
178 | 0 | it++; |
179 | 0 | continue; |
180 | 0 | } |
181 | 0 | it = m_records.erase(it); |
182 | 0 | } |
183 | 0 | return true; |
184 | 0 | } |
185 | | |
186 | | std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records) |
187 | 0 | { |
188 | 0 | return std::make_unique<MockableDatabase>(records); |
189 | 0 | } |
190 | | |
191 | | MockableDatabase& GetMockableDatabase(CWallet& wallet) |
192 | 0 | { |
193 | 0 | return dynamic_cast<MockableDatabase&>(wallet.GetDatabase()); |
194 | 0 | } |
195 | | |
196 | | wallet::ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success) |
197 | 0 | { |
198 | 0 | keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS); |
199 | |
|
200 | 0 | FlatSigningProvider keys; |
201 | 0 | std::string error; |
202 | 0 | auto parsed_descs = Parse(desc_str, keys, error, false); |
203 | 0 | Assert(success == (!parsed_descs.empty())); |
204 | 0 | if (!success) return nullptr; |
205 | 0 | auto& desc = parsed_descs.at(0); |
206 | |
|
207 | 0 | const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1; |
208 | |
|
209 | 0 | WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index); |
210 | |
|
211 | 0 | LOCK(keystore.cs_wallet); |
212 | |
|
213 | 0 | return Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false)); |
214 | 0 | }; |
215 | | } // namespace wallet |