DisconnectBlock undoes ConnectBlock
ProvedBitcoinBlock.disconnect_connectFor a well-formed state, a block with pairwise distinct txids whose outputs do not pre-exist (the BIP30 condition that BIP34 guarantees on mainnet) and that is not the genesis, if validate S b now = accept S' undo then disconnectBlock S'.utxo height hash b undo = some (u, true) for a ledger with exactly the coins of S.utxo: the undo data restores the previous ledger and the disconnect is clean.
Deprecated. Superseded by BitcoinBlockV2.disconnect_connect (revision 2 of the block mission): the p2shSigOpCount of this revision counted every push of scriptSig instead of only the redeem script, over-counting P2SH sigops; the V2 declarations carry the corrected definition.
import Definitions.Def_BitcoinBlock import Definitions.Def_BitcoinSer
theorem BitcoinBlock.disconnect_connect (S : ChainState) (b : BitcoinSer.Block) (now : Int) (S' : ChainState)
(undo : BlockUndo) (hwf : BitcoinBlock.WellFormed S) (hnd : (b.vtx.map BitcoinSer.txid).Nodup)
(hfresh : ∀ tx ∈ b.vtx, ∀ o, BitcoinBlock.getCoin S.utxo ⟨BitcoinSer.txid tx, o⟩ = none)
(hg : BitcoinSer.headerHash b.header ≠ BitcoinSer.genesisHash)
(h : BitcoinBlock.validate S b now = .accept S' undo) :
∃ u, BitcoinBlock.disconnectBlock S'.utxo S.nextHeight (BitcoinSer.headerHash b.header) b undo = some (u, true) ∧
BitcoinBlock.Equiv u S.utxo := by sorryRead-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: BitcoinBlock.disconnect_connect
Binders and hypotheses
- : a chain state .
- : a serialized block (header, transaction list ).
- .
- : a chain state.
- : a block undo list (list of lists of coins).
- Hypothesis : : the first header of exists and is the genesis header; the keys (outpoints) of are pairwise distinct; and every entry of has and is not unspendable (does not start with byte and is at most bytes).
- Hypothesis : the list has no duplicates ( = double-SHA256 of the witness-stripped serialization).
- Hypothesis : for every and every , (no outpoint of any transaction of the block, at any index whatsoever, is a key of ).
- Hypothesis : .
- Hypothesis : .
Conclusion
where means (first-match lookup agrees on every outpoint).
Hypothesis , unfolded
returns accept only when the header is accepted (PoW, parent tip hash, bits required bits, time tip median-time-past, time , version not outdated), holds, holds and ; then . Under the genesis shortcut of is not taken, so and come from the transaction loop at height : is with, for each transaction in order, the prevouts removed (non-coinbase only) and the spendable outputs added at that height with the coinbase flag; has one entry per non-coinbase transaction, in order, each being the list of coins (from on the running ledger) spent by its inputs.
Conclusion, unfolded
: if then none; otherwise pair the transactions with (the first transaction gets no undo record, the -th transaction gets the -th undo entry), reverse the list, and process it with where (true only for the two fixed (height, hash) pairs and ). handles the pairs in that (reversed) order, failing if any step fails and conjoining the clean flags. One step : let ; first walks in forward order with index : an unspendable output is skipped (clean); otherwise the entry is removed from the ledger and the step is clean iff that outpoint was present with a coin equal to , or ( and ). Then if is none the result is that ledger and flag; if it is : fail if ; else on the zipped (input, coin) list, which recurses on the tail first (so restores the last input first) and for each pair applies : clean iff the prevout is currently absent; if then look for any entry of the ledger whose key hash equals the prevout hash — if found, take its height and coinbase flag for , if not found fail; then add under the prevout (unspendable scripts are not added; otherwise the new entry replaces an existing one). The final flag is the conjunction of all step flags.
So the conclusion asserts that this disconnection of from , with the height and the block's own header hash, succeeds, reports clean true, and yields a ledger that agrees with on every lookup.
Edge cases silently included
quantifies over all indices , not only existing outputs; the conclusion needs the undo count to match, which under requires exactly one coinbase at the front (ensured by inside ); compares first-match lookups only (ordering and duplicate shadowed entries are ignored); the path () is reachable only at heights / with the fixed hashes; may be unsatisfiable for some , making the claim vacuous; is used only inside ; coins with height restored via the "find any entry with the same hash" fallback.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.