Skip to content

Commit 364e069

Browse files
committed
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
1 parent 32ab430 commit 364e069

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/wallet/spend.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,12 @@ bool CWallet::CreateTransactionInternal(
580580
{
581581
AssertLockHeld(cs_wallet);
582582

583+
CMutableTransaction txNew; // The resulting transaction that we make
584+
txNew.nLockTime = GetLocktimeForNewTransaction(chain(), GetLastBlockHash(), GetLastBlockHeight());
585+
586+
CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
587+
coin_selection_params.m_avoid_partial_spends = coin_control.m_avoid_partial_spends;
588+
583589
CAmount recipients_sum = 0;
584590
const OutputType change_type = TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type : m_default_change_type, vecSend);
585591
ReserveDestination reservedest(this, change_type);
@@ -592,17 +598,6 @@ bool CWallet::CreateTransactionInternal(
592598
outputs_to_subtract_fee_from++;
593599
}
594600

595-
CMutableTransaction txNew;
596-
FeeCalculation feeCalc;
597-
TxSize tx_sizes;
598-
int nBytes;
599-
std::set<CInputCoin> setCoins;
600-
txNew.nLockTime = GetLocktimeForNewTransaction(chain(), GetLastBlockHash(), GetLastBlockHeight());
601-
std::vector<COutput> vAvailableCoins;
602-
AvailableCoins(vAvailableCoins, &coin_control, 1, MAX_MONEY, MAX_MONEY, 0);
603-
CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
604-
coin_selection_params.m_avoid_partial_spends = coin_control.m_avoid_partial_spends;
605-
606601
// Create change script that will be used if we need change
607602
// TODO: pass in scriptChange instead of reservedest so
608603
// change transaction isn't always pay-to-bitcoin-address
@@ -648,6 +643,7 @@ bool CWallet::CreateTransactionInternal(
648643
coin_selection_params.m_discard_feerate = GetDiscardRate(*this);
649644

650645
// Get the fee rate to use effective values in coin selection
646+
FeeCalculation feeCalc;
651647
coin_selection_params.m_effective_feerate = GetMinimumFeeRate(*this, coin_control, &feeCalc);
652648
// Do not, ever, assume that it's fine to change the fee rate if the user has explicitly
653649
// provided one
@@ -701,9 +697,13 @@ bool CWallet::CreateTransactionInternal(
701697
const CAmount not_input_fees = coin_selection_params.m_effective_feerate.GetFee(coin_selection_params.tx_noinputs_size);
702698
CAmount selection_target = recipients_sum + not_input_fees;
703699

700+
// Get available coins
701+
std::vector<COutput> vAvailableCoins;
702+
AvailableCoins(vAvailableCoins, &coin_control, 1, MAX_MONEY, MAX_MONEY, 0);
703+
704704
// Choose coins to use
705705
CAmount inputs_sum = 0;
706-
setCoins.clear();
706+
std::set<CInputCoin> setCoins;
707707
if (!SelectCoins(vAvailableCoins, /* nTargetValue */ selection_target, setCoins, inputs_sum, coin_control, coin_selection_params))
708708
{
709709
error = _("Insufficient funds");
@@ -737,8 +737,8 @@ bool CWallet::CreateTransactionInternal(
737737
}
738738

739739
// Calculate the transaction fee
740-
tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
741-
nBytes = tx_sizes.vsize;
740+
TxSize tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
741+
int nBytes = tx_sizes.vsize;
742742
if (nBytes < 0) {
743743
error = _("Signing transaction failed");
744744
return false;

0 commit comments

Comments
 (0)