mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-06 14:19:59 -05:00
Remove CreateTransaction while loop and some related variables
Remove the CreateTransaction while loop. Removes variables that were only needed because of that loop. Also renames a few variables and moves their declarations to where they are used. Some subtractFeeFromOutputs handling is moved to after coin selection in order to reduce their amounts once the fee is known. If subtracting the fee reduces the change to dust, we will also now remove the change output
This commit is contained in:
parent
6f0d5189af
commit
9d3bd74ab4
1 changed files with 111 additions and 124 deletions
|
@ -2804,7 +2804,6 @@ bool CWallet::CreateTransactionInternal(
|
||||||
CAmount nValue = 0;
|
CAmount nValue = 0;
|
||||||
const OutputType change_type = TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type : m_default_change_type, vecSend);
|
const OutputType change_type = TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type : m_default_change_type, vecSend);
|
||||||
ReserveDestination reservedest(this, change_type);
|
ReserveDestination reservedest(this, change_type);
|
||||||
int nChangePosRequest = nChangePosInOut;
|
|
||||||
unsigned int nSubtractFeeFromAmount = 0;
|
unsigned int nSubtractFeeFromAmount = 0;
|
||||||
for (const auto& recipient : vecSend)
|
for (const auto& recipient : vecSend)
|
||||||
{
|
{
|
||||||
|
@ -2909,16 +2908,7 @@ bool CWallet::CreateTransactionInternal(
|
||||||
coin_selection_params.m_change_fee = coin_selection_params.m_effective_feerate.GetFee(coin_selection_params.change_output_size);
|
coin_selection_params.m_change_fee = coin_selection_params.m_effective_feerate.GetFee(coin_selection_params.change_output_size);
|
||||||
coin_selection_params.m_cost_of_change = coin_selection_params.m_discard_feerate.GetFee(coin_selection_params.change_spend_size) + coin_selection_params.m_change_fee;
|
coin_selection_params.m_cost_of_change = coin_selection_params.m_discard_feerate.GetFee(coin_selection_params.change_spend_size) + coin_selection_params.m_change_fee;
|
||||||
|
|
||||||
nFeeRet = 0;
|
|
||||||
CAmount nValueIn = 0;
|
|
||||||
|
|
||||||
coin_selection_params.m_subtract_fee_outputs = nSubtractFeeFromAmount != 0; // If we are doing subtract fee from recipient, don't use effective values
|
coin_selection_params.m_subtract_fee_outputs = nSubtractFeeFromAmount != 0; // If we are doing subtract fee from recipient, don't use effective values
|
||||||
// Start with no fee and loop until there is enough fee
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
nChangePosInOut = nChangePosRequest;
|
|
||||||
txNew.vin.clear();
|
|
||||||
txNew.vout.clear();
|
|
||||||
|
|
||||||
// vouts to the payees
|
// vouts to the payees
|
||||||
if (!coin_selection_params.m_subtract_fee_outputs) {
|
if (!coin_selection_params.m_subtract_fee_outputs) {
|
||||||
|
@ -2946,9 +2936,9 @@ bool CWallet::CreateTransactionInternal(
|
||||||
CAmount nValueToSelect = nValue + not_input_fees;
|
CAmount nValueToSelect = nValue + not_input_fees;
|
||||||
|
|
||||||
// Choose coins to use
|
// Choose coins to use
|
||||||
nValueIn = 0;
|
CAmount inputs_sum = 0;
|
||||||
setCoins.clear();
|
setCoins.clear();
|
||||||
if (!SelectCoins(vAvailableCoins, /* nTargetValue */ nValueToSelect, setCoins, nValueIn, coin_control, coin_selection_params))
|
if (!SelectCoins(vAvailableCoins, /* nTargetValue */ nValueToSelect, setCoins, inputs_sum, coin_control, coin_selection_params))
|
||||||
{
|
{
|
||||||
error = _("Insufficient funds");
|
error = _("Insufficient funds");
|
||||||
return false;
|
return false;
|
||||||
|
@ -2956,7 +2946,7 @@ bool CWallet::CreateTransactionInternal(
|
||||||
|
|
||||||
// Always make a change output
|
// Always make a change output
|
||||||
// We will reduce the fee from this change output later, and remove the output if it is too small.
|
// We will reduce the fee from this change output later, and remove the output if it is too small.
|
||||||
const CAmount change_and_fee = nValueIn - nValue;
|
const CAmount change_and_fee = inputs_sum - nValue;
|
||||||
assert(change_and_fee >= 0);
|
assert(change_and_fee >= 0);
|
||||||
CTxOut newTxOut(change_and_fee, scriptChange);
|
CTxOut newTxOut(change_and_fee, scriptChange);
|
||||||
|
|
||||||
|
@ -3011,10 +3001,9 @@ bool CWallet::CreateTransactionInternal(
|
||||||
fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes);
|
fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the fee is covered, there's no need to loop or subtract from recipients
|
// Update nFeeRet in case fee_needed changed due to dropping the change output
|
||||||
if (fee_needed <= change_and_fee - change_amount) {
|
if (fee_needed <= change_and_fee - change_amount) {
|
||||||
nFeeRet = change_and_fee - change_amount;
|
nFeeRet = change_and_fee - change_amount;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce output values for subtractFeeFromAmount
|
// Reduce output values for subtractFeeFromAmount
|
||||||
|
@ -3052,8 +3041,6 @@ bool CWallet::CreateTransactionInternal(
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
nFeeRet = fee_needed;
|
nFeeRet = fee_needed;
|
||||||
break; // The fee has been deducted from the recipients, nothing left to do here
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give up if change keypool ran out and change is required
|
// Give up if change keypool ran out and change is required
|
||||||
|
|
Loading…
Add table
Reference in a new issue