Library SetcodeConcrete.supplysequence
Balance and representation bounds across transactions
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.supply QedkConcrete.hostaccountformat
QedkConcrete.transactionframes QedkConcrete.nativeinputs QedkConcrete.wordrange
QedkConcrete.hashwords QedkConcrete.assemblychecked QedkConcrete.credits.
Require Import SetcodeConcrete.transactionsupply.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module B := State.BlockState.
Module W := State.WorldState.
Module T := Chain.Ethereum.Transaction.
Section Execution.
Context {F : E.FArgs}.
Lemma delete_accounts_current_block block addresses :
B.t.current_block (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(B.account_opt None addr) None) block addresses) = B.t.current_block block.
Proof.
revert block. induction addresses as [|addr addresses IH]; intro block.
- reflexivity.
- cbn [OCamlSeq.fold_left]. rewrite IH. reflexivity.
Qed.
Lemma process_transaction_current_block block tx post :
E.process_transaction block tx = inl post →
B.t.current_block post = B.t.current_block block.
Proof.
intro Hrun. unfold E.process_transaction in Hrun.
destruct (E.validate_transaction block tx) as [[sender fee price intrinsic]|error];
[|discriminate Hrun].
change (@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 [Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
destruct (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.current_block].
all: change (@Result.Seq.fold_left) with (@OCamlSeq.fold_left).
all: rewrite delete_accounts_current_block; reflexivity.
Qed.
Lemma system_message_current_block block addr data :
B.t.current_block (snd (E.process_system_message block addr data)) = B.t.current_block 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.
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Context (Hhash : keccak256OutputSize) (Hnative : @nativeOutputBounds E.Instantiation_fargs).
Theorem transactions_preserve_supply block txs post :
wordRange (Support.UintN.to_z E.Params.(Execution.Make_Params_signature.chain_id)) →
headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧ Forall
(fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) (T.authorization_list tx)) txs →
balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
Result.List.fold_leftM E.process_transaction block txs = inl post →
balanceInvariant (B.t.world_state post) ∧
totalBalance (B.t.world_state post) ≤ totalBalance (B.t.world_state block) ∧
worldFormat (B.t.world_state post).
Proof.
intros Hchain Hheader Htxs. revert block Hheader.
induction Htxs as [|tx txs [Hvalue Hwidth] Htxs IH];
intros block Hheader Hinitial Hformat Hrun.
- injection Hrun as <-. split; [exact Hinitial|]. split; [lia|exact Hformat].
- change (Result.op_letdollar (E.process_transaction block tx)
(fun next ⇒ Result.List.fold_leftM E.process_transaction next txs) = inl post) in Hrun.
destruct (E.process_transaction block tx) as [next|error] eqn:Hstep;
[|discriminate Hrun].
pose proof (process_transaction_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative block tx next Hchain
(proj1 (proj2 Hheader)) (proj2 (proj2 Hheader)) Hinitial Hformat Hwidth Hvalue Hstep)
as [Hnext [Htotal HnextFormat]].
assert (HnextHeader : headerWords (Chain.Ethereum.Block.t.header (B.t.current_block next))).
{ rewrite (process_transaction_current_block block tx next Hstep). exact Hheader. }
destruct (IH next HnextHeader Hnext HnextFormat Hrun) as [Hpost [HpostTotal HpostFormat]].
split; [exact Hpost|]. split; [lia|exact HpostFormat].
Qed.
Lemma withdrawals_preserve_format block withdrawals :
worldFormat (B.t.world_state block) →
worldFormat (B.t.world_state (Result.List.fold_left E.process_withdrawal block withdrawals)).
Proof.
revert block. induction withdrawals as [|wd withdrawals IH]; intros block Hworld.
- exact Hworld.
- change (worldFormat (B.t.world_state (Result.List.fold_left
E.process_withdrawal (E.process_withdrawal block wd) withdrawals))).
apply IH. unfold E.process_withdrawal.
apply write_account_format; [exact Hworld|].
apply balance_update_format, read_account_format. exact Hworld.
Qed.
End Execution.
Print Assumptions transactions_preserve_supply.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.supply QedkConcrete.hostaccountformat
QedkConcrete.transactionframes QedkConcrete.nativeinputs QedkConcrete.wordrange
QedkConcrete.hashwords QedkConcrete.assemblychecked QedkConcrete.credits.
Require Import SetcodeConcrete.transactionsupply.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module B := State.BlockState.
Module W := State.WorldState.
Module T := Chain.Ethereum.Transaction.
Section Execution.
Context {F : E.FArgs}.
Lemma delete_accounts_current_block block addresses :
B.t.current_block (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(B.account_opt None addr) None) block addresses) = B.t.current_block block.
Proof.
revert block. induction addresses as [|addr addresses IH]; intro block.
- reflexivity.
- cbn [OCamlSeq.fold_left]. rewrite IH. reflexivity.
Qed.
Lemma process_transaction_current_block block tx post :
E.process_transaction block tx = inl post →
B.t.current_block post = B.t.current_block block.
Proof.
intro Hrun. unfold E.process_transaction in Hrun.
destruct (E.validate_transaction block tx) as [[sender fee price intrinsic]|error];
[|discriminate Hrun].
change (@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 [Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
destruct (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.current_block].
all: change (@Result.Seq.fold_left) with (@OCamlSeq.fold_left).
all: rewrite delete_accounts_current_block; reflexivity.
Qed.
Lemma system_message_current_block block addr data :
B.t.current_block (snd (E.process_system_message block addr data)) = B.t.current_block 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.
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Context (Hhash : keccak256OutputSize) (Hnative : @nativeOutputBounds E.Instantiation_fargs).
Theorem transactions_preserve_supply block txs post :
wordRange (Support.UintN.to_z E.Params.(Execution.Make_Params_signature.chain_id)) →
headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧ Forall
(fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) (T.authorization_list tx)) txs →
balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
Result.List.fold_leftM E.process_transaction block txs = inl post →
balanceInvariant (B.t.world_state post) ∧
totalBalance (B.t.world_state post) ≤ totalBalance (B.t.world_state block) ∧
worldFormat (B.t.world_state post).
Proof.
intros Hchain Hheader Htxs. revert block Hheader.
induction Htxs as [|tx txs [Hvalue Hwidth] Htxs IH];
intros block Hheader Hinitial Hformat Hrun.
- injection Hrun as <-. split; [exact Hinitial|]. split; [lia|exact Hformat].
- change (Result.op_letdollar (E.process_transaction block tx)
(fun next ⇒ Result.List.fold_leftM E.process_transaction next txs) = inl post) in Hrun.
destruct (E.process_transaction block tx) as [next|error] eqn:Hstep;
[|discriminate Hrun].
pose proof (process_transaction_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative block tx next Hchain
(proj1 (proj2 Hheader)) (proj2 (proj2 Hheader)) Hinitial Hformat Hwidth Hvalue Hstep)
as [Hnext [Htotal HnextFormat]].
assert (HnextHeader : headerWords (Chain.Ethereum.Block.t.header (B.t.current_block next))).
{ rewrite (process_transaction_current_block block tx next Hstep). exact Hheader. }
destruct (IH next HnextHeader Hnext HnextFormat Hrun) as [Hpost [HpostTotal HpostFormat]].
split; [exact Hpost|]. split; [lia|exact HpostFormat].
Qed.
Lemma withdrawals_preserve_format block withdrawals :
worldFormat (B.t.world_state block) →
worldFormat (B.t.world_state (Result.List.fold_left E.process_withdrawal block withdrawals)).
Proof.
revert block. induction withdrawals as [|wd withdrawals IH]; intros block Hworld.
- exact Hworld.
- change (worldFormat (B.t.world_state (Result.List.fold_left
E.process_withdrawal (E.process_withdrawal block wd) withdrawals))).
apply IH. unfold E.process_withdrawal.
apply write_account_format; [exact Hworld|].
apply balance_update_format, read_account_format. exact Hworld.
Qed.
End Execution.
Print Assumptions transactions_preserve_supply.