Skip to content

Commit

Permalink
wallet: copy and sort vecSend if BIP69 sorting enabled, enable sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Feb 9, 2025
1 parent 9e9e66f commit 7e54bd9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/wallet/spend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ bool CWallet::CreateTransactionInternal(
CAmount nValue = 0;
ReserveDestination reservedest(this);
int nChangePosRequest = nChangePosInOut;
const bool sort_bip69{false};
const bool sort_bip69{nChangePosRequest == -1};
unsigned int nSubtractFeeFromAmount = 0;
for (const auto& recipient : vecSend)
{
Expand Down Expand Up @@ -798,8 +798,17 @@ bool CWallet::CreateTransactionInternal(
assert(nChangePosInOut != -1);
auto change_position = txNew.vout.insert(txNew.vout.begin() + nChangePosInOut, newTxOut);

// We're making a copy of vecSend because it's const, sortedVecSend should be used
// in place of vecSend in all subsequent usage.
std::vector<CRecipient> sortedVecSend{vecSend};
if (sort_bip69) {
std::sort(txNew.vout.begin(), txNew.vout.end(), CompareOutputBIP69());
// The output reduction loop uses vecSend to map to txNew.vout, we need to
// shuffle them both to ensure this mapping remains consistent
std::sort(sortedVecSend.begin(), sortedVecSend.end(),
[](const CRecipient& a, const CRecipient& b) {
return a.nAmount < b.nAmount || (a.nAmount == b.nAmount && a.scriptPubKey < b.scriptPubKey);
});

// If there was a change output added before, we must update its position now
if (const auto it = std::find(txNew.vout.begin(), txNew.vout.end(), newTxOut); it != txNew.vout.end()) {
Expand Down Expand Up @@ -859,7 +868,7 @@ bool CWallet::CreateTransactionInternal(
CAmount to_reduce = fee_needed + change_amount - change_and_fee;
int i = 0;
bool fFirst = true;
for (const auto& recipient : vecSend)
for (const auto& recipient : sortedVecSend)
{
if (i == nChangePosInOut) {
++i;
Expand Down

0 comments on commit 7e54bd9

Please sign in to comment.