Branch data Line data Source code
# 1 : : // Copyright (c) 2012-2021 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/wallet.h>
# 6 : :
# 7 : : #include <any>
# 8 : : #include <future>
# 9 : : #include <memory>
# 10 : : #include <stdint.h>
# 11 : : #include <vector>
# 12 : :
# 13 : : #include <interfaces/chain.h>
# 14 : : #include <key_io.h>
# 15 : : #include <node/blockstorage.h>
# 16 : : #include <node/context.h>
# 17 : : #include <policy/policy.h>
# 18 : : #include <rpc/server.h>
# 19 : : #include <test/util/logging.h>
# 20 : : #include <test/util/setup_common.h>
# 21 : : #include <util/translation.h>
# 22 : : #include <validation.h>
# 23 : : #include <wallet/coincontrol.h>
# 24 : : #include <wallet/context.h>
# 25 : : #include <wallet/receive.h>
# 26 : : #include <wallet/spend.h>
# 27 : : #include <wallet/test/util.h>
# 28 : : #include <wallet/test/wallet_test_fixture.h>
# 29 : :
# 30 : : #include <boost/test/unit_test.hpp>
# 31 : : #include <univalue.h>
# 32 : :
# 33 : : using node::MAX_BLOCKFILE_SIZE;
# 34 : : using node::UnlinkPrunedFiles;
# 35 : :
# 36 : : namespace wallet {
# 37 : : RPCHelpMan importmulti();
# 38 : : RPCHelpMan dumpwallet();
# 39 : : RPCHelpMan importwallet();
# 40 : :
# 41 : : // Ensure that fee levels defined in the wallet are at least as high
# 42 : : // as the default levels for node policy.
# 43 : : static_assert(DEFAULT_TRANSACTION_MINFEE >= DEFAULT_MIN_RELAY_TX_FEE, "wallet minimum fee is smaller than default relay fee");
# 44 : : static_assert(WALLET_INCREMENTAL_RELAY_FEE >= DEFAULT_INCREMENTAL_RELAY_FEE, "wallet incremental fee is smaller than default incremental relay fee");
# 45 : :
# 46 : : BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
# 47 : :
# 48 : : static const std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
# 49 : 5 : {
# 50 : 5 : DatabaseOptions options;
# 51 : 5 : options.create_flags = WALLET_FLAG_DESCRIPTORS;
# 52 : 5 : DatabaseStatus status;
# 53 : 5 : bilingual_str error;
# 54 : 5 : std::vector<bilingual_str> warnings;
# 55 : 5 : auto database = MakeWalletDatabase("", options, status, error);
# 56 : 5 : auto wallet = CWallet::Create(context, "", std::move(database), options.create_flags, error, warnings);
# 57 : 5 : NotifyWalletLoaded(context, wallet);
# 58 [ + + ]: 5 : if (context.chain) {
# 59 : 4 : wallet->postInitProcess();
# 60 : 4 : }
# 61 : 5 : return wallet;
# 62 : 5 : }
# 63 : :
# 64 : : static void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
# 65 : 4 : {
# 66 : 4 : SyncWithValidationInterfaceQueue();
# 67 : 4 : wallet->m_chain_notifications_handler.reset();
# 68 : 4 : UnloadWallet(std::move(wallet));
# 69 : 4 : }
# 70 : :
# 71 : : static CMutableTransaction TestSimpleSpend(const CTransaction& from, uint32_t index, const CKey& key, const CScript& pubkey)
# 72 : 5 : {
# 73 : 5 : CMutableTransaction mtx;
# 74 : 5 : mtx.vout.push_back({from.vout[index].nValue - DEFAULT_TRANSACTION_MAXFEE, pubkey});
# 75 : 5 : mtx.vin.push_back({CTxIn{from.GetHash(), index}});
# 76 : 5 : FillableSigningProvider keystore;
# 77 : 5 : keystore.AddKey(key);
# 78 : 5 : std::map<COutPoint, Coin> coins;
# 79 : 5 : coins[mtx.vin[0].prevout].out = from.vout[index];
# 80 : 5 : std::map<int, bilingual_str> input_errors;
# 81 : 5 : BOOST_CHECK(SignTransaction(mtx, &keystore, coins, SIGHASH_ALL, input_errors));
# 82 : 5 : return mtx;
# 83 : 5 : }
# 84 : :
# 85 : : static void AddKey(CWallet& wallet, const CKey& key)
# 86 : 7 : {
# 87 : 7 : LOCK(wallet.cs_wallet);
# 88 : 7 : FlatSigningProvider provider;
# 89 : 7 : std::string error;
# 90 : 7 : std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
# 91 : 7 : assert(desc);
# 92 : 0 : WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
# 93 [ - + ]: 7 : if (!wallet.AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
# 94 : 7 : }
# 95 : :
# 96 : : BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
# 97 : 1 : {
# 98 : : // Cap last block file size, and mine new block in a new block file.
# 99 : 1 : CBlockIndex* oldTip = m_node.chainman->ActiveChain().Tip();
# 100 : 1 : WITH_LOCK(::cs_main, m_node.chainman->m_blockman.GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
# 101 : 1 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
# 102 : 1 : CBlockIndex* newTip = m_node.chainman->ActiveChain().Tip();
# 103 : :
# 104 : : // Verify ScanForWalletTransactions fails to read an unknown start block.
# 105 : 1 : {
# 106 : 1 : CWallet wallet(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 107 : 1 : {
# 108 : 1 : LOCK(wallet.cs_wallet);
# 109 : 1 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
# 110 : 1 : wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
# 111 : 1 : }
# 112 : 1 : AddKey(wallet, coinbaseKey);
# 113 : 1 : WalletRescanReserver reserver(wallet);
# 114 : 1 : reserver.reserve();
# 115 : 1 : CWallet::ScanResult result = wallet.ScanForWalletTransactions({} /* start_block */, 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
# 116 : 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
# 117 : 1 : BOOST_CHECK(result.last_failed_block.IsNull());
# 118 : 1 : BOOST_CHECK(result.last_scanned_block.IsNull());
# 119 : 1 : BOOST_CHECK(!result.last_scanned_height);
# 120 : 1 : BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 0);
# 121 : 1 : }
# 122 : :
# 123 : : // Verify ScanForWalletTransactions picks up transactions in both the old
# 124 : : // and new block files.
# 125 : 1 : {
# 126 : 1 : CWallet wallet(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 127 : 1 : {
# 128 : 1 : LOCK(wallet.cs_wallet);
# 129 : 1 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
# 130 : 1 : wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
# 131 : 1 : }
# 132 : 1 : AddKey(wallet, coinbaseKey);
# 133 : 1 : WalletRescanReserver reserver(wallet);
# 134 : 1 : reserver.reserve();
# 135 : 1 : CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */);
# 136 : 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
# 137 : 1 : BOOST_CHECK(result.last_failed_block.IsNull());
# 138 : 1 : BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
# 139 : 1 : BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
# 140 : 1 : BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 100 * COIN);
# 141 : 1 : }
# 142 : :
# 143 : : // Prune the older block file.
# 144 : 1 : int file_number;
# 145 : 1 : {
# 146 : 1 : LOCK(cs_main);
# 147 : 1 : file_number = oldTip->GetBlockPos().nFile;
# 148 : 1 : Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
# 149 : 1 : }
# 150 : 1 : UnlinkPrunedFiles({file_number});
# 151 : :
# 152 : : // Verify ScanForWalletTransactions only picks transactions in the new block
# 153 : : // file.
# 154 : 1 : {
# 155 : 1 : CWallet wallet(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 156 : 1 : {
# 157 : 1 : LOCK(wallet.cs_wallet);
# 158 : 1 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
# 159 : 1 : wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
# 160 : 1 : }
# 161 : 1 : AddKey(wallet, coinbaseKey);
# 162 : 1 : WalletRescanReserver reserver(wallet);
# 163 : 1 : reserver.reserve();
# 164 : 1 : CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */);
# 165 : 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
# 166 : 1 : BOOST_CHECK_EQUAL(result.last_failed_block, oldTip->GetBlockHash());
# 167 : 1 : BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
# 168 : 1 : BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
# 169 : 1 : BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 50 * COIN);
# 170 : 1 : }
# 171 : :
# 172 : : // Prune the remaining block file.
# 173 : 1 : {
# 174 : 1 : LOCK(cs_main);
# 175 : 1 : file_number = newTip->GetBlockPos().nFile;
# 176 : 1 : Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
# 177 : 1 : }
# 178 : 1 : UnlinkPrunedFiles({file_number});
# 179 : :
# 180 : : // Verify ScanForWalletTransactions scans no blocks.
# 181 : 1 : {
# 182 : 1 : CWallet wallet(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 183 : 1 : {
# 184 : 1 : LOCK(wallet.cs_wallet);
# 185 : 1 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
# 186 : 1 : wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
# 187 : 1 : }
# 188 : 1 : AddKey(wallet, coinbaseKey);
# 189 : 1 : WalletRescanReserver reserver(wallet);
# 190 : 1 : reserver.reserve();
# 191 : 1 : CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */);
# 192 : 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
# 193 : 1 : BOOST_CHECK_EQUAL(result.last_failed_block, newTip->GetBlockHash());
# 194 : 1 : BOOST_CHECK(result.last_scanned_block.IsNull());
# 195 : 1 : BOOST_CHECK(!result.last_scanned_height);
# 196 : 1 : BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 0);
# 197 : 1 : }
# 198 : 1 : }
# 199 : :
# 200 : : BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
# 201 : 1 : {
# 202 : : // Cap last block file size, and mine new block in a new block file.
# 203 : 1 : CBlockIndex* oldTip = m_node.chainman->ActiveChain().Tip();
# 204 : 1 : WITH_LOCK(::cs_main, m_node.chainman->m_blockman.GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
# 205 : 1 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
# 206 : 1 : CBlockIndex* newTip = m_node.chainman->ActiveChain().Tip();
# 207 : :
# 208 : : // Prune the older block file.
# 209 : 1 : int file_number;
# 210 : 1 : {
# 211 : 1 : LOCK(cs_main);
# 212 : 1 : file_number = oldTip->GetBlockPos().nFile;
# 213 : 1 : Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
# 214 : 1 : }
# 215 : 1 : UnlinkPrunedFiles({file_number});
# 216 : :
# 217 : : // Verify importmulti RPC returns failure for a key whose creation time is
# 218 : : // before the missing block, and success for a key whose creation time is
# 219 : : // after.
# 220 : 1 : {
# 221 : 1 : const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 222 : 1 : wallet->SetupLegacyScriptPubKeyMan();
# 223 : 1 : WITH_LOCK(wallet->cs_wallet, wallet->SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash()));
# 224 : 1 : WalletContext context;
# 225 : 1 : context.args = &m_args;
# 226 : 1 : AddWallet(context, wallet);
# 227 : 1 : UniValue keys;
# 228 : 1 : keys.setArray();
# 229 : 1 : UniValue key;
# 230 : 1 : key.setObject();
# 231 : 1 : key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(coinbaseKey.GetPubKey())));
# 232 : 1 : key.pushKV("timestamp", 0);
# 233 : 1 : key.pushKV("internal", UniValue(true));
# 234 : 1 : keys.push_back(key);
# 235 : 1 : key.clear();
# 236 : 1 : key.setObject();
# 237 : 1 : CKey futureKey;
# 238 : 1 : futureKey.MakeNewKey(true);
# 239 : 1 : key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(futureKey.GetPubKey())));
# 240 : 1 : key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
# 241 : 1 : key.pushKV("internal", UniValue(true));
# 242 : 1 : keys.push_back(key);
# 243 : 1 : JSONRPCRequest request;
# 244 : 1 : request.context = &context;
# 245 : 1 : request.params.setArray();
# 246 : 1 : request.params.push_back(keys);
# 247 : :
# 248 : 1 : UniValue response = importmulti().HandleRequest(request);
# 249 : 1 : BOOST_CHECK_EQUAL(response.write(),
# 250 : 1 : strprintf("[{\"success\":false,\"error\":{\"code\":-1,\"message\":\"Rescan failed for key with creation "
# 251 : 1 : "timestamp %d. There was an error reading a block from time %d, which is after or within %d "
# 252 : 1 : "seconds of key creation, and could contain transactions pertaining to the key. As a result, "
# 253 : 1 : "transactions and coins using this key may not appear in the wallet. This error could be caused "
# 254 : 1 : "by pruning or data corruption (see bitcoind log for details) and could be dealt with by "
# 255 : 1 : "downloading and rescanning the relevant blocks (see -reindex option and rescanblockchain "
# 256 : 1 : "RPC).\"}},{\"success\":true}]",
# 257 : 1 : 0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW));
# 258 : 1 : RemoveWallet(context, wallet, /* load_on_start= */ std::nullopt);
# 259 : 1 : }
# 260 : 1 : }
# 261 : :
# 262 : : // Verify importwallet RPC starts rescan at earliest block with timestamp
# 263 : : // greater or equal than key birthday. Previously there was a bug where
# 264 : : // importwallet RPC would start the scan at the latest block with timestamp less
# 265 : : // than or equal to key birthday.
# 266 : : BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
# 267 : 1 : {
# 268 : : // Create two blocks with same timestamp to verify that importwallet rescan
# 269 : : // will pick up both blocks, not just the first.
# 270 : 1 : const int64_t BLOCK_TIME = m_node.chainman->ActiveChain().Tip()->GetBlockTimeMax() + 5;
# 271 : 1 : SetMockTime(BLOCK_TIME);
# 272 : 1 : m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
# 273 : 1 : m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
# 274 : :
# 275 : : // Set key birthday to block time increased by the timestamp window, so
# 276 : : // rescan will start at the block time.
# 277 : 1 : const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW;
# 278 : 1 : SetMockTime(KEY_TIME);
# 279 : 1 : m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
# 280 : :
# 281 : 1 : std::string backup_file = fs::PathToString(m_args.GetDataDirNet() / "wallet.backup");
# 282 : :
# 283 : : // Import key into wallet and call dumpwallet to create backup file.
# 284 : 1 : {
# 285 : 1 : WalletContext context;
# 286 : 1 : context.args = &m_args;
# 287 : 1 : const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 288 : 1 : {
# 289 : 1 : auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
# 290 : 1 : LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
# 291 : 1 : spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
# 292 : 1 : spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
# 293 : :
# 294 : 1 : AddWallet(context, wallet);
# 295 : 1 : wallet->SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
# 296 : 1 : }
# 297 : 1 : JSONRPCRequest request;
# 298 : 1 : request.context = &context;
# 299 : 1 : request.params.setArray();
# 300 : 1 : request.params.push_back(backup_file);
# 301 : :
# 302 : 1 : wallet::dumpwallet().HandleRequest(request);
# 303 : 1 : RemoveWallet(context, wallet, /* load_on_start= */ std::nullopt);
# 304 : 1 : }
# 305 : :
# 306 : : // Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME
# 307 : : // were scanned, and no prior blocks were scanned.
# 308 : 1 : {
# 309 : 1 : const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 310 : 1 : LOCK(wallet->cs_wallet);
# 311 : 1 : wallet->SetupLegacyScriptPubKeyMan();
# 312 : :
# 313 : 1 : WalletContext context;
# 314 : 1 : context.args = &m_args;
# 315 : 1 : JSONRPCRequest request;
# 316 : 1 : request.context = &context;
# 317 : 1 : request.params.setArray();
# 318 : 1 : request.params.push_back(backup_file);
# 319 : 1 : AddWallet(context, wallet);
# 320 : 1 : wallet->SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
# 321 : 1 : wallet::importwallet().HandleRequest(request);
# 322 : 1 : RemoveWallet(context, wallet, /* load_on_start= */ std::nullopt);
# 323 : :
# 324 : 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.size(), 3U);
# 325 : 1 : BOOST_CHECK_EQUAL(m_coinbase_txns.size(), 103U);
# 326 [ + + ]: 104 : for (size_t i = 0; i < m_coinbase_txns.size(); ++i) {
# 327 : 103 : bool found = wallet->GetWalletTx(m_coinbase_txns[i]->GetHash());
# 328 : 103 : bool expected = i >= 100;
# 329 : 103 : BOOST_CHECK_EQUAL(found, expected);
# 330 : 103 : }
# 331 : 1 : }
# 332 : 1 : }
# 333 : :
# 334 : : // Check that GetImmatureCredit() returns a newly calculated value instead of
# 335 : : // the cached value after a MarkDirty() call.
# 336 : : //
# 337 : : // This is a regression test written to verify a bugfix for the immature credit
# 338 : : // function. Similar tests probably should be written for the other credit and
# 339 : : // debit functions.
# 340 : : BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
# 341 : 1 : {
# 342 : 1 : CWallet wallet(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 343 : 1 : CWalletTx wtx{m_coinbase_txns.back(), TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*index=*/0}};
# 344 : :
# 345 : 1 : LOCK(wallet.cs_wallet);
# 346 : 1 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
# 347 : 1 : wallet.SetupDescriptorScriptPubKeyMans();
# 348 : :
# 349 : 1 : wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
# 350 : :
# 351 : : // Call GetImmatureCredit() once before adding the key to the wallet to
# 352 : : // cache the current immature credit amount, which is 0.
# 353 : 1 : BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx), 0);
# 354 : :
# 355 : : // Invalidate the cached value, add the key, and make sure a new immature
# 356 : : // credit amount is calculated.
# 357 : 1 : wtx.MarkDirty();
# 358 : 1 : AddKey(wallet, coinbaseKey);
# 359 : 1 : BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx), 50*COIN);
# 360 : 1 : }
# 361 : :
# 362 : : static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
# 363 : 6 : {
# 364 : 6 : CMutableTransaction tx;
# 365 : 6 : TxState state = TxStateInactive{};
# 366 : 6 : tx.nLockTime = lockTime;
# 367 : 6 : SetMockTime(mockTime);
# 368 : 6 : CBlockIndex* block = nullptr;
# 369 [ + + ]: 6 : if (blockTime > 0) {
# 370 : 5 : LOCK(cs_main);
# 371 : 5 : auto inserted = chainman.BlockIndex().emplace(std::piecewise_construct, std::make_tuple(GetRandHash()), std::make_tuple());
# 372 : 5 : assert(inserted.second);
# 373 : 0 : const uint256& hash = inserted.first->first;
# 374 : 5 : block = &inserted.first->second;
# 375 : 5 : block->nTime = blockTime;
# 376 : 5 : block->phashBlock = &hash;
# 377 : 5 : state = TxStateConfirmed{hash, block->nHeight, /*index=*/0};
# 378 : 5 : }
# 379 : 6 : return wallet.AddToWallet(MakeTransactionRef(tx), state, [&](CWalletTx& wtx, bool /* new_tx */) {
# 380 : : // Assign wtx.m_state to simplify test and avoid the need to simulate
# 381 : : // reorg events. Without this, AddToWallet asserts false when the same
# 382 : : // transaction is confirmed in different blocks.
# 383 : 6 : wtx.m_state = state;
# 384 : 6 : return true;
# 385 : 6 : })->nTimeSmart;
# 386 : 6 : }
# 387 : :
# 388 : : // Simple test to verify assignment of CWalletTx::nSmartTime value. Could be
# 389 : : // expanded to cover more corner cases of smart time logic.
# 390 : : BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
# 391 : 1 : {
# 392 : : // New transaction should use clock time if lower than block time.
# 393 : 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 100, 120), 100);
# 394 : :
# 395 : : // Test that updating existing transaction does not change smart time.
# 396 : 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 200, 220), 100);
# 397 : :
# 398 : : // New transaction should use clock time if there's no block time.
# 399 : 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 2, 300, 0), 300);
# 400 : :
# 401 : : // New transaction should use block time if lower than clock time.
# 402 : 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 3, 420, 400), 400);
# 403 : :
# 404 : : // New transaction should use latest entry time if higher than
# 405 : : // min(block time, clock time).
# 406 : 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 4, 500, 390), 400);
# 407 : :
# 408 : : // If there are future entries, new transaction should use time of the
# 409 : : // newest entry that is no more than 300 seconds ahead of the clock time.
# 410 : 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 5, 50, 600), 300);
# 411 : 1 : }
# 412 : :
# 413 : : BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
# 414 : 1 : {
# 415 : 1 : CTxDestination dest = PKHash();
# 416 : 1 : LOCK(m_wallet.cs_wallet);
# 417 : 1 : WalletBatch batch{m_wallet.GetDatabase()};
# 418 : 1 : m_wallet.SetAddressUsed(batch, dest, true);
# 419 : 1 : m_wallet.SetAddressReceiveRequest(batch, dest, "0", "val_rr0");
# 420 : 1 : m_wallet.SetAddressReceiveRequest(batch, dest, "1", "val_rr1");
# 421 : :
# 422 : 1 : auto values = m_wallet.GetAddressReceiveRequests();
# 423 : 1 : BOOST_CHECK_EQUAL(values.size(), 2U);
# 424 : 1 : BOOST_CHECK_EQUAL(values[0], "val_rr0");
# 425 : 1 : BOOST_CHECK_EQUAL(values[1], "val_rr1");
# 426 : 1 : }
# 427 : :
# 428 : : // Test some watch-only LegacyScriptPubKeyMan methods by the procedure of loading (LoadWatchOnly),
# 429 : : // checking (HaveWatchOnly), getting (GetWatchPubKey) and removing (RemoveWatchOnly) a
# 430 : : // given PubKey, resp. its corresponding P2PK Script. Results of the impact on
# 431 : : // the address -> PubKey map is dependent on whether the PubKey is a point on the curve
# 432 : : static void TestWatchOnlyPubKey(LegacyScriptPubKeyMan* spk_man, const CPubKey& add_pubkey)
# 433 : 5 : {
# 434 : 5 : CScript p2pk = GetScriptForRawPubKey(add_pubkey);
# 435 : 5 : CKeyID add_address = add_pubkey.GetID();
# 436 : 5 : CPubKey found_pubkey;
# 437 : 5 : LOCK(spk_man->cs_KeyStore);
# 438 : :
# 439 : : // all Scripts (i.e. also all PubKeys) are added to the general watch-only set
# 440 : 5 : BOOST_CHECK(!spk_man->HaveWatchOnly(p2pk));
# 441 : 5 : spk_man->LoadWatchOnly(p2pk);
# 442 : 5 : BOOST_CHECK(spk_man->HaveWatchOnly(p2pk));
# 443 : :
# 444 : : // only PubKeys on the curve shall be added to the watch-only address -> PubKey map
# 445 : 5 : bool is_pubkey_fully_valid = add_pubkey.IsFullyValid();
# 446 [ + + ]: 5 : if (is_pubkey_fully_valid) {
# 447 : 2 : BOOST_CHECK(spk_man->GetWatchPubKey(add_address, found_pubkey));
# 448 : 2 : BOOST_CHECK(found_pubkey == add_pubkey);
# 449 : 3 : } else {
# 450 : 3 : BOOST_CHECK(!spk_man->GetWatchPubKey(add_address, found_pubkey));
# 451 : 3 : BOOST_CHECK(found_pubkey == CPubKey()); // passed key is unchanged
# 452 : 3 : }
# 453 : :
# 454 : 5 : spk_man->RemoveWatchOnly(p2pk);
# 455 : 5 : BOOST_CHECK(!spk_man->HaveWatchOnly(p2pk));
# 456 : :
# 457 [ + + ]: 5 : if (is_pubkey_fully_valid) {
# 458 : 2 : BOOST_CHECK(!spk_man->GetWatchPubKey(add_address, found_pubkey));
# 459 : 2 : BOOST_CHECK(found_pubkey == add_pubkey); // passed key is unchanged
# 460 : 2 : }
# 461 : 5 : }
# 462 : :
# 463 : : // Cryptographically invalidate a PubKey whilst keeping length and first byte
# 464 : : static void PollutePubKey(CPubKey& pubkey)
# 465 : 2 : {
# 466 : 2 : std::vector<unsigned char> pubkey_raw(pubkey.begin(), pubkey.end());
# 467 : 2 : std::fill(pubkey_raw.begin()+1, pubkey_raw.end(), 0);
# 468 : 2 : pubkey = CPubKey(pubkey_raw);
# 469 : 2 : assert(!pubkey.IsFullyValid());
# 470 : 0 : assert(pubkey.IsValid());
# 471 : 2 : }
# 472 : :
# 473 : : // Test watch-only logic for PubKeys
# 474 : : BOOST_AUTO_TEST_CASE(WatchOnlyPubKeys)
# 475 : 1 : {
# 476 : 1 : CKey key;
# 477 : 1 : CPubKey pubkey;
# 478 : 1 : LegacyScriptPubKeyMan* spk_man = m_wallet.GetOrCreateLegacyScriptPubKeyMan();
# 479 : :
# 480 : 1 : BOOST_CHECK(!spk_man->HaveWatchOnly());
# 481 : :
# 482 : : // uncompressed valid PubKey
# 483 : 1 : key.MakeNewKey(false);
# 484 : 1 : pubkey = key.GetPubKey();
# 485 : 1 : assert(!pubkey.IsCompressed());
# 486 : 0 : TestWatchOnlyPubKey(spk_man, pubkey);
# 487 : :
# 488 : : // uncompressed cryptographically invalid PubKey
# 489 : 1 : PollutePubKey(pubkey);
# 490 : 1 : TestWatchOnlyPubKey(spk_man, pubkey);
# 491 : :
# 492 : : // compressed valid PubKey
# 493 : 1 : key.MakeNewKey(true);
# 494 : 1 : pubkey = key.GetPubKey();
# 495 : 1 : assert(pubkey.IsCompressed());
# 496 : 0 : TestWatchOnlyPubKey(spk_man, pubkey);
# 497 : :
# 498 : : // compressed cryptographically invalid PubKey
# 499 : 1 : PollutePubKey(pubkey);
# 500 : 1 : TestWatchOnlyPubKey(spk_man, pubkey);
# 501 : :
# 502 : : // invalid empty PubKey
# 503 : 1 : pubkey = CPubKey();
# 504 : 1 : TestWatchOnlyPubKey(spk_man, pubkey);
# 505 : 1 : }
# 506 : :
# 507 : : class ListCoinsTestingSetup : public TestChain100Setup
# 508 : : {
# 509 : : public:
# 510 : : ListCoinsTestingSetup()
# 511 : 1 : {
# 512 : 1 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
# 513 : 1 : wallet = CreateSyncedWallet(*m_node.chain, m_node.chainman->ActiveChain(), m_args, coinbaseKey);
# 514 : 1 : }
# 515 : :
# 516 : : ~ListCoinsTestingSetup()
# 517 : 1 : {
# 518 : 1 : wallet.reset();
# 519 : 1 : }
# 520 : :
# 521 : : CWalletTx& AddTx(CRecipient recipient)
# 522 : 1 : {
# 523 : 1 : CTransactionRef tx;
# 524 : 1 : CAmount fee;
# 525 : 1 : int changePos = -1;
# 526 : 1 : bilingual_str error;
# 527 : 1 : CCoinControl dummy;
# 528 : 1 : FeeCalculation fee_calc_out;
# 529 : 1 : {
# 530 : 1 : BOOST_CHECK(CreateTransaction(*wallet, {recipient}, tx, fee, changePos, error, dummy, fee_calc_out));
# 531 : 1 : }
# 532 : 1 : wallet->CommitTransaction(tx, {}, {});
# 533 : 1 : CMutableTransaction blocktx;
# 534 : 1 : {
# 535 : 1 : LOCK(wallet->cs_wallet);
# 536 : 1 : blocktx = CMutableTransaction(*wallet->mapWallet.at(tx->GetHash()).tx);
# 537 : 1 : }
# 538 : 1 : CreateAndProcessBlock({CMutableTransaction(blocktx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
# 539 : :
# 540 : 1 : LOCK(wallet->cs_wallet);
# 541 : 1 : wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, m_node.chainman->ActiveChain().Tip()->GetBlockHash());
# 542 : 1 : auto it = wallet->mapWallet.find(tx->GetHash());
# 543 : 1 : BOOST_CHECK(it != wallet->mapWallet.end());
# 544 : 1 : it->second.m_state = TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*index=*/1};
# 545 : 1 : return it->second;
# 546 : 1 : }
# 547 : :
# 548 : : std::unique_ptr<CWallet> wallet;
# 549 : : };
# 550 : :
# 551 : : BOOST_FIXTURE_TEST_CASE(ListCoinsTest, ListCoinsTestingSetup)
# 552 : 1 : {
# 553 : 1 : std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString();
# 554 : :
# 555 : : // Confirm ListCoins initially returns 1 coin grouped under coinbaseKey
# 556 : : // address.
# 557 : 1 : std::map<CTxDestination, std::vector<COutput>> list;
# 558 : 1 : {
# 559 : 1 : LOCK(wallet->cs_wallet);
# 560 : 1 : list = ListCoins(*wallet);
# 561 : 1 : }
# 562 : 1 : BOOST_CHECK_EQUAL(list.size(), 1U);
# 563 : 1 : BOOST_CHECK_EQUAL(std::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);
# 564 : 1 : BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U);
# 565 : :
# 566 : : // Check initial balance from one mature coinbase transaction.
# 567 : 1 : BOOST_CHECK_EQUAL(50 * COIN, GetAvailableBalance(*wallet));
# 568 : :
# 569 : : // Add a transaction creating a change address, and confirm ListCoins still
# 570 : : // returns the coin associated with the change address underneath the
# 571 : : // coinbaseKey pubkey, even though the change address has a different
# 572 : : // pubkey.
# 573 : 1 : AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
# 574 : 1 : {
# 575 : 1 : LOCK(wallet->cs_wallet);
# 576 : 1 : list = ListCoins(*wallet);
# 577 : 1 : }
# 578 : 1 : BOOST_CHECK_EQUAL(list.size(), 1U);
# 579 : 1 : BOOST_CHECK_EQUAL(std::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);
# 580 : 1 : BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);
# 581 : :
# 582 : : // Lock both coins. Confirm number of available coins drops to 0.
# 583 : 1 : {
# 584 : 1 : LOCK(wallet->cs_wallet);
# 585 : 1 : std::vector<COutput> available;
# 586 : 1 : AvailableCoins(*wallet, available);
# 587 : 1 : BOOST_CHECK_EQUAL(available.size(), 2U);
# 588 : 1 : }
# 589 [ + + ]: 1 : for (const auto& group : list) {
# 590 [ + + ]: 2 : for (const auto& coin : group.second) {
# 591 : 2 : LOCK(wallet->cs_wallet);
# 592 : 2 : wallet->LockCoin(coin.outpoint);
# 593 : 2 : }
# 594 : 1 : }
# 595 : 1 : {
# 596 : 1 : LOCK(wallet->cs_wallet);
# 597 : 1 : std::vector<COutput> available;
# 598 : 1 : AvailableCoins(*wallet, available);
# 599 : 1 : BOOST_CHECK_EQUAL(available.size(), 0U);
# 600 : 1 : }
# 601 : : // Confirm ListCoins still returns same result as before, despite coins
# 602 : : // being locked.
# 603 : 1 : {
# 604 : 1 : LOCK(wallet->cs_wallet);
# 605 : 1 : list = ListCoins(*wallet);
# 606 : 1 : }
# 607 : 1 : BOOST_CHECK_EQUAL(list.size(), 1U);
# 608 : 1 : BOOST_CHECK_EQUAL(std::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);
# 609 : 1 : BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);
# 610 : 1 : }
# 611 : :
# 612 : : BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
# 613 : 1 : {
# 614 : 1 : {
# 615 : 1 : const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 616 : 1 : wallet->SetupLegacyScriptPubKeyMan();
# 617 : 1 : wallet->SetMinVersion(FEATURE_LATEST);
# 618 : 1 : wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
# 619 : 1 : BOOST_CHECK(!wallet->TopUpKeyPool(1000));
# 620 : 1 : CTxDestination dest;
# 621 : 1 : bilingual_str error;
# 622 : 1 : BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
# 623 : 1 : }
# 624 : 1 : {
# 625 : 1 : const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
# 626 : 1 : LOCK(wallet->cs_wallet);
# 627 : 1 : wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
# 628 : 1 : wallet->SetMinVersion(FEATURE_LATEST);
# 629 : 1 : wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
# 630 : 1 : CTxDestination dest;
# 631 : 1 : bilingual_str error;
# 632 : 1 : BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
# 633 : 1 : }
# 634 : 1 : }
# 635 : :
# 636 : : // Explicit calculation which is used to test the wallet constant
# 637 : : // We get the same virtual size due to rounding(weight/4) for both use_max_sig values
# 638 : : static size_t CalculateNestedKeyhashInputSize(bool use_max_sig)
# 639 : 2 : {
# 640 : : // Generate ephemeral valid pubkey
# 641 : 2 : CKey key;
# 642 : 2 : key.MakeNewKey(true);
# 643 : 2 : CPubKey pubkey = key.GetPubKey();
# 644 : :
# 645 : : // Generate pubkey hash
# 646 : 2 : uint160 key_hash(Hash160(pubkey));
# 647 : :
# 648 : : // Create inner-script to enter into keystore. Key hash can't be 0...
# 649 : 2 : CScript inner_script = CScript() << OP_0 << std::vector<unsigned char>(key_hash.begin(), key_hash.end());
# 650 : :
# 651 : : // Create outer P2SH script for the output
# 652 : 2 : uint160 script_id(Hash160(inner_script));
# 653 : 2 : CScript script_pubkey = CScript() << OP_HASH160 << std::vector<unsigned char>(script_id.begin(), script_id.end()) << OP_EQUAL;
# 654 : :
# 655 : : // Add inner-script to key store and key to watchonly
# 656 : 2 : FillableSigningProvider keystore;
# 657 : 2 : keystore.AddCScript(inner_script);
# 658 : 2 : keystore.AddKeyPubKey(key, pubkey);
# 659 : :
# 660 : : // Fill in dummy signatures for fee calculation.
# 661 : 2 : SignatureData sig_data;
# 662 : :
# 663 [ - + ][ + + ]: 2 : if (!ProduceSignature(keystore, use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR : DUMMY_SIGNATURE_CREATOR, script_pubkey, sig_data)) {
# 664 : : // We're hand-feeding it correct arguments; shouldn't happen
# 665 : 0 : assert(false);
# 666 : 0 : }
# 667 : :
# 668 : 0 : CTxIn tx_in;
# 669 : 2 : UpdateInput(tx_in, sig_data);
# 670 : 2 : return (size_t)GetVirtualTransactionInputSize(tx_in);
# 671 : 2 : }
# 672 : :
# 673 : : BOOST_FIXTURE_TEST_CASE(dummy_input_size_test, TestChain100Setup)
# 674 : 1 : {
# 675 : 1 : BOOST_CHECK_EQUAL(CalculateNestedKeyhashInputSize(false), DUMMY_NESTED_P2WPKH_INPUT_SIZE);
# 676 : 1 : BOOST_CHECK_EQUAL(CalculateNestedKeyhashInputSize(true), DUMMY_NESTED_P2WPKH_INPUT_SIZE);
# 677 : 1 : }
# 678 : :
# 679 : : bool malformed_descriptor(std::ios_base::failure e)
# 680 : 1 : {
# 681 : 1 : std::string s(e.what());
# 682 : 1 : return s.find("Missing checksum") != std::string::npos;
# 683 : 1 : }
# 684 : :
# 685 : : BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup)
# 686 : 1 : {
# 687 : 1 : std::vector<unsigned char> malformed_record;
# 688 : 1 : CVectorWriter vw(0, 0, malformed_record, 0);
# 689 : 1 : vw << std::string("notadescriptor");
# 690 : 1 : vw << (uint64_t)0;
# 691 : 1 : vw << (int32_t)0;
# 692 : 1 : vw << (int32_t)0;
# 693 : 1 : vw << (int32_t)1;
# 694 : :
# 695 : 1 : SpanReader vr{0, 0, malformed_record};
# 696 : 1 : WalletDescriptor w_desc;
# 697 : 1 : BOOST_CHECK_EXCEPTION(vr >> w_desc, std::ios_base::failure, malformed_descriptor);
# 698 : 1 : }
# 699 : :
# 700 : : //! Test CWallet::Create() and its behavior handling potential race
# 701 : : //! conditions if it's called the same time an incoming transaction shows up in
# 702 : : //! the mempool or a new block.
# 703 : : //!
# 704 : : //! It isn't possible to verify there aren't race condition in every case, so
# 705 : : //! this test just checks two specific cases and ensures that timing of
# 706 : : //! notifications in these cases doesn't prevent the wallet from detecting
# 707 : : //! transactions.
# 708 : : //!
# 709 : : //! In the first case, block and mempool transactions are created before the
# 710 : : //! wallet is loaded, but notifications about these transactions are delayed
# 711 : : //! until after it is loaded. The notifications are superfluous in this case, so
# 712 : : //! the test verifies the transactions are detected before they arrive.
# 713 : : //!
# 714 : : //! In the second case, block and mempool transactions are created after the
# 715 : : //! wallet rescan and notifications are immediately synced, to verify the wallet
# 716 : : //! must already have a handler in place for them, and there's no gap after
# 717 : : //! rescanning where new transactions in new blocks could be lost.
# 718 : : BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
# 719 : 1 : {
# 720 : 1 : m_args.ForceSetArg("-unsafesqlitesync", "1");
# 721 : : // Create new wallet with known key and unload it.
# 722 : 1 : WalletContext context;
# 723 : 1 : context.args = &m_args;
# 724 : 1 : context.chain = m_node.chain.get();
# 725 : 1 : auto wallet = TestLoadWallet(context);
# 726 : 1 : CKey key;
# 727 : 1 : key.MakeNewKey(true);
# 728 : 1 : AddKey(*wallet, key);
# 729 : 1 : TestUnloadWallet(std::move(wallet));
# 730 : :
# 731 : :
# 732 : : // Add log hook to detect AddToWallet events from rescans, blockConnected,
# 733 : : // and transactionAddedToMempool notifications
# 734 : 1 : int addtx_count = 0;
# 735 : 9 : DebugLogHelper addtx_counter("[default wallet] AddToWallet", [&](const std::string* s) {
# 736 [ + + ]: 9 : if (s) ++addtx_count;
# 737 : 9 : return false;
# 738 : 9 : });
# 739 : :
# 740 : :
# 741 : 1 : bool rescan_completed = false;
# 742 : 2 : DebugLogHelper rescan_check("[default wallet] Rescan completed", [&](const std::string* s) {
# 743 [ + + ]: 2 : if (s) rescan_completed = true;
# 744 : 2 : return false;
# 745 : 2 : });
# 746 : :
# 747 : :
# 748 : : // Block the queue to prevent the wallet receiving blockConnected and
# 749 : : // transactionAddedToMempool notifications, and create block and mempool
# 750 : : // transactions paying to the wallet
# 751 : 1 : std::promise<void> promise;
# 752 : 1 : CallFunctionInValidationInterfaceQueue([&promise] {
# 753 : 1 : promise.get_future().wait();
# 754 : 1 : });
# 755 : 1 : std::string error;
# 756 : 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
# 757 : 1 : auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
# 758 : 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
# 759 : 1 : auto mempool_tx = TestSimpleSpend(*m_coinbase_txns[1], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
# 760 : 1 : BOOST_CHECK(m_node.chain->broadcastTransaction(MakeTransactionRef(mempool_tx), DEFAULT_TRANSACTION_MAXFEE, false, error));
# 761 : :
# 762 : :
# 763 : : // Reload wallet and make sure new transactions are detected despite events
# 764 : : // being blocked
# 765 : 1 : wallet = TestLoadWallet(context);
# 766 : 1 : BOOST_CHECK(rescan_completed);
# 767 : 1 : BOOST_CHECK_EQUAL(addtx_count, 2);
# 768 : 1 : {
# 769 : 1 : LOCK(wallet->cs_wallet);
# 770 : 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_tx.GetHash()), 1U);
# 771 : 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(mempool_tx.GetHash()), 1U);
# 772 : 1 : }
# 773 : :
# 774 : :
# 775 : : // Unblock notification queue and make sure stale blockConnected and
# 776 : : // transactionAddedToMempool events are processed
# 777 : 1 : promise.set_value();
# 778 : 1 : SyncWithValidationInterfaceQueue();
# 779 : 1 : BOOST_CHECK_EQUAL(addtx_count, 4);
# 780 : :
# 781 : :
# 782 : 1 : TestUnloadWallet(std::move(wallet));
# 783 : :
# 784 : :
# 785 : : // Load wallet again, this time creating new block and mempool transactions
# 786 : : // paying to the wallet as the wallet finishes loading and syncing the
# 787 : : // queue so the events have to be handled immediately. Releasing the wallet
# 788 : : // lock during the sync is a little artificial but is needed to avoid a
# 789 : : // deadlock during the sync and simulates a new block notification happening
# 790 : : // as soon as possible.
# 791 : 1 : addtx_count = 0;
# 792 : 1 : auto handler = HandleLoadWallet(context, [&](std::unique_ptr<interfaces::Wallet> wallet) {
# 793 : 1 : BOOST_CHECK(rescan_completed);
# 794 : 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
# 795 : 1 : block_tx = TestSimpleSpend(*m_coinbase_txns[2], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
# 796 : 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
# 797 : 1 : mempool_tx = TestSimpleSpend(*m_coinbase_txns[3], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
# 798 : 1 : BOOST_CHECK(m_node.chain->broadcastTransaction(MakeTransactionRef(mempool_tx), DEFAULT_TRANSACTION_MAXFEE, false, error));
# 799 : 1 : SyncWithValidationInterfaceQueue();
# 800 : 1 : });
# 801 : 1 : wallet = TestLoadWallet(context);
# 802 : 1 : BOOST_CHECK_EQUAL(addtx_count, 4);
# 803 : 1 : {
# 804 : 1 : LOCK(wallet->cs_wallet);
# 805 : 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_tx.GetHash()), 1U);
# 806 : 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(mempool_tx.GetHash()), 1U);
# 807 : 1 : }
# 808 : :
# 809 : :
# 810 : 1 : TestUnloadWallet(std::move(wallet));
# 811 : 1 : }
# 812 : :
# 813 : : BOOST_FIXTURE_TEST_CASE(CreateWalletWithoutChain, BasicTestingSetup)
# 814 : 1 : {
# 815 : 1 : WalletContext context;
# 816 : 1 : context.args = &m_args;
# 817 : 1 : auto wallet = TestLoadWallet(context);
# 818 : 1 : BOOST_CHECK(wallet);
# 819 : 1 : UnloadWallet(std::move(wallet));
# 820 : 1 : }
# 821 : :
# 822 : : BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
# 823 : 1 : {
# 824 : 1 : m_args.ForceSetArg("-unsafesqlitesync", "1");
# 825 : 1 : WalletContext context;
# 826 : 1 : context.args = &m_args;
# 827 : 1 : context.chain = m_node.chain.get();
# 828 : 1 : auto wallet = TestLoadWallet(context);
# 829 : 1 : CKey key;
# 830 : 1 : key.MakeNewKey(true);
# 831 : 1 : AddKey(*wallet, key);
# 832 : :
# 833 : 1 : std::string error;
# 834 : 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
# 835 : 1 : auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
# 836 : 1 : CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
# 837 : :
# 838 : 1 : SyncWithValidationInterfaceQueue();
# 839 : :
# 840 : 1 : {
# 841 : 1 : auto block_hash = block_tx.GetHash();
# 842 : 1 : auto prev_hash = m_coinbase_txns[0]->GetHash();
# 843 : :
# 844 : 1 : LOCK(wallet->cs_wallet);
# 845 : 1 : BOOST_CHECK(wallet->HasWalletSpend(prev_hash));
# 846 : 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 1u);
# 847 : :
# 848 : 1 : std::vector<uint256> vHashIn{ block_hash }, vHashOut;
# 849 : 1 : BOOST_CHECK_EQUAL(wallet->ZapSelectTx(vHashIn, vHashOut), DBErrors::LOAD_OK);
# 850 : :
# 851 : 1 : BOOST_CHECK(!wallet->HasWalletSpend(prev_hash));
# 852 : 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 0u);
# 853 : 1 : }
# 854 : :
# 855 : 1 : TestUnloadWallet(std::move(wallet));
# 856 : 1 : }
# 857 : :
# 858 : : BOOST_AUTO_TEST_SUITE_END()
# 859 : : } // namespace wallet
|