Skip to content

Commit 8819fbe

Browse files
kwvgUdjinM6
andcommitted
fix: correct fee calculations in CreateTransactionInternal
Adapted from 87e0ef9 in bitcoin#25647 and c1a84f1 in bitcoin#26643 Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
1 parent 29f64cf commit 8819fbe

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/wallet/wallet.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,7 @@ bool CWallet::CreateTransactionInternal(
35493549
CMutableTransaction txNew;
35503550
FeeCalculation feeCalc;
35513551
int nBytes{0};
3552+
CAmount fee_needed{0};
35523553
{
35533554
std::set<CInputCoin> setCoins;
35543555
LOCK(cs_wallet);
@@ -3730,9 +3731,8 @@ bool CWallet::CreateTransactionInternal(
37303731
nBytes += GetSizeOfCompactSize(nExtraPayloadSize) + nExtraPayloadSize;
37313732
}
37323733

3733-
nFeeRet = coin_selection_params.m_effective_feerate.GetFee(nBytes);
3734+
fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes);
37343735

3735-
CAmount fee_needed = nFeeRet;
37363736
if (nSubtractFeeFromAmount == 0) {
37373737
change_position->nValue -= fee_needed;
37383738
}
@@ -3752,6 +3752,8 @@ bool CWallet::CreateTransactionInternal(
37523752
fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes);
37533753
}
37543754

3755+
nFeeRet = inputs_sum - nValue - change_amount;
3756+
37553757
// Update nFeeRet in case fee_needed changed due to dropping the change output
37563758
if (fee_needed <= change_and_fee - change_amount) {
37573759
nFeeRet = change_and_fee - change_amount;
@@ -3829,6 +3831,11 @@ bool CWallet::CreateTransactionInternal(
38293831
}
38303832
}
38313833

3834+
if (fee_needed > nFeeRet) {
3835+
error = _("Fee needed > fee paid");
3836+
return false;
3837+
}
3838+
38323839
if (nFeeRet > m_default_max_tx_fee) {
38333840
error = TransactionErrorString(TransactionError::MAX_FEE_EXCEEDED);
38343841
return false;

0 commit comments

Comments
 (0)