Skip to content

Commit 2fb9c1e

Browse files
committed
shuffle selected coins before transaction finalization
1 parent 1ec1602 commit 2fb9c1e

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/wallet/wallet.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,20 +2889,11 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CTransac
28892889
nChangePosInOut = -1;
28902890
}
28912891

2892-
// Fill vin
2892+
// Dummy fill vin for maximum size estimation
28932893
//
2894-
// Note how the sequence number is set to non-maxint so that
2895-
// the nLockTime set above actually works.
2896-
//
2897-
// BIP125 defines opt-in RBF as any nSequence < maxint-1, so
2898-
// we use the highest possible value in that range (maxint-2)
2899-
// to avoid conflicting with other possible uses of nSequence,
2900-
// and in the spirit of "smallest possible change from prior
2901-
// behavior."
2902-
const uint32_t nSequence = coin_control.signalRbf ? MAX_BIP125_RBF_SEQUENCE : (CTxIn::SEQUENCE_FINAL - 1);
2903-
for (const auto& coin : setCoins)
2904-
txNew.vin.push_back(CTxIn(coin.outpoint,CScript(),
2905-
nSequence));
2894+
for (const auto& coin : setCoins) {
2895+
txNew.vin.push_back(CTxIn(coin.outpoint,CScript()));
2896+
}
29062897

29072898
nBytes = CalculateMaximumSignedTxSize(txNew, this);
29082899
if (nBytes < 0) {
@@ -2992,11 +2983,29 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CTransac
29922983

29932984
if (nChangePosInOut == -1) reservekey.ReturnKey(); // Return any reserved key if we don't have change
29942985

2986+
// Shuffle selected coins and fill in final vin
2987+
txNew.vin.clear();
2988+
std::vector<CInputCoin> selected_coins(setCoins.begin(), setCoins.end());
2989+
std::shuffle(selected_coins.begin(), selected_coins.end(), FastRandomContext());
2990+
2991+
// Note how the sequence number is set to non-maxint so that
2992+
// the nLockTime set above actually works.
2993+
//
2994+
// BIP125 defines opt-in RBF as any nSequence < maxint-1, so
2995+
// we use the highest possible value in that range (maxint-2)
2996+
// to avoid conflicting with other possible uses of nSequence,
2997+
// and in the spirit of "smallest possible change from prior
2998+
// behavior."
2999+
const uint32_t nSequence = coin_control.signalRbf ? MAX_BIP125_RBF_SEQUENCE : (CTxIn::SEQUENCE_FINAL - 1);
3000+
for (const auto& coin : selected_coins) {
3001+
txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
3002+
}
3003+
29953004
if (sign)
29963005
{
29973006
CTransaction txNewConst(txNew);
29983007
int nIn = 0;
2999-
for (const auto& coin : setCoins)
3008+
for (const auto& coin : selected_coins)
30003009
{
30013010
const CScript& scriptPubKey = coin.txout.scriptPubKey;
30023011
SignatureData sigdata;

0 commit comments

Comments
 (0)