Skip to content

Commit e8d490f

Browse files
author
MarcoFalke
committed
Merge #14636: Avoid using numeric_limits for sequence numbers and lock times
5352030 Avoid using numeric_limits for sequence numbers and lock times (Russell Yanofsky) bafb921 Remove duplicated code (Hennadii Stepanov) e4dc39b Replace platform dependent type with proper const (Hennadii Stepanov) Pull request description: Switches to named constants, because numeric_limits calls can be harder to read and less portable. Change was suggested by jamesob in bitcoin/bitcoin#10973 (comment) There are no changes in behavior except on some platforms we don't support (ILP64, IP16L32, I16LP32), where `SignalsOptInRBF` and `MutateTxAddInput` functions would now work correctly. Tree-SHA512: 3f5c6393c260551f65a0edfba55ef7eb3625232eec8d85b1457f26e144aa0b90c7ef5f44b2fd2f7d9be3c3bcb301030a9f5473c21b3bac566cc59b8c8780737c
2 parents 66c7024 + 5352030 commit e8d490f

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static void MutateTxAddInput(CMutableTransaction& tx, const std::string& strInpu
255255
throw std::runtime_error("invalid TX input vout '" + strVout + "'");
256256

257257
// extract the optional sequence number
258-
uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max();
258+
uint32_t nSequenceIn = CTxIn::SEQUENCE_FINAL;
259259
if (vStrInputParts.size() > 2)
260260
nSequenceIn = std::stoul(vStrInputParts[2]);
261261

src/policy/rbf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
bool SignalsOptInRBF(const CTransaction &tx)
88
{
99
for (const CTxIn &txin : tx.vin) {
10-
if (txin.nSequence < std::numeric_limits<unsigned int>::max()-1) {
10+
if (txin.nSequence <= MAX_BIP125_RBF_SEQUENCE) {
1111
return true;
1212
}
1313
}

src/rpc/rawtransaction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
346346

347347
if (!locktime.isNull()) {
348348
int64_t nLockTime = locktime.get_int64();
349-
if (nLockTime < 0 || nLockTime > std::numeric_limits<uint32_t>::max())
349+
if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
350350
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
351351
rawTx.nLockTime = nLockTime;
352352
}
@@ -368,18 +368,18 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
368368

369369
uint32_t nSequence;
370370
if (rbfOptIn) {
371-
nSequence = MAX_BIP125_RBF_SEQUENCE;
371+
nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
372372
} else if (rawTx.nLockTime) {
373-
nSequence = std::numeric_limits<uint32_t>::max() - 1;
373+
nSequence = CTxIn::SEQUENCE_FINAL - 1;
374374
} else {
375-
nSequence = std::numeric_limits<uint32_t>::max();
375+
nSequence = CTxIn::SEQUENCE_FINAL;
376376
}
377377

378378
// set the sequence number if passed in the parameters object
379379
const UniValue& sequenceObj = find_value(o, "sequence");
380380
if (sequenceObj.isNum()) {
381381
int64_t seqNr64 = sequenceObj.get_int64();
382-
if (seqNr64 < 0 || seqNr64 > std::numeric_limits<uint32_t>::max()) {
382+
if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
383383
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
384384
} else {
385385
nSequence = (uint32_t)seqNr64;

src/script/script.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ static const int MAX_STACK_SIZE = 1000;
3838
// otherwise as UNIX timestamp.
3939
static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC
4040

41+
// Maximum nLockTime. Since a lock time indicates the last invalid timestamp, a
42+
// transaction with this lock time will never be valid unless lock time
43+
// checking is disabled (by setting all input sequence numbers to
44+
// SEQUENCE_FINAL).
45+
static const uint32_t LOCKTIME_MAX = 0xFFFFFFFFU;
46+
4147
template <typename T>
4248
std::vector<unsigned char> ToByteVector(const T& in)
4349
{

src/test/skiplist_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_edge_test)
170170
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(-1)->nHeight, 0);
171171

172172
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(std::numeric_limits<int64_t>::min())->nHeight, 0);
173-
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(std::numeric_limits<unsigned int>::min())->nHeight, 0);
174173
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(-int64_t(std::numeric_limits<unsigned int>::max()) - 1)->nHeight, 0);
175174
BOOST_CHECK(!chain.FindEarliestAtLeast(std::numeric_limits<int64_t>::max()));
176175
BOOST_CHECK(!chain.FindEarliestAtLeast(std::numeric_limits<unsigned int>::max()));

0 commit comments

Comments
 (0)