Skip to content

Commit

Permalink
wallet: add back missing CoinSelectionParams assignments
Browse files Browse the repository at this point in the history
They were removed as part of dash#3668 and omitted in related backports
but as bitcoin#17331 will be a logical partial revert of the former,
we need to add them back in preparation for it.
  • Loading branch information
kwvg committed Feb 9, 2025
1 parent bd35042 commit 6e4d789
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/wallet/spend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,18 @@ bool CWallet::CreateTransactionInternal(
// change keypool ran out, but change is required.
CHECK_NONFATAL(IsValidDestination(dest) != scriptChange.empty());
}
CTxOut change_prototype_txout(0, scriptChange);
coin_selection_params.change_output_size = GetSerializeSize(change_prototype_txout);

// Get size of spending the change output
int change_spend_size = CalculateMaximumSignedInputSize(change_prototype_txout, this);
// If the wallet doesn't know how to sign change output, assume p2sh-p2pkh
// as lower-bound to allow BnB to do it's thing
if (change_spend_size == -1) {
coin_selection_params.change_spend_size = DUMMY_NESTED_P2PKH_INPUT_SIZE;
} else {
coin_selection_params.change_spend_size = (size_t)change_spend_size;
}

// Set discard feerate
coin_selection_params.m_discard_feerate = coin_control.m_discard_feerate ? *coin_control.m_discard_feerate : GetDiscardRate(*this);
Expand All @@ -704,12 +716,18 @@ bool CWallet::CreateTransactionInternal(
return false;
}

// Get long term estimate
CCoinControl cc_temp;
cc_temp.m_confirm_target = chain().estimateMaxBlocks();
coin_selection_params.m_long_term_feerate = GetMinimumFeeRate(*this, cc_temp, nullptr);

nFeeRet = 0;
bool pick_new_inputs = true;
CAmount nValueIn = 0;

// BnB selector is the only selector used when this is true.
coin_selection_params.use_bnb = false; // Dash: never use BnB
coin_selection_params.m_subtract_fee_outputs = nSubtractFeeFromAmount != 0; // If we are doing subtract fee from recipient, don't use effective values

CAmount nAmountToSelectAdditional{0};
// Start with nAmountToSelectAdditional=0 and loop until there is enough to cover the request + fees, try it 500 times.
Expand All @@ -726,7 +744,11 @@ bool CWallet::CreateTransactionInternal(
assert(nAmountToSelectAdditional >= 0);
nValueToSelect += nAmountToSelectAdditional;
}

// vouts to the payees
if (!coin_selection_params.m_subtract_fee_outputs) {
coin_selection_params.tx_noinputs_size = 10; // Static vsize overhead + outputs vsize. 4 nVersion, 4 nLocktime, 1 input count, 1 output count
}
for (const auto& recipient : vecSend)
{
CTxOut txout(recipient.nAmount, recipient.scriptPubKey);
Expand All @@ -743,6 +765,11 @@ bool CWallet::CreateTransactionInternal(
}
}

// Include the fee cost for outputs. Note this is only used for BnB right now
if (!coin_selection_params.m_subtract_fee_outputs) {
coin_selection_params.tx_noinputs_size += ::GetSerializeSize(txout, PROTOCOL_VERSION);
}

if (IsDust(txout, chain().relayDustFee()))
{
if (recipient.fSubtractFeeFromAmount && nFeeRet > 0)
Expand Down
1 change: 0 additions & 1 deletion src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,6 @@ static size_t CalculateNestedKeyhashInputSize(bool use_max_sig)
return ::GetSerializeSize(tx_in, PROTOCOL_VERSION);
}

static constexpr size_t DUMMY_NESTED_P2PKH_INPUT_SIZE = 113;
BOOST_FIXTURE_TEST_CASE(dummy_input_size_test, TestChain100Setup)
{
BOOST_CHECK_EQUAL(CalculateNestedKeyhashInputSize(false), DUMMY_NESTED_P2PKH_INPUT_SIZE);
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ static const CAmount DEFAULT_TRANSACTION_MAXFEE = COIN / 10;
static const CAmount HIGH_TX_FEE_PER_KB = COIN / 100;
//! -maxtxfee will warn if called with a higher fee than this amount (in satoshis)
static const CAmount HIGH_MAX_TX_FEE = 100 * HIGH_TX_FEE_PER_KB;
//! Pre-calculated constants for input size estimation in *virtual size*
static constexpr size_t DUMMY_NESTED_P2PKH_INPUT_SIZE = 113;

//! if set, all keys will be derived by using BIP39/BIP44
static const bool DEFAULT_USE_HD_WALLET = true;
Expand Down

0 comments on commit 6e4d789

Please sign in to comment.