Skip to content

Commit

Permalink
Avoid using numeric_limits for sequence numbers and lock times
Browse files Browse the repository at this point in the history
Switches to named constants, because numeric_limits calls can be harder to read
and less portable.

Change was suggested by James O'Beirne <james.obeirne@gmail.com> in
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.
  • Loading branch information
ryanofsky authored and Michael Polzer committed Nov 14, 2018
1 parent aa4be14 commit 992d4c0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/chaincoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static void MutateTxAddInput(CMutableTransaction& tx, const std::string& strInpu
throw std::runtime_error("invalid TX input vout '" + strVout + "'");

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

Expand Down
10 changes: 5 additions & 5 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal

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

uint32_t nSequence;
if (rbfOptIn) {
nSequence = MAX_BIP125_RBF_SEQUENCE;
nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
} else if (rawTx.nLockTime) {
nSequence = std::numeric_limits<uint32_t>::max() - 1;
nSequence = CTxIn::SEQUENCE_FINAL - 1;
} else {
nSequence = std::numeric_limits<uint32_t>::max();
nSequence = CTxIn::SEQUENCE_FINAL;
}

// set the sequence number if passed in the parameters object
const UniValue& sequenceObj = find_value(o, "sequence");
if (sequenceObj.isNum()) {
int64_t seqNr64 = sequenceObj.get_int64();
if (seqNr64 < 0 || seqNr64 > std::numeric_limits<uint32_t>::max()) {
if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
} else {
nSequence = (uint32_t)seqNr64;
Expand Down
6 changes: 6 additions & 0 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ static const int MAX_STACK_SIZE = 1000;
// otherwise as UNIX timestamp.
static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC

// Maximum nLockTime. Since a lock time indicates the last invalid timestamp, a
// transaction with this lock time will never be valid unless lock time
// checking is disabled (by setting all input sequence numbers to
// SEQUENCE_FINAL).
static const uint32_t LOCKTIME_MAX = 0xFFFFFFFFU;

template <typename T>
std::vector<unsigned char> ToByteVector(const T& in)
{
Expand Down

0 comments on commit 992d4c0

Please sign in to comment.