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