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