diff options
author | furszy <matiasfurszyfer@protonmail.com> | 2022-08-05 15:46:34 -0300 |
---|---|---|
committer | furszy <matiasfurszyfer@protonmail.com> | 2022-12-14 11:16:01 -0300 |
commit | 9622fe64b8785430c71d4abc8637075026dc690c (patch) | |
tree | cfb8c5c6a5af010bfeea4f3f77f9071b707ddc21 /src/wallet/test | |
parent | f69347d0588647ff9a4e986c7be987827a0417f4 (diff) |
test: move coins result test to wallet_tests.cpp
The class `AvailableCoinsTestingSetup` inside `availablecoins_tests.cpp` is a plain copy
of `ListCoinsTestingSetup` that is inside wallet_tests.cpp.
Diffstat (limited to 'src/wallet/test')
-rw-r--r-- | src/wallet/test/availablecoins_tests.cpp | 99 | ||||
-rw-r--r-- | src/wallet/test/wallet_tests.cpp | 40 |
2 files changed, 40 insertions, 99 deletions
diff --git a/src/wallet/test/availablecoins_tests.cpp b/src/wallet/test/availablecoins_tests.cpp deleted file mode 100644 index 9f155e874d..0000000000 --- a/src/wallet/test/availablecoins_tests.cpp +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) 2022 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or https://www.opensource.org/licenses/mit-license.php. - -#include <validation.h> -#include <wallet/coincontrol.h> -#include <wallet/spend.h> -#include <wallet/test/util.h> -#include <wallet/test/wallet_test_fixture.h> - -#include <boost/test/unit_test.hpp> - -namespace wallet { -BOOST_FIXTURE_TEST_SUITE(availablecoins_tests, WalletTestingSetup) -class AvailableCoinsTestingSetup : public TestChain100Setup -{ -public: - AvailableCoinsTestingSetup() - { - CreateAndProcessBlock({}, {}); - wallet = CreateSyncedWallet(*m_node.chain, m_node.chainman->ActiveChain(), m_args, coinbaseKey); - } - - ~AvailableCoinsTestingSetup() - { - wallet.reset(); - } - CWalletTx& AddTx(CRecipient recipient) - { - CTransactionRef tx; - CCoinControl dummy; - { - constexpr int RANDOM_CHANGE_POSITION = -1; - auto res = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, dummy); - BOOST_CHECK(res); - tx = res->tx; - } - wallet->CommitTransaction(tx, {}, {}); - CMutableTransaction blocktx; - { - LOCK(wallet->cs_wallet); - blocktx = CMutableTransaction(*wallet->mapWallet.at(tx->GetHash()).tx); - } - CreateAndProcessBlock({CMutableTransaction(blocktx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); - - LOCK(wallet->cs_wallet); - LOCK(m_node.chainman->GetMutex()); - wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, m_node.chainman->ActiveChain().Tip()->GetBlockHash()); - auto it = wallet->mapWallet.find(tx->GetHash()); - BOOST_CHECK(it != wallet->mapWallet.end()); - it->second.m_state = TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*index=*/1}; - return it->second; - } - - std::unique_ptr<CWallet> wallet; -}; - -void TestCoinsResult(AvailableCoinsTestingSetup& context, OutputType out_type, CAmount amount, - std::map<OutputType, size_t>& expected_coins_sizes) -{ - LOCK(context.wallet->cs_wallet); - util::Result<CTxDestination> dest = Assert(context.wallet->GetNewDestination(out_type, "")); - CWalletTx& wtx = context.AddTx(CRecipient{{GetScriptForDestination(*dest)}, amount, /*fSubtractFeeFromAmount=*/true}); - CoinFilterParams filter; - filter.skip_locked = false; - CoinsResult available_coins = AvailableCoins(*context.wallet, nullptr, std::nullopt, filter); - // Lock outputs so they are not spent in follow-up transactions - for (uint32_t i = 0; i < wtx.tx->vout.size(); i++) context.wallet->LockCoin({wtx.GetHash(), i}); - for (const auto& [type, size] : expected_coins_sizes) BOOST_CHECK_EQUAL(size, available_coins.coins[type].size()); -} - -BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTest, AvailableCoinsTestingSetup) -{ - std::map<OutputType, size_t> expected_coins_sizes; - for (const auto& out_type : OUTPUT_TYPES) { expected_coins_sizes[out_type] = 0U; } - - // Verify our wallet has one usable coinbase UTXO before starting - // This UTXO is a P2PK, so it should show up in the Other bucket - expected_coins_sizes[OutputType::UNKNOWN] = 1U; - CoinsResult available_coins = WITH_LOCK(wallet->cs_wallet, return AvailableCoins(*wallet)); - BOOST_CHECK_EQUAL(available_coins.Size(), expected_coins_sizes[OutputType::UNKNOWN]); - BOOST_CHECK_EQUAL(available_coins.coins[OutputType::UNKNOWN].size(), expected_coins_sizes[OutputType::UNKNOWN]); - - // We will create a self transfer for each of the OutputTypes and - // verify it is put in the correct bucket after running GetAvailablecoins - // - // For each OutputType, We expect 2 UTXOs in our wallet following the self transfer: - // 1. One UTXO as the recipient - // 2. One UTXO from the change, due to payment address matching logic - - for (const auto& out_type : OUTPUT_TYPES) { - if (out_type == OutputType::UNKNOWN) continue; - expected_coins_sizes[out_type] = 2U; - TestCoinsResult(*this, out_type, 1 * COIN, expected_coins_sizes); - } -} - -BOOST_AUTO_TEST_SUITE_END() -} // namespace wallet diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 60fdbde71b..0361d3df99 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -622,6 +622,46 @@ BOOST_FIXTURE_TEST_CASE(ListCoinsTest, ListCoinsTestingSetup) BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U); } +void TestCoinsResult(ListCoinsTest& context, OutputType out_type, CAmount amount, + std::map<OutputType, size_t>& expected_coins_sizes) +{ + LOCK(context.wallet->cs_wallet); + util::Result<CTxDestination> dest = Assert(context.wallet->GetNewDestination(out_type, "")); + CWalletTx& wtx = context.AddTx(CRecipient{{GetScriptForDestination(*dest)}, amount, /*fSubtractFeeFromAmount=*/true}); + CoinFilterParams filter; + filter.skip_locked = false; + CoinsResult available_coins = AvailableCoins(*context.wallet, nullptr, std::nullopt, filter); + // Lock outputs so they are not spent in follow-up transactions + for (uint32_t i = 0; i < wtx.tx->vout.size(); i++) context.wallet->LockCoin({wtx.GetHash(), i}); + for (const auto& [type, size] : expected_coins_sizes) BOOST_CHECK_EQUAL(size, available_coins.coins[type].size()); +} + +BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTest, ListCoinsTest) +{ + std::map<OutputType, size_t> expected_coins_sizes; + for (const auto& out_type : OUTPUT_TYPES) { expected_coins_sizes[out_type] = 0U; } + + // Verify our wallet has one usable coinbase UTXO before starting + // This UTXO is a P2PK, so it should show up in the Other bucket + expected_coins_sizes[OutputType::UNKNOWN] = 1U; + CoinsResult available_coins = WITH_LOCK(wallet->cs_wallet, return AvailableCoins(*wallet)); + BOOST_CHECK_EQUAL(available_coins.Size(), expected_coins_sizes[OutputType::UNKNOWN]); + BOOST_CHECK_EQUAL(available_coins.coins[OutputType::UNKNOWN].size(), expected_coins_sizes[OutputType::UNKNOWN]); + + // We will create a self transfer for each of the OutputTypes and + // verify it is put in the correct bucket after running GetAvailablecoins + // + // For each OutputType, We expect 2 UTXOs in our wallet following the self transfer: + // 1. One UTXO as the recipient + // 2. One UTXO from the change, due to payment address matching logic + + for (const auto& out_type : OUTPUT_TYPES) { + if (out_type == OutputType::UNKNOWN) continue; + expected_coins_sizes[out_type] = 2U; + TestCoinsResult(*this, out_type, 1 * COIN, expected_coins_sizes); + } +} + BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup) { { |