|
| 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 | +BOOST_FIXTURE_TEST_SUITE(availablecoins_tests, WalletTestingSetup) |
| 15 | +class AvailableCoinsTestingSetup : public TestChain100Setup |
| 16 | +{ |
| 17 | +public: |
| 18 | + AvailableCoinsTestingSetup() |
| 19 | + { |
| 20 | + CreateAndProcessBlock({}, {}); |
| 21 | + wallet = CreateSyncedWallet(*m_node.chain, m_node.chainman->ActiveChain(), m_args, coinbaseKey); |
| 22 | + } |
| 23 | + |
| 24 | + ~AvailableCoinsTestingSetup() |
| 25 | + { |
| 26 | + wallet.reset(); |
| 27 | + } |
| 28 | + CWalletTx& AddTx(CRecipient recipient) |
| 29 | + { |
| 30 | + CTransactionRef tx; |
| 31 | + CCoinControl dummy; |
| 32 | + { |
| 33 | + constexpr int RANDOM_CHANGE_POSITION = -1; |
| 34 | + auto res = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, dummy); |
| 35 | + BOOST_CHECK(res); |
| 36 | + tx = res.GetObj().tx; |
| 37 | + } |
| 38 | + wallet->CommitTransaction(tx, {}, {}); |
| 39 | + CMutableTransaction blocktx; |
| 40 | + { |
| 41 | + LOCK(wallet->cs_wallet); |
| 42 | + blocktx = CMutableTransaction(*wallet->mapWallet.at(tx->GetHash()).tx); |
| 43 | + } |
| 44 | + CreateAndProcessBlock({CMutableTransaction(blocktx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); |
| 45 | + |
| 46 | + LOCK(wallet->cs_wallet); |
| 47 | + wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, m_node.chainman->ActiveChain().Tip()->GetBlockHash()); |
| 48 | + auto it = wallet->mapWallet.find(tx->GetHash()); |
| 49 | + BOOST_CHECK(it != wallet->mapWallet.end()); |
| 50 | + it->second.m_state = TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*index=*/1}; |
| 51 | + return it->second; |
| 52 | + } |
| 53 | + |
| 54 | + std::unique_ptr<CWallet> wallet; |
| 55 | +}; |
| 56 | + |
| 57 | + // ELEMENTS FIXME: swapping these around so that bech32 is run first before bech32m |
| 58 | + // fixes an intermittent issue with the transaction failing to validate its own signature, |
| 59 | + // showing an "invalid schnorr signature" error. this requires further investigation. |
| 60 | + // see https://github.com/ElementsProject/elements/issues/1370 |
| 61 | + |
| 62 | +BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTestBech32m, AvailableCoinsTestingSetup) |
| 63 | +{ |
| 64 | + CoinsResult available_coins; |
| 65 | + BResult<CTxDestination> dest; |
| 66 | + LOCK(wallet->cs_wallet); |
| 67 | + |
| 68 | + // Verify our wallet has one usable coinbase UTXO before starting |
| 69 | + // This UTXO is a P2PK, so it should show up in the Other bucket |
| 70 | + available_coins = AvailableCoins(*wallet); |
| 71 | + BOOST_CHECK_EQUAL(available_coins.size(), 1U); |
| 72 | + BOOST_CHECK_EQUAL(available_coins.other.size(), 1U); |
| 73 | + |
| 74 | + // We will create a self transfer for each of the OutputTypes and |
| 75 | + // verify it is put in the correct bucket after running GetAvailablecoins |
| 76 | + // |
| 77 | + // For each OutputType, We expect 2 UTXOs in our wallet following the self transfer: |
| 78 | + // 1. One UTXO as the recipient |
| 79 | + // 2. One UTXO from the change, due to payment address matching logic |
| 80 | + |
| 81 | + |
| 82 | + // Bech32m |
| 83 | + dest = wallet->GetNewDestination(OutputType::BECH32M, ""); |
| 84 | + BOOST_ASSERT(dest.HasRes()); |
| 85 | + AddTx(CRecipient{{GetScriptForDestination(dest.GetObj())}, 1 * COIN, CAsset(), CPubKey(), /*fSubtractFeeFromAmount=*/true}); |
| 86 | + available_coins = AvailableCoins(*wallet); |
| 87 | + BOOST_CHECK_EQUAL(available_coins.bech32m.size(), 2U); |
| 88 | +} |
| 89 | + |
| 90 | +BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTestBech32, AvailableCoinsTestingSetup) |
| 91 | +{ |
| 92 | + CoinsResult available_coins; |
| 93 | + BResult<CTxDestination> dest; |
| 94 | + LOCK(wallet->cs_wallet); |
| 95 | + |
| 96 | + available_coins = AvailableCoins(*wallet); |
| 97 | + BOOST_CHECK_EQUAL(available_coins.size(), 1U); |
| 98 | + BOOST_CHECK_EQUAL(available_coins.other.size(), 1U); |
| 99 | + |
| 100 | + // Bech32 |
| 101 | + dest = wallet->GetNewDestination(OutputType::BECH32, ""); |
| 102 | + BOOST_ASSERT(dest.HasRes()); |
| 103 | + AddTx(CRecipient{{GetScriptForDestination(dest.GetObj())}, 2 * COIN, CAsset(), CPubKey(), /*fSubtractFeeFromAmount=*/true}); |
| 104 | + available_coins = AvailableCoins(*wallet); |
| 105 | + BOOST_CHECK_EQUAL(available_coins.bech32.size(), 2U); |
| 106 | +} |
| 107 | + |
| 108 | +BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTestP2SHSegWit, AvailableCoinsTestingSetup) |
| 109 | +{ |
| 110 | + CoinsResult available_coins; |
| 111 | + BResult<CTxDestination> dest; |
| 112 | + LOCK(wallet->cs_wallet); |
| 113 | + |
| 114 | + available_coins = AvailableCoins(*wallet); |
| 115 | + BOOST_CHECK_EQUAL(available_coins.size(), 1U); |
| 116 | + BOOST_CHECK_EQUAL(available_coins.other.size(), 1U); |
| 117 | + |
| 118 | + // P2SH-SEGWIT |
| 119 | + dest = wallet->GetNewDestination(OutputType::P2SH_SEGWIT, ""); |
| 120 | + AddTx(CRecipient{{GetScriptForDestination(dest.GetObj())}, 3 * COIN, CAsset(), CPubKey(), /*fSubtractFeeFromAmount=*/true}); |
| 121 | + available_coins = AvailableCoins(*wallet); |
| 122 | + BOOST_CHECK_EQUAL(available_coins.P2SH_segwit.size(), 2U); |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTestLegacy, AvailableCoinsTestingSetup) |
| 127 | +{ |
| 128 | + CoinsResult available_coins; |
| 129 | + BResult<CTxDestination> dest; |
| 130 | + LOCK(wallet->cs_wallet); |
| 131 | + |
| 132 | + available_coins = AvailableCoins(*wallet); |
| 133 | + BOOST_CHECK_EQUAL(available_coins.size(), 1U); |
| 134 | + BOOST_CHECK_EQUAL(available_coins.other.size(), 1U); |
| 135 | + |
| 136 | + // Legacy (P2PKH) |
| 137 | + dest = wallet->GetNewDestination(OutputType::LEGACY, ""); |
| 138 | + BOOST_ASSERT(dest.HasRes()); |
| 139 | + AddTx(CRecipient{{GetScriptForDestination(dest.GetObj())}, 4 * COIN, CAsset(), CPubKey(), /*fSubtractFeeFromAmount=*/true}); |
| 140 | + available_coins = AvailableCoins(*wallet); |
| 141 | + BOOST_CHECK_EQUAL(available_coins.legacy.size(), 2U); |
| 142 | +} |
| 143 | + |
| 144 | +BOOST_AUTO_TEST_SUITE_END() |
| 145 | +} // namespace wallet |
0 commit comments