Library SetcodeConcrete.blockbudget
Preserving the consensus reserve budget across complete SETCODE blocks
From Stdlib Require Import List String NArith ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe
QedkConcrete.vmhostframe QedkConcrete.hostframe QedkConcrete.wordrange
QedkConcrete.systemcode.
Require Import SetcodeConcrete.vmprogress SetcodeConcrete.vmframes
SetcodeConcrete.vmpc SetcodeConcrete.vmpcopcodes SetcodeConcrete.codebytes.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
MonadExecutionSpecs.Termination.VmUnsignedGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section CallFreeAt.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (cold warm access write reserve : N).
Context (Hrevision : V.Params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : V.Params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hprogress : @setcodeGasProgress Host_t F).
Context (property : Host_t → Prop).
Section LoopAt.
Context {E : @V.Executor.FArgs Host_t F}.
Context (Hprimitive : PrimitiveHostFrameAt V.Host property (@V.Executor.self Host_t F E)).
Context (Hsetcode : adoptionFreeBytes
(V.Executor.C.(V.Executor_C_signature.execution_environment).(V.ExecutionEnvironment.t.bytecode)) = false →
∀ current source static gas,
preservesCallback property (V.Host.(Evmc.HOST.setcodefrom) current source static gas)).
Lemma setcode_pc_safe_at : pcSafe (@V.Executor.setcodefrom Host_t F E).
Proof.
unfold V.Executor.setcodefrom. rewrite Hrevision.
unfold V.M.HostAPI.setcodefrom, V.M.HostAPI.Base. solve_pc_safe.
Qed.
Theorem call_free_run_preserves_at state :
callFreeBytes (V.Executor.C.(V.Executor_C_signature.execution_environment)
.(V.ExecutionEnvironment.t.bytecode)) = true →
Z.of_N (MS.t.gas state) < 2^63 → wordRange (MS.t.pc state) →
property (MS.t.host state) →
property (MS.t.host (snd (@generated_run Host_t F E state))).
Proof.
intros Hcode Hgas Hpc Hinitial.
assert (Hrun : wordRange (MS.t.pc (snd (@generated_run Host_t F E state))) ∧
property (MS.t.host (snd (@generated_run Host_t F E state)))).
{ apply (setcode_run_preserves cold warm access write reserve
(fun st ⇒ wordRange (MS.t.pc st) ∧ property (MS.t.host st)) state Hrevision Hprogress).
- intros before Hbound [Hcounter Hbefore].
set (opcode := @run_selected_opcode Host_t F E before).
assert (HnoCalls : recursiveCallOpcode opcode = false).
{ unfold opcode. rewrite run_selected_opcode_eq_model.
apply call_free_selected_opcode; [exact Hcode|exact (proj1 Hcounter)]. }
split.
+ destruct (machineOnlyOpcode opcode) eqn:Hmachine.
× apply (machine_pc_safe opcode); [| |exact Hdebug|exact Hmachine|exact Hcounter].
-- unfold dispatch, V.Executor.execute_opcode, V.Executor.clz.
rewrite Hrevision. solve_pc_safe.
-- intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
++ exact setcode_pc_safe_at.
++ unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply pc_fail.
× eapply primitive_pc_safe; [exact Hdebug| |exact Hcounter].
destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *; congruence.
+ destruct (machineOnlyOpcode opcode) eqn:Hmachine.
× apply (machine_only_preserves_host_at property opcode);
[| |exact Hdebug|exact Hmachine|exact Hbefore].
-- eapply setcode_clz_preserves_host; exact Hrevision.
-- intros extra Hequal Hextra. destruct extra; cbn in Hextra; try contradiction.
++ eapply setcode_opcode_preserves_host; [exact Hrevision|].
apply Hsetcode.
destruct (adoptionFreeBytes
(V.Executor.C.(V.Executor_C_signature.execution_environment)
.(V.ExecutionEnvironment.t.bytecode))) eqn:Hfree; [|reflexivity].
assert (Hselected : notAdoption opcode = true).
{ unfold opcode. rewrite run_selected_opcode_eq_model.
apply adoption_free_selected_opcode; [exact Hfree|exact (proj1 Hcounter)]. }
rewrite <- Hequal in Hselected. discriminate.
++ unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply preserves_fail.
× eapply primitive_host_preserves_at_any_revision;
[exact Hdebug|exact Hprimitive| |exact Hbefore].
destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *; congruence.
- exact Hgas.
- split; assumption. }
exact (proj2 Hrun).
Qed.
End LoopAt.
Context (Hcontext : ∀ host, snd (V.Host.(Evmc.HOST.get_tx_context) host) = host).
Context (Hsize : ∀ addr host, snd (V.Host.(Evmc.HOST.get_code_size) addr host) = host).
Context (Hcopy : ∀ addr offset size host, snd (V.Host.(Evmc.HOST.copy_code) addr offset size host) = host).
Theorem call_free_execute_preserves_at_guarded msg code host :
PrimitiveHostFrameAt V.Host property (Evmc.Message.t.recipient msg) →
(adoptionFreeBytes code = false → ∀ current source static gas,
preservesCallback property (V.Host.(Evmc.HOST.setcodefrom) current source static gas)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 → callFreeBytes code = true →
property host → property (snd (@V.execute Host_t F msg code host)).
Proof.
intros Hprimitive Hsetcode Hgas Hcode Hinitial.
unfold V.execute.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
pose proof (Hcontext host) as Hread.
destruct (V.Host.(Evmc.HOST.get_tx_context) host) as [context middle] eqn:Hctx.
cbn [snd] in Hread. subst middle.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
rewrite Hrevision.
pose proof (Hsize (Evmc.Message.t.recipient msg) host) as HreadSize.
destruct (V.Host.(Evmc.HOST.get_code_size) (Evmc.Message.t.recipient msg) host)
as [size middle] eqn:Hsz. cbn [snd] in HreadSize. subst middle.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hlength end.
2: cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
1: (pose proof (Hcopy (Evmc.Message.t.recipient msg) 0 Delegation.eoa_delegated_code_length host) as HreadCopy;
destruct (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0
Delegation.eoa_delegated_code_length host) as [ownCode middle] eqn:HownCode;
cbn [snd] in HreadCopy; subst middle;
change (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0 23 host = (ownCode, host)) in HownCode;
rewrite HownCode;
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host]).
all: match goal with |- context [@V.Executor.run _ _ ?env ?state] ⇒
pose proof (@call_free_run_preserves_at env Hprimitive Hsetcode state Hcode
ltac:(apply initial_gas_fits_signed_int64; exact Hgas) word_zero Hinitial) as Hrun;
unfold generated_run in Hrun;
destruct (@V.Executor.run Host_t F env state) as [outcome after]
end.
all: destruct outcome as [[]|error]; [exact Hrun|].
all: destruct error; exact Hrun.
Qed.
End CallFreeAt.
Require Import MonadVM.Execution.
Require Import QedkConcrete.worldaccounts QedkConcrete.validation
QedkConcrete.publictransaction QedkConcrete.reservecheck
QedkConcrete.reservestorage QedkConcrete.reserveencoding
QedkConcrete.hoststoragewords QedkConcrete.hostaccountformat
QedkConcrete.hashwords QedkConcrete.assemblychecked
QedkConcrete.nativeinputs QedkConcrete.supply
QedkConcrete.protectedstorage QedkConcrete.protectedstoragenative
QedkConcrete.protectedstorageboundarycore QedkConcrete.persistentcode
QedkConcrete.transactionframes QedkConcrete.transactionchecks
QedkConcrete.authorization QedkConcrete.blockentry
QedkConcrete.credits QedkConcrete.reservewrites
QedkConcrete.recursiveassembly
QedkConcrete.nondebitframe QedkConcrete.hostbalance
QedkConcrete.reserveprecompileproperties QedkConcrete.hostcode.
Require QedkConcrete.reserveconfig.
Require Import SetcodeConcrete.consensuschecks SetcodeConcrete.reserve
SetcodeConcrete.contextframe
SetcodeConcrete.transactionentry SetcodeConcrete.transactionentrysafe
SetcodeConcrete.transactionsupply SetcodeConcrete.supplysequence
SetcodeConcrete.nonceagreement SetcodeConcrete.nonceagreementsequence
SetcodeConcrete.activityagreement SetcodeConcrete.emptyingagreement
SetcodeConcrete.transactionactivity SetcodeConcrete.transactionregular
SetcodeConcrete.regularaccount SetcodeConcrete.executionregular
SetcodeConcrete.codeevolution SetcodeConcrete.transactioncode
SetcodeConcrete.transactionnonce SetcodeConcrete.blockconfig
SetcodeConcrete.windowconfig SetcodeConcrete.transactionconfig
SetcodeConcrete.validatedexecution SetcodeConcrete.transactionaccountfinalize
SetcodeConcrete.guardedaccountframe SetcodeConcrete.transactionguardedreserve
SetcodeConcrete.transactionbudget SetcodeConcrete.blockcode
SetcodeConcrete.blocknonceagreement SetcodeConcrete.blockactivityagreement
SetcodeConcrete.blockemptyingagreement SetcodeConcrete.blocksupply
SetcodeConcrete.miscblockproofs SetcodeConcrete.codeframe
SetcodeConcrete.systemcodeframe SetcodeConcrete.systemnonce
SetcodeConcrete.assembly SetcodeConcrete.nonceprecompile
SetcodeConcrete.consensusvalidation SetcodeConcrete.blocknonce
SetcodeConcrete.windownonceagreement.
Import ListNotations.
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := consensuschecks.W.
Module A := consensuschecks.A.
Module T := consensuschecks.T.
Module U := Numeric.U256.
Module Addr := consensuschecks.Addr.
Module Block := consensuschecks.Block.
Local Abbreviation number state :=
(Block.Header.t.number (Block.t.header (B.t.current_block state))).
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation accountAt := consensuschecks.accountAt.
Local Abbreviation codeAt world a := (A.t.code (accountAt world a)).
Lemma reserve_precompile_preserves_balance_floor {F : Reserve_balance_introspection.Make.FArgs}
query minimum msg :
preservesCallback (balanceFloor query minimum)
(Reserve_balance_introspection.Make.precompile msg).
Proof.
apply balance_floor_callback.
- intros s Hinv.
destruct (precompile_preserves_supply msg s Hinv) as [Hafter Htotal].
split; [exact Hafter|apply Z.eq_le_incl; exact Htotal].
- intros s Hinv.
rewrite (precompile_preserves_balances msg s query (proj1 Hinv)).
apply Z.le_refl.
Qed.
Lemma precompile_preserves_balance_floor {F : Precompiles.Make.FArgs}
cold warm access write reserve query minimum msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (balanceFloor query minimum) action.
Proof.
intros Hrevision Hselected. unfold Precompiles.Make.try_precompile in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
- destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action;
intros s Hinitial; exact Hinitial.
- unfold Precompiles.Make.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [nat|] eqn:Hnative in Hselected end.
2: discriminate.
apply nonceprecompile.find_singleton_value in Hnative. subst nat.
match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
+ inversion Hselected; subst action. intros s Hinitial. exact Hinitial.
+ inversion Hselected; subst action. apply reserve_precompile_preserves_balance_floor.
Qed.
Lemma reserve_at_nonnegative snapshot h a :
storageWords (A.t.storage (accountAt snapshot native)) →
0 ≤ reserveAt snapshot h a.
Proof.
intro Hwords. unfold reserveAt, Reserve_balance.user_reserve_balance.
assert (Hbound : (Reserve_config.active (Reserve_balance.stored_config snapshot a) h < 2^256)%N).
{ rewrite reserveconfig.active_equation.
destruct (N.eqb _ _); [vm_compute; reflexivity|].
destruct (N.ltb _ _).
- change (readConfigField (A.t.storage (accountAt snapshot native)) a 0%Z < 2^256)%N.
apply read_config_bounded. exact Hwords.
- change (readConfigField (A.t.storage (accountAt snapshot native)) a 1%Z < 2^256)%N.
apply read_config_bounded. exact Hwords. }
assert (HboundZ : (Z.of_N (Reserve_config.active (Reserve_balance.stored_config snapshot a) h) < 2^256)%Z).
{ change (2^256)%Z with (Z.of_N (2^256)%N). apply N2Z.inj_lt. exact Hbound. }
rewrite (u256_of_uint _ HboundZ). apply N2Z.is_nonneg.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe
QedkConcrete.vmhostframe QedkConcrete.hostframe QedkConcrete.wordrange
QedkConcrete.systemcode.
Require Import SetcodeConcrete.vmprogress SetcodeConcrete.vmframes
SetcodeConcrete.vmpc SetcodeConcrete.vmpcopcodes SetcodeConcrete.codebytes.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
MonadExecutionSpecs.Termination.VmUnsignedGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section CallFreeAt.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (cold warm access write reserve : N).
Context (Hrevision : V.Params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : V.Params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hprogress : @setcodeGasProgress Host_t F).
Context (property : Host_t → Prop).
Section LoopAt.
Context {E : @V.Executor.FArgs Host_t F}.
Context (Hprimitive : PrimitiveHostFrameAt V.Host property (@V.Executor.self Host_t F E)).
Context (Hsetcode : adoptionFreeBytes
(V.Executor.C.(V.Executor_C_signature.execution_environment).(V.ExecutionEnvironment.t.bytecode)) = false →
∀ current source static gas,
preservesCallback property (V.Host.(Evmc.HOST.setcodefrom) current source static gas)).
Lemma setcode_pc_safe_at : pcSafe (@V.Executor.setcodefrom Host_t F E).
Proof.
unfold V.Executor.setcodefrom. rewrite Hrevision.
unfold V.M.HostAPI.setcodefrom, V.M.HostAPI.Base. solve_pc_safe.
Qed.
Theorem call_free_run_preserves_at state :
callFreeBytes (V.Executor.C.(V.Executor_C_signature.execution_environment)
.(V.ExecutionEnvironment.t.bytecode)) = true →
Z.of_N (MS.t.gas state) < 2^63 → wordRange (MS.t.pc state) →
property (MS.t.host state) →
property (MS.t.host (snd (@generated_run Host_t F E state))).
Proof.
intros Hcode Hgas Hpc Hinitial.
assert (Hrun : wordRange (MS.t.pc (snd (@generated_run Host_t F E state))) ∧
property (MS.t.host (snd (@generated_run Host_t F E state)))).
{ apply (setcode_run_preserves cold warm access write reserve
(fun st ⇒ wordRange (MS.t.pc st) ∧ property (MS.t.host st)) state Hrevision Hprogress).
- intros before Hbound [Hcounter Hbefore].
set (opcode := @run_selected_opcode Host_t F E before).
assert (HnoCalls : recursiveCallOpcode opcode = false).
{ unfold opcode. rewrite run_selected_opcode_eq_model.
apply call_free_selected_opcode; [exact Hcode|exact (proj1 Hcounter)]. }
split.
+ destruct (machineOnlyOpcode opcode) eqn:Hmachine.
× apply (machine_pc_safe opcode); [| |exact Hdebug|exact Hmachine|exact Hcounter].
-- unfold dispatch, V.Executor.execute_opcode, V.Executor.clz.
rewrite Hrevision. solve_pc_safe.
-- intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
++ exact setcode_pc_safe_at.
++ unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply pc_fail.
× eapply primitive_pc_safe; [exact Hdebug| |exact Hcounter].
destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *; congruence.
+ destruct (machineOnlyOpcode opcode) eqn:Hmachine.
× apply (machine_only_preserves_host_at property opcode);
[| |exact Hdebug|exact Hmachine|exact Hbefore].
-- eapply setcode_clz_preserves_host; exact Hrevision.
-- intros extra Hequal Hextra. destruct extra; cbn in Hextra; try contradiction.
++ eapply setcode_opcode_preserves_host; [exact Hrevision|].
apply Hsetcode.
destruct (adoptionFreeBytes
(V.Executor.C.(V.Executor_C_signature.execution_environment)
.(V.ExecutionEnvironment.t.bytecode))) eqn:Hfree; [|reflexivity].
assert (Hselected : notAdoption opcode = true).
{ unfold opcode. rewrite run_selected_opcode_eq_model.
apply adoption_free_selected_opcode; [exact Hfree|exact (proj1 Hcounter)]. }
rewrite <- Hequal in Hselected. discriminate.
++ unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply preserves_fail.
× eapply primitive_host_preserves_at_any_revision;
[exact Hdebug|exact Hprimitive| |exact Hbefore].
destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *; congruence.
- exact Hgas.
- split; assumption. }
exact (proj2 Hrun).
Qed.
End LoopAt.
Context (Hcontext : ∀ host, snd (V.Host.(Evmc.HOST.get_tx_context) host) = host).
Context (Hsize : ∀ addr host, snd (V.Host.(Evmc.HOST.get_code_size) addr host) = host).
Context (Hcopy : ∀ addr offset size host, snd (V.Host.(Evmc.HOST.copy_code) addr offset size host) = host).
Theorem call_free_execute_preserves_at_guarded msg code host :
PrimitiveHostFrameAt V.Host property (Evmc.Message.t.recipient msg) →
(adoptionFreeBytes code = false → ∀ current source static gas,
preservesCallback property (V.Host.(Evmc.HOST.setcodefrom) current source static gas)) →
0 ≤ Evmc.Message.t.gas msg < 2^63 → callFreeBytes code = true →
property host → property (snd (@V.execute Host_t F msg code host)).
Proof.
intros Hprimitive Hsetcode Hgas Hcode Hinitial.
unfold V.execute.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
pose proof (Hcontext host) as Hread.
destruct (V.Host.(Evmc.HOST.get_tx_context) host) as [context middle] eqn:Hctx.
cbn [snd] in Hread. subst middle.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
rewrite Hrevision.
pose proof (Hsize (Evmc.Message.t.recipient msg) host) as HreadSize.
destruct (V.Host.(Evmc.HOST.get_code_size) (Evmc.Message.t.recipient msg) host)
as [size middle] eqn:Hsz. cbn [snd] in HreadSize. subst middle.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hlength end.
2: cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
1: (pose proof (Hcopy (Evmc.Message.t.recipient msg) 0 Delegation.eoa_delegated_code_length host) as HreadCopy;
destruct (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0
Delegation.eoa_delegated_code_length host) as [ownCode middle] eqn:HownCode;
cbn [snd] in HreadCopy; subst middle;
change (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0 23 host = (ownCode, host)) in HownCode;
rewrite HownCode;
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host]).
all: match goal with |- context [@V.Executor.run _ _ ?env ?state] ⇒
pose proof (@call_free_run_preserves_at env Hprimitive Hsetcode state Hcode
ltac:(apply initial_gas_fits_signed_int64; exact Hgas) word_zero Hinitial) as Hrun;
unfold generated_run in Hrun;
destruct (@V.Executor.run Host_t F env state) as [outcome after]
end.
all: destruct outcome as [[]|error]; [exact Hrun|].
all: destruct error; exact Hrun.
Qed.
End CallFreeAt.
Require Import MonadVM.Execution.
Require Import QedkConcrete.worldaccounts QedkConcrete.validation
QedkConcrete.publictransaction QedkConcrete.reservecheck
QedkConcrete.reservestorage QedkConcrete.reserveencoding
QedkConcrete.hoststoragewords QedkConcrete.hostaccountformat
QedkConcrete.hashwords QedkConcrete.assemblychecked
QedkConcrete.nativeinputs QedkConcrete.supply
QedkConcrete.protectedstorage QedkConcrete.protectedstoragenative
QedkConcrete.protectedstorageboundarycore QedkConcrete.persistentcode
QedkConcrete.transactionframes QedkConcrete.transactionchecks
QedkConcrete.authorization QedkConcrete.blockentry
QedkConcrete.credits QedkConcrete.reservewrites
QedkConcrete.recursiveassembly
QedkConcrete.nondebitframe QedkConcrete.hostbalance
QedkConcrete.reserveprecompileproperties QedkConcrete.hostcode.
Require QedkConcrete.reserveconfig.
Require Import SetcodeConcrete.consensuschecks SetcodeConcrete.reserve
SetcodeConcrete.contextframe
SetcodeConcrete.transactionentry SetcodeConcrete.transactionentrysafe
SetcodeConcrete.transactionsupply SetcodeConcrete.supplysequence
SetcodeConcrete.nonceagreement SetcodeConcrete.nonceagreementsequence
SetcodeConcrete.activityagreement SetcodeConcrete.emptyingagreement
SetcodeConcrete.transactionactivity SetcodeConcrete.transactionregular
SetcodeConcrete.regularaccount SetcodeConcrete.executionregular
SetcodeConcrete.codeevolution SetcodeConcrete.transactioncode
SetcodeConcrete.transactionnonce SetcodeConcrete.blockconfig
SetcodeConcrete.windowconfig SetcodeConcrete.transactionconfig
SetcodeConcrete.validatedexecution SetcodeConcrete.transactionaccountfinalize
SetcodeConcrete.guardedaccountframe SetcodeConcrete.transactionguardedreserve
SetcodeConcrete.transactionbudget SetcodeConcrete.blockcode
SetcodeConcrete.blocknonceagreement SetcodeConcrete.blockactivityagreement
SetcodeConcrete.blockemptyingagreement SetcodeConcrete.blocksupply
SetcodeConcrete.miscblockproofs SetcodeConcrete.codeframe
SetcodeConcrete.systemcodeframe SetcodeConcrete.systemnonce
SetcodeConcrete.assembly SetcodeConcrete.nonceprecompile
SetcodeConcrete.consensusvalidation SetcodeConcrete.blocknonce
SetcodeConcrete.windownonceagreement.
Import ListNotations.
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := consensuschecks.W.
Module A := consensuschecks.A.
Module T := consensuschecks.T.
Module U := Numeric.U256.
Module Addr := consensuschecks.Addr.
Module Block := consensuschecks.Block.
Local Abbreviation number state :=
(Block.Header.t.number (Block.t.header (B.t.current_block state))).
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation accountAt := consensuschecks.accountAt.
Local Abbreviation codeAt world a := (A.t.code (accountAt world a)).
Lemma reserve_precompile_preserves_balance_floor {F : Reserve_balance_introspection.Make.FArgs}
query minimum msg :
preservesCallback (balanceFloor query minimum)
(Reserve_balance_introspection.Make.precompile msg).
Proof.
apply balance_floor_callback.
- intros s Hinv.
destruct (precompile_preserves_supply msg s Hinv) as [Hafter Htotal].
split; [exact Hafter|apply Z.eq_le_incl; exact Htotal].
- intros s Hinv.
rewrite (precompile_preserves_balances msg s query (proj1 Hinv)).
apply Z.le_refl.
Qed.
Lemma precompile_preserves_balance_floor {F : Precompiles.Make.FArgs}
cold warm access write reserve query minimum msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (balanceFloor query minimum) action.
Proof.
intros Hrevision Hselected. unfold Precompiles.Make.try_precompile in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
- destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action;
intros s Hinitial; exact Hinitial.
- unfold Precompiles.Make.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [nat|] eqn:Hnative in Hselected end.
2: discriminate.
apply nonceprecompile.find_singleton_value in Hnative. subst nat.
match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
+ inversion Hselected; subst action. intros s Hinitial. exact Hinitial.
+ inversion Hselected; subst action. apply reserve_precompile_preserves_balance_floor.
Qed.
Lemma reserve_at_nonnegative snapshot h a :
storageWords (A.t.storage (accountAt snapshot native)) →
0 ≤ reserveAt snapshot h a.
Proof.
intro Hwords. unfold reserveAt, Reserve_balance.user_reserve_balance.
assert (Hbound : (Reserve_config.active (Reserve_balance.stored_config snapshot a) h < 2^256)%N).
{ rewrite reserveconfig.active_equation.
destruct (N.eqb _ _); [vm_compute; reflexivity|].
destruct (N.ltb _ _).
- change (readConfigField (A.t.storage (accountAt snapshot native)) a 0%Z < 2^256)%N.
apply read_config_bounded. exact Hwords.
- change (readConfigField (A.t.storage (accountAt snapshot native)) a 1%Z < 2^256)%N.
apply read_config_bounded. exact Hwords. }
assert (HboundZ : (Z.of_N (Reserve_config.active (Reserve_balance.stored_config snapshot a) h) < 2^256)%Z).
{ change (2^256)%Z with (Z.of_N (2^256)%N). apply N2Z.inj_lt. exact Hbound. }
rewrite (u256_of_uint _ HboundZ). apply N2Z.is_nonneg.
Qed.
Apply this block's reserve cap before its transactions. snapshot
supplies stored configuration; header supplies the block height;
previous holds the budgets after the earlier checks. Return the same
nonce/activity state with each budget replaced by min(budget, reserve).
No actual balance changes and no increase replenishes a budget.
Definition capBudgets (snapshot : W.t) (header : Block.Header.t)
(previous : AdmissionState) : AdmissionState :=
{| admissionNonces := admissionNonces previous;
admissionActivity := admissionActivity previous;
admissionBudgets := fun a ⇒ Z.min (admissionBudgets previous a)
(reserveAt snapshot (Block.Header.t.number header) a) |}.
(previous : AdmissionState) : AdmissionState :=
{| admissionNonces := admissionNonces previous;
admissionActivity := admissionActivity previous;
admissionBudgets := fun a ⇒ Z.min (admissionBudgets previous a)
(reserveAt snapshot (Block.Header.t.number header) a) |}.
Relation between original consensus accounting previous and actual
execution state world before a block. snapshot is the fixed original
state; senders is the protected sender list; from and until bound
the block heights whose configuration and recent-send protection are
needed. The window proof establishes this relation, rather than assuming
it for every block.
Record BlockBudgetBacking {F : E.FArgs} (snapshot : W.t) (previous : AdmissionState)
(world : W.t) (senders : list Addr.t) (from until : N) : Prop := {
(world : W.t) (senders : list Addr.t) (from until : N) : Prop := {
Prescribed system programs remain unchanged.
Account map and balances satisfy their representation/supply invariant.
Storage values and account code lengths remain representable.
Reserve-precompile storage remains well formed.
Nonces match prediction for protected unmigrated accounts; regular-code
accounts are handled by the migrated-sender execution path.
block_backing_nonce :
NonceAgreement (eligibleAt snapshot) (admissionNonces previous) world senders;
NonceAgreement (eligibleAt snapshot) (admissionNonces previous) world senders;
Consensus activity and actual next-emptying counters agree through until.
Empty-code facts needed for the emptying exception remain valid.
Actual and snapshot reserve lookups coincide at every remaining height.
block_backing_config :
∀ target a, (from ≤ target ≤ until)%N →
Reserve_balance.user_reserve_balance world target a =
Reserve_balance.user_reserve_balance snapshot target a;
∀ target a, (from ≤ target ≤ until)%N →
Reserve_balance.user_reserve_balance world target a =
Reserve_balance.user_reserve_balance snapshot target a;
Every protected sender's remaining gas budget is nonnegative and funded.
block_backing_budgets :
∀ a, In a senders →
0 ≤ admissionBudgets previous a ∧
admissionBudgets previous a ≤ balanceAt world a;
∀ a, In a senders →
0 ≤ admissionBudgets previous a ∧
admissionBudgets previous a ≤ balanceAt world a;
An initially empty-code sender with no recorded subsequent activity
still has empty code and at least its snapshot balance.
block_backing_idle :
∀ a, In a senders → ¬ In a (admissionActivity previous) →
A.t.code (accountAt snapshot a) = Byte_string.Bytes.empty →
A.t.code (accountAt world a) = Byte_string.Bytes.empty ∧
balanceAt snapshot a ≤ balanceAt world a;
∀ a, In a senders → ¬ In a (admissionActivity previous) →
A.t.code (accountAt snapshot a) = Byte_string.Bytes.empty →
A.t.code (accountAt world a) = Byte_string.Bytes.empty ∧
balanceAt snapshot a ≤ balanceAt world a;
A positive-budget sender either has not migrated or has a prior send
that keeps it reserve-protected at every remaining height.
block_backing_status :
∀ a, In a senders →
admissionBudgets previous a = 0 ∨
A.is_smart_contract (accountAt world a) = false ∨
(∀ target, (from ≤ target ≤ until)%N →
sentInHistory world target a = true)
}.
Lemma block_budget_backing_weaken_from {F : E.FArgs} snapshot previous world senders from from' until :
(from ≤ from')%N →
BlockBudgetBacking snapshot previous world senders from until →
BlockBudgetBacking snapshot previous world senders from' until.
Proof.
intros Hle Hbacking.
destruct Hbacking. constructor; try assumption.
- intros target a Htarget. apply block_backing_config0. lia.
- intros a Ha. destruct (block_backing_status0 a Ha) as [Hz|[Hsc|Hhist]].
+ left. exact Hz.
+ right. left. exact Hsc.
+ right. right. intros target Htarget. apply Hhist. lia.
Qed.
Section Execution.
Context {F : E.FArgs}.
Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
{UB : Basics.Unreachable Bloom.t}
{U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation params := args.(Host.Instantiate.ChainParams).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).
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 args).
Lemma balance_floor_vm_constructor vm query minimum msg code state :
Evmc.Message.t.recipient msg ≠ query →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
callFreeBytes code = true → adoptionFreeBytes code = true →
balanceFloor query minimum state →
balanceFloor query minimum (snd ((VmF (HostF vm)).(Evmc.Vm.SIG.execute) msg code state)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hgas Hcode Hadoption Hinitial. cbn -[Vm.Make.execute balanceFloor].
eapply call_free_execute_preserves_at_guarded with (property := balanceFloor query minimum);
try exact Hrevision; try reflexivity.
- exact (@concrete_setcode_gas_progress args vm _ cold warm access write reserve Hrevision Hcold Hwarm).
- intros. apply copy_code_read_only.
- exact (@concrete_primitives_preserve_other_balance args vm
(Evmc.Message.t.recipient msg) query minimum Hdifferent).
- intros Hbad. rewrite Hadoption in Hbad. discriminate.
- exact Hgas.
- exact Hcode.
- exact Hinitial.
Qed.
Theorem execution_call_free_balance_floor query minimum msg code state :
Evmc.Message.t.recipient msg ≠ query →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
callFreeBytes code = true → adoptionFreeBytes code = true →
balanceFloor query minimum state →
balanceFloor query minimum (snd (@Host.Instantiate.Vm.execute args msg code state)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hgas Hcode Hadoption Hinitial.
change (balanceFloor query minimum (snd
((@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).(Evmc.Vm.SIG.execute) msg code state))).
unfold Evmc.Instantiate.Vm, Evmc.Instantiate._recursive_modules_Host_Vm.
generalize Evmc.Instantiate.recursive_module_fuel. intros [|fuel].
- exact Hinitial.
- destruct fuel; cbn [recursive_module_unroll fst snd];
apply balance_floor_vm_constructor; assumption.
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Lemma zero_transfer_balance_floor sender recipient amount query minimum s :
Numeric.U256.to_z amount = 0 → balanceFloor query minimum s →
balanceFloor query minimum (snd (@H.transfer_ether (@Host.Instantiate.Host_fargs args)
sender recipient amount s)).
Proof using F Hcold Hwarm cold warm.
intros Hzero [Hinv Hfloor].
destruct (@transfer_balance_effects (@Host.Instantiate.Host_fargs args)
sender recipient amount s Hinv ltac:(lia)) as [Hafter [_ Hbalances]].
split; [exact Hafter|]. specialize (Hbalances query).
rewrite Hzero in Hbalances.
match type of Hbalances with _ = _ + ?effect ⇒
replace effect with 0 in Hbalances by
(repeat match goal with |- context [if ?b then _ else _] ⇒ destruct b end; reflexivity)
end.
rewrite Z.add_0_r in Hbalances.
eapply Z.le_trans; [exact Hfloor|]. rewrite Hbalances. apply Z.le_refl.
Qed.
Lemma system_process_call_balance_floor query minimum msg state :
Evmc.Message.t.recipient msg ≠ query →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
callFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
adoptionFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
balanceFloor query minimum state →
balanceFloor query minimum (snd (@H.process_call (@Host.Instantiate.Host_fargs args) None msg state)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hgas Hzero Hcode Hadoption Hinitial.
set (transfer := if @H.should_transfer (@Host.Instantiate.Host_fargs args) msg
then @H.transfer_ether (@Host.Instantiate.Host_fargs args)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)).
assert (Hfloor : balanceFloor query minimum (snd (transfer state))).
{ unfold transfer. destruct (H.should_transfer msg); [|exact Hinitial].
exact (zero_transfer_balance_floor (Evmc.Message.t.sender msg)
(Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
query minimum state Hzero Hinitial). }
assert (Hbody : codeAt (S.t.world_state (snd (transfer state))) (Evmc.Message.t.code_address msg) =
codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)).
{ unfold transfer. destruct (H.should_transfer msg); [|reflexivity].
apply transfer_preserves_code. exact (proj1 (proj1 Hinitial)). }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile balanceFloor].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
change run with (transfer state); destruct (transfer state) as [ok middle] eqn:Htransfer end.
cbn beta iota zeta delta [snd] in Hfloor, Hbody.
destruct ok; [|exact Hfloor].
cbn -[W.account Precompiles.Make.try_precompile balanceFloor].
match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
- eapply precompile_preserves_balance_floor.
2: exact Hselected.
+ exact Hrevision.
+ exact Hfloor.
- apply execution_call_free_balance_floor.
+ exact Hdifferent.
+ exact Hgas.
+ exact (eq_trans (f_equal callFreeBytes Hbody) Hcode).
+ exact (eq_trans (f_equal adoptionFreeBytes Hbody) Hadoption).
+ exact Hfloor.
Qed.
Theorem execution_system_call_balance_floor query minimum msg state :
Evmc.Message.t.recipient msg ≠ query →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
callFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
adoptionFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
balanceFloor query minimum state →
balanceFloor query minimum (snd (@E.Host.call F msg state)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hgas Hkind Hzero Hcode Hadoption Hinitial.
pose proof (system_process_call_balance_floor query minimum msg state
Hdifferent Hgas Hzero Hcode Hadoption Hinitial) as Hbody.
change (balanceFloor query minimum (snd (@H.call (@Host.Instantiate.Host_fargs args) msg state))).
unfold H.call, H.call_impl. rewrite Hkind.
cbn -[H.process_call balanceFloor].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
destruct run as [result after] end.
cbn beta iota zeta delta [snd] in Hbody.
destruct result as [status gasleft refund output address]; destruct status;
cbn -[balanceFloor]; first [exact Hbody|exact Hinitial].
Qed.
Theorem system_message_nondebit block addr data query :
addr ≠ query →
callFreeBytes (codeAt (B.t.world_state block) addr) = true →
adoptionFreeBytes (codeAt (B.t.world_state block) addr) = true →
balanceInvariant (B.t.world_state block) →
balanceInvariant (B.t.world_state (snd (E.process_system_message block addr data))) ∧
balanceAt (B.t.world_state block) query ≤
balanceAt (B.t.world_state (snd (E.process_system_message block addr data))) query.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hcode Hadoption Hinv.
unfold E.process_system_message.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
- split; [exact Hinv|apply Z.le_refl].
- cbn beta iota zeta.
match goal with |- context [E.Host.call ?msg ?entry] ⇒
pose proof (execution_system_call_balance_floor query
(balanceAt (B.t.world_state block) query) msg entry
Hdifferent ltac:(change (0 ≤ 30000000 < 2^63); lia) eq_refl eq_refl
Hcode Hadoption (conj Hinv (Z.le_refl _))) as Hrun;
destruct (E.Host.call msg entry) as [result after]
end.
exact Hrun.
Qed.
Lemma block_entry_nondebit world block query :
prescribedSystemCode world →
balanceInvariant world →
E.beacon_roots_address ≠ query →
E.history_storage_address ≠ query →
balanceInvariant (B.t.world_state (blockTransactionEntry world block)) ∧
balanceAt world query ≤
balanceAt (B.t.world_state (blockTransactionEntry world block)) query.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros [[Hmap Hbeacon] [_ Hhistory]] Hinv HnotBeacon HnotHistory.
set (activated := Reserve_balance.activate_precompile world).
pose proof (activate_preserves_supply world Hinv) as [HactInv _].
pose proof (activate_preserves_balances world query Hmap) as HactBal.
change (balanceAt activated query = balanceAt world query) in HactBal.
assert (Hactive : stringmaps.WellFormed (W.t.accounts activated))
by (apply account_write_wellformed; exact Hmap).
assert (Hb : codeAt activated E.beacon_roots_address = beaconRootsCode)
by (exact (eq_trans (activation_preserves_code world _ Hmap) Hbeacon)).
assert (Hh : codeAt activated E.history_storage_address = historyStorageCode)
by (exact (eq_trans (activation_preserves_code world _ Hmap) Hhistory)).
unfold blockTransactionEntry. cbn zeta. fold activated.
match goal with |- context [E.process_system_message (B.make activated block) ?addr ?data] ⇒
set (beaconData := data) end.
assert (Hbc : callFreeBytes (codeAt activated E.beacon_roots_address) = true)
by (rewrite Hb; exact beaconRootsCode_call_free).
assert (Hba : adoptionFreeBytes (codeAt activated E.beacon_roots_address) = true)
by (rewrite Hb; exact beacon_roots_no_adoption).
destruct (system_message_code cold warm access write reserve Hrevision Hcold Hwarm
(B.make activated block) E.beacon_roots_address beaconData E.history_storage_address
historyStorageCode Hactive Hh Hbc Hba) as [_ Hhistory'].
destruct (system_message_nondebit (B.make activated block) E.beacon_roots_address
beaconData query HnotBeacon Hbc Hba HactInv) as [HbeaconInv HbeaconBal].
match goal with |- context [E.process_system_message ?beacon E.history_storage_address ?data] ⇒
destruct (system_message_nondebit beacon E.history_storage_address data query
HnotHistory
(eq_rect _ (fun code ⇒ callFreeBytes code = true) historyStorageCode_call_free _ (eq_sym Hhistory'))
(eq_rect _ (fun code ⇒ adoptionFreeBytes code = true) history_storage_no_adoption _ (eq_sym Hhistory'))
HbeaconInv) as [HhistInv HhistBal] end.
split; [exact HhistInv|].
change (B.t.world_state (B.make activated block)) with activated in HbeaconBal.
rewrite HactBal in HbeaconBal.
exact (Z.le_trans _ _ _ HbeaconBal HhistBal).
Qed.
Lemma eligible_not_system_contract snapshot a :
@prescribedSystemCode F snapshot →
eligibleAt snapshot a = true →
E.beacon_roots_address ≠ a ∧ E.history_storage_address ≠ a.
Proof using.
intros [[_ Hbeacon] [_ Hhistory]] Helig.
change (codeAt snapshot E.beacon_roots_address = beaconRootsCode) in Hbeacon.
change (codeAt snapshot E.history_storage_address = historyStorageCode) in Hhistory.
unfold eligibleAt in Helig. apply Bool.negb_true_iff in Helig.
split; intro Heq; subst a.
- assert (Hreg : regularCode (codeAt snapshot E.beacon_roots_address))
by (rewrite Hbeacon; vm_compute; reflexivity).
apply regular_code_contract in Hreg. congruence.
- assert (Hreg : regularCode (codeAt snapshot E.history_storage_address))
by (rewrite Hhistory; vm_compute; reflexivity).
apply regular_code_contract in Hreg. congruence.
Qed.
Lemma system_message_keeps_history state addr data :
W.t.history (B.t.world_state (snd (E.process_system_message state addr data))) =
W.t.history (B.t.world_state state).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
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 ?message ?entry] ⇒
set (msg := message); set (initial := entry) end.
assert (Hgas : (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z)
by (change (0 ≤ 30000000 < 2^63)%Z; lia).
pose proof (host_call_keeps_context cold warm access write reserve
Hrevision Hcold Hwarm msg Hgas initial) as Hcontext.
destruct (E.Host.call msg initial) as [result afterVm].
cbn [snd] in Hcontext. unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounters Hprior Hchain.
exact Hhistory.
Qed.
Lemma block_entry_keeps_history world block :
W.t.history (B.t.world_state (blockTransactionEntry world block)) = W.t.history world.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
unfold blockTransactionEntry. cbn zeta.
rewrite !system_message_keeps_history. reflexivity.
Qed.
Lemma system_message_transactions_processed state addr data :
B.t.transactions_processed (snd (E.process_system_message state addr data)) =
B.t.transactions_processed state.
Proof using.
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 ?entry] ⇒
destruct (E.Host.call msg entry) as [result after] end.
reflexivity.
Qed.
Lemma block_entry_transactions_processed world block :
B.t.transactions_processed (blockTransactionEntry world block) = [].
Proof using.
unfold blockTransactionEntry. cbn zeta.
rewrite !system_message_transactions_processed. reflexivity.
Qed.
Lemma is_smart_contract_code acc1 acc2 :
A.t.code acc1 = A.t.code acc2 →
A.is_smart_contract acc1 = A.is_smart_contract acc2.
Proof using.
destruct acc1, acc2. cbn [A.t.code A.is_smart_contract]. intro H; rewrite H. reflexivity.
Qed.
Lemma block_entry_smart_contract world block query :
prescribedSystemCode world →
A.is_smart_contract (accountAt (B.t.world_state (blockTransactionEntry world block)) query) =
A.is_smart_contract (accountAt world query).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intro Hsystem.
apply is_smart_contract_code.
exact (block_entry_code cold warm access write reserve Hrevision Hcold Hwarm
world block query Hsystem).
Qed.
Lemma block_entry_preserves_supply world block :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header block) →
OCamlString.length (Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32 →
balanceInvariant world →
worldFormat world →
balanceInvariant (B.t.world_state (blockTransactionEntry world block)) ∧
totalBalance (B.t.world_state (blockTransactionEntry world block)) ≤ totalBalance world ∧
worldFormat (B.t.world_state (blockTransactionEntry world block)).
Proof using F Hcold Hhash Hnative Hrevision Hwarm access cold reserve warm write.
intros Hchain Hheader Hroot Hinitial Hformat.
unfold blockTransactionEntry. cbn zeta.
set (initial := B.make (Reserve_balance.activate_precompile world) block).
assert (HinitialFormat : worldFormat (B.t.world_state initial))
by (apply activate_preserves_format; exact Hformat).
assert (HinitialSupply : balanceInvariant (B.t.world_state initial) ∧
totalBalance (B.t.world_state initial) = totalBalance world)
by (apply activate_preserves_supply; exact Hinitial).
match goal with |- context [E.process_system_message initial ?addr ?data] ⇒
assert (Hdata : OCamlString.length data < 2^62) by
(change (OCamlString.length (Block.Header.t.parent_beacon_block_root
(Block.t.header block)) < 2^62); rewrite Hroot; lia);
pose proof (system_message_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative initial addr data Hchain Hheader
Hdata (proj1 HinitialSupply) HinitialFormat) as Hbeacon;
pose proof (@system_message_current_block F initial addr data) as HbeaconBlock;
destruct (E.process_system_message initial addr data) as [beaconResult afterBeacon]
end.
cbn beta iota zeta delta [snd] in Hbeacon, HbeaconBlock |- ×.
destruct Hbeacon as [Hbeacon [HbeaconTotal HbeaconFormat]].
assert (HbeaconHeader : headerWords (Block.t.header (B.t.current_block afterBeacon)))
by (rewrite HbeaconBlock; exact Hheader).
match goal with |- context [E.process_system_message afterBeacon ?addr ?data] ⇒
assert (HdataHash : OCamlString.length data < 2^62) by
(lazymatch data with ?toBytes ?hash ⇒ change (OCamlString.length hash < 2^62) end;
rewrite (block_hash_length Hhash); lia);
pose proof (system_message_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative afterBeacon addr data Hchain HbeaconHeader
HdataHash Hbeacon HbeaconFormat) as Hhistory;
destruct (E.process_system_message afterBeacon addr data) as [historyResult afterHistory]
end.
cbn beta iota zeta delta [snd] in Hhistory |- ×.
destruct Hhistory as [Hhistory [HhistoryTotal HhistoryFormat]].
split; [exact Hhistory|]. split; [lia|exact HhistoryFormat].
Qed.
Theorem block_entry_budget_backing snapshot previous world block senders from until :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header block) →
OCamlString.length (Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32 →
(∀ a, In a senders → a ≠ native) →
prescribedSystemCode snapshot →
storageWords (A.t.storage (accountAt snapshot native)) →
(from ≤ Block.Header.t.number (Block.t.header block))%N →
(Block.Header.t.number (Block.t.header block) ≤ until)%N →
(until < Block.Header.t.number (Block.t.header block) + 3)%N →
BlockBudgetBacking snapshot previous world senders from until →
TransactionBudgetBacking snapshot (capBudgets snapshot (Block.t.header block) previous)
(blockTransactionEntry world block) senders until.
Proof using F Hcold Hhash Hnative Hrevision Hwarm access cold reserve warm write.
intros Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Hfrom HleUntil HltDelay Hbacking.
destruct Hbacking as [Hsys Hbal Hfmt HcfgWf Hnonce Hact Hempty Hcfg Hbudgets Hidle Hstatus].
destruct (block_entry_preserves_supply world block Hchain Hheader Hroot Hbal Hfmt)
as [HentryBal [_ HentryFmt]].
pose proof (block_entry_current world block) as Hcurr.
constructor.
- exact HentryBal.
- exact HentryFmt.
- exact (block_entry_nonce_agreement cold warm access write reserve
Hrevision Hcold Hwarm world block _ _ senders HnotNative Hsys Hnonce).
- eapply activity_same_counters; [apply (block_entry_keeps_counters cold warm access write reserve Hrevision Hcold Hwarm)|exact Hact].
- exact (block_entry_empty_code cold warm access write reserve
Hrevision Hcold Hwarm snapshot _ world block senders Hsys Hempty).
- destruct HcfgWf as [Hmap [Hcode [HstMap HstWords]]].
∃ (A.t.storage (accountAt world native)).
rewrite Hcurr.
refine (conj HstMap (conj HstWords (conj _ _))).
+ intro a. symmetry.
exact (Hcfg (Block.Header.t.number (Block.t.header block)) a ltac:(lia)).
+ exact (block_entry_config cold warm access write reserve Hrevision Hcold Hwarm
world block Hmap Hcode).
- intros a Ha. cbn [capBudgets admissionBudgets]. rewrite Hcurr.
destruct (Hbudgets a Ha) as [HprevNonneg HprevBal].
destruct (Hnonce a Ha) as [Helig _].
destruct (eligible_not_system_contract snapshot a HsnapSystem Helig)
as [HnotBeacon HnotHistory].
destruct (block_entry_nondebit world block a Hsys Hbal HnotBeacon HnotHistory)
as [_ HentryNondebit].
pose proof (reserve_at_nonnegative snapshot (Block.Header.t.number (Block.t.header block))
a HsnapWords) as HresNonneg.
split; [lia|split; [lia|apply Z.le_min_r]].
- intros a Ha HnotAct HsnapEmpty.
destruct (Hidle a Ha HnotAct HsnapEmpty) as [HworldEmpty HworldBal].
destruct (Hnonce a Ha) as [Helig _].
destruct (eligible_not_system_contract snapshot a HsnapSystem Helig)
as [HnotBeacon HnotHistory].
destruct (block_entry_nondebit world block a Hsys Hbal HnotBeacon HnotHistory)
as [_ HentryNondebit].
split.
+ rewrite (block_entry_code cold warm access write reserve Hrevision Hcold Hwarm
world block a Hsys).
exact HworldEmpty.
+ lia.
- intros a Ha. cbn [capBudgets admissionBudgets]. rewrite Hcurr.
pose proof (reserve_at_nonnegative snapshot (Block.Header.t.number (Block.t.header block))
a HsnapWords) as HresNonneg.
destruct (Hstatus a Ha) as [Hzero|[Hsc|Hhist]].
+ left. lia.
+ right. left.
rewrite (block_entry_smart_contract world block a Hsys). exact Hsc.
+ right. right. right.
assert (HhistEq : ∀ t,
sentInHistory (B.t.world_state (blockTransactionEntry world block)) t a =
sentInHistory world t a).
{ intro t. unfold sentInHistory.
rewrite (block_entry_keeps_history world block). reflexivity. }
split.
× rewrite HhistEq. apply Hhist. lia.
× intros target Htarget. rewrite HhistEq. apply Hhist. lia.
Qed.
Lemma withdrawals_smart_contract withdrawals state a :
stringmaps.WellFormed (W.t.accounts (B.t.world_state state)) →
A.is_smart_contract (accountAt (B.t.world_state
(Result.List.fold_left E.process_withdrawal state withdrawals)) a) =
A.is_smart_contract (accountAt (B.t.world_state state) a).
Proof using.
intro Hmap.
assert (Hcode : worldCode a (codeAt (B.t.world_state state) a) (B.t.world_state state))
by (split; [exact Hmap|reflexivity]).
destruct (withdrawals_preserve_code a (codeAt (B.t.world_state state) a)
withdrawals state Hcode) as [_ HafterCode].
apply is_smart_contract_code.
exact HafterCode.
Qed.
Lemma withdrawals_code_at withdrawals state a :
stringmaps.WellFormed (W.t.accounts (B.t.world_state state)) →
codeAt (B.t.world_state (Result.List.fold_left E.process_withdrawal state withdrawals)) a =
codeAt (B.t.world_state state) a.
Proof using.
intro Hmap.
assert (Hcode : worldCode a (codeAt (B.t.world_state state) a) (B.t.world_state state))
by (split; [exact Hmap|reflexivity]).
exact (proj2 (withdrawals_preserve_code a (codeAt (B.t.world_state state) a)
withdrawals state Hcode)).
Qed.
Lemma finalized_block_sent_in_history finalized history block afterTransactions target a :
Block.Header.t.number (Block.t.header finalized) =
Block.Header.t.number (Block.t.header block) →
Block.t.transactions finalized =
OCamlList.map fst (B.t.transactions_processed afterTransactions) →
sentInPriorTransactions afterTransactions a = true →
(Block.Header.t.number (Block.t.header block) < target <
Block.Header.t.number (Block.t.header block) + 3)%N →
OCamlList._exists
(fun b ⇒
Support.UintN.op_lt (Block.Header.t.number (Block.t.header b)) target &&
Support.UintN.op_lt target
(Support.UintN.op_plus (Block.Header.t.number (Block.t.header b))
Reserve_balance.execution_consensus_delay) &&
OCamlList._exists (fun tx ⇒ sentBy tx a) (Block.t.transactions b))
(finalized :: history) = true.
Proof using F Hcold Hwarm cold warm.
intros Hnum Htxs Hsent Hrange.
cbn [OCamlList._exists].
rewrite Hnum, Htxs.
change (Support.UintN.op_lt (Block.Header.t.number (Block.t.header block)) target)
with (N.ltb (Block.Header.t.number (Block.t.header block)) target).
change (Support.UintN.op_lt target
(Support.UintN.op_plus (Block.Header.t.number (Block.t.header block))
Reserve_balance.execution_consensus_delay))
with (N.ltb target (Block.Header.t.number (Block.t.header block) + 3)%N).
assert (Hlt1 : N.ltb (Block.Header.t.number (Block.t.header block)) target = true)
by (apply N.ltb_lt; lia).
assert (Hlt2 : N.ltb target (Block.Header.t.number (Block.t.header block) + 3)%N = true)
by (apply N.ltb_lt; lia).
unfold sentInPriorTransactions in Hsent.
rewrite Hlt1, Hlt2, Hsent. reflexivity.
Qed.
Theorem checked_block_no_insufficient_balance verify snapshot previous next
world block senders from until details fee :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header block) →
OCamlString.length (Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32 →
(∀ a, In a senders → a ≠ native) →
prescribedSystemCode snapshot →
storageWords (A.t.storage (accountAt snapshot native)) →
Forall (fun tx ⇒
transactionValueAndDataBounds tx ∧
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx) ∧
signaturesAvoidAddress tx native ∧
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders)) (Block.t.transactions block) →
(E.validate_block world block = inl tt →
transactionsAvoidSenderCreation senders (blockTransactionEntry world block)
(Block.t.transactions block)) →
(from ≤ Block.Header.t.number (Block.t.header block))%N →
(Block.Header.t.number (Block.t.header block) ≤ until)%N →
(until < Block.Header.t.number (Block.t.header block) + 3)%N →
BlockBudgetBacking snapshot previous world senders from until →
checkTransactions snapshot (Block.t.header block)
(capBudgets snapshot (Block.t.header block) previous)
(Block.t.transactions block) = Some next →
E.process_block verify world block = inr (Execution.Error.Invalid_transaction details) →
Execution.Error.t.Invalid_transaction.reason details ≠
Execution.Error.Insufficient_balance fee.
Proof using F Hcold Hhash Hnative Hrevision Hwarm U8 UB UI0 access cold reserve warm write.
intros Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Htxs Hcreation
Hfrom HleUntil HltDelay Hbacking Hcheck Hrun.
pose proof (block_entry_budget_backing snapshot previous world block senders from until
Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Hfrom HleUntil HltDelay Hbacking)
as HentryBacking.
pose proof (block_entry_current world block) as Hcurr.
pose proof (checked_transactions_budget_backing cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative snapshot
(capBudgets snapshot (Block.t.header block) previous) next
(blockTransactionEntry world block) (Block.t.transactions block) senders until
Hchain ltac:(rewrite Hcurr; exact Hheader) HnotNative Htxs
(Hcreation (consensusvalidation.transaction_error_has_valid_block_header verify world block details Hrun))
ltac:(cbv beta; rewrite Hcurr; exact HleUntil)
ltac:(cbv beta; rewrite Hcurr; exact HltDelay)
HentryBacking ltac:(rewrite Hcurr; exact Hcheck)) as HtxResult.
rewrite (block_transaction_error_from_entry cold warm access write reserve
Hrevision verify world block details Hrun) in HtxResult.
exact (HtxResult fee).
Qed.
Theorem checked_block_budget_backing verify snapshot previous next
world block post senders from until :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header block) →
OCamlString.length (Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32 →
(∀ a, In a senders → a ≠ native) →
prescribedSystemCode snapshot →
storageWords (A.t.storage (accountAt snapshot native)) →
Forall (fun tx ⇒
transactionValueAndDataBounds tx ∧
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx) ∧
signaturesAvoidAddress tx native ∧
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders)) (Block.t.transactions block) →
Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (Chain.Ethereum.Withdrawal.t.amount wd) < 2^64)
(Block.t.withdrawals block) →
let credits := Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0
(Block.t.withdrawals block) in
totalBalance world + credits < 2^256 →
(E.validate_block world block = inl tt →
transactionsAvoidSenderCreation senders (blockTransactionEntry world block)
(Block.t.transactions block)) →
(from ≤ Block.Header.t.number (Block.t.header block))%N →
(Block.Header.t.number (Block.t.header block) ≤ until)%N →
(until < Block.Header.t.number (Block.t.header block) + 3)%N →
BlockBudgetBacking snapshot previous world senders from until →
checkTransactions snapshot (Block.t.header block)
(capBudgets snapshot (Block.t.header block) previous)
(Block.t.transactions block) = Some next →
E.process_block verify world block = inl post →
BlockBudgetBacking snapshot next post senders
(Block.Header.t.number (Block.t.header block) + 1)%N until ∧
totalBalance post ≤ totalBalance world + credits.
Proof using F Hcold Hhash Hnative Hrevision Hwarm U8 UB UI0 access cold reserve warm write.
intros Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Htxs Hwithdrawals
credits Hsupply Hcreation Hfrom HleUntil HltDelay Hbacking Hcheck Hrun.
pose proof (processed_block_valid_header verify world block post Hrun) as HvalidHeader.
pose proof (block_entry_budget_backing snapshot previous world block senders from until
Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Hfrom HleUntil HltDelay Hbacking)
as HentryBacking.
pose proof (block_entry_current world block) as Hcurr.
assert (HtxNumeric : Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx)) (Block.t.transactions block)).
{ eapply Forall_impl; [|exact Htxs]. intros tx [Hv [Hw _]]. exact (conj Hv Hw). }
assert (HtxWidth : Forall (fun tx ⇒
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx)) (Block.t.transactions block)).
{ eapply Forall_impl; [|exact HtxNumeric]. intros tx [_ Hw]. exact Hw. }
assert (HtxSigs : Forall (fun tx ⇒ signaturesAvoidAddress tx native)
(Block.t.transactions block)).
{ eapply Forall_impl; [|exact Htxs]. intros tx [_ [_ [Hs _]]]. exact Hs. }
destruct (process_block_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative verify world block post
Hchain Hheader Hroot (block_backing_balance _ _ _ _ _ _ Hbacking)
(block_backing_format _ _ _ _ _ _ Hbacking) HtxNumeric Hwithdrawals Hsupply Hrun)
as [HpostBal [HpostTotal HpostFmt]].
pose proof (block_preserves_system_code cold warm access write reserve
verify world block post Hrevision Hcold Hwarm
(block_backing_system _ _ _ _ _ _ Hbacking) Hrun) as HpostSys.
destruct (block_configured_window cold warm access write reserve
Hrevision Hcold Hwarm verify world block post
(block_backing_config_wf _ _ _ _ _ _ Hbacking) HtxSigs Hrun)
as [HpostCfgWf HpostCfgWindow].
pose proof (block_activity_agreement cold warm access write reserve
Hrevision Hcold Hwarm verify snapshot
(capBudgets snapshot (Block.t.header block) previous) next world block post until
(block_backing_activity _ _ _ _ _ _ Hbacking) HltDelay Hcheck Hrun) as HpostAct.
pose proof (block_nonce_agreement cold warm access write reserve
Hrevision Hcold Hwarm verify snapshot
(capBudgets snapshot (Block.t.header block) previous) next world block post senders
HnotNative HtxWidth
(block_backing_system _ _ _ _ _ _ Hbacking)
(block_backing_nonce _ _ _ _ _ _ Hbacking)
(Hcreation HvalidHeader) Hcheck Hrun) as HpostNonce.
pose proof (block_empty_code_agreement cold warm access write reserve
Hrevision Hcold Hwarm verify snapshot
(capBudgets snapshot (Block.t.header block) previous) next world block post senders
HnotNative HtxWidth
(block_backing_system _ _ _ _ _ _ Hbacking)
(block_backing_nonce _ _ _ _ _ _ Hbacking)
(block_backing_empty_code _ _ _ _ _ _ Hbacking)
(Hcreation HvalidHeader) Hcheck Hrun) as HpostEmpty.
pose proof (checked_transactions_budget_backing cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative snapshot
(capBudgets snapshot (Block.t.header block) previous) next
(blockTransactionEntry world block) (Block.t.transactions block) senders until
Hchain ltac:(rewrite Hcurr; exact Hheader) HnotNative Htxs
(Hcreation HvalidHeader)
ltac:(cbv beta; rewrite Hcurr; exact HleUntil)
ltac:(cbv beta; rewrite Hcurr; exact HltDelay)
HentryBacking ltac:(rewrite Hcurr; exact Hcheck)) as HtxResult.
pose proof Hrun as HrunUnfold.
unfold E.process_block in HrunUnfold. rewrite Hrevision, HvalidHeader in HrunUnfold.
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 HrunUnfold.
cbn beta iota zeta in HrunUnfold.
match type of HrunUnfold with context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
change entry with (blockTransactionEntry world block) in HrunUnfold end.
destruct (Result.List.fold_leftM E.process_transaction
(blockTransactionEntry world block) (Block.t.transactions block))
as [afterTransactions|err] eqn:HtxsRun; [|discriminate HrunUnfold].
destruct HtxResult as [HafterBacking [HafterCurr [HafterHist HafterProc]]].
rewrite (block_entry_transactions_processed world block), app_nil_l in HafterProc.
rewrite (block_entry_keeps_history world block) in HafterHist.
rewrite Hcurr in HafterCurr.
cbn beta iota zeta in HrunUnfold.
match type of HrunUnfold with context [E.validate_block ?w ?b] ⇒
destruct (E.validate_block w b) as [[]|error] end; [|discriminate HrunUnfold].
assert (HpostEq : post = W.t.with_history
(B.finalize_current_block (Result.List.fold_left E.process_withdrawal
afterTransactions (Block.t.withdrawals block)) :: W.t.history world)
(B.t.world_state (Result.List.fold_left E.process_withdrawal
afterTransactions (Block.t.withdrawals block)))).
{ destruct verify.
- match type of HrunUnfold with context [match ?chk with inl _ ⇒ _ | inr _ ⇒ _ end] ⇒
destruct chk as [[]|error] end; [|discriminate HrunUnfold].
injection HrunUnfold as <-. reflexivity.
- injection HrunUnfold as <-. reflexivity. }
assert (HremSupply : totalBalance (B.t.world_state afterTransactions) + credits < 2^256).
{ destruct (block_entry_preserves_supply world block Hchain Hheader Hroot
(block_backing_balance _ _ _ _ _ _ Hbacking)
(block_backing_format _ _ _ _ _ _ Hbacking)) as [HeBal [HeTot HeFmt]].
destruct (transactions_preserve_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative (blockTransactionEntry world block)
(Block.t.transactions block) afterTransactions Hchain
ltac:(rewrite Hcurr; exact Hheader) HtxNumeric HeBal HeFmt HtxsRun)
as [_ [HtxTot _]].
lia. }
destruct (@withdrawals_preserve_supply_bound F afterTransactions
(Block.t.withdrawals block) (tx_backing_balance _ _ _ _ _ HafterBacking)
Hwithdrawals HremSupply) as [_ [_ HwdNondebit]].
set (afterWithdrawals := Result.List.fold_left E.process_withdrawal
afterTransactions (Block.t.withdrawals block)) in ×.
assert (HwdCurr : B.t.current_block afterWithdrawals = block).
{ unfold afterWithdrawals. rewrite withdrawals_current_block. exact HafterCurr. }
assert (HpostBalanceAt : ∀ a, balanceAt post a = balanceAt (B.t.world_state afterWithdrawals) a).
{ intro a. rewrite HpostEq. reflexivity. }
assert (HpostAccountAt : ∀ a, accountAt post a = accountAt (B.t.world_state afterWithdrawals) a).
{ intro a. rewrite HpostEq. reflexivity. }
split; [|exact HpostTotal].
constructor; try assumption.
- intros target a Htarget.
etransitivity.
+ apply HpostCfgWindow. lia.
+ apply (block_backing_config _ _ _ _ _ _ Hbacking). lia.
- intros a Ha.
destruct (tx_backing_budgets _ _ _ _ _ HafterBacking a Ha) as [Hnonneg [HbalAfter _]].
pose proof (HwdNondebit a) as HwdA.
change (balanceAt (B.t.world_state afterTransactions) a ≤
balanceAt (B.t.world_state afterWithdrawals) a) in HwdA.
rewrite HpostBalanceAt. lia.
- intros a Ha HnotAct HsnapEmpty.
destruct (tx_backing_idle _ _ _ _ _ HafterBacking a Ha HnotAct HsnapEmpty)
as [HcodeAfter HbalAfter].
pose proof (HwdNondebit a) as HwdA.
change (balanceAt (B.t.world_state afterTransactions) a ≤
balanceAt (B.t.world_state afterWithdrawals) a) in HwdA.
rewrite HpostAccountAt, HpostBalanceAt.
split; [|lia].
unfold afterWithdrawals.
rewrite (withdrawals_code_at (Block.t.withdrawals block) afterTransactions a
(proj1 (tx_backing_balance _ _ _ _ _ HafterBacking))).
exact HcodeAfter.
- intros a Ha.
destruct (tx_backing_status _ _ _ _ _ HafterBacking a Ha)
as [Hzero|[Hsc|[Hprior|[HhistNow HhistFuture]]]].
+ left. exact Hzero.
+ right. left. rewrite HpostAccountAt. unfold afterWithdrawals.
rewrite (withdrawals_smart_contract (Block.t.withdrawals block) afterTransactions a
(proj1 (tx_backing_balance _ _ _ _ _ HafterBacking))).
exact Hsc.
+ right. right. intros target Htarget.
rewrite HpostEq. unfold sentInHistory. cbn [W.t.history W.t.with_history].
apply (finalized_block_sent_in_history
(B.finalize_current_block afterWithdrawals) (W.t.history world)
block afterTransactions target a).
× change (Block.Header.t.number (Block.t.header (B.t.current_block afterWithdrawals)) =
Block.Header.t.number (Block.t.header block)).
rewrite HwdCurr. reflexivity.
× change (Block.t.transactions (B.t.current_block afterWithdrawals) =
OCamlList.map fst (B.t.transactions_processed afterTransactions)).
rewrite HwdCurr, HafterProc. reflexivity.
× exact Hprior.
× lia.
+ right. right. intros target Htarget.
assert (HprevHist : sentInHistory (B.t.world_state afterTransactions) target a = true).
{ apply HhistFuture. rewrite HafterCurr. lia. }
unfold sentInHistory in HprevHist |- ×.
rewrite HafterHist in HprevHist.
rewrite HpostEq. cbn [W.t.history W.t.with_history OCamlList._exists].
rewrite HprevHist, Bool.orb_true_r. reflexivity.
Qed.
End Execution.
Print Assumptions block_entry_budget_backing.
Print Assumptions checked_block_no_insufficient_balance.
Print Assumptions checked_block_budget_backing.
∀ a, In a senders →
admissionBudgets previous a = 0 ∨
A.is_smart_contract (accountAt world a) = false ∨
(∀ target, (from ≤ target ≤ until)%N →
sentInHistory world target a = true)
}.
Lemma block_budget_backing_weaken_from {F : E.FArgs} snapshot previous world senders from from' until :
(from ≤ from')%N →
BlockBudgetBacking snapshot previous world senders from until →
BlockBudgetBacking snapshot previous world senders from' until.
Proof.
intros Hle Hbacking.
destruct Hbacking. constructor; try assumption.
- intros target a Htarget. apply block_backing_config0. lia.
- intros a Ha. destruct (block_backing_status0 a Ha) as [Hz|[Hsc|Hhist]].
+ left. exact Hz.
+ right. left. exact Hsc.
+ right. right. intros target Htarget. apply Hhist. lia.
Qed.
Section Execution.
Context {F : E.FArgs}.
Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
{UB : Basics.Unreachable Bloom.t}
{U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation params := args.(Host.Instantiate.ChainParams).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).
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 args).
Lemma balance_floor_vm_constructor vm query minimum msg code state :
Evmc.Message.t.recipient msg ≠ query →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
callFreeBytes code = true → adoptionFreeBytes code = true →
balanceFloor query minimum state →
balanceFloor query minimum (snd ((VmF (HostF vm)).(Evmc.Vm.SIG.execute) msg code state)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hgas Hcode Hadoption Hinitial. cbn -[Vm.Make.execute balanceFloor].
eapply call_free_execute_preserves_at_guarded with (property := balanceFloor query minimum);
try exact Hrevision; try reflexivity.
- exact (@concrete_setcode_gas_progress args vm _ cold warm access write reserve Hrevision Hcold Hwarm).
- intros. apply copy_code_read_only.
- exact (@concrete_primitives_preserve_other_balance args vm
(Evmc.Message.t.recipient msg) query minimum Hdifferent).
- intros Hbad. rewrite Hadoption in Hbad. discriminate.
- exact Hgas.
- exact Hcode.
- exact Hinitial.
Qed.
Theorem execution_call_free_balance_floor query minimum msg code state :
Evmc.Message.t.recipient msg ≠ query →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
callFreeBytes code = true → adoptionFreeBytes code = true →
balanceFloor query minimum state →
balanceFloor query minimum (snd (@Host.Instantiate.Vm.execute args msg code state)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hgas Hcode Hadoption Hinitial.
change (balanceFloor query minimum (snd
((@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).(Evmc.Vm.SIG.execute) msg code state))).
unfold Evmc.Instantiate.Vm, Evmc.Instantiate._recursive_modules_Host_Vm.
generalize Evmc.Instantiate.recursive_module_fuel. intros [|fuel].
- exact Hinitial.
- destruct fuel; cbn [recursive_module_unroll fst snd];
apply balance_floor_vm_constructor; assumption.
Qed.
Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Lemma zero_transfer_balance_floor sender recipient amount query minimum s :
Numeric.U256.to_z amount = 0 → balanceFloor query minimum s →
balanceFloor query minimum (snd (@H.transfer_ether (@Host.Instantiate.Host_fargs args)
sender recipient amount s)).
Proof using F Hcold Hwarm cold warm.
intros Hzero [Hinv Hfloor].
destruct (@transfer_balance_effects (@Host.Instantiate.Host_fargs args)
sender recipient amount s Hinv ltac:(lia)) as [Hafter [_ Hbalances]].
split; [exact Hafter|]. specialize (Hbalances query).
rewrite Hzero in Hbalances.
match type of Hbalances with _ = _ + ?effect ⇒
replace effect with 0 in Hbalances by
(repeat match goal with |- context [if ?b then _ else _] ⇒ destruct b end; reflexivity)
end.
rewrite Z.add_0_r in Hbalances.
eapply Z.le_trans; [exact Hfloor|]. rewrite Hbalances. apply Z.le_refl.
Qed.
Lemma system_process_call_balance_floor query minimum msg state :
Evmc.Message.t.recipient msg ≠ query →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
callFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
adoptionFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
balanceFloor query minimum state →
balanceFloor query minimum (snd (@H.process_call (@Host.Instantiate.Host_fargs args) None msg state)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hgas Hzero Hcode Hadoption Hinitial.
set (transfer := if @H.should_transfer (@Host.Instantiate.Host_fargs args) msg
then @H.transfer_ether (@Host.Instantiate.Host_fargs args)
(Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
else fun st ⇒ (true, st)).
assert (Hfloor : balanceFloor query minimum (snd (transfer state))).
{ unfold transfer. destruct (H.should_transfer msg); [|exact Hinitial].
exact (zero_transfer_balance_floor (Evmc.Message.t.sender msg)
(Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
query minimum state Hzero Hinitial). }
assert (Hbody : codeAt (S.t.world_state (snd (transfer state))) (Evmc.Message.t.code_address msg) =
codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)).
{ unfold transfer. destruct (H.should_transfer msg); [|reflexivity].
apply transfer_preserves_code. exact (proj1 (proj1 Hinitial)). }
unfold H.process_call, H.M.
cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile balanceFloor].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
change run with (transfer state); destruct (transfer state) as [ok middle] eqn:Htransfer end.
cbn beta iota zeta delta [snd] in Hfloor, Hbody.
destruct ok; [|exact Hfloor].
cbn -[W.account Precompiles.Make.try_precompile balanceFloor].
match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
destruct selection as [action|] eqn:Hselected end.
- eapply precompile_preserves_balance_floor.
2: exact Hselected.
+ exact Hrevision.
+ exact Hfloor.
- apply execution_call_free_balance_floor.
+ exact Hdifferent.
+ exact Hgas.
+ exact (eq_trans (f_equal callFreeBytes Hbody) Hcode).
+ exact (eq_trans (f_equal adoptionFreeBytes Hbody) Hadoption).
+ exact Hfloor.
Qed.
Theorem execution_system_call_balance_floor query minimum msg state :
Evmc.Message.t.recipient msg ≠ query →
0 ≤ Evmc.Message.t.gas msg < 2^63 →
Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
callFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
adoptionFreeBytes (codeAt (S.t.world_state state) (Evmc.Message.t.code_address msg)) = true →
balanceFloor query minimum state →
balanceFloor query minimum (snd (@E.Host.call F msg state)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hgas Hkind Hzero Hcode Hadoption Hinitial.
pose proof (system_process_call_balance_floor query minimum msg state
Hdifferent Hgas Hzero Hcode Hadoption Hinitial) as Hbody.
change (balanceFloor query minimum (snd (@H.call (@Host.Instantiate.Host_fargs args) msg state))).
unfold H.call, H.call_impl. rewrite Hkind.
cbn -[H.process_call balanceFloor].
match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
destruct run as [result after] end.
cbn beta iota zeta delta [snd] in Hbody.
destruct result as [status gasleft refund output address]; destruct status;
cbn -[balanceFloor]; first [exact Hbody|exact Hinitial].
Qed.
Theorem system_message_nondebit block addr data query :
addr ≠ query →
callFreeBytes (codeAt (B.t.world_state block) addr) = true →
adoptionFreeBytes (codeAt (B.t.world_state block) addr) = true →
balanceInvariant (B.t.world_state block) →
balanceInvariant (B.t.world_state (snd (E.process_system_message block addr data))) ∧
balanceAt (B.t.world_state block) query ≤
balanceAt (B.t.world_state (snd (E.process_system_message block addr data))) query.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hdifferent Hcode Hadoption Hinv.
unfold E.process_system_message.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
- split; [exact Hinv|apply Z.le_refl].
- cbn beta iota zeta.
match goal with |- context [E.Host.call ?msg ?entry] ⇒
pose proof (execution_system_call_balance_floor query
(balanceAt (B.t.world_state block) query) msg entry
Hdifferent ltac:(change (0 ≤ 30000000 < 2^63); lia) eq_refl eq_refl
Hcode Hadoption (conj Hinv (Z.le_refl _))) as Hrun;
destruct (E.Host.call msg entry) as [result after]
end.
exact Hrun.
Qed.
Lemma block_entry_nondebit world block query :
prescribedSystemCode world →
balanceInvariant world →
E.beacon_roots_address ≠ query →
E.history_storage_address ≠ query →
balanceInvariant (B.t.world_state (blockTransactionEntry world block)) ∧
balanceAt world query ≤
balanceAt (B.t.world_state (blockTransactionEntry world block)) query.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros [[Hmap Hbeacon] [_ Hhistory]] Hinv HnotBeacon HnotHistory.
set (activated := Reserve_balance.activate_precompile world).
pose proof (activate_preserves_supply world Hinv) as [HactInv _].
pose proof (activate_preserves_balances world query Hmap) as HactBal.
change (balanceAt activated query = balanceAt world query) in HactBal.
assert (Hactive : stringmaps.WellFormed (W.t.accounts activated))
by (apply account_write_wellformed; exact Hmap).
assert (Hb : codeAt activated E.beacon_roots_address = beaconRootsCode)
by (exact (eq_trans (activation_preserves_code world _ Hmap) Hbeacon)).
assert (Hh : codeAt activated E.history_storage_address = historyStorageCode)
by (exact (eq_trans (activation_preserves_code world _ Hmap) Hhistory)).
unfold blockTransactionEntry. cbn zeta. fold activated.
match goal with |- context [E.process_system_message (B.make activated block) ?addr ?data] ⇒
set (beaconData := data) end.
assert (Hbc : callFreeBytes (codeAt activated E.beacon_roots_address) = true)
by (rewrite Hb; exact beaconRootsCode_call_free).
assert (Hba : adoptionFreeBytes (codeAt activated E.beacon_roots_address) = true)
by (rewrite Hb; exact beacon_roots_no_adoption).
destruct (system_message_code cold warm access write reserve Hrevision Hcold Hwarm
(B.make activated block) E.beacon_roots_address beaconData E.history_storage_address
historyStorageCode Hactive Hh Hbc Hba) as [_ Hhistory'].
destruct (system_message_nondebit (B.make activated block) E.beacon_roots_address
beaconData query HnotBeacon Hbc Hba HactInv) as [HbeaconInv HbeaconBal].
match goal with |- context [E.process_system_message ?beacon E.history_storage_address ?data] ⇒
destruct (system_message_nondebit beacon E.history_storage_address data query
HnotHistory
(eq_rect _ (fun code ⇒ callFreeBytes code = true) historyStorageCode_call_free _ (eq_sym Hhistory'))
(eq_rect _ (fun code ⇒ adoptionFreeBytes code = true) history_storage_no_adoption _ (eq_sym Hhistory'))
HbeaconInv) as [HhistInv HhistBal] end.
split; [exact HhistInv|].
change (B.t.world_state (B.make activated block)) with activated in HbeaconBal.
rewrite HactBal in HbeaconBal.
exact (Z.le_trans _ _ _ HbeaconBal HhistBal).
Qed.
Lemma eligible_not_system_contract snapshot a :
@prescribedSystemCode F snapshot →
eligibleAt snapshot a = true →
E.beacon_roots_address ≠ a ∧ E.history_storage_address ≠ a.
Proof using.
intros [[_ Hbeacon] [_ Hhistory]] Helig.
change (codeAt snapshot E.beacon_roots_address = beaconRootsCode) in Hbeacon.
change (codeAt snapshot E.history_storage_address = historyStorageCode) in Hhistory.
unfold eligibleAt in Helig. apply Bool.negb_true_iff in Helig.
split; intro Heq; subst a.
- assert (Hreg : regularCode (codeAt snapshot E.beacon_roots_address))
by (rewrite Hbeacon; vm_compute; reflexivity).
apply regular_code_contract in Hreg. congruence.
- assert (Hreg : regularCode (codeAt snapshot E.history_storage_address))
by (rewrite Hhistory; vm_compute; reflexivity).
apply regular_code_contract in Hreg. congruence.
Qed.
Lemma system_message_keeps_history state addr data :
W.t.history (B.t.world_state (snd (E.process_system_message state addr data))) =
W.t.history (B.t.world_state state).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
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 ?message ?entry] ⇒
set (msg := message); set (initial := entry) end.
assert (Hgas : (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z)
by (change (0 ≤ 30000000 < 2^63)%Z; lia).
pose proof (host_call_keeps_context cold warm access write reserve
Hrevision Hcold Hwarm msg Hgas initial) as Hcontext.
destruct (E.Host.call msg initial) as [result afterVm].
cbn [snd] in Hcontext. unfold migrationContext, transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounters Hprior Hchain.
exact Hhistory.
Qed.
Lemma block_entry_keeps_history world block :
W.t.history (B.t.world_state (blockTransactionEntry world block)) = W.t.history world.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
unfold blockTransactionEntry. cbn zeta.
rewrite !system_message_keeps_history. reflexivity.
Qed.
Lemma system_message_transactions_processed state addr data :
B.t.transactions_processed (snd (E.process_system_message state addr data)) =
B.t.transactions_processed state.
Proof using.
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 ?entry] ⇒
destruct (E.Host.call msg entry) as [result after] end.
reflexivity.
Qed.
Lemma block_entry_transactions_processed world block :
B.t.transactions_processed (blockTransactionEntry world block) = [].
Proof using.
unfold blockTransactionEntry. cbn zeta.
rewrite !system_message_transactions_processed. reflexivity.
Qed.
Lemma is_smart_contract_code acc1 acc2 :
A.t.code acc1 = A.t.code acc2 →
A.is_smart_contract acc1 = A.is_smart_contract acc2.
Proof using.
destruct acc1, acc2. cbn [A.t.code A.is_smart_contract]. intro H; rewrite H. reflexivity.
Qed.
Lemma block_entry_smart_contract world block query :
prescribedSystemCode world →
A.is_smart_contract (accountAt (B.t.world_state (blockTransactionEntry world block)) query) =
A.is_smart_contract (accountAt world query).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intro Hsystem.
apply is_smart_contract_code.
exact (block_entry_code cold warm access write reserve Hrevision Hcold Hwarm
world block query Hsystem).
Qed.
Lemma block_entry_preserves_supply world block :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header block) →
OCamlString.length (Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32 →
balanceInvariant world →
worldFormat world →
balanceInvariant (B.t.world_state (blockTransactionEntry world block)) ∧
totalBalance (B.t.world_state (blockTransactionEntry world block)) ≤ totalBalance world ∧
worldFormat (B.t.world_state (blockTransactionEntry world block)).
Proof using F Hcold Hhash Hnative Hrevision Hwarm access cold reserve warm write.
intros Hchain Hheader Hroot Hinitial Hformat.
unfold blockTransactionEntry. cbn zeta.
set (initial := B.make (Reserve_balance.activate_precompile world) block).
assert (HinitialFormat : worldFormat (B.t.world_state initial))
by (apply activate_preserves_format; exact Hformat).
assert (HinitialSupply : balanceInvariant (B.t.world_state initial) ∧
totalBalance (B.t.world_state initial) = totalBalance world)
by (apply activate_preserves_supply; exact Hinitial).
match goal with |- context [E.process_system_message initial ?addr ?data] ⇒
assert (Hdata : OCamlString.length data < 2^62) by
(change (OCamlString.length (Block.Header.t.parent_beacon_block_root
(Block.t.header block)) < 2^62); rewrite Hroot; lia);
pose proof (system_message_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative initial addr data Hchain Hheader
Hdata (proj1 HinitialSupply) HinitialFormat) as Hbeacon;
pose proof (@system_message_current_block F initial addr data) as HbeaconBlock;
destruct (E.process_system_message initial addr data) as [beaconResult afterBeacon]
end.
cbn beta iota zeta delta [snd] in Hbeacon, HbeaconBlock |- ×.
destruct Hbeacon as [Hbeacon [HbeaconTotal HbeaconFormat]].
assert (HbeaconHeader : headerWords (Block.t.header (B.t.current_block afterBeacon)))
by (rewrite HbeaconBlock; exact Hheader).
match goal with |- context [E.process_system_message afterBeacon ?addr ?data] ⇒
assert (HdataHash : OCamlString.length data < 2^62) by
(lazymatch data with ?toBytes ?hash ⇒ change (OCamlString.length hash < 2^62) end;
rewrite (block_hash_length Hhash); lia);
pose proof (system_message_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative afterBeacon addr data Hchain HbeaconHeader
HdataHash Hbeacon HbeaconFormat) as Hhistory;
destruct (E.process_system_message afterBeacon addr data) as [historyResult afterHistory]
end.
cbn beta iota zeta delta [snd] in Hhistory |- ×.
destruct Hhistory as [Hhistory [HhistoryTotal HhistoryFormat]].
split; [exact Hhistory|]. split; [lia|exact HhistoryFormat].
Qed.
Theorem block_entry_budget_backing snapshot previous world block senders from until :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header block) →
OCamlString.length (Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32 →
(∀ a, In a senders → a ≠ native) →
prescribedSystemCode snapshot →
storageWords (A.t.storage (accountAt snapshot native)) →
(from ≤ Block.Header.t.number (Block.t.header block))%N →
(Block.Header.t.number (Block.t.header block) ≤ until)%N →
(until < Block.Header.t.number (Block.t.header block) + 3)%N →
BlockBudgetBacking snapshot previous world senders from until →
TransactionBudgetBacking snapshot (capBudgets snapshot (Block.t.header block) previous)
(blockTransactionEntry world block) senders until.
Proof using F Hcold Hhash Hnative Hrevision Hwarm access cold reserve warm write.
intros Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Hfrom HleUntil HltDelay Hbacking.
destruct Hbacking as [Hsys Hbal Hfmt HcfgWf Hnonce Hact Hempty Hcfg Hbudgets Hidle Hstatus].
destruct (block_entry_preserves_supply world block Hchain Hheader Hroot Hbal Hfmt)
as [HentryBal [_ HentryFmt]].
pose proof (block_entry_current world block) as Hcurr.
constructor.
- exact HentryBal.
- exact HentryFmt.
- exact (block_entry_nonce_agreement cold warm access write reserve
Hrevision Hcold Hwarm world block _ _ senders HnotNative Hsys Hnonce).
- eapply activity_same_counters; [apply (block_entry_keeps_counters cold warm access write reserve Hrevision Hcold Hwarm)|exact Hact].
- exact (block_entry_empty_code cold warm access write reserve
Hrevision Hcold Hwarm snapshot _ world block senders Hsys Hempty).
- destruct HcfgWf as [Hmap [Hcode [HstMap HstWords]]].
∃ (A.t.storage (accountAt world native)).
rewrite Hcurr.
refine (conj HstMap (conj HstWords (conj _ _))).
+ intro a. symmetry.
exact (Hcfg (Block.Header.t.number (Block.t.header block)) a ltac:(lia)).
+ exact (block_entry_config cold warm access write reserve Hrevision Hcold Hwarm
world block Hmap Hcode).
- intros a Ha. cbn [capBudgets admissionBudgets]. rewrite Hcurr.
destruct (Hbudgets a Ha) as [HprevNonneg HprevBal].
destruct (Hnonce a Ha) as [Helig _].
destruct (eligible_not_system_contract snapshot a HsnapSystem Helig)
as [HnotBeacon HnotHistory].
destruct (block_entry_nondebit world block a Hsys Hbal HnotBeacon HnotHistory)
as [_ HentryNondebit].
pose proof (reserve_at_nonnegative snapshot (Block.Header.t.number (Block.t.header block))
a HsnapWords) as HresNonneg.
split; [lia|split; [lia|apply Z.le_min_r]].
- intros a Ha HnotAct HsnapEmpty.
destruct (Hidle a Ha HnotAct HsnapEmpty) as [HworldEmpty HworldBal].
destruct (Hnonce a Ha) as [Helig _].
destruct (eligible_not_system_contract snapshot a HsnapSystem Helig)
as [HnotBeacon HnotHistory].
destruct (block_entry_nondebit world block a Hsys Hbal HnotBeacon HnotHistory)
as [_ HentryNondebit].
split.
+ rewrite (block_entry_code cold warm access write reserve Hrevision Hcold Hwarm
world block a Hsys).
exact HworldEmpty.
+ lia.
- intros a Ha. cbn [capBudgets admissionBudgets]. rewrite Hcurr.
pose proof (reserve_at_nonnegative snapshot (Block.Header.t.number (Block.t.header block))
a HsnapWords) as HresNonneg.
destruct (Hstatus a Ha) as [Hzero|[Hsc|Hhist]].
+ left. lia.
+ right. left.
rewrite (block_entry_smart_contract world block a Hsys). exact Hsc.
+ right. right. right.
assert (HhistEq : ∀ t,
sentInHistory (B.t.world_state (blockTransactionEntry world block)) t a =
sentInHistory world t a).
{ intro t. unfold sentInHistory.
rewrite (block_entry_keeps_history world block). reflexivity. }
split.
× rewrite HhistEq. apply Hhist. lia.
× intros target Htarget. rewrite HhistEq. apply Hhist. lia.
Qed.
Lemma withdrawals_smart_contract withdrawals state a :
stringmaps.WellFormed (W.t.accounts (B.t.world_state state)) →
A.is_smart_contract (accountAt (B.t.world_state
(Result.List.fold_left E.process_withdrawal state withdrawals)) a) =
A.is_smart_contract (accountAt (B.t.world_state state) a).
Proof using.
intro Hmap.
assert (Hcode : worldCode a (codeAt (B.t.world_state state) a) (B.t.world_state state))
by (split; [exact Hmap|reflexivity]).
destruct (withdrawals_preserve_code a (codeAt (B.t.world_state state) a)
withdrawals state Hcode) as [_ HafterCode].
apply is_smart_contract_code.
exact HafterCode.
Qed.
Lemma withdrawals_code_at withdrawals state a :
stringmaps.WellFormed (W.t.accounts (B.t.world_state state)) →
codeAt (B.t.world_state (Result.List.fold_left E.process_withdrawal state withdrawals)) a =
codeAt (B.t.world_state state) a.
Proof using.
intro Hmap.
assert (Hcode : worldCode a (codeAt (B.t.world_state state) a) (B.t.world_state state))
by (split; [exact Hmap|reflexivity]).
exact (proj2 (withdrawals_preserve_code a (codeAt (B.t.world_state state) a)
withdrawals state Hcode)).
Qed.
Lemma finalized_block_sent_in_history finalized history block afterTransactions target a :
Block.Header.t.number (Block.t.header finalized) =
Block.Header.t.number (Block.t.header block) →
Block.t.transactions finalized =
OCamlList.map fst (B.t.transactions_processed afterTransactions) →
sentInPriorTransactions afterTransactions a = true →
(Block.Header.t.number (Block.t.header block) < target <
Block.Header.t.number (Block.t.header block) + 3)%N →
OCamlList._exists
(fun b ⇒
Support.UintN.op_lt (Block.Header.t.number (Block.t.header b)) target &&
Support.UintN.op_lt target
(Support.UintN.op_plus (Block.Header.t.number (Block.t.header b))
Reserve_balance.execution_consensus_delay) &&
OCamlList._exists (fun tx ⇒ sentBy tx a) (Block.t.transactions b))
(finalized :: history) = true.
Proof using F Hcold Hwarm cold warm.
intros Hnum Htxs Hsent Hrange.
cbn [OCamlList._exists].
rewrite Hnum, Htxs.
change (Support.UintN.op_lt (Block.Header.t.number (Block.t.header block)) target)
with (N.ltb (Block.Header.t.number (Block.t.header block)) target).
change (Support.UintN.op_lt target
(Support.UintN.op_plus (Block.Header.t.number (Block.t.header block))
Reserve_balance.execution_consensus_delay))
with (N.ltb target (Block.Header.t.number (Block.t.header block) + 3)%N).
assert (Hlt1 : N.ltb (Block.Header.t.number (Block.t.header block)) target = true)
by (apply N.ltb_lt; lia).
assert (Hlt2 : N.ltb target (Block.Header.t.number (Block.t.header block) + 3)%N = true)
by (apply N.ltb_lt; lia).
unfold sentInPriorTransactions in Hsent.
rewrite Hlt1, Hlt2, Hsent. reflexivity.
Qed.
Theorem checked_block_no_insufficient_balance verify snapshot previous next
world block senders from until details fee :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header block) →
OCamlString.length (Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32 →
(∀ a, In a senders → a ≠ native) →
prescribedSystemCode snapshot →
storageWords (A.t.storage (accountAt snapshot native)) →
Forall (fun tx ⇒
transactionValueAndDataBounds tx ∧
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx) ∧
signaturesAvoidAddress tx native ∧
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders)) (Block.t.transactions block) →
(E.validate_block world block = inl tt →
transactionsAvoidSenderCreation senders (blockTransactionEntry world block)
(Block.t.transactions block)) →
(from ≤ Block.Header.t.number (Block.t.header block))%N →
(Block.Header.t.number (Block.t.header block) ≤ until)%N →
(until < Block.Header.t.number (Block.t.header block) + 3)%N →
BlockBudgetBacking snapshot previous world senders from until →
checkTransactions snapshot (Block.t.header block)
(capBudgets snapshot (Block.t.header block) previous)
(Block.t.transactions block) = Some next →
E.process_block verify world block = inr (Execution.Error.Invalid_transaction details) →
Execution.Error.t.Invalid_transaction.reason details ≠
Execution.Error.Insufficient_balance fee.
Proof using F Hcold Hhash Hnative Hrevision Hwarm U8 UB UI0 access cold reserve warm write.
intros Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Htxs Hcreation
Hfrom HleUntil HltDelay Hbacking Hcheck Hrun.
pose proof (block_entry_budget_backing snapshot previous world block senders from until
Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Hfrom HleUntil HltDelay Hbacking)
as HentryBacking.
pose proof (block_entry_current world block) as Hcurr.
pose proof (checked_transactions_budget_backing cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative snapshot
(capBudgets snapshot (Block.t.header block) previous) next
(blockTransactionEntry world block) (Block.t.transactions block) senders until
Hchain ltac:(rewrite Hcurr; exact Hheader) HnotNative Htxs
(Hcreation (consensusvalidation.transaction_error_has_valid_block_header verify world block details Hrun))
ltac:(cbv beta; rewrite Hcurr; exact HleUntil)
ltac:(cbv beta; rewrite Hcurr; exact HltDelay)
HentryBacking ltac:(rewrite Hcurr; exact Hcheck)) as HtxResult.
rewrite (block_transaction_error_from_entry cold warm access write reserve
Hrevision verify world block details Hrun) in HtxResult.
exact (HtxResult fee).
Qed.
Theorem checked_block_budget_backing verify snapshot previous next
world block post senders from until :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header block) →
OCamlString.length (Block.Header.t.parent_beacon_block_root (Block.t.header block)) = 32 →
(∀ a, In a senders → a ≠ native) →
prescribedSystemCode snapshot →
storageWords (A.t.storage (accountAt snapshot native)) →
Forall (fun tx ⇒
transactionValueAndDataBounds tx ∧
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx) ∧
signaturesAvoidAddress tx native ∧
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders)) (Block.t.transactions block) →
Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (Chain.Ethereum.Withdrawal.t.amount wd) < 2^64)
(Block.t.withdrawals block) →
let credits := Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0
(Block.t.withdrawals block) in
totalBalance world + credits < 2^256 →
(E.validate_block world block = inl tt →
transactionsAvoidSenderCreation senders (blockTransactionEntry world block)
(Block.t.transactions block)) →
(from ≤ Block.Header.t.number (Block.t.header block))%N →
(Block.Header.t.number (Block.t.header block) ≤ until)%N →
(until < Block.Header.t.number (Block.t.header block) + 3)%N →
BlockBudgetBacking snapshot previous world senders from until →
checkTransactions snapshot (Block.t.header block)
(capBudgets snapshot (Block.t.header block) previous)
(Block.t.transactions block) = Some next →
E.process_block verify world block = inl post →
BlockBudgetBacking snapshot next post senders
(Block.Header.t.number (Block.t.header block) + 1)%N until ∧
totalBalance post ≤ totalBalance world + credits.
Proof using F Hcold Hhash Hnative Hrevision Hwarm U8 UB UI0 access cold reserve warm write.
intros Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Htxs Hwithdrawals
credits Hsupply Hcreation Hfrom HleUntil HltDelay Hbacking Hcheck Hrun.
pose proof (processed_block_valid_header verify world block post Hrun) as HvalidHeader.
pose proof (block_entry_budget_backing snapshot previous world block senders from until
Hchain Hheader Hroot HnotNative HsnapSystem HsnapWords Hfrom HleUntil HltDelay Hbacking)
as HentryBacking.
pose proof (block_entry_current world block) as Hcurr.
assert (HtxNumeric : Forall (fun tx ⇒ transactionValueAndDataBounds tx ∧
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx)) (Block.t.transactions block)).
{ eapply Forall_impl; [|exact Htxs]. intros tx [Hv [Hw _]]. exact (conj Hv Hw). }
assert (HtxWidth : Forall (fun tx ⇒
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx)) (Block.t.transactions block)).
{ eapply Forall_impl; [|exact HtxNumeric]. intros tx [_ Hw]. exact Hw. }
assert (HtxSigs : Forall (fun tx ⇒ signaturesAvoidAddress tx native)
(Block.t.transactions block)).
{ eapply Forall_impl; [|exact Htxs]. intros tx [_ [_ [Hs _]]]. exact Hs. }
destruct (process_block_preserves_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative verify world block post
Hchain Hheader Hroot (block_backing_balance _ _ _ _ _ _ Hbacking)
(block_backing_format _ _ _ _ _ _ Hbacking) HtxNumeric Hwithdrawals Hsupply Hrun)
as [HpostBal [HpostTotal HpostFmt]].
pose proof (block_preserves_system_code cold warm access write reserve
verify world block post Hrevision Hcold Hwarm
(block_backing_system _ _ _ _ _ _ Hbacking) Hrun) as HpostSys.
destruct (block_configured_window cold warm access write reserve
Hrevision Hcold Hwarm verify world block post
(block_backing_config_wf _ _ _ _ _ _ Hbacking) HtxSigs Hrun)
as [HpostCfgWf HpostCfgWindow].
pose proof (block_activity_agreement cold warm access write reserve
Hrevision Hcold Hwarm verify snapshot
(capBudgets snapshot (Block.t.header block) previous) next world block post until
(block_backing_activity _ _ _ _ _ _ Hbacking) HltDelay Hcheck Hrun) as HpostAct.
pose proof (block_nonce_agreement cold warm access write reserve
Hrevision Hcold Hwarm verify snapshot
(capBudgets snapshot (Block.t.header block) previous) next world block post senders
HnotNative HtxWidth
(block_backing_system _ _ _ _ _ _ Hbacking)
(block_backing_nonce _ _ _ _ _ _ Hbacking)
(Hcreation HvalidHeader) Hcheck Hrun) as HpostNonce.
pose proof (block_empty_code_agreement cold warm access write reserve
Hrevision Hcold Hwarm verify snapshot
(capBudgets snapshot (Block.t.header block) previous) next world block post senders
HnotNative HtxWidth
(block_backing_system _ _ _ _ _ _ Hbacking)
(block_backing_nonce _ _ _ _ _ _ Hbacking)
(block_backing_empty_code _ _ _ _ _ _ Hbacking)
(Hcreation HvalidHeader) Hcheck Hrun) as HpostEmpty.
pose proof (checked_transactions_budget_backing cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative snapshot
(capBudgets snapshot (Block.t.header block) previous) next
(blockTransactionEntry world block) (Block.t.transactions block) senders until
Hchain ltac:(rewrite Hcurr; exact Hheader) HnotNative Htxs
(Hcreation HvalidHeader)
ltac:(cbv beta; rewrite Hcurr; exact HleUntil)
ltac:(cbv beta; rewrite Hcurr; exact HltDelay)
HentryBacking ltac:(rewrite Hcurr; exact Hcheck)) as HtxResult.
pose proof Hrun as HrunUnfold.
unfold E.process_block in HrunUnfold. rewrite Hrevision, HvalidHeader in HrunUnfold.
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 HrunUnfold.
cbn beta iota zeta in HrunUnfold.
match type of HrunUnfold with context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
change entry with (blockTransactionEntry world block) in HrunUnfold end.
destruct (Result.List.fold_leftM E.process_transaction
(blockTransactionEntry world block) (Block.t.transactions block))
as [afterTransactions|err] eqn:HtxsRun; [|discriminate HrunUnfold].
destruct HtxResult as [HafterBacking [HafterCurr [HafterHist HafterProc]]].
rewrite (block_entry_transactions_processed world block), app_nil_l in HafterProc.
rewrite (block_entry_keeps_history world block) in HafterHist.
rewrite Hcurr in HafterCurr.
cbn beta iota zeta in HrunUnfold.
match type of HrunUnfold with context [E.validate_block ?w ?b] ⇒
destruct (E.validate_block w b) as [[]|error] end; [|discriminate HrunUnfold].
assert (HpostEq : post = W.t.with_history
(B.finalize_current_block (Result.List.fold_left E.process_withdrawal
afterTransactions (Block.t.withdrawals block)) :: W.t.history world)
(B.t.world_state (Result.List.fold_left E.process_withdrawal
afterTransactions (Block.t.withdrawals block)))).
{ destruct verify.
- match type of HrunUnfold with context [match ?chk with inl _ ⇒ _ | inr _ ⇒ _ end] ⇒
destruct chk as [[]|error] end; [|discriminate HrunUnfold].
injection HrunUnfold as <-. reflexivity.
- injection HrunUnfold as <-. reflexivity. }
assert (HremSupply : totalBalance (B.t.world_state afterTransactions) + credits < 2^256).
{ destruct (block_entry_preserves_supply world block Hchain Hheader Hroot
(block_backing_balance _ _ _ _ _ _ Hbacking)
(block_backing_format _ _ _ _ _ _ Hbacking)) as [HeBal [HeTot HeFmt]].
destruct (transactions_preserve_supply cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative (blockTransactionEntry world block)
(Block.t.transactions block) afterTransactions Hchain
ltac:(rewrite Hcurr; exact Hheader) HtxNumeric HeBal HeFmt HtxsRun)
as [_ [HtxTot _]].
lia. }
destruct (@withdrawals_preserve_supply_bound F afterTransactions
(Block.t.withdrawals block) (tx_backing_balance _ _ _ _ _ HafterBacking)
Hwithdrawals HremSupply) as [_ [_ HwdNondebit]].
set (afterWithdrawals := Result.List.fold_left E.process_withdrawal
afterTransactions (Block.t.withdrawals block)) in ×.
assert (HwdCurr : B.t.current_block afterWithdrawals = block).
{ unfold afterWithdrawals. rewrite withdrawals_current_block. exact HafterCurr. }
assert (HpostBalanceAt : ∀ a, balanceAt post a = balanceAt (B.t.world_state afterWithdrawals) a).
{ intro a. rewrite HpostEq. reflexivity. }
assert (HpostAccountAt : ∀ a, accountAt post a = accountAt (B.t.world_state afterWithdrawals) a).
{ intro a. rewrite HpostEq. reflexivity. }
split; [|exact HpostTotal].
constructor; try assumption.
- intros target a Htarget.
etransitivity.
+ apply HpostCfgWindow. lia.
+ apply (block_backing_config _ _ _ _ _ _ Hbacking). lia.
- intros a Ha.
destruct (tx_backing_budgets _ _ _ _ _ HafterBacking a Ha) as [Hnonneg [HbalAfter _]].
pose proof (HwdNondebit a) as HwdA.
change (balanceAt (B.t.world_state afterTransactions) a ≤
balanceAt (B.t.world_state afterWithdrawals) a) in HwdA.
rewrite HpostBalanceAt. lia.
- intros a Ha HnotAct HsnapEmpty.
destruct (tx_backing_idle _ _ _ _ _ HafterBacking a Ha HnotAct HsnapEmpty)
as [HcodeAfter HbalAfter].
pose proof (HwdNondebit a) as HwdA.
change (balanceAt (B.t.world_state afterTransactions) a ≤
balanceAt (B.t.world_state afterWithdrawals) a) in HwdA.
rewrite HpostAccountAt, HpostBalanceAt.
split; [|lia].
unfold afterWithdrawals.
rewrite (withdrawals_code_at (Block.t.withdrawals block) afterTransactions a
(proj1 (tx_backing_balance _ _ _ _ _ HafterBacking))).
exact HcodeAfter.
- intros a Ha.
destruct (tx_backing_status _ _ _ _ _ HafterBacking a Ha)
as [Hzero|[Hsc|[Hprior|[HhistNow HhistFuture]]]].
+ left. exact Hzero.
+ right. left. rewrite HpostAccountAt. unfold afterWithdrawals.
rewrite (withdrawals_smart_contract (Block.t.withdrawals block) afterTransactions a
(proj1 (tx_backing_balance _ _ _ _ _ HafterBacking))).
exact Hsc.
+ right. right. intros target Htarget.
rewrite HpostEq. unfold sentInHistory. cbn [W.t.history W.t.with_history].
apply (finalized_block_sent_in_history
(B.finalize_current_block afterWithdrawals) (W.t.history world)
block afterTransactions target a).
× change (Block.Header.t.number (Block.t.header (B.t.current_block afterWithdrawals)) =
Block.Header.t.number (Block.t.header block)).
rewrite HwdCurr. reflexivity.
× change (Block.t.transactions (B.t.current_block afterWithdrawals) =
OCamlList.map fst (B.t.transactions_processed afterTransactions)).
rewrite HwdCurr, HafterProc. reflexivity.
× exact Hprior.
× lia.
+ right. right. intros target Htarget.
assert (HprevHist : sentInHistory (B.t.world_state afterTransactions) target a = true).
{ apply HhistFuture. rewrite HafterCurr. lia. }
unfold sentInHistory in HprevHist |- ×.
rewrite HafterHist in HprevHist.
rewrite HpostEq. cbn [W.t.history W.t.with_history OCamlList._exists].
rewrite HprevHist, Bool.orb_true_r. reflexivity.
Qed.
End Execution.
Print Assumptions block_entry_budget_backing.
Print Assumptions checked_block_no_insufficient_balance.
Print Assumptions checked_block_budget_backing.