Skip to content

Commit d0d9cf7

Browse files
committed
test: Check external coin effective value is used in CoinSelection
1 parent 76b79c1 commit d0d9cf7

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/wallet/test/coinselector_tests.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,5 +922,52 @@ BOOST_AUTO_TEST_CASE(effective_value_test)
922922
BOOST_CHECK_EQUAL(output5.GetEffectiveValue(), nValue); // The effective value should be equal to the absolute value if input_bytes is -1
923923
}
924924

925+
BOOST_AUTO_TEST_CASE(SelectCoins_effective_value_test)
926+
{
927+
// Test that the effective value is used to check whether preset inputs provide sufficient funds when subtract_fee_outputs is not used.
928+
// This test creates a coin whose value is higher than the target but whose effective value is lower than the target.
929+
// The coin is selected using coin control, with m_allow_other_inputs = false. SelectCoins should fail due to insufficient funds.
930+
931+
std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", m_args, CreateMockWalletDatabase());
932+
wallet->LoadWallet();
933+
LOCK(wallet->cs_wallet);
934+
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
935+
wallet->SetupDescriptorScriptPubKeyMans();
936+
937+
CoinsResult available_coins;
938+
{
939+
std::unique_ptr<CWallet> dummyWallet = std::make_unique<CWallet>(m_node.chain.get(), "dummy", m_args, CreateMockWalletDatabase());
940+
dummyWallet->LoadWallet();
941+
LOCK(dummyWallet->cs_wallet);
942+
dummyWallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
943+
dummyWallet->SetupDescriptorScriptPubKeyMans();
944+
945+
add_coin(available_coins, *dummyWallet, 100000); // 0.001 BTC
946+
}
947+
948+
CAmount target{99900}; // 0.000999 BTC
949+
950+
FastRandomContext rand;
951+
CoinSelectionParams cs_params{
952+
rand,
953+
/*change_output_size=*/34,
954+
/*change_spend_size=*/148,
955+
/*min_change_target=*/1000,
956+
/*effective_feerate=*/CFeeRate(3000),
957+
/*long_term_feerate=*/CFeeRate(1000),
958+
/*discard_feerate=*/CFeeRate(1000),
959+
/*tx_noinputs_size=*/0,
960+
/*avoid_partial=*/false,
961+
};
962+
CCoinControl cc;
963+
cc.m_allow_other_inputs = false;
964+
COutput output = available_coins.All().at(0);
965+
cc.SetInputWeight(output.outpoint, 148);
966+
cc.SelectExternal(output.outpoint, output.txout);
967+
968+
const auto result = SelectCoins(*wallet, available_coins, target, cc, cs_params);
969+
BOOST_CHECK(!result);
970+
}
971+
925972
BOOST_AUTO_TEST_SUITE_END()
926973
} // namespace wallet

0 commit comments

Comments
 (0)