Skip to content

Commit e8eab74

Browse files
committed
Clean up handling of known txin.nSeqeunce types; add type for UNCHECKED_METADATA for Lightning BOLT-3 compatability
1 parent 19557ee commit e8eab74

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/policy/policy.cpp

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,43 @@ bool IsStandardTx(const CTransaction& tx, bool permit_bare_multisig, const CFeeR
113113
// that we do not treat it as standard as new rules may apply
114114
// in the future due to an upgrade, so we prefer not to treat
115115
// such inputs as standard.
116-
const bool seq_is_reserved = (txin.nSequence < CTxIn::SEQUENCE_FINAL-2) && (
117-
// when sequence is set to disabled, it is reserved for future use
118-
((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) != 0) ||
119-
// when sequence has bits set outside of the type flag and locktime mask,
120-
// it is reserved for future use.
121-
((~(CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK) &
122-
txin.nSequence) != 0)
123-
);
124-
if (seq_is_reserved) {
116+
bool seq_is_reserved_for_upgrade;
117+
switch (static_cast<CTxIn::SEQUENCE_ROOT_TYPE>(txin.nSequence >> CTxIn::SEQUENCE_ROOT_TYPE_SHIFT)) {
118+
case CTxIn::SEQUENCE_ROOT_TYPE::NORMAL: {
119+
// when sequence has bits set outside of the type flag and locktime mask,
120+
// it is reserved for future use.
121+
// (locktime disable flag is implicitly 0 in SEQUENCE_ROOT_TYPE::NORMAL)
122+
const uint32_t mask = ~(CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK);
123+
seq_is_reserved_for_upgrade = (mask & txin.nSequence) != 0;
124+
break;
125+
}
126+
case CTxIn::SEQUENCE_ROOT_TYPE::UNCHECKED_METADATA: {
127+
seq_is_reserved_for_upgrade = false;
128+
break;
129+
}
130+
case CTxIn::SEQUENCE_ROOT_TYPE::SPECIAL: {
131+
switch (txin.nSequence) {
132+
case CTxIn::SEQUENCE_FINAL:
133+
case CTxIn::SEQUENCE_VALUE_RESERVED_NO_RBF_YES_CLTV:
134+
case CTxIn::SEQUENCE_VALUE_RESERVED_YES_RBF_NO_CSV: {
135+
seq_is_reserved_for_upgrade = false;
136+
break;
137+
}
138+
default: {
139+
// was not a special reserved value, keep reserved for upgrade.
140+
seq_is_reserved_for_upgrade = true;
141+
break;
142+
}
143+
}
144+
break;
145+
}
146+
default: {
147+
// unknown root type, keep reserved for upgrade.
148+
seq_is_reserved_for_upgrade = true;
149+
break;
150+
}
151+
}
152+
if (seq_is_reserved_for_upgrade) {
125153
reason = "sequence-flags-reserved";
126154
return false;
127155
}

src/primitives/transaction.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,31 @@ class CTxIn
7979
* relative lock-time. */
8080
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
8181

82+
/* The top 8 bits may be used as a type flag for marking specific
83+
* use cases. New uses cases should define their semantics via a
84+
* BIP before use, typically. This code was introducded in particular
85+
* for compatibility with the lightning network which uses UNCHECKED_METADATA
86+
* application. See Bolt-3:
87+
* https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#commitment-transaction
88+
*/
89+
static const uint32_t SEQUENCE_ROOT_TYPE_SHIFT = 24;
90+
91+
/* enum of all root sequence types */
92+
enum class SEQUENCE_ROOT_TYPE : uint8_t {
93+
NORMAL = 0x00,
94+
UNCHECKED_METADATA = 0x80,
95+
// Used for Special Values
96+
SPECIAL = 0xff,
97+
};
98+
99+
/* Reserved Values: */
100+
/* Sequence number that is rbf-opt-out (BIP 125); relative-timelock-opt-out
101+
* (BIP 68); and absolute-timelock-opt-in. */
102+
static const uint32_t SEQUENCE_VALUE_RESERVED_NO_RBF_YES_CLTV = 0xfffffffe;
103+
/* Sequence number that is rbf-opt-in (BIP 125); and relative-timelock-opt-out
104+
* (BIP 68); and absolute-timelock-opt-in. */
105+
static const uint32_t SEQUENCE_VALUE_RESERVED_YES_RBF_NO_CSV = 0xfffffffd;
106+
82107
/* If CTxIn::nSequence encodes a relative lock-time and this flag
83108
* is set, the relative lock-time has units of 512 seconds,
84109
* otherwise it specifies blocks with a granularity of 1. */

0 commit comments

Comments
 (0)