Skip to content

Commit fa4339e

Browse files
author
MarcoFalke
committed
Extract CTxIn::MAX_SEQUENCE_NONFINAL constant
1 parent 973c390 commit fa4339e

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

src/primitives/transaction.h

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,45 @@ class CTxIn
7070
uint32_t nSequence;
7171
CScriptWitness scriptWitness; //!< Only serialized through CTransaction
7272

73-
/* Setting nSequence to this value for every input in a transaction
74-
* disables nLockTime. */
73+
/**
74+
* Setting nSequence to this value for every input in a transaction
75+
* disables nLockTime/IsFinalTx().
76+
* It fails OP_CHECKLOCKTIMEVERIFY/CheckLockTime() for any input that has
77+
* it set (BIP 65).
78+
* It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112).
79+
*/
7580
static const uint32_t SEQUENCE_FINAL = 0xffffffff;
81+
/**
82+
* This is the maximum sequence number that enables both nLockTime and
83+
* OP_CHECKLOCKTIMEVERIFY (BIP 65).
84+
* It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112).
85+
*/
86+
static const uint32_t MAX_SEQUENCE_NONFINAL{SEQUENCE_FINAL - 1};
7687

77-
/* Below flags apply in the context of BIP 68*/
78-
/* If this flag set, CTxIn::nSequence is NOT interpreted as a
79-
* relative lock-time. */
88+
// Below flags apply in the context of BIP 68. BIP 68 requires the tx
89+
// version to be set to 2, or higher.
90+
/**
91+
* If this flag is set, CTxIn::nSequence is NOT interpreted as a
92+
* relative lock-time.
93+
* It skips SequenceLocks() for any input that has it set (BIP 68).
94+
* It fails OP_CHECKSEQUENCEVERIFY/CheckSequence() for any input that has
95+
* it set (BIP 112).
96+
*/
8097
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
8198

82-
/* If CTxIn::nSequence encodes a relative lock-time and this flag
99+
/**
100+
* If CTxIn::nSequence encodes a relative lock-time and this flag
83101
* is set, the relative lock-time has units of 512 seconds,
84102
* otherwise it specifies blocks with a granularity of 1. */
85103
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
86104

87-
/* If CTxIn::nSequence encodes a relative lock-time, this mask is
105+
/**
106+
* If CTxIn::nSequence encodes a relative lock-time, this mask is
88107
* applied to extract that lock-time from the sequence field. */
89108
static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
90109

91-
/* In order to use the same number of bits to encode roughly the
110+
/**
111+
* In order to use the same number of bits to encode roughly the
92112
* same wall-clock duration, and because blocks are naturally
93113
* limited to occur every 600s on average, the minimum granularity
94114
* for time-based relative lock-time is fixed at 512 seconds.

src/rpc/rawtransaction_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
6363
if (rbf) {
6464
nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
6565
} else if (rawTx.nLockTime) {
66-
nSequence = CTxIn::SEQUENCE_FINAL - 1;
66+
nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; /* CTxIn::SEQUENCE_FINAL - 1 */
6767
} else {
6868
nSequence = CTxIn::SEQUENCE_FINAL;
6969
}

src/test/fuzz/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept
408408
return fuzzed_data_provider.ConsumeBool() ?
409409
fuzzed_data_provider.PickValueInArray({
410410
CTxIn::SEQUENCE_FINAL,
411-
CTxIn::SEQUENCE_FINAL - 1,
411+
CTxIn::MAX_SEQUENCE_NONFINAL,
412412
MAX_BIP125_RBF_SEQUENCE,
413413
}) :
414414
fuzzed_data_provider.ConsumeIntegral<uint32_t>();

src/test/miner_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
459459

460460
// absolute height locked
461461
tx.vin[0].prevout.hash = txFirst[2]->GetHash();
462-
tx.vin[0].nSequence = CTxIn::SEQUENCE_FINAL - 1;
462+
tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL;
463463
prevheights[0] = baseheight + 3;
464464
tx.nLockTime = m_node.chainman->ActiveChain().Tip()->nHeight + 1;
465465
hash = tx.GetHash();

src/wallet/spend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ static bool CreateTransactionInternal(
798798
// to avoid conflicting with other possible uses of nSequence,
799799
// and in the spirit of "smallest possible change from prior
800800
// behavior."
801-
const uint32_t nSequence = coin_control.m_signal_bip125_rbf.value_or(wallet.m_signal_rbf) ? MAX_BIP125_RBF_SEQUENCE : (CTxIn::SEQUENCE_FINAL - 1);
801+
const uint32_t nSequence{coin_control.m_signal_bip125_rbf.value_or(wallet.m_signal_rbf) ? MAX_BIP125_RBF_SEQUENCE : CTxIn::MAX_SEQUENCE_NONFINAL};
802802
for (const auto& coin : selected_coins) {
803803
txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
804804
}

0 commit comments

Comments
 (0)