0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-13 11:25:02 -05:00

Merge #14636: Avoid using numeric_limits for sequence numbers and lock times

535203075e Avoid using numeric_limits for sequence numbers and lock times (Russell Yanofsky)
bafb921507 Remove duplicated code (Hennadii Stepanov)
e4dc39b3bc 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 https://github.com/bitcoin/bitcoin/pull/10973#discussion_r213473620

  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
This commit is contained in:
MarcoFalke 2018-11-07 11:14:45 -05:00
commit e8d490f27e
No known key found for this signature in database
GPG key ID: D2EA4850E7528B25
5 changed files with 13 additions and 8 deletions

View file

@ -255,7 +255,7 @@ static void MutateTxAddInput(CMutableTransaction& tx, const std::string& strInpu
throw std::runtime_error("invalid TX input vout '" + strVout + "'"); throw std::runtime_error("invalid TX input vout '" + strVout + "'");
// extract the optional sequence number // extract the optional sequence number
uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max(); uint32_t nSequenceIn = CTxIn::SEQUENCE_FINAL;
if (vStrInputParts.size() > 2) if (vStrInputParts.size() > 2)
nSequenceIn = std::stoul(vStrInputParts[2]); nSequenceIn = std::stoul(vStrInputParts[2]);

View file

@ -7,7 +7,7 @@
bool SignalsOptInRBF(const CTransaction &tx) bool SignalsOptInRBF(const CTransaction &tx)
{ {
for (const CTxIn &txin : tx.vin) { for (const CTxIn &txin : tx.vin) {
if (txin.nSequence < std::numeric_limits<unsigned int>::max()-1) { if (txin.nSequence <= MAX_BIP125_RBF_SEQUENCE) {
return true; return true;
} }
} }

View file

@ -346,7 +346,7 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
if (!locktime.isNull()) { if (!locktime.isNull()) {
int64_t nLockTime = locktime.get_int64(); 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"); throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
rawTx.nLockTime = nLockTime; rawTx.nLockTime = nLockTime;
} }
@ -368,18 +368,18 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
uint32_t nSequence; uint32_t nSequence;
if (rbfOptIn) { if (rbfOptIn) {
nSequence = MAX_BIP125_RBF_SEQUENCE; nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
} else if (rawTx.nLockTime) { } else if (rawTx.nLockTime) {
nSequence = std::numeric_limits<uint32_t>::max() - 1; nSequence = CTxIn::SEQUENCE_FINAL - 1;
} else { } else {
nSequence = std::numeric_limits<uint32_t>::max(); nSequence = CTxIn::SEQUENCE_FINAL;
} }
// set the sequence number if passed in the parameters object // set the sequence number if passed in the parameters object
const UniValue& sequenceObj = find_value(o, "sequence"); const UniValue& sequenceObj = find_value(o, "sequence");
if (sequenceObj.isNum()) { if (sequenceObj.isNum()) {
int64_t seqNr64 = sequenceObj.get_int64(); 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"); throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
} else { } else {
nSequence = (uint32_t)seqNr64; nSequence = (uint32_t)seqNr64;

View file

@ -38,6 +38,12 @@ static const int MAX_STACK_SIZE = 1000;
// otherwise as UNIX timestamp. // otherwise as UNIX timestamp.
static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC 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> template <typename T>
std::vector<unsigned char> ToByteVector(const T& in) std::vector<unsigned char> ToByteVector(const T& in)
{ {

View file

@ -170,7 +170,6 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_edge_test)
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(-1)->nHeight, 0); BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(-1)->nHeight, 0);
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(std::numeric_limits<int64_t>::min())->nHeight, 0); BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(std::numeric_limits<int64_t>::min())->nHeight, 0);
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(std::numeric_limits<unsigned int>::min())->nHeight, 0);
BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(-int64_t(std::numeric_limits<unsigned int>::max()) - 1)->nHeight, 0); BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(-int64_t(std::numeric_limits<unsigned int>::max()) - 1)->nHeight, 0);
BOOST_CHECK(!chain.FindEarliestAtLeast(std::numeric_limits<int64_t>::max())); BOOST_CHECK(!chain.FindEarliestAtLeast(std::numeric_limits<int64_t>::max()));
BOOST_CHECK(!chain.FindEarliestAtLeast(std::numeric_limits<unsigned int>::max())); BOOST_CHECK(!chain.FindEarliestAtLeast(std::numeric_limits<unsigned int>::max()));