Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
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>
11#include <validationinterface.h>
12#include <wallet/context.h>
13#include <wallet/scan.h>
14#include <wallet/wallet.h>
15#include <wallet/walletdb.h>
16
17#include <sqlite3.h>
18
19#include <memory>
20
21namespace wallet {
22std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
23{
24 auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
25 {
26 LOCK2(wallet->cs_wallet, ::cs_main);
27 wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
28 }
29 {
30 LOCK(wallet->cs_wallet);
31 wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
32 wallet->SetupDescriptorScriptPubKeyMans();
33 }
34 AddKey(*wallet, key);
36 reserver.reserve();
37 ScanResult result = wallet->Scanner().Scan(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
39 assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());
40 assert(*result.last_scanned_height == cchain.Height());
42 return wallet;
43}
44
45void AddKey(CWallet& wallet, const CKey& key)
46{
47 LOCK(wallet.cs_wallet);
49 std::string error;
50 auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
51 assert(descs.size() == 1);
52 auto& desc = descs.at(0);
53 WalletDescriptor w_desc(std::move(desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1);
54 Assert(wallet.AddWalletDescriptor(w_desc, provider, /*label=*/"", /*internal=*/false));
55}
56
57std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
58{
60 std::vector<bilingual_str> _warnings;
61 auto wallet = CWallet::CreateNew(context, "", std::move(database), create_flags, /*born_encrypted=*/false, _error, _warnings);
63 if (context.chain) {
64 wallet->postInitProcess();
65 }
66 return wallet;
67}
68
69std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context)
70{
71 DatabaseOptions options;
72 options.require_create = true;
74 DatabaseStatus status;
75 bilingual_str error;
76 auto database = MakeWalletDatabase("", options, status, error);
77 return TestCreateWallet(std::move(database), context, options.create_flags);
78}
79
80
81std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context)
82{
83 bilingual_str error;
84 std::vector<bilingual_str> warnings;
85 auto wallet = CWallet::LoadExisting(context, "", std::move(database), error, warnings);
87 if (context.chain) {
88 wallet->postInitProcess();
89 }
90 return wallet;
91}
92
93std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
94{
95 DatabaseOptions options;
96 options.require_existing = true;
97 DatabaseStatus status;
98 bilingual_str error;
99 auto database = MakeWalletDatabase("", options, status, error);
100 return TestLoadWallet(std::move(database), context);
101}
102
103void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
104{
105 // Calls SyncWithValidationInterfaceQueue
106 wallet->chain().waitForNotificationsIfTipChanged({});
107 wallet->m_chain_notifications_handler.reset();
108 WaitForDeleteWallet(std::move(wallet));
109}
110
112{
113 constexpr auto output_type = OutputType::BECH32;
115}
116
118{
119 return *Assert(w.GetNewDestination(output_type, ""));
120}
121
125
126std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase()
127{
128 return std::make_unique<MockableSQLiteDatabase>();
129}
130
132{
133 keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
134
136 std::string error;
137 auto parsed_descs = Parse(desc_str, keys, error, false);
138 Assert(success == (!parsed_descs.empty()));
139 if (!success) return nullptr;
140 auto& desc = parsed_descs.at(0);
141
142 const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1;
143
144 WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index);
145
146 LOCK(keystore.cs_wallet);
147 auto spkm = Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false));
148 return &spkm.value().get();
149};
150} // namespace wallet
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
#define Assert(val)
Identity function.
Definition check.h:119
An in-memory indexed chain of blocks.
Definition chain.h:380
An encapsulated private key.
Definition key.h:46
constexpr bool IsNull() const
Definition uint256.h:50
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition chain.h:117
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition wallet.h:318
static std::shared_ptr< CWallet > LoadExisting(WalletContext &context, const std::string &name, std::unique_ptr< WalletDatabase > database, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition wallet.cpp:2998
static std::shared_ptr< CWallet > CreateNew(WalletContext &context, const std::string &name, std::unique_ptr< WalletDatabase > database, uint64_t wallet_creation_flags, bool born_encrypted, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition wallet.cpp:2936
An in-memory SQLiteDatabase.
Definition sqlite.h:181
Descriptor with some wallet metadata.
Definition walletutil.h:86
RAII object to check and reserve a wallet rescan.
Definition scan.h:37
static UniValue Parse(std::string_view raw, ParamFormat format=ParamFormat::JSON)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition client.cpp:411
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:8
std::string EncodeSecret(const CKey &key)
Definition key_io.cpp:233
std::string EncodeDestination(const CTxDestination &dest)
Definition key_io.cpp:292
wallet::DescriptorScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
Definition util.cpp:131
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase()
Definition util.cpp:126
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, const CKey &key)
Definition util.cpp:22
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition util.cpp:103
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
Definition wallet.cpp:2777
void NotifyWalletLoaded(WalletContext &context, const std::shared_ptr< CWallet > &wallet)
Definition wallet.cpp:235
void AddKey(CWallet &wallet, const CKey &key)
Definition util.cpp:45
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context)
Definition util.cpp:81
std::shared_ptr< CWallet > TestCreateWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition util.cpp:57
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition walletutil.h:53
void WaitForDeleteWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly delete the wallet.
Definition wallet.cpp:266
RPCMethod getnewaddress()
Definition addresses.cpp:21
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition util.cpp:117
DatabaseStatus
Definition db.h:180
OutputType
Definition outputtype.h:18
Bilingual messages:
Definition translation.h:24
uint64_t create_flags
Definition db.h:172
Result of a wallet scan.
Definition scan.h:19
uint256 last_scanned_block
Hash and height of most recent block that was successfully scanned.
Definition scan.h:25
enum wallet::ScanResult::@19 status
std::optional< int > last_scanned_height
Definition scan.h:26
uint256 last_failed_block
Height of the most recent block that could not be scanned due to read errors or pruning.
Definition scan.h:32
WalletContext struct containing references to state shared between CWallet instances,...
Definition context.h:36
interfaces::Chain * chain
Definition context.h:37
#define LOCK2(cs1, cs2)
Definition sync.h:269
#define LOCK(cs)
Definition sync.h:268
std::vector< uint16_t > keys
FuzzedDataProvider provider
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())
WalletContext context