Skip to content

Commit 8f418aa

Browse files
josibakeSjors
authored andcommitted
Compare COutPoints lexicographically
Comparing outpoints lexicographically is required for BIP352, but also generally a less ambigious way to compare outpoints considering this will match any orderings applied to the serialized transaction.
1 parent 9d78a2c commit 8f418aa

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/primitives/transaction.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
#include <attributes.h>
1010
#include <consensus/amount.h>
11+
#include <crypto/common.h>
1112
#include <script/script.h>
1213
#include <serialize.h>
1314
#include <uint256.h>
1415
#include <util/transaction_identifier.h> // IWYU pragma: export
1516

1617
#include <cstddef>
1718
#include <cstdint>
19+
#include <cstring>
1820
#include <ios>
1921
#include <limits>
2022
#include <memory>
@@ -43,7 +45,12 @@ class COutPoint
4345

4446
friend bool operator<(const COutPoint& a, const COutPoint& b)
4547
{
46-
return std::tie(a.hash, a.n) < std::tie(b.hash, b.n);
48+
// Compare outpoints based on their 36-byte serialization (<txid little-endian>:<vout little-endian>)
49+
if (a.hash != b.hash) return a.hash < b.hash;
50+
uint8_t a_vout[4], b_vout[4];
51+
WriteLE32(a_vout, a.n);
52+
WriteLE32(b_vout, b.n);
53+
return std::memcmp(a_vout, b_vout, 4) < 0;
4754
}
4855

4956
friend bool operator==(const COutPoint& a, const COutPoint& b)

0 commit comments

Comments
 (0)