Skip to content

Commit b583f73

Browse files
committed
Move vin filling to before final fee setting
It's unnecessary to fill in the vin with dummy inputs, calculate the fee, then fill in the vin with the actual inputs. Just fill the vin with the actual inputs the first time.
1 parent d39cac0 commit b583f73

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/wallet/spend.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,21 @@ bool CWallet::CreateTransactionInternal(
729729
assert(nChangePosInOut != -1);
730730
auto change_position = txNew.vout.insert(txNew.vout.begin() + nChangePosInOut, newTxOut);
731731

732-
// Dummy fill vin for maximum size estimation
732+
// Shuffle selected coins and fill in final vin
733+
std::vector<CInputCoin> selected_coins(setCoins.begin(), setCoins.end());
734+
Shuffle(selected_coins.begin(), selected_coins.end(), FastRandomContext());
735+
736+
// Note how the sequence number is set to non-maxint so that
737+
// the nLockTime set above actually works.
733738
//
734-
for (const auto& coin : setCoins) {
735-
txNew.vin.push_back(CTxIn(coin.outpoint,CScript()));
739+
// BIP125 defines opt-in RBF as any nSequence < maxint-1, so
740+
// we use the highest possible value in that range (maxint-2)
741+
// to avoid conflicting with other possible uses of nSequence,
742+
// and in the spirit of "smallest possible change from prior
743+
// behavior."
744+
const uint32_t nSequence = coin_control.m_signal_bip125_rbf.value_or(m_signal_rbf) ? MAX_BIP125_RBF_SEQUENCE : (CTxIn::SEQUENCE_FINAL - 1);
745+
for (const auto& coin : selected_coins) {
746+
txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
736747
}
737748

738749
// Calculate the transaction fee
@@ -813,24 +824,6 @@ bool CWallet::CreateTransactionInternal(
813824
return false;
814825
}
815826

816-
// Shuffle selected coins and fill in final vin
817-
txNew.vin.clear();
818-
std::vector<CInputCoin> selected_coins(setCoins.begin(), setCoins.end());
819-
Shuffle(selected_coins.begin(), selected_coins.end(), FastRandomContext());
820-
821-
// Note how the sequence number is set to non-maxint so that
822-
// the nLockTime set above actually works.
823-
//
824-
// BIP125 defines opt-in RBF as any nSequence < maxint-1, so
825-
// we use the highest possible value in that range (maxint-2)
826-
// to avoid conflicting with other possible uses of nSequence,
827-
// and in the spirit of "smallest possible change from prior
828-
// behavior."
829-
const uint32_t nSequence = coin_control.m_signal_bip125_rbf.value_or(m_signal_rbf) ? MAX_BIP125_RBF_SEQUENCE : (CTxIn::SEQUENCE_FINAL - 1);
830-
for (const auto& coin : selected_coins) {
831-
txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
832-
}
833-
834827
if (sign && !SignTransaction(txNew)) {
835828
error = _("Signing transaction failed");
836829
return false;

0 commit comments

Comments
 (0)