Skip to content

Commit 0cc10f8

Browse files
committed
refactor: Remove CreateTransactionInternal fee_needed variable
This commit does not change behavior in any way. It just removes a variable and adds a comment to explain nFeeRet return value better. The fee_needed variable became redundant after commit 9d3bd74 removed looping in CreateTransactionInternal. This change was originally posted #22008 (comment) and discussed in the thread of that PR and in IRC. It might be followed up with more changes later to alter subtract from recipient behavior, but this commit just cleans up and documents the existing code.
1 parent 68f7985 commit 0cc10f8

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/wallet/spend.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,8 @@ bool CWallet::CreateTransactionInternal(
756756
nFeeRet = coin_selection_params.m_effective_feerate.GetFee(nBytes);
757757

758758
// Subtract fee from the change output if not subtracting it from recipient outputs
759-
CAmount fee_needed = nFeeRet;
760759
if (!coin_selection_params.m_subtract_fee_outputs) {
761-
change_position->nValue -= fee_needed;
760+
change_position->nValue -= nFeeRet;
762761
}
763762

764763
// We want to drop the change to fees if:
@@ -774,17 +773,12 @@ bool CWallet::CreateTransactionInternal(
774773
// Because we have dropped this change, the tx size and required fee will be different, so let's recalculate those
775774
tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
776775
nBytes = tx_sizes.vsize;
777-
fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes);
778-
}
779-
780-
// Update nFeeRet in case fee_needed changed due to dropping the change output
781-
if (fee_needed <= change_and_fee - change_amount) {
782-
nFeeRet = change_and_fee - change_amount;
776+
nFeeRet = coin_selection_params.m_effective_feerate.GetFee(nBytes);
783777
}
784778

785779
// Reduce output values for subtractFeeFromAmount
786780
if (coin_selection_params.m_subtract_fee_outputs) {
787-
CAmount to_reduce = fee_needed + change_amount - change_and_fee;
781+
CAmount to_reduce = nFeeRet + change_amount - change_and_fee;
788782
int i = 0;
789783
bool fFirst = true;
790784
for (const auto& recipient : vecSend)
@@ -816,7 +810,15 @@ bool CWallet::CreateTransactionInternal(
816810
}
817811
++i;
818812
}
819-
nFeeRet = fee_needed;
813+
} else if (nFeeRet <= change_and_fee - change_amount) {
814+
// If dropping the change output covered the fee, update the returned
815+
// fee amount. Note that that in subtract-fee-from-recipients case
816+
// above, if the change output is dropped, the change dust value will
817+
// be paid to recipients, rather than to the miner (`to_reduce` above
818+
// will be negative). In that case, the dust amount sent is an
819+
// additional cost of the transaction, but it's not considered part of
820+
// the fee since it isn't paid to the miner.
821+
nFeeRet = change_and_fee - change_amount;
820822
}
821823

822824
// Give up if change keypool ran out and change is required

0 commit comments

Comments
 (0)