Library SetcodeConcrete.miscblockproofs
Header and gas checks for complete SETCODE blocks
From Stdlib Require Import List String NArith ZArith Lia Bool.
From stdpp Require Import decidable.
Require Export QedkConcrete.miscblockchecks.
Require Import QedkConcrete.blockentry QedkConcrete.reservecheck.
Require Import SetcodeConcrete.validatedexecution SetcodeConcrete.supplysequence
SetcodeConcrete.blocknonceagreement.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module B := State.BlockState.
Section Execution.
Context {F : E.FArgs}.
Context (cold warm access write reserve : N).
Lemma checked_header_validates world parent history block :
W.t.history world = parent :: history →
headerChecks (Block.t.header parent) (Block.t.header block) = true →
Block.t.ommers block = [] →
(Header.t.gas_used (Block.t.header block) ≤
Header.t.gas_limit (Block.t.header block))%N →
@E.validate_block F world block = inl tt.
Proof.
intros Hhistory Hheader Hommers Hused.
unfold headerChecks in Hheader. apply bool_decide_eq_true in Hheader.
destruct Hheader as [Hnumber [Hminimum [[Hlower Hupper]
[Htime [Hhash [Hdifficulty [Hnonce Hextra]]]]]]].
unfold E.validate_block.
change (@Result.op_letdollar) with
(fun (A Error C : Set) (r : A + Error) (f : A → C + Error) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end).
change (@Result.when_) with
(fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
if condition then failure else inl tt).
rewrite Hhistory. cbn zeta.
change (@Result.List.hd) with
(fun (A : Set) (fallback : A) (xs : list A) ⇒
match xs with [] ⇒ fallback | x :: _ ⇒ x end).
cbn beta iota zeta.
assert (Hn : Support.UintN.op_ltgt (Header.t.number (Block.t.header block))
(Support.UintN.op_plus (Header.t.number (Block.t.header parent)) Support.UintN.one) = false).
{ change (negb (N.eqb (Header.t.number (Block.t.header block))
(Header.t.number (Block.t.header parent) + 1)) = false). apply negb_false_iff, N.eqb_eq. exact Hnumber. }
rewrite Hn. cbn beta iota.
assert (Hsub : N.leb (Header.t.gas_limit (Block.t.header parent) / 1024)
(Header.t.gas_limit (Block.t.header parent)) = true).
{ apply N.leb_le, N.Div0.div_le_upper_bound; lia. }
assert (Hlimit :
(Gas.op_lt (Header.t.gas_limit (Block.t.header block)) (Gas.op_tildedollar 5000%Z) ||
(Gas.op_gteq (Header.t.gas_limit (Block.t.header block))
(Gas.op_plus (Header.t.gas_limit (Block.t.header parent))
(Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))) ||
Gas.op_lteq (Header.t.gas_limit (Block.t.header block))
(Gas.op_minus (Header.t.gas_limit (Block.t.header parent))
(Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))))) = false).
{ change (Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))
with (Header.t.gas_limit (Block.t.header parent) / 1024)%N.
unfold Gas.op_minus, Support.UintN.op_minus. rewrite Hsub.
change ((N.ltb (Header.t.gas_limit (Block.t.header block)) 5000 ||
(N.leb (Header.t.gas_limit (Block.t.header parent) + Header.t.gas_limit (Block.t.header parent) / 1024)
(Header.t.gas_limit (Block.t.header block)) ||
N.leb (Header.t.gas_limit (Block.t.header block))
(Header.t.gas_limit (Block.t.header parent) - Header.t.gas_limit (Block.t.header parent) / 1024))) = false).
apply orb_false_iff. split; [apply N.ltb_ge; exact Hminimum|].
apply orb_false_iff. split; apply N.leb_gt; assumption. }
rewrite Hlimit. cbn beta iota.
assert (Hu : Gas.op_gt (Header.t.gas_used (Block.t.header block))
(Header.t.gas_limit (Block.t.header block)) = false).
{ apply N.ltb_ge. exact Hused. }
rewrite Hu. cbn beta iota.
assert (Ht : Numeric.U256.op_lt (Header.t.timestamp (Block.t.header block))
(Header.t.timestamp (Block.t.header parent)) = false).
{ rewrite reservecheck.u256_lt. apply Z.ltb_ge. exact Htime. }
rewrite Ht, Hommers, Hhash.
change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_ltgt)) with
(fun x y ⇒ OCamlInt.not_equal (OCamlString.compare x y) 0%Z).
cbn beta. rewrite OCamlString.compare_matches_structural.
unfold OCamlString.compare_structural. rewrite String.eqb_refl. cbn beta iota.
rewrite Hdifficulty. cbn beta iota.
change (Byte_string.B8.(Byte_string.Fixed.Fixed_result.op_ltgt)) with
(fun x y ⇒ OCamlInt.not_equal (OCamlString.compare x y) 0%Z).
rewrite Hnonce. cbn beta. rewrite OCamlString.compare_matches_structural.
unfold OCamlString.compare_structural. rewrite String.eqb_refl. cbn beta iota.
assert (He : Z.gtb (OCamlString.length (Header.t.extra_data (Block.t.header block))) 32 = false)
by (rewrite Z.gtb_ltb; apply Z.ltb_ge; exact Hextra).
change (Byte_string.Bytes.length) with OCamlString.length.
rewrite He. reflexivity.
Qed.
Lemma transactions_current_block block txs post :
Result.List.fold_leftM (@E.process_transaction F) block txs = inl post →
B.t.current_block post = B.t.current_block block.
Proof.
revert block. induction txs as [|tx txs IH]; intros block Hrun.
- injection Hrun as <-. reflexivity.
- change (Result.op_letdollar (@E.process_transaction F block tx)
(fun next ⇒ Result.List.fold_leftM (@E.process_transaction F) next txs) = inl post) in Hrun.
destruct (@E.process_transaction F block tx) as [next|error] eqn:Hstep;
[|discriminate Hrun].
rewrite (IH next Hrun). exact (process_transaction_current_block block tx next Hstep).
Qed.
Lemma withdrawals_current_block block withdrawals :
B.t.current_block (Result.List.fold_left (@E.process_withdrawal F) block withdrawals) =
B.t.current_block block.
Proof.
revert block. induction withdrawals as [|withdrawal withdrawals IH]; intro block.
- reflexivity.
- change (B.t.current_block (Result.List.fold_left (@E.process_withdrawal F)
(@E.process_withdrawal F block withdrawal) withdrawals) = B.t.current_block block).
rewrite IH. reflexivity.
Qed.
Lemma delete_accounts_gas block addresses :
B.t.gas_used (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(@B.account_opt None addr) None) block addresses) = B.t.gas_used block.
Proof.
revert block. induction addresses as [|addr addresses IH]; intro block.
- reflexivity.
- cbn [OCamlSeq.fold_left]. rewrite IH. reflexivity.
Qed.
Lemma transaction_gas_exact block tx post :
@E.process_transaction F block tx = inl post →
B.t.gas_used post = (B.t.gas_used block + T.gas_limit tx)%N.
Proof.
intro Hrun. unfold E.process_transaction in Hrun.
destruct (@E.validate_transaction F block tx) as [[sender fee price intrinsic]|error];
[|discriminate Hrun].
change (@MonadVM.Result.op_letdollar) with
(fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
cbn beta iota zeta delta [E.transaction_validation.sender E.transaction_validation.total_fee
E.transaction_validation.intrinsic_gas E.transaction_validation.effective_gas_price] in Hrun.
match type of Hrun with context [Setcode_rules.advance_sender_nonce ?flag _ _] ⇒
set (migrated := flag) in × end.
match type of Hrun with context [MonadVM.Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
destruct (MonadVM.Result.List.fold_left f start (T.authorization_list tx)) as [authorities prepared]
end.
cbn beta iota zeta in Hrun.
match type of Hrun with context [E.Host.call_from_eoa tx ?msg ?initial] ⇒
destruct (E.Host.call_from_eoa tx msg initial) as [result afterVm]
end.
destruct migrated; cbn beta iota zeta in Hrun.
all: repeat match type of Hrun with
context [if Evmc.Message.CallKind.is_create ?kind then _ else _] ⇒
destruct (Evmc.Message.CallKind.is_create kind); cbn beta iota zeta in Hrun
end.
all: injection Hrun as <-.
all: cbn beta iota zeta delta [B.t.with_transactions_processed
B.t.with_transaction_frames B.t.with_gas_used B.t.gas_used].
all: change (@MonadVM.Result.Seq.fold_left) with (@OCamlSeq.fold_left).
all: rewrite delete_accounts_gas; reflexivity.
Qed.
Lemma transactions_gas_exact block txs post :
Result.List.fold_leftM (@E.process_transaction F) block txs = inl post →
B.t.gas_used post = (B.t.gas_used block + transactionGas txs)%N.
Proof.
revert block. induction txs as [|tx txs IH]; intros block Hrun.
- injection Hrun as <-. unfold transactionGas. cbn [Stdlib.Lists.List.fold_right]. lia.
- change (Result.op_letdollar (@E.process_transaction F block tx)
(fun next ⇒ Result.List.fold_leftM (@E.process_transaction F) next txs) = inl post) in Hrun.
destruct (@E.process_transaction F block tx) as [next|error] eqn:Hstep;
[|discriminate Hrun].
rewrite (IH next Hrun), (transaction_gas_exact block tx next Hstep).
unfold transactionGas. cbn [Stdlib.Lists.List.fold_right]. lia.
Qed.
Lemma system_message_gas block addr data :
B.t.gas_used (snd (@E.process_system_message F block addr data)) = B.t.gas_used block.
Proof.
unfold E.process_system_message.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [reflexivity|].
cbn beta iota zeta.
match goal with |- context [E.Host.call ?msg ?initial] ⇒
destruct (E.Host.call msg initial) as [result afterVm]
end. reflexivity.
Qed.
Lemma transaction_entry_gas world block :
B.t.gas_used (@blockTransactionEntry F world block) = 0%N.
Proof.
unfold blockTransactionEntry. rewrite !system_message_gas. reflexivity.
Qed.
Lemma withdrawals_gas block withdrawals :
B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F) block withdrawals) =
B.t.gas_used block.
Proof.
revert block. induction withdrawals as [|withdrawal withdrawals IH]; intro block.
- reflexivity.
- change (B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F)
(@E.process_withdrawal F block withdrawal) withdrawals) = B.t.gas_used block).
rewrite IH. reflexivity.
Qed.
Lemma finalized_header_checks parent block :
headerChecks parent (Block.t.header (B.finalize_current_block block)) =
headerChecks parent (Block.t.header (B.t.current_block block)).
Proof. reflexivity. Qed.
Lemma finalized_parent_checks block rest :
checkBlockSequence (Block.t.header (B.finalize_current_block block)) rest =
checkBlockSequence (Block.t.header (B.t.current_block block)) rest.
Proof. destruct rest; reflexivity. Qed.
From stdpp Require Import decidable.
Require Export QedkConcrete.miscblockchecks.
Require Import QedkConcrete.blockentry QedkConcrete.reservecheck.
Require Import SetcodeConcrete.validatedexecution SetcodeConcrete.supplysequence
SetcodeConcrete.blocknonceagreement.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module B := State.BlockState.
Section Execution.
Context {F : E.FArgs}.
Context (cold warm access write reserve : N).
Lemma checked_header_validates world parent history block :
W.t.history world = parent :: history →
headerChecks (Block.t.header parent) (Block.t.header block) = true →
Block.t.ommers block = [] →
(Header.t.gas_used (Block.t.header block) ≤
Header.t.gas_limit (Block.t.header block))%N →
@E.validate_block F world block = inl tt.
Proof.
intros Hhistory Hheader Hommers Hused.
unfold headerChecks in Hheader. apply bool_decide_eq_true in Hheader.
destruct Hheader as [Hnumber [Hminimum [[Hlower Hupper]
[Htime [Hhash [Hdifficulty [Hnonce Hextra]]]]]]].
unfold E.validate_block.
change (@Result.op_letdollar) with
(fun (A Error C : Set) (r : A + Error) (f : A → C + Error) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end).
change (@Result.when_) with
(fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
if condition then failure else inl tt).
rewrite Hhistory. cbn zeta.
change (@Result.List.hd) with
(fun (A : Set) (fallback : A) (xs : list A) ⇒
match xs with [] ⇒ fallback | x :: _ ⇒ x end).
cbn beta iota zeta.
assert (Hn : Support.UintN.op_ltgt (Header.t.number (Block.t.header block))
(Support.UintN.op_plus (Header.t.number (Block.t.header parent)) Support.UintN.one) = false).
{ change (negb (N.eqb (Header.t.number (Block.t.header block))
(Header.t.number (Block.t.header parent) + 1)) = false). apply negb_false_iff, N.eqb_eq. exact Hnumber. }
rewrite Hn. cbn beta iota.
assert (Hsub : N.leb (Header.t.gas_limit (Block.t.header parent) / 1024)
(Header.t.gas_limit (Block.t.header parent)) = true).
{ apply N.leb_le, N.Div0.div_le_upper_bound; lia. }
assert (Hlimit :
(Gas.op_lt (Header.t.gas_limit (Block.t.header block)) (Gas.op_tildedollar 5000%Z) ||
(Gas.op_gteq (Header.t.gas_limit (Block.t.header block))
(Gas.op_plus (Header.t.gas_limit (Block.t.header parent))
(Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))) ||
Gas.op_lteq (Header.t.gas_limit (Block.t.header block))
(Gas.op_minus (Header.t.gas_limit (Block.t.header parent))
(Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))))) = false).
{ change (Gas.op_div (Header.t.gas_limit (Block.t.header parent)) (Gas.op_tildedollar 1024%Z))
with (Header.t.gas_limit (Block.t.header parent) / 1024)%N.
unfold Gas.op_minus, Support.UintN.op_minus. rewrite Hsub.
change ((N.ltb (Header.t.gas_limit (Block.t.header block)) 5000 ||
(N.leb (Header.t.gas_limit (Block.t.header parent) + Header.t.gas_limit (Block.t.header parent) / 1024)
(Header.t.gas_limit (Block.t.header block)) ||
N.leb (Header.t.gas_limit (Block.t.header block))
(Header.t.gas_limit (Block.t.header parent) - Header.t.gas_limit (Block.t.header parent) / 1024))) = false).
apply orb_false_iff. split; [apply N.ltb_ge; exact Hminimum|].
apply orb_false_iff. split; apply N.leb_gt; assumption. }
rewrite Hlimit. cbn beta iota.
assert (Hu : Gas.op_gt (Header.t.gas_used (Block.t.header block))
(Header.t.gas_limit (Block.t.header block)) = false).
{ apply N.ltb_ge. exact Hused. }
rewrite Hu. cbn beta iota.
assert (Ht : Numeric.U256.op_lt (Header.t.timestamp (Block.t.header block))
(Header.t.timestamp (Block.t.header parent)) = false).
{ rewrite reservecheck.u256_lt. apply Z.ltb_ge. exact Htime. }
rewrite Ht, Hommers, Hhash.
change (Byte_string.B32.(Byte_string.Fixed.Fixed_result.op_ltgt)) with
(fun x y ⇒ OCamlInt.not_equal (OCamlString.compare x y) 0%Z).
cbn beta. rewrite OCamlString.compare_matches_structural.
unfold OCamlString.compare_structural. rewrite String.eqb_refl. cbn beta iota.
rewrite Hdifficulty. cbn beta iota.
change (Byte_string.B8.(Byte_string.Fixed.Fixed_result.op_ltgt)) with
(fun x y ⇒ OCamlInt.not_equal (OCamlString.compare x y) 0%Z).
rewrite Hnonce. cbn beta. rewrite OCamlString.compare_matches_structural.
unfold OCamlString.compare_structural. rewrite String.eqb_refl. cbn beta iota.
assert (He : Z.gtb (OCamlString.length (Header.t.extra_data (Block.t.header block))) 32 = false)
by (rewrite Z.gtb_ltb; apply Z.ltb_ge; exact Hextra).
change (Byte_string.Bytes.length) with OCamlString.length.
rewrite He. reflexivity.
Qed.
Lemma transactions_current_block block txs post :
Result.List.fold_leftM (@E.process_transaction F) block txs = inl post →
B.t.current_block post = B.t.current_block block.
Proof.
revert block. induction txs as [|tx txs IH]; intros block Hrun.
- injection Hrun as <-. reflexivity.
- change (Result.op_letdollar (@E.process_transaction F block tx)
(fun next ⇒ Result.List.fold_leftM (@E.process_transaction F) next txs) = inl post) in Hrun.
destruct (@E.process_transaction F block tx) as [next|error] eqn:Hstep;
[|discriminate Hrun].
rewrite (IH next Hrun). exact (process_transaction_current_block block tx next Hstep).
Qed.
Lemma withdrawals_current_block block withdrawals :
B.t.current_block (Result.List.fold_left (@E.process_withdrawal F) block withdrawals) =
B.t.current_block block.
Proof.
revert block. induction withdrawals as [|withdrawal withdrawals IH]; intro block.
- reflexivity.
- change (B.t.current_block (Result.List.fold_left (@E.process_withdrawal F)
(@E.process_withdrawal F block withdrawal) withdrawals) = B.t.current_block block).
rewrite IH. reflexivity.
Qed.
Lemma delete_accounts_gas block addresses :
B.t.gas_used (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(@B.account_opt None addr) None) block addresses) = B.t.gas_used block.
Proof.
revert block. induction addresses as [|addr addresses IH]; intro block.
- reflexivity.
- cbn [OCamlSeq.fold_left]. rewrite IH. reflexivity.
Qed.
Lemma transaction_gas_exact block tx post :
@E.process_transaction F block tx = inl post →
B.t.gas_used post = (B.t.gas_used block + T.gas_limit tx)%N.
Proof.
intro Hrun. unfold E.process_transaction in Hrun.
destruct (@E.validate_transaction F block tx) as [[sender fee price intrinsic]|error];
[|discriminate Hrun].
change (@MonadVM.Result.op_letdollar) with
(fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
cbn beta iota zeta delta [E.transaction_validation.sender E.transaction_validation.total_fee
E.transaction_validation.intrinsic_gas E.transaction_validation.effective_gas_price] in Hrun.
match type of Hrun with context [Setcode_rules.advance_sender_nonce ?flag _ _] ⇒
set (migrated := flag) in × end.
match type of Hrun with context [MonadVM.Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
destruct (MonadVM.Result.List.fold_left f start (T.authorization_list tx)) as [authorities prepared]
end.
cbn beta iota zeta in Hrun.
match type of Hrun with context [E.Host.call_from_eoa tx ?msg ?initial] ⇒
destruct (E.Host.call_from_eoa tx msg initial) as [result afterVm]
end.
destruct migrated; cbn beta iota zeta in Hrun.
all: repeat match type of Hrun with
context [if Evmc.Message.CallKind.is_create ?kind then _ else _] ⇒
destruct (Evmc.Message.CallKind.is_create kind); cbn beta iota zeta in Hrun
end.
all: injection Hrun as <-.
all: cbn beta iota zeta delta [B.t.with_transactions_processed
B.t.with_transaction_frames B.t.with_gas_used B.t.gas_used].
all: change (@MonadVM.Result.Seq.fold_left) with (@OCamlSeq.fold_left).
all: rewrite delete_accounts_gas; reflexivity.
Qed.
Lemma transactions_gas_exact block txs post :
Result.List.fold_leftM (@E.process_transaction F) block txs = inl post →
B.t.gas_used post = (B.t.gas_used block + transactionGas txs)%N.
Proof.
revert block. induction txs as [|tx txs IH]; intros block Hrun.
- injection Hrun as <-. unfold transactionGas. cbn [Stdlib.Lists.List.fold_right]. lia.
- change (Result.op_letdollar (@E.process_transaction F block tx)
(fun next ⇒ Result.List.fold_leftM (@E.process_transaction F) next txs) = inl post) in Hrun.
destruct (@E.process_transaction F block tx) as [next|error] eqn:Hstep;
[|discriminate Hrun].
rewrite (IH next Hrun), (transaction_gas_exact block tx next Hstep).
unfold transactionGas. cbn [Stdlib.Lists.List.fold_right]. lia.
Qed.
Lemma system_message_gas block addr data :
B.t.gas_used (snd (@E.process_system_message F block addr data)) = B.t.gas_used block.
Proof.
unfold E.process_system_message.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [reflexivity|].
cbn beta iota zeta.
match goal with |- context [E.Host.call ?msg ?initial] ⇒
destruct (E.Host.call msg initial) as [result afterVm]
end. reflexivity.
Qed.
Lemma transaction_entry_gas world block :
B.t.gas_used (@blockTransactionEntry F world block) = 0%N.
Proof.
unfold blockTransactionEntry. rewrite !system_message_gas. reflexivity.
Qed.
Lemma withdrawals_gas block withdrawals :
B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F) block withdrawals) =
B.t.gas_used block.
Proof.
revert block. induction withdrawals as [|withdrawal withdrawals IH]; intro block.
- reflexivity.
- change (B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F)
(@E.process_withdrawal F block withdrawal) withdrawals) = B.t.gas_used block).
rewrite IH. reflexivity.
Qed.
Lemma finalized_header_checks parent block :
headerChecks parent (Block.t.header (B.finalize_current_block block)) =
headerChecks parent (Block.t.header (B.t.current_block block)).
Proof. reflexivity. Qed.
Lemma finalized_parent_checks block rest :
checkBlockSequence (Block.t.header (B.finalize_current_block block)) rest =
checkBlockSequence (Block.t.header (B.t.current_block block)) rest.
Proof. destruct rest; reflexivity. Qed.
Successful transaction processing accumulates exactly the public gas
limits. The block checks therefore also validate the finalized header.
Lemma checked_finalized_block world parent history block after :
W.t.history world = parent :: history →
headerChecks (Block.t.header parent) (Block.t.header block) = true →
blockDataChecks block = true →
Result.List.fold_leftM (@E.process_transaction F) (@blockTransactionEntry F world block)
(Block.t.transactions block) = inl after →
@E.validate_block F world
(B.finalize_current_block (Result.List.fold_left (@E.process_withdrawal F)
after (Block.t.withdrawals block))) = inl tt.
Proof.
intros Hhistory Hheader Hdata Hrun.
unfold blockDataChecks in Hdata. apply bool_decide_eq_true in Hdata.
destruct Hdata as [Hommers [Hused Hgas]].
pose proof (transactions_gas_exact _ _ _ Hrun) as Htotal.
rewrite transaction_entry_gas, N.add_0_l in Htotal.
pose proof (transactions_current_block _ _ _ Hrun) as Hcurrent.
rewrite block_entry_current in Hcurrent.
eapply checked_header_validates; [exact Hhistory| | |].
- rewrite finalized_header_checks, withdrawals_current_block, Hcurrent. exact Hheader.
- change (Block.t.ommers (B.t.current_block
(Result.List.fold_left (@E.process_withdrawal F) after (Block.t.withdrawals block))) = []).
rewrite withdrawals_current_block, Hcurrent. exact Hommers.
- change (B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F) after
(Block.t.withdrawals block)) ≤ Header.t.gas_limit (Block.t.header
(B.t.current_block (Result.List.fold_left (@E.process_withdrawal F) after
(Block.t.withdrawals block)))))%N.
rewrite withdrawals_gas, withdrawals_current_block, Hcurrent, Htotal. exact Hgas.
Qed.
Lemma transactions_no_block_error block txs details :
Result.List.fold_leftM (@E.process_transaction F) block txs ≠
inr (Execution.Error.Invalid_block details).
Proof.
revert block. induction txs as [|tx txs IH]; intros block Hrun; [discriminate|].
change (Result.op_letdollar (@E.process_transaction F block tx)
(fun next ⇒ Result.List.fold_leftM (@E.process_transaction F) next txs) =
inr (Execution.Error.Invalid_block details)) in Hrun.
destruct (@E.process_transaction F block tx) as [next|error] eqn:Hstep.
- exact (IH next Hrun).
- injection Hrun as Heq. subst error.
pose proof (@transaction_error_from_validation F block tx _ Hstep) as Hvalid.
unfold E.validate_transaction in Hvalid.
change (@Result.op_letdollar) with
(fun (A Error C : Set) (r : A + Error) (f : A → C + Error) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hvalid.
unfold E.validate_authorizations, E.invalid_transaction in Hvalid.
change (@Result.when_) with
(fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
if condition then failure else inl tt) in Hvalid.
change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value) in Hvalid.
destruct tx; cbn beta iota zeta in Hvalid.
all: repeat (cbn beta iota zeta in Hvalid;
match type of Hvalid with
| context [if ?test then _ else _] ⇒ destruct test eqn:?
| context [match ?test with Some _ ⇒ _ | None ⇒ _ end] ⇒ destruct test eqn:?
| context [match ?test with [] ⇒ _ | _ :: _ ⇒ _ end] ⇒ destruct test eqn:?
end); try discriminate.
Qed.
W.t.history world = parent :: history →
headerChecks (Block.t.header parent) (Block.t.header block) = true →
blockDataChecks block = true →
Result.List.fold_leftM (@E.process_transaction F) (@blockTransactionEntry F world block)
(Block.t.transactions block) = inl after →
@E.validate_block F world
(B.finalize_current_block (Result.List.fold_left (@E.process_withdrawal F)
after (Block.t.withdrawals block))) = inl tt.
Proof.
intros Hhistory Hheader Hdata Hrun.
unfold blockDataChecks in Hdata. apply bool_decide_eq_true in Hdata.
destruct Hdata as [Hommers [Hused Hgas]].
pose proof (transactions_gas_exact _ _ _ Hrun) as Htotal.
rewrite transaction_entry_gas, N.add_0_l in Htotal.
pose proof (transactions_current_block _ _ _ Hrun) as Hcurrent.
rewrite block_entry_current in Hcurrent.
eapply checked_header_validates; [exact Hhistory| | |].
- rewrite finalized_header_checks, withdrawals_current_block, Hcurrent. exact Hheader.
- change (Block.t.ommers (B.t.current_block
(Result.List.fold_left (@E.process_withdrawal F) after (Block.t.withdrawals block))) = []).
rewrite withdrawals_current_block, Hcurrent. exact Hommers.
- change (B.t.gas_used (Result.List.fold_left (@E.process_withdrawal F) after
(Block.t.withdrawals block)) ≤ Header.t.gas_limit (Block.t.header
(B.t.current_block (Result.List.fold_left (@E.process_withdrawal F) after
(Block.t.withdrawals block)))))%N.
rewrite withdrawals_gas, withdrawals_current_block, Hcurrent, Htotal. exact Hgas.
Qed.
Lemma transactions_no_block_error block txs details :
Result.List.fold_leftM (@E.process_transaction F) block txs ≠
inr (Execution.Error.Invalid_block details).
Proof.
revert block. induction txs as [|tx txs IH]; intros block Hrun; [discriminate|].
change (Result.op_letdollar (@E.process_transaction F block tx)
(fun next ⇒ Result.List.fold_leftM (@E.process_transaction F) next txs) =
inr (Execution.Error.Invalid_block details)) in Hrun.
destruct (@E.process_transaction F block tx) as [next|error] eqn:Hstep.
- exact (IH next Hrun).
- injection Hrun as Heq. subst error.
pose proof (@transaction_error_from_validation F block tx _ Hstep) as Hvalid.
unfold E.validate_transaction in Hvalid.
change (@Result.op_letdollar) with
(fun (A Error C : Set) (r : A + Error) (f : A → C + Error) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hvalid.
unfold E.validate_authorizations, E.invalid_transaction in Hvalid.
change (@Result.when_) with
(fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
if condition then failure else inl tt) in Hvalid.
change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value) in Hvalid.
destruct tx; cbn beta iota zeta in Hvalid.
all: repeat (cbn beta iota zeta in Hvalid;
match type of Hvalid with
| context [if ?test then _ else _] ⇒ destruct test eqn:?
| context [match ?test with Some _ ⇒ _ | None ⇒ _ end] ⇒ destruct test eqn:?
| context [match ?test with [] ⇒ _ | _ :: _ ⇒ _ end] ⇒ destruct test eqn:?
end); try discriminate.
Qed.
A checked block cannot produce a block error. If it succeeds, its
finalized header supplies the same number, timestamp and gas limit to
the remaining checks as its supplied header did.
Lemma misc_checks_block_step world block rest :
(@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
miscBlockChecks world (block :: rest) = true →
match @E.process_block F false world block with
| inl post ⇒ miscBlockChecks post rest = true
| inr (Execution.Error.Invalid_block _) ⇒ False
| inr (Execution.Error.Invalid_transaction _) ⇒ True
end.
Proof.
intros Hrevision Hchecks.
unfold miscBlockChecks in Hchecks.
destruct (W.t.history world) as [|parent history] eqn:Hhistory; [discriminate|].
change (headerChecks (Block.t.header parent) (Block.t.header block) &&
blockDataChecks block && checkBlockSequence (Block.t.header block) rest = true) in Hchecks.
apply andb_true_iff in Hchecks as [Hfirst Hrest].
apply andb_true_iff in Hfirst as [Hheader Hdata].
assert (Hvalid : @E.validate_block F world block = inl tt).
{ pose proof Hdata as Hfields. unfold blockDataChecks in Hfields.
apply bool_decide_eq_true in Hfields. destruct Hfields as [Hommers [Hused _]].
eapply checked_header_validates; eassumption. }
unfold E.process_block. rewrite Hrevision, Hvalid.
change (@Result.op_letdollar) with
(fun (A Error C : Set) (r : A + Error) (f : A → C + Error) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end).
cbn beta iota zeta.
match goal with |- context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
change entry with (@blockTransactionEntry F world block)
end.
destruct (Result.List.fold_leftM (@E.process_transaction F) (@blockTransactionEntry F world block)
(Block.t.transactions block)) as [after|error] eqn:Htxs.
- cbn beta iota zeta.
match goal with |- context [E.validate_block ?entry ?finalized] ⇒
change (E.validate_block entry finalized) with (E.validate_block world finalized)
end.
rewrite (checked_finalized_block world parent history block after Hhistory Hheader Hdata Htxs).
cbn beta iota zeta.
pose proof (transactions_current_block _ _ _ Htxs) as Hcurrent.
rewrite block_entry_current in Hcurrent.
destruct rest as [|first rest]; [reflexivity|].
change (checkBlockSequence (Block.t.header (B.finalize_current_block
(Result.List.fold_left (@E.process_withdrawal F) after (Block.t.withdrawals block))))
(first :: rest) = true).
rewrite finalized_parent_checks, withdrawals_current_block, Hcurrent. exact Hrest.
- destruct error as [details|details]; [|exact I].
exact (transactions_no_block_error _ _ details Htxs).
Qed.
(@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
miscBlockChecks world (block :: rest) = true →
match @E.process_block F false world block with
| inl post ⇒ miscBlockChecks post rest = true
| inr (Execution.Error.Invalid_block _) ⇒ False
| inr (Execution.Error.Invalid_transaction _) ⇒ True
end.
Proof.
intros Hrevision Hchecks.
unfold miscBlockChecks in Hchecks.
destruct (W.t.history world) as [|parent history] eqn:Hhistory; [discriminate|].
change (headerChecks (Block.t.header parent) (Block.t.header block) &&
blockDataChecks block && checkBlockSequence (Block.t.header block) rest = true) in Hchecks.
apply andb_true_iff in Hchecks as [Hfirst Hrest].
apply andb_true_iff in Hfirst as [Hheader Hdata].
assert (Hvalid : @E.validate_block F world block = inl tt).
{ pose proof Hdata as Hfields. unfold blockDataChecks in Hfields.
apply bool_decide_eq_true in Hfields. destruct Hfields as [Hommers [Hused _]].
eapply checked_header_validates; eassumption. }
unfold E.process_block. rewrite Hrevision, Hvalid.
change (@Result.op_letdollar) with
(fun (A Error C : Set) (r : A + Error) (f : A → C + Error) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end).
cbn beta iota zeta.
match goal with |- context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
change entry with (@blockTransactionEntry F world block)
end.
destruct (Result.List.fold_leftM (@E.process_transaction F) (@blockTransactionEntry F world block)
(Block.t.transactions block)) as [after|error] eqn:Htxs.
- cbn beta iota zeta.
match goal with |- context [E.validate_block ?entry ?finalized] ⇒
change (E.validate_block entry finalized) with (E.validate_block world finalized)
end.
rewrite (checked_finalized_block world parent history block after Hhistory Hheader Hdata Htxs).
cbn beta iota zeta.
pose proof (transactions_current_block _ _ _ Htxs) as Hcurrent.
rewrite block_entry_current in Hcurrent.
destruct rest as [|first rest]; [reflexivity|].
change (checkBlockSequence (Block.t.header (B.finalize_current_block
(Result.List.fold_left (@E.process_withdrawal F) after (Block.t.withdrawals block))))
(first :: rest) = true).
rewrite finalized_parent_checks, withdrawals_current_block, Hcurrent. exact Hrest.
- destruct error as [details|details]; [|exact I].
exact (transactions_no_block_error _ _ details Htxs).
Qed.
Check the whole list once. Execution of any prefix either encounters a
transaction error, or succeeds and preserves the checks on its suffix.
No successful-prefix or transaction-validation premise is assumed here.
Theorem misc_checks_prefix_execution world past rest :
(@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
miscBlockChecks world (past ++ rest) = true →
match Result.List.fold_leftM (@E.process_block F false) world past with
| inl post ⇒ miscBlockChecks post rest = true
| inr (Execution.Error.Invalid_block _) ⇒ False
| inr (Execution.Error.Invalid_transaction _) ⇒ True
end.
Proof.
revert world. induction past as [|block past IH]; intros world Hrevision Hchecks.
- exact Hchecks.
- pose proof (misc_checks_block_step world block (past ++ rest) Hrevision Hchecks) as Hstep.
change (match Result.op_letdollar (@E.process_block F false world block)
(fun next ⇒ Result.List.fold_leftM (@E.process_block F false) next past) with
| inl post ⇒ miscBlockChecks post rest = true
| inr (Execution.Error.Invalid_block _) ⇒ False
| inr (Execution.Error.Invalid_transaction _) ⇒ True
end).
destruct (@E.process_block F false world block) as [next|[details|details]] eqn:Hrun.
+ exact (IH next Hrevision Hstep).
+ exact Hstep.
+ exact I.
Qed.
End Execution.
Print Assumptions misc_checks_prefix_execution.
(@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
miscBlockChecks world (past ++ rest) = true →
match Result.List.fold_leftM (@E.process_block F false) world past with
| inl post ⇒ miscBlockChecks post rest = true
| inr (Execution.Error.Invalid_block _) ⇒ False
| inr (Execution.Error.Invalid_transaction _) ⇒ True
end.
Proof.
revert world. induction past as [|block past IH]; intros world Hrevision Hchecks.
- exact Hchecks.
- pose proof (misc_checks_block_step world block (past ++ rest) Hrevision Hchecks) as Hstep.
change (match Result.op_letdollar (@E.process_block F false world block)
(fun next ⇒ Result.List.fold_leftM (@E.process_block F false) next past) with
| inl post ⇒ miscBlockChecks post rest = true
| inr (Execution.Error.Invalid_block _) ⇒ False
| inr (Execution.Error.Invalid_transaction _) ⇒ True
end).
destruct (@E.process_block F false world block) as [next|[details|details]] eqn:Hrun.
+ exact (IH next Hrevision Hstep).
+ exact Hstep.
+ exact I.
Qed.
End Execution.
Print Assumptions misc_checks_prefix_execution.