mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-26 19:23:26 -03:00
test: extend and simplify availablecoins_tests
Clean redundant code and add coverage for 'AvailableCoins' incremental result.
This commit is contained in:
parent
212ccdf2c2
commit
f69347d058
1 changed files with 25 additions and 33 deletions
|
@ -55,17 +55,31 @@ public:
|
||||||
std::unique_ptr<CWallet> wallet;
|
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)
|
BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTest, AvailableCoinsTestingSetup)
|
||||||
{
|
{
|
||||||
CoinsResult available_coins;
|
std::map<OutputType, size_t> expected_coins_sizes;
|
||||||
util::Result<CTxDestination> dest{util::Error{}};
|
for (const auto& out_type : OUTPUT_TYPES) { expected_coins_sizes[out_type] = 0U; }
|
||||||
LOCK(wallet->cs_wallet);
|
|
||||||
|
|
||||||
// Verify our wallet has one usable coinbase UTXO before starting
|
// Verify our wallet has one usable coinbase UTXO before starting
|
||||||
// This UTXO is a P2PK, so it should show up in the Other bucket
|
// This UTXO is a P2PK, so it should show up in the Other bucket
|
||||||
available_coins = AvailableCoins(*wallet);
|
expected_coins_sizes[OutputType::UNKNOWN] = 1U;
|
||||||
BOOST_CHECK_EQUAL(available_coins.Size(), 1U);
|
CoinsResult available_coins = WITH_LOCK(wallet->cs_wallet, return AvailableCoins(*wallet));
|
||||||
BOOST_CHECK_EQUAL(available_coins.coins[OutputType::UNKNOWN].size(), 1U);
|
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
|
// We will create a self transfer for each of the OutputTypes and
|
||||||
// verify it is put in the correct bucket after running GetAvailablecoins
|
// verify it is put in the correct bucket after running GetAvailablecoins
|
||||||
|
@ -74,33 +88,11 @@ BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTest, AvailableCoinsTestingSetup)
|
||||||
// 1. One UTXO as the recipient
|
// 1. One UTXO as the recipient
|
||||||
// 2. One UTXO from the change, due to payment address matching logic
|
// 2. One UTXO from the change, due to payment address matching logic
|
||||||
|
|
||||||
// Bech32m
|
for (const auto& out_type : OUTPUT_TYPES) {
|
||||||
dest = wallet->GetNewDestination(OutputType::BECH32M, "");
|
if (out_type == OutputType::UNKNOWN) continue;
|
||||||
BOOST_ASSERT(dest);
|
expected_coins_sizes[out_type] = 2U;
|
||||||
AddTx(CRecipient{{GetScriptForDestination(*dest)}, 1 * COIN, /*fSubtractFeeFromAmount=*/true});
|
TestCoinsResult(*this, out_type, 1 * COIN, expected_coins_sizes);
|
||||||
available_coins = AvailableCoins(*wallet);
|
}
|
||||||
BOOST_CHECK_EQUAL(available_coins.coins[OutputType::BECH32M].size(), 2U);
|
|
||||||
|
|
||||||
// Bech32
|
|
||||||
dest = wallet->GetNewDestination(OutputType::BECH32, "");
|
|
||||||
BOOST_ASSERT(dest);
|
|
||||||
AddTx(CRecipient{{GetScriptForDestination(*dest)}, 2 * COIN, /*fSubtractFeeFromAmount=*/true});
|
|
||||||
available_coins = AvailableCoins(*wallet);
|
|
||||||
BOOST_CHECK_EQUAL(available_coins.coins[OutputType::BECH32].size(), 2U);
|
|
||||||
|
|
||||||
// P2SH-SEGWIT
|
|
||||||
dest = wallet->GetNewDestination(OutputType::P2SH_SEGWIT, "");
|
|
||||||
BOOST_ASSERT(dest);
|
|
||||||
AddTx(CRecipient{{GetScriptForDestination(*dest)}, 3 * COIN, /*fSubtractFeeFromAmount=*/true});
|
|
||||||
available_coins = AvailableCoins(*wallet);
|
|
||||||
BOOST_CHECK_EQUAL(available_coins.coins[OutputType::P2SH_SEGWIT].size(), 2U);
|
|
||||||
|
|
||||||
// Legacy (P2PKH)
|
|
||||||
dest = wallet->GetNewDestination(OutputType::LEGACY, "");
|
|
||||||
BOOST_ASSERT(dest);
|
|
||||||
AddTx(CRecipient{{GetScriptForDestination(*dest)}, 4 * COIN, /*fSubtractFeeFromAmount=*/true});
|
|
||||||
available_coins = AvailableCoins(*wallet);
|
|
||||||
BOOST_CHECK_EQUAL(available_coins.coins[OutputType::LEGACY].size(), 2U);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
Loading…
Add table
Reference in a new issue