Line data Source code
1 : // Copyright (c) 2022 The Bitcoin Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or https://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <validation.h> 6 : #include <wallet/coincontrol.h> 7 : #include <wallet/spend.h> 8 : #include <wallet/test/util.h> 9 : #include <wallet/test/wallet_test_fixture.h> 10 : 11 : #include <boost/test/unit_test.hpp> 12 : 13 : namespace wallet { 14 146 : BOOST_FIXTURE_TEST_SUITE(availablecoins_tests, WalletTestingSetup) 15 : class AvailableCoinsTestingSetup : public TestChain100Setup 16 : { 17 : public: 18 1 : AvailableCoinsTestingSetup() 19 : { 20 1 : CreateAndProcessBlock({}, {}); 21 1 : wallet = CreateSyncedWallet(*m_node.chain, *m_node.coinjoin_loader, m_node.chainman->ActiveChain(), m_args, coinbaseKey); 22 1 : } 23 : 24 1 : ~AvailableCoinsTestingSetup() 25 : { 26 1 : wallet.reset(); 27 1 : } 28 1 : CWalletTx& AddTx(CRecipient recipient) 29 : { 30 1 : CTransactionRef tx; 31 1 : CCoinControl dummy; 32 : { 33 1 : auto res = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, dummy); 34 1 : BOOST_CHECK(res); 35 1 : tx = res->tx; 36 1 : } 37 1 : wallet->CommitTransaction(tx, {}, {}); 38 1 : CMutableTransaction blocktx; 39 : { 40 1 : LOCK(wallet->cs_wallet); 41 1 : blocktx = CMutableTransaction(*wallet->mapWallet.at(tx->GetHash()).tx); 42 1 : } 43 1 : CreateAndProcessBlock({CMutableTransaction(blocktx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); 44 : 45 1 : LOCK(wallet->cs_wallet); 46 1 : wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, m_node.chainman->ActiveChain().Tip()->GetBlockHash()); 47 1 : auto it = wallet->mapWallet.find(tx->GetHash()); 48 1 : BOOST_CHECK(it != wallet->mapWallet.end()); 49 1 : it->second.m_state = TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*index=*/1}; 50 1 : return it->second; 51 1 : } 52 : 53 : std::unique_ptr<CWallet> wallet; 54 : }; 55 : 56 148 : BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTest, AvailableCoinsTestingSetup) 57 : { 58 1 : CoinsResult available_coins; 59 1 : util::Result<CTxDestination> dest{util::Error{}}; 60 1 : LOCK(wallet->cs_wallet); 61 : 62 : // Verify our wallet has one usable coinbase UTXO before starting 63 : // This UTXO is a P2PK, so it should show up in the Other bucket 64 1 : available_coins = AvailableCoins(*wallet); 65 1 : BOOST_CHECK_EQUAL(available_coins.size(), 1U); 66 1 : BOOST_CHECK_EQUAL(available_coins.other.size(), 1U); 67 : 68 : // We will create a self transfer for each of the OutputTypes and 69 : // verify it is put in the correct bucket after running GetAvailablecoins 70 : // 71 : // For each OutputType, We expect 2 UTXOs in our wallet following the self transfer: 72 : // 1. One UTXO as the recipient 73 : // 2. One UTXO from the change, due to payment address matching logic 74 : 75 : // Legacy (P2PKH) 76 1 : dest = wallet->GetNewDestination(""); 77 1 : BOOST_ASSERT(dest); 78 1 : AddTx(CRecipient{{GetScriptForDestination(*dest)}, 4 * COIN, /*fSubtractFeeFromAmount=*/true}); 79 1 : available_coins = AvailableCoins(*wallet); 80 1 : BOOST_CHECK_EQUAL(available_coins.legacy.size(), 2U); 81 1 : } 82 : 83 146 : BOOST_AUTO_TEST_SUITE_END() 84 : } // namespace wallet