Buried deployments, script-verification flags by height, and the two exception blocks
DefinitionBitcoinActivationA buried deployment is one of HEIGHTINCB (BIP34), CLTV (BIP65), DERSIG (BIP66), CSV, SEGWIT, with mainnet activation heights , , , , (deploymentHeight). activeAt(height, dep) holds when the deployment height is at most the block height, as DeploymentActiveAt; activeAfter(prevHeight, dep) holds when it is at most prevHeight , as DeploymentActiveAfter.
A flag set is a record of seven Booleans: p2sh, witness, taproot, dersig, cltv, csv, nulldummy. noFlags has none; baseFlags has p2sh, witness, taproot. A block's exception status is none, bip16Block (the hash-keyed P2SH violation) or taprootBlock (the hash-keyed Taproot violation); exceptionBase yields baseFlags, noFlags, and p2sh+witness respectively.
blockScriptFlags(height, exc) is GetBlockScriptFlags: the exception base, with dersig, cltv, csv, nulldummy each OR-ed with activeAt of DERSIG, CLTV, CSV, SEGWIT at the height. ScriptFlags.le is pointwise inclusion: every flag true in the first is true in the second.
, and lockTimeFlags(height) is that value when CSV is active at the height and otherwise, as nLockTimeFlags in ConnectBlock.
Formalization Note. The exception table is modeled by the three-valued classification rather than a hash lookup. Versionbits deployments are not modeled, since the pinned source consults none for mainnet consensus flags. Only Std is imported.
import Std
namespace BitcoinActivation
/-! Transcription of the historical rule selection of Bitcoin Core at revision
`a773e1e33b349dc819b5a0edc1c7a4839dc73b48`: the buried deployment heights of `CMainParams`
(`kernel/chainparams.cpp`), `DeploymentActiveAt` / `DeploymentActiveAfter` for buried deployments
(`deploymentstatus.h`), `GetBlockScriptFlags` with the two `script_flag_exceptions`
(`validation.cpp`), and the `nLockTimeFlags` selection in `ConnectBlock`. -/
/-- `Consensus::BuriedDeployment`. -/
inductive BuriedDeployment where
| heightInCB -- BIP34
| cltv -- BIP65
| derSig -- BIP66
| csv -- BIP68/112/113
| segwit -- BIP141/143/147
deriving DecidableEq, Repr
/-- `Consensus::Params::DeploymentHeight` at mainnet values. -/
def deploymentHeight : BuriedDeployment → Nat
| .heightInCB => 227931
| .cltv => 388381
| .derSig => 363725
| .csv => 419328
| .segwit => 481824
/-- `DeploymentActiveAt(index, dep) = index.nHeight >= DeploymentHeight(dep)`. -/
def activeAt (height : Nat) (dep : BuriedDeployment) : Bool := decide (deploymentHeight dep ≤ height)
/-- `DeploymentActiveAfter(pindexPrev, dep) = pindexPrev->nHeight + 1 >= DeploymentHeight(dep)`. -/
def activeAfter (prevHeight : Nat) (dep : BuriedDeployment) : Bool :=
decide (deploymentHeight dep ≤ prevHeight + 1)
/-- The consensus script-verification flags `GetBlockScriptFlags` can set. -/
structure ScriptFlags where
p2sh : Bool
witness : Bool
taproot : Bool
dersig : Bool
cltv : Bool
csv : Bool
nulldummy : Bool
deriving DecidableEq, Repr
/-- `SCRIPT_VERIFY_NONE`. -/
def noFlags : ScriptFlags := ⟨false, false, false, false, false, false, false⟩
/-- The default base `SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_TAPROOT`. -/
def baseFlags : ScriptFlags := ⟨true, true, true, false, false, false, false⟩
/-- `consensus.script_flag_exceptions`: the block is one of the two historical exceptions, or
not. The BIP16 exception (`00000000000002dc…9c22`) maps to `SCRIPT_VERIFY_NONE`; the Taproot
exception (`0000000000000000000f14c3…95ad`) maps to `SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS`. -/
inductive ScriptException where
| none
| bip16Block
| taprootBlock
deriving DecidableEq, Repr
/-- The flags the exception table yields, or the default base. -/
def exceptionBase : ScriptException → ScriptFlags
| .none => baseFlags
| .bip16Block => noFlags
| .taprootBlock => ⟨true, true, false, false, false, false, false⟩
/-- `GetBlockScriptFlags(block_index)` at mainnet: the base (default or exception), then
`DERSIG`, `CHECKLOCKTIMEVERIFY`, `CHECKSEQUENCEVERIFY` and `NULLDUMMY` added when BIP66, BIP65,
CSV and SegWit respectively are active at the block's height. -/
def blockScriptFlags (height : Nat) (exc : ScriptException) : ScriptFlags :=
let f := exceptionBase exc
{ f with
dersig := f.dersig || activeAt height .derSig
cltv := f.cltv || activeAt height .cltv
csv := f.csv || activeAt height .csv
nulldummy := f.nulldummy || activeAt height .segwit }
/-- Pointwise inclusion of flag sets: every rule enforced by the first is enforced by the second. -/
def ScriptFlags.le (a b : ScriptFlags) : Prop :=
(a.p2sh = true → b.p2sh = true) ∧ (a.witness = true → b.witness = true) ∧
(a.taproot = true → b.taproot = true) ∧ (a.dersig = true → b.dersig = true) ∧
(a.cltv = true → b.cltv = true) ∧ (a.csv = true → b.csv = true) ∧
(a.nulldummy = true → b.nulldummy = true)
/-- `consensus/consensus.h`: `LOCKTIME_VERIFY_SEQUENCE = (1 << 0)`. -/
def LOCKTIME_VERIFY_SEQUENCE : Nat := 1
/-- The `nLockTimeFlags` of `ConnectBlock`: `LOCKTIME_VERIFY_SEQUENCE` once CSV is active at the
block's height, else `0`. -/
def lockTimeFlags (height : Nat) : Nat :=
if activeAt height .csv then LOCKTIME_VERIFY_SEQUENCE else 0
end BitcoinActivation
Read-back
What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)
Read-back: Def_BitcoinActivation (all declarations)
Everything below lives in a single namespace, here called BitcoinActivation. There are thirteen declarations: two inductive types, one record type, and ten definitions. No theorem is stated in this file.
BuriedDeployment (inductive type). A finite type with exactly five distinct elements and nothing else. The five elements are named , , , , and . The type comes with decidable equality (any two elements can be compared) and a printable representation. The comments next to the constructors mention BIP numbers, but those comments carry no formal content: an element of this type is just one of five labels.
deploymentHeight (function ). A total function from the five labels to natural numbers, given by the table
Note that the numeric order of these values () is not the order in which the constructors are listed: () is numerically below () although is listed first.
activeAt (function , where ). For a natural number ("height") and a label , is the Boolean value (not a proposition) that is exactly when and otherwise. In particular the comparison is non-strict: at itself the value is . Heights are natural numbers, so is allowed (giving for every label, since every tabulated height is positive) and no negative height exists.
activeAfter (function ). For a natural number ("previous height") and a label , is the Boolean value that is exactly when , and otherwise. Here is ordinary natural-number successor (no wrap-around). Nothing in the definition relates it to ; it is defined directly by the inequality.
ScriptFlags (record type). A record with exactly seven Boolean fields, in this order: , , , , , , . A value of this type is therefore a 7-tuple of Booleans, and two values are equal exactly when all seven fields agree. Equality is decidable and values are printable. Below, a value is written as a tuple in that field order, with for and for .
noFlags (constant of type ). The record with all seven fields :
baseFlags (constant of type ). The record whose first three fields are and whose last four are :
i.e. and .
ScriptException (inductive type). A finite type with exactly three distinct elements, named , , and , with decidable equality and a printable representation. Important: here is an ordinary constructor of this three-element type — it is not the "absent value" of an option type. Writing in later statements picks this element. The docstring mentions two block hashes, but no hash appears in the formal content; the type is just three labels.
exceptionBase (function ). A total function on the three labels:
So the case has and true, false, and the remaining four fields false.
blockScriptFlags (function ). For a natural number and a label , let (one of the three records above). Then is the record obtained from by keeping the fields , , exactly as in , and replacing the other four fields by a Boolean OR with an activation test:
where denotes the Boolean truth value of the decidable inequality and is Boolean OR. Since in all three values of the fields are , these four fields of the result are in fact exactly , , , regardless of ; the dependence on is only through the first three fields (, , ), which are copied from and never altered by the height. Note also that the label and its height are not consulted by this function at all. The definition is total: every natural (including ) and every yields a record.
ScriptFlags.le (binary relation ). For records , the proposition is the conjunction of seven implications, one per field in the fixed order:
Each implication is a one-directional implication between the propositions "this Boolean field equals "; nothing is asserted when the field of is . This is a proposition, not a Boolean, and it is not declared as an instance of any order-theoretic class; it is just a named predicate on pairs of records.
LOCKTIME_VERIFY_SEQUENCE (constant of type ). The natural number . The docstring mentions a bit shift, but the definition is literally the numeral .
lockTimeFlags (function ). For a natural number ,
The branch condition is the Boolean used as a condition; the result is a natural number that is always either or .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.