Skip to content

Commit

Permalink
fix: correct fee calculations in CreateTransactionInternal
Browse files Browse the repository at this point in the history
Adapted from 87e0ef9 in bitcoin#25647 and c1a84f1 in bitcoin#26643

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
  • Loading branch information
kwvg and UdjinM6 committed Feb 9, 2025
1 parent 445f489 commit 0185847
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/wallet/spend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ bool CWallet::CreateTransactionInternal(
CMutableTransaction txNew;
FeeCalculation feeCalc;
int nBytes{0};
CAmount fee_needed{0};
{
std::set<CInputCoin> setCoins;
LOCK(cs_wallet);
Expand Down Expand Up @@ -806,9 +807,8 @@ bool CWallet::CreateTransactionInternal(
nBytes += GetSizeOfCompactSize(nExtraPayloadSize) + nExtraPayloadSize;
}

nFeeRet = coin_selection_params.m_effective_feerate.GetFee(nBytes);
fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes);

CAmount fee_needed = nFeeRet;
if (nSubtractFeeFromAmount == 0) {
change_position->nValue -= fee_needed;
}
Expand All @@ -828,6 +828,8 @@ bool CWallet::CreateTransactionInternal(
fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes);
}

nFeeRet = inputs_sum - nValue - change_amount;

// Update nFeeRet in case fee_needed changed due to dropping the change output
if (fee_needed <= change_and_fee - change_amount) {
nFeeRet = change_and_fee - change_amount;
Expand Down Expand Up @@ -903,6 +905,11 @@ bool CWallet::CreateTransactionInternal(
}
}

if (fee_needed > nFeeRet) {
error = _("Fee needed > fee paid");
return false;
}

if (nFeeRet > m_default_max_tx_fee) {
error = TransactionErrorString(TransactionError::MAX_FEE_EXCEEDED);
return false;
Expand Down

0 comments on commit 0185847

Please sign in to comment.