From 2d299f128aad0cbb01cb83d36931ef307140470a Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Mon, 10 Jan 2022 14:48:13 +0100 Subject: [PATCH] merge bitcoin#24136: Extract CTxIn::MAX_SEQUENCE_NONFINAL constant, rework BIP 65/68/112 docs --- src/primitives/transaction.h | 36 +++++++++++++++++++++++++-------- src/rpc/rawtransaction_util.cpp | 7 ++++++- src/test/fuzz/util.cpp | 2 +- src/test/miner_tests.cpp | 2 +- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index 4e9a04b914..4a408958e2 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -74,25 +74,45 @@ public: CScript scriptSig; uint32_t nSequence; - /* Setting nSequence to this value for every input in a transaction - * disables nLockTime. */ + /** + * Setting nSequence to this value for every input in a transaction + * disables nLockTime/IsFinalTx(). + * It fails OP_CHECKLOCKTIMEVERIFY/CheckLockTime() for any input that has + * it set (BIP 65). + * It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112). + */ static const uint32_t SEQUENCE_FINAL = 0xffffffff; + /** + * This is the maximum sequence number that enables both nLockTime and + * OP_CHECKLOCKTIMEVERIFY (BIP 65). + * It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112). + */ + static const uint32_t MAX_SEQUENCE_NONFINAL{SEQUENCE_FINAL - 1}; - /* Below flags apply in the context of BIP 68*/ - /* If this flag set, CTxIn::nSequence is NOT interpreted as a - * relative lock-time. */ + // Below flags apply in the context of BIP 68. BIP 68 requires the tx + // version to be set to 2, or higher. + /** + * If this flag is set, CTxIn::nSequence is NOT interpreted as a + * relative lock-time. + * It skips SequenceLocks() for any input that has it set (BIP 68). + * It fails OP_CHECKSEQUENCEVERIFY/CheckSequence() for any input that has + * it set (BIP 112). + */ static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31); - /* If CTxIn::nSequence encodes a relative lock-time and this flag + /** + * If CTxIn::nSequence encodes a relative lock-time and this flag * is set, the relative lock-time has units of 512 seconds, * otherwise it specifies blocks with a granularity of 1. */ static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22); - /* If CTxIn::nSequence encodes a relative lock-time, this mask is + /** + * If CTxIn::nSequence encodes a relative lock-time, this mask is * applied to extract that lock-time from the sequence field. */ static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff; - /* In order to use the same number of bits to encode roughly the + /** + * In order to use the same number of bits to encode roughly the * same wall-clock duration, and because blocks are naturally * limited to occur every 600s on average, the minimum granularity * for time-based relative lock-time is fixed at 512 seconds. diff --git a/src/rpc/rawtransaction_util.cpp b/src/rpc/rawtransaction_util.cpp index 9e41942965..5291696b85 100644 --- a/src/rpc/rawtransaction_util.cpp +++ b/src/rpc/rawtransaction_util.cpp @@ -58,7 +58,12 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal if (nOutput < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative"); - uint32_t nSequence = (rawTx.nLockTime ? CTxIn::SEQUENCE_FINAL - 1 : CTxIn::SEQUENCE_FINAL); + uint32_t nSequence; + if (rawTx.nLockTime) { + nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; /* CTxIn::SEQUENCE_FINAL - 1 */ + } else { + nSequence = CTxIn::SEQUENCE_FINAL; + } // set the sequence number if passed in the parameters object const UniValue& sequenceObj = find_value(o, "sequence"); diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index 848040bb6f..bd55d99f97 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -348,7 +348,7 @@ uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept return fuzzed_data_provider.ConsumeBool() ? fuzzed_data_provider.PickValueInArray({ CTxIn::SEQUENCE_FINAL, - CTxIn::SEQUENCE_FINAL - 1 + CTxIn::MAX_SEQUENCE_NONFINAL, }) : fuzzed_data_provider.ConsumeIntegral(); } diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index 76017966b0..3788934cac 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -478,7 +478,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) // absolute height locked tx.vin[0].prevout.hash = txFirst[2]->GetHash(); - tx.vin[0].nSequence = CTxIn::SEQUENCE_FINAL - 1; + tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; prevheights[0] = baseheight + 3; tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1; hash = tx.GetHash();