Move variable initializations to where they are used

- txNew nLockTime setting to txNew init
- FeeCalc to the fee estimation fetching
- setCoins to prior to SelectCoins
- nBytes to CalculateMaximumSignedTxSize call
- tx_sizes to CalculateMaximumSignedTxSize call
- coin_selection_params.m_avoid_partial_spends to params init
This commit is contained in:
Andrew Chow 2021-05-17 16:26:40 -04:00
parent 32ab430651
commit 364e0698a5

View file

@ -580,6 +580,12 @@ bool CWallet::CreateTransactionInternal(
{ {
AssertLockHeld(cs_wallet); AssertLockHeld(cs_wallet);
CMutableTransaction txNew; // The resulting transaction that we make
txNew.nLockTime = GetLocktimeForNewTransaction(chain(), GetLastBlockHash(), GetLastBlockHeight());
CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
coin_selection_params.m_avoid_partial_spends = coin_control.m_avoid_partial_spends;
CAmount recipients_sum = 0; CAmount recipients_sum = 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);
@ -592,17 +598,6 @@ bool CWallet::CreateTransactionInternal(
outputs_to_subtract_fee_from++; outputs_to_subtract_fee_from++;
} }
CMutableTransaction txNew;
FeeCalculation feeCalc;
TxSize tx_sizes;
int nBytes;
std::set<CInputCoin> setCoins;
txNew.nLockTime = GetLocktimeForNewTransaction(chain(), GetLastBlockHash(), GetLastBlockHeight());
std::vector<COutput> vAvailableCoins;
AvailableCoins(vAvailableCoins, &coin_control, 1, MAX_MONEY, MAX_MONEY, 0);
CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
coin_selection_params.m_avoid_partial_spends = coin_control.m_avoid_partial_spends;
// Create change script that will be used if we need change // Create change script that will be used if we need change
// TODO: pass in scriptChange instead of reservedest so // TODO: pass in scriptChange instead of reservedest so
// change transaction isn't always pay-to-bitcoin-address // change transaction isn't always pay-to-bitcoin-address
@ -648,6 +643,7 @@ bool CWallet::CreateTransactionInternal(
coin_selection_params.m_discard_feerate = GetDiscardRate(*this); coin_selection_params.m_discard_feerate = GetDiscardRate(*this);
// Get the fee rate to use effective values in coin selection // Get the fee rate to use effective values in coin selection
FeeCalculation feeCalc;
coin_selection_params.m_effective_feerate = GetMinimumFeeRate(*this, coin_control, &feeCalc); coin_selection_params.m_effective_feerate = GetMinimumFeeRate(*this, coin_control, &feeCalc);
// Do not, ever, assume that it's fine to change the fee rate if the user has explicitly // Do not, ever, assume that it's fine to change the fee rate if the user has explicitly
// provided one // provided one
@ -701,9 +697,13 @@ bool CWallet::CreateTransactionInternal(
const CAmount not_input_fees = coin_selection_params.m_effective_feerate.GetFee(coin_selection_params.tx_noinputs_size); const CAmount not_input_fees = coin_selection_params.m_effective_feerate.GetFee(coin_selection_params.tx_noinputs_size);
CAmount selection_target = recipients_sum + not_input_fees; CAmount selection_target = recipients_sum + not_input_fees;
// Get available coins
std::vector<COutput> vAvailableCoins;
AvailableCoins(vAvailableCoins, &coin_control, 1, MAX_MONEY, MAX_MONEY, 0);
// Choose coins to use // Choose coins to use
CAmount inputs_sum = 0; CAmount inputs_sum = 0;
setCoins.clear(); std::set<CInputCoin> setCoins;
if (!SelectCoins(vAvailableCoins, /* nTargetValue */ selection_target, setCoins, inputs_sum, coin_control, coin_selection_params)) if (!SelectCoins(vAvailableCoins, /* nTargetValue */ selection_target, setCoins, inputs_sum, coin_control, coin_selection_params))
{ {
error = _("Insufficient funds"); error = _("Insufficient funds");
@ -737,8 +737,8 @@ bool CWallet::CreateTransactionInternal(
} }
// Calculate the transaction fee // Calculate the transaction fee
tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly); TxSize tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
nBytes = tx_sizes.vsize; int nBytes = tx_sizes.vsize;
if (nBytes < 0) { if (nBytes < 0) {
error = _("Signing transaction failed"); error = _("Signing transaction failed");
return false; return false;