Skip to content

Commit cda6c79

Browse files
committed
Merge bitcoin/bitcoin#26203: wallet: Use correct effective value when checking target
d0d9cf7 test: Check external coin effective value is used in CoinSelection (Aurèle Oulès) 76b79c1 wallet: Use correct effective value when checking target (Aurèle Oulès) Pull request description: Fixes #26185. The following assert failed because it was not checked in the parent function. https://github.com/bitcoin/bitcoin/blob/2bd9aa5a44b88c866c4d98f8a7bf7154049cba31/src/wallet/coinselection.cpp#L391 ACKs for top commit: glozow: reACK d0d9cf7 furszy: ACK d0d9cf7 Tree-SHA512: e126daba1115e9d143f2a582c6953e7ea55e96853b6e819c7744fd7a23668f7d9854681d43ef55d8774655bc54e7e87c1c9fccd746d9e30fbf3caa82ef808ae9
2 parents 1730f6c + d0d9cf7 commit cda6c79

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/wallet/spend.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,13 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& a
582582
if (coin_control.HasSelected() && !coin_control.m_allow_other_inputs) {
583583
SelectionResult result(nTargetValue, SelectionAlgorithm::MANUAL);
584584
result.AddInput(preset_inputs);
585-
if (result.GetSelectedValue() < nTargetValue) return std::nullopt;
585+
586+
if (!coin_selection_params.m_subtract_fee_outputs && result.GetSelectedEffectiveValue() < nTargetValue) {
587+
return std::nullopt;
588+
} else if (result.GetSelectedValue() < nTargetValue) {
589+
return std::nullopt;
590+
}
591+
586592
result.ComputeAndSetWaste(coin_selection_params.min_viable_change, coin_selection_params.m_cost_of_change, coin_selection_params.m_change_fee);
587593
return result;
588594
}

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)