Skip to content

Commit b007efd

Browse files
committed
Allow BnB when subtract fee from outputs
1 parent db15e71 commit b007efd

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/wallet/test/coinselector_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
260260
vCoins.at(0).nInputBytes = 40; // Make sure that it has a negative effective value. The next check should assert if this somehow got through. Otherwise it will fail
261261
BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params_bnb, bnb_used));
262262

263+
// Test fees subtracted from output:
264+
empty_wallet();
265+
add_coin(1 * CENT);
266+
vCoins.at(0).nInputBytes = 40;
267+
BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params_bnb, bnb_used));
268+
coin_selection_params_bnb.m_subtract_fee_outputs = true;
269+
BOOST_CHECK(testWallet.SelectCoinsMinConf( 1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params_bnb, bnb_used));
270+
BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
271+
263272
// Make sure that can use BnB when there are preset inputs
264273
empty_wallet();
265274
{

src/wallet/wallet.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,7 +2648,11 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibil
26482648
if (effective_value > 0) {
26492649
group.fee += coin.m_input_bytes < 0 ? 0 : coin_selection_params.effective_fee.GetFee(coin.m_input_bytes);
26502650
group.long_term_fee += coin.m_input_bytes < 0 ? 0 : long_term_feerate.GetFee(coin.m_input_bytes);
2651-
group.effective_value += effective_value;
2651+
if (coin_selection_params.m_subtract_fee_outputs) {
2652+
group.effective_value += coin.txout.nValue;
2653+
} else {
2654+
group.effective_value += effective_value;
2655+
}
26522656
++it;
26532657
} else {
26542658
it = group.Discard(coin);
@@ -3022,7 +3026,8 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
30223026

30233027
// BnB selector is the only selector used when this is true.
30243028
// That should only happen on the first pass through the loop.
3025-
coin_selection_params.use_bnb = nSubtractFeeFromAmount == 0; // If we are doing subtract fee from recipient, then don't use BnB
3029+
coin_selection_params.use_bnb = true;
3030+
coin_selection_params.m_subtract_fee_outputs = nSubtractFeeFromAmount != 0; // If we are doing subtract fee from recipient, don't use effective values
30263031
// Start with no fee and loop until there is enough fee
30273032
while (true)
30283033
{
@@ -3036,7 +3041,9 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
30363041
nValueToSelect += nFeeRet;
30373042

30383043
// vouts to the payees
3039-
coin_selection_params.tx_noinputs_size = 11; // Static vsize overhead + outputs vsize. 4 nVersion, 4 nLocktime, 1 input count, 1 output count, 1 witness overhead (dummy, flag, stack size)
3044+
if (!coin_selection_params.m_subtract_fee_outputs) {
3045+
coin_selection_params.tx_noinputs_size = 11; // Static vsize overhead + outputs vsize. 4 nVersion, 4 nLocktime, 1 input count, 1 output count, 1 witness overhead (dummy, flag, stack size)
3046+
}
30403047
for (const auto& recipient : vecSend)
30413048
{
30423049
CTxOut txout(recipient.nAmount, recipient.scriptPubKey);
@@ -3053,7 +3060,9 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
30533060
}
30543061
}
30553062
// Include the fee cost for outputs. Note this is only used for BnB right now
3056-
coin_selection_params.tx_noinputs_size += ::GetSerializeSize(txout, PROTOCOL_VERSION);
3063+
if (!coin_selection_params.m_subtract_fee_outputs) {
3064+
coin_selection_params.tx_noinputs_size += ::GetSerializeSize(txout, PROTOCOL_VERSION);
3065+
}
30573066

30583067
if (IsDust(txout, chain().relayDustFee()))
30593068
{

src/wallet/wallet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,8 @@ struct CoinSelectionParams
715715
size_t change_spend_size = 0;
716716
CFeeRate effective_fee = CFeeRate(0);
717717
size_t tx_noinputs_size = 0;
718+
//! Indicate that we are subtracting the fee from outputs
719+
bool m_subtract_fee_outputs = false;
718720

719721
CoinSelectionParams(bool use_bnb, size_t change_output_size, size_t change_spend_size, CFeeRate effective_fee, size_t tx_noinputs_size) : use_bnb(use_bnb), change_output_size(change_output_size), change_spend_size(change_spend_size), effective_fee(effective_fee), tx_noinputs_size(tx_noinputs_size) {}
720722
CoinSelectionParams() {}

0 commit comments

Comments
 (0)