Library QedkConcrete.executionreserve
Reserve lower bounds for the actual top-level Host call
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution MonadVM.Vm QedkConcrete.hostcode
QedkConcrete.hostrollback QedkConcrete.reservecheck QedkConcrete.executioncreation
QedkConcrete.executioncontext QedkConcrete.hostframe QedkConcrete.contexthostinstance.
Set Default Goal Selector "!".
Module E := Execution.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.
Module U := Numeric.U256.
Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation callFromEoa := (@E.Host.call_from_eoa F).
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation dip := (@Reserve_balance.dipped_into_reserve).
Local Abbreviation protected := (@reservecheck.protected).
Local Abbreviation nonsenderBound := (@reservecheck.nonsender_reserve_bound).
Local Abbreviation isSC := (@A.is_smart_contract).
Lemma protected_same_context before after addr original :
transactionContext after = transactionContext before →
protected after addr original = protected before addr original.
Proof.
intro Hcontext. unfold transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounter.
unfold reservecheck.protected. rewrite Hinitial, Hblock. reflexivity.
Qed.
Lemma call_rollback_or_reserve tx msg initial :
let after := snd (callFromEoa tx msg initial) in
S.t.world_state after = S.t.world_state initial ∨
fst (dip ((@E.Params F).(Execution.Make_Params_signature.revision)) after) = false.
Proof.
cbn zeta. unfold E.Host.call_from_eoa.
cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
Host.Make.functor Host.Make.Make_result.call_from_eoa Host.Make.call_from_eoa].
exact (@transaction_world_rollback_or_reserve (@Host.Instantiate.Host_fargs args) tx msg initial).
Qed.
Lemma contract_test_same_code before after :
A.t.code after = A.t.code before → isSC after = isSC before.
Proof. destruct before, after. cbn. intro Hcode. subst. reflexivity. Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution MonadVM.Vm QedkConcrete.hostcode
QedkConcrete.hostrollback QedkConcrete.reservecheck QedkConcrete.executioncreation
QedkConcrete.executioncontext QedkConcrete.hostframe QedkConcrete.contexthostinstance.
Set Default Goal Selector "!".
Module E := Execution.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.
Module U := Numeric.U256.
Section Execution.
Context {F : E.FArgs}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation callFromEoa := (@E.Host.call_from_eoa F).
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation dip := (@Reserve_balance.dipped_into_reserve).
Local Abbreviation protected := (@reservecheck.protected).
Local Abbreviation nonsenderBound := (@reservecheck.nonsender_reserve_bound).
Local Abbreviation isSC := (@A.is_smart_contract).
Lemma protected_same_context before after addr original :
transactionContext after = transactionContext before →
protected after addr original = protected before addr original.
Proof.
intro Hcontext. unfold transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounter.
unfold reservecheck.protected. rewrite Hinitial, Hblock. reflexivity.
Qed.
Lemma call_rollback_or_reserve tx msg initial :
let after := snd (callFromEoa tx msg initial) in
S.t.world_state after = S.t.world_state initial ∨
fst (dip ((@E.Params F).(Execution.Make_Params_signature.revision)) after) = false.
Proof.
cbn zeta. unfold E.Host.call_from_eoa.
cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
Host.Make.functor Host.Make.Make_result.call_from_eoa Host.Make.call_from_eoa].
exact (@transaction_world_rollback_or_reserve (@Host.Instantiate.Host_fargs args) tx msg initial).
Qed.
Lemma contract_test_same_code before after :
A.t.code after = A.t.code before → isSC after = isSC before.
Proof. destruct before, after. cbn. intro Hcode. subst. reflexivity. Qed.
Reversion restores the entry balance; success retains the protected
amount. The minimum combines these alternatives without assuming that
the VM succeeded. At transaction entry a nonsender's balance is unchanged
by fee debiting and authorization processing, so that minimum simplifies.
Theorem call_nonsender_reserve_lower_bound tx msg initial addr original :
(@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
Addr.Map.find_opt addr (W.t.accounts (S.t.initial_world_state initial)) = Some original →
isSC (readAccount (S.t.world_state initial) addr) = false →
Addr.op_eq addr MonadVM.Staking.staking_address = false →
¬ List.In addr (S.t.self_destruct initial) →
Addr.op_eq addr (S.t.tx_origin initial) = false →
let after := snd (callFromEoa tx msg initial) in
¬ List.In addr (S.t.accounts_created_in_current_transaction after) →
(Z.min (U.to_z (A.t.balance (readAccount (S.t.world_state initial) addr)))
(U.to_z (protected initial addr original)) ≤
U.to_z (A.t.balance (readAccount (S.t.world_state after) addr)))%Z.
Proof.
intros Hrevision Hgas Hwf Hfind Hsc Hstaking Hdes Hsender. cbn zeta. intro Hcreated.
pose proof (@execution_call_from_eoa_preserves_context F
tx msg Hrevision Hgas initial) as Hcontext.
pose proof (protected_same_context initial _ addr original Hcontext) as Hprotected.
destruct (@execution_call_noncreated_code F
tx msg initial addr Hrevision Hgas Hwf Hdes Hcreated)
as [Hcode Hdestroyed].
destruct (call_rollback_or_reserve tx msg initial) as [Hrollback|Hcheck].
- rewrite Hrollback. apply Z.le_min_l.
- rewrite Hrevision in Hcheck.
unfold transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounter.
assert (Hpostsc : isSC (readAccount (S.t.world_state (snd (callFromEoa tx msg initial))) addr) = false).
{ etransitivity; [apply contract_test_same_code; exact Hcode|exact Hsc]. }
assert (Hbound : (U.to_z (protected (snd (callFromEoa tx msg initial)) addr original) ≤
U.to_z (A.t.balance (readAccount (S.t.world_state (snd (callFromEoa tx msg initial))) addr)))%Z).
{ apply nonsenderBound.
- exact Hcheck.
- apply address_find_member. etransitivity.
+ exact (f_equal (fun world ⇒ Addr.Map.find_opt addr (W.t.accounts world)) Hinitial).
+ exact Hfind.
- exact Hdestroyed.
- exact Hpostsc.
- exact Hstaking.
- etransitivity; [exact (f_equal (fun a ⇒ Addr.op_eq addr a) Horigin)|exact Hsender]. }
pose proof (f_equal U.to_z Hprotected) as Hamount.
eapply Z.le_trans; [apply Z.le_min_r|].
eapply Z.le_trans; [apply Z.eq_le_incl; symmetry; exact Hamount|exact Hbound].
Qed.
Local Abbreviation senderBound := (@reservecheck.sender_reserve_bound).
Local Abbreviation emptying := (@Reserve_balance.is_emptying_tx).
(@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
Addr.Map.find_opt addr (W.t.accounts (S.t.initial_world_state initial)) = Some original →
isSC (readAccount (S.t.world_state initial) addr) = false →
Addr.op_eq addr MonadVM.Staking.staking_address = false →
¬ List.In addr (S.t.self_destruct initial) →
Addr.op_eq addr (S.t.tx_origin initial) = false →
let after := snd (callFromEoa tx msg initial) in
¬ List.In addr (S.t.accounts_created_in_current_transaction after) →
(Z.min (U.to_z (A.t.balance (readAccount (S.t.world_state initial) addr)))
(U.to_z (protected initial addr original)) ≤
U.to_z (A.t.balance (readAccount (S.t.world_state after) addr)))%Z.
Proof.
intros Hrevision Hgas Hwf Hfind Hsc Hstaking Hdes Hsender. cbn zeta. intro Hcreated.
pose proof (@execution_call_from_eoa_preserves_context F
tx msg Hrevision Hgas initial) as Hcontext.
pose proof (protected_same_context initial _ addr original Hcontext) as Hprotected.
destruct (@execution_call_noncreated_code F
tx msg initial addr Hrevision Hgas Hwf Hdes Hcreated)
as [Hcode Hdestroyed].
destruct (call_rollback_or_reserve tx msg initial) as [Hrollback|Hcheck].
- rewrite Hrollback. apply Z.le_min_l.
- rewrite Hrevision in Hcheck.
unfold transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounter.
assert (Hpostsc : isSC (readAccount (S.t.world_state (snd (callFromEoa tx msg initial))) addr) = false).
{ etransitivity; [apply contract_test_same_code; exact Hcode|exact Hsc]. }
assert (Hbound : (U.to_z (protected (snd (callFromEoa tx msg initial)) addr original) ≤
U.to_z (A.t.balance (readAccount (S.t.world_state (snd (callFromEoa tx msg initial))) addr)))%Z).
{ apply nonsenderBound.
- exact Hcheck.
- apply address_find_member. etransitivity.
+ exact (f_equal (fun world ⇒ Addr.Map.find_opt addr (W.t.accounts world)) Hinitial).
+ exact Hfind.
- exact Hdestroyed.
- exact Hpostsc.
- exact Hstaking.
- etransitivity; [exact (f_equal (fun a ⇒ Addr.op_eq addr a) Horigin)|exact Hsender]. }
pose proof (f_equal U.to_z Hprotected) as Hamount.
eapply Z.le_trans; [apply Z.le_min_r|].
eapply Z.le_trans; [apply Z.eq_le_incl; symmetry; exact Hamount|exact Hbound].
Qed.
Local Abbreviation senderBound := (@reservecheck.sender_reserve_bound).
Local Abbreviation emptying := (@Reserve_balance.is_emptying_tx).
For an ordinary sender, success may spend the protected amount only by
the charged fee. Reversion restores the VM-entry balance, which already
includes the upfront fee debit. This is not a claim that every admitted
transaction succeeds or passes its nonce/signature checks.
Theorem call_sender_reserve_lower_bound tx msg initial original :
(@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
let addr := S.t.tx_origin initial in
Addr.Map.find_opt addr (W.t.accounts (S.t.initial_world_state initial)) = Some original →
isSC (readAccount (S.t.world_state initial) addr) = false →
Addr.op_eq addr MonadVM.Staking.staking_address = false →
¬ List.In addr (S.t.self_destruct initial) →
fst (emptying initial) = false →
(0 ≤ U.to_z (protected initial addr original) < 2^256)%Z →
let after := snd (callFromEoa tx msg initial) in
¬ List.In addr (S.t.accounts_created_in_current_transaction after) →
(Z.min (U.to_z (A.t.balance (readAccount (S.t.world_state initial) addr)))
(U.to_z (protected initial addr original) - Z.of_N (S.t.tx_gas_limit initial × S.t.tx_gas_price initial)) ≤
U.to_z (A.t.balance (readAccount (S.t.world_state after) addr)))%Z.
Proof.
intros Hrevision Hgas Hwf. cbn zeta. intros Hfind Hsc Hstaking Hdes Hempty Hrange Hcreated.
pose proof (@execution_call_from_eoa_preserves_context F
tx msg Hrevision Hgas initial) as Hcontext.
pose proof (protected_same_context initial _ (S.t.tx_origin initial) original Hcontext) as Hprotected.
destruct (@execution_call_noncreated_code F
tx msg initial (S.t.tx_origin initial) Hrevision Hgas Hwf Hdes Hcreated)
as [Hcode Hdestroyed].
pose proof (@execution_call_preserves_emptying F
tx msg initial Hrevision Hgas Hwf Hcreated) as HemptyAfter.
pose proof (call_rollback_or_reserve tx msg initial) as Hchecked.
destruct (callFromEoa tx msg initial) as [result after] eqn:Hrun.
cbn beta iota zeta delta [snd] in Hcontext, Hprotected, Hcode, Hdestroyed, HemptyAfter, Hchecked |- ×.
destruct Hchecked as [Hrollback|Hcheck].
- rewrite Hrollback. apply Z.le_min_l.
- rewrite Hrevision in Hcheck.
unfold transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounter.
assert (Hpostsc : isSC (readAccount (S.t.world_state after) (S.t.tx_origin initial)) = false).
{ etransitivity; [apply contract_test_same_code; exact Hcode|exact Hsc]. }
pose proof (senderBound after original Hcheck) as Hbound.
cbn zeta in Hbound.
rewrite Horigin in Hbound.
assert (Hmember : List.In (S.t.tx_origin initial, original)
(W.t.accounts (S.t.initial_world_state after))).
{ apply address_find_member. rewrite Hinitial. exact Hfind. }
assert (Hpostempty : fst (emptying after) = false) by (etransitivity; eassumption).
assert (Hpostrange : (0 ≤ U.to_z (protected after (S.t.tx_origin initial) original) < 2^256)%Z).
{ rewrite Hprotected. exact Hrange. }
specialize (Hbound Hmember Hdestroyed Hpostsc Hstaking Hpostempty Hpostrange).
rewrite Hprotected, Hlimit, Hprice in Hbound. cbv beta in Hbound.
eapply Z.le_trans; [apply Z.le_min_r|exact (proj2 Hbound)].
Qed.
End Execution.
Print Assumptions call_nonsender_reserve_lower_bound.
Print Assumptions call_sender_reserve_lower_bound.
(@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
stringmaps.WellFormed (W.t.accounts (S.t.world_state initial)) →
let addr := S.t.tx_origin initial in
Addr.Map.find_opt addr (W.t.accounts (S.t.initial_world_state initial)) = Some original →
isSC (readAccount (S.t.world_state initial) addr) = false →
Addr.op_eq addr MonadVM.Staking.staking_address = false →
¬ List.In addr (S.t.self_destruct initial) →
fst (emptying initial) = false →
(0 ≤ U.to_z (protected initial addr original) < 2^256)%Z →
let after := snd (callFromEoa tx msg initial) in
¬ List.In addr (S.t.accounts_created_in_current_transaction after) →
(Z.min (U.to_z (A.t.balance (readAccount (S.t.world_state initial) addr)))
(U.to_z (protected initial addr original) - Z.of_N (S.t.tx_gas_limit initial × S.t.tx_gas_price initial)) ≤
U.to_z (A.t.balance (readAccount (S.t.world_state after) addr)))%Z.
Proof.
intros Hrevision Hgas Hwf. cbn zeta. intros Hfind Hsc Hstaking Hdes Hempty Hrange Hcreated.
pose proof (@execution_call_from_eoa_preserves_context F
tx msg Hrevision Hgas initial) as Hcontext.
pose proof (protected_same_context initial _ (S.t.tx_origin initial) original Hcontext) as Hprotected.
destruct (@execution_call_noncreated_code F
tx msg initial (S.t.tx_origin initial) Hrevision Hgas Hwf Hdes Hcreated)
as [Hcode Hdestroyed].
pose proof (@execution_call_preserves_emptying F
tx msg initial Hrevision Hgas Hwf Hcreated) as HemptyAfter.
pose proof (call_rollback_or_reserve tx msg initial) as Hchecked.
destruct (callFromEoa tx msg initial) as [result after] eqn:Hrun.
cbn beta iota zeta delta [snd] in Hcontext, Hprotected, Hcode, Hdestroyed, HemptyAfter, Hchecked |- ×.
destruct Hchecked as [Hrollback|Hcheck].
- rewrite Hrollback. apply Z.le_min_l.
- rewrite Hrevision in Hcheck.
unfold transactionContext in Hcontext.
injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounter.
assert (Hpostsc : isSC (readAccount (S.t.world_state after) (S.t.tx_origin initial)) = false).
{ etransitivity; [apply contract_test_same_code; exact Hcode|exact Hsc]. }
pose proof (senderBound after original Hcheck) as Hbound.
cbn zeta in Hbound.
rewrite Horigin in Hbound.
assert (Hmember : List.In (S.t.tx_origin initial, original)
(W.t.accounts (S.t.initial_world_state after))).
{ apply address_find_member. rewrite Hinitial. exact Hfind. }
assert (Hpostempty : fst (emptying after) = false) by (etransitivity; eassumption).
assert (Hpostrange : (0 ≤ U.to_z (protected after (S.t.tx_origin initial) original) < 2^256)%Z).
{ rewrite Hprotected. exact Hrange. }
specialize (Hbound Hmember Hdestroyed Hpostsc Hstaking Hpostempty Hpostrange).
rewrite Hprotected, Hlimit, Hprice in Hbound. cbv beta in Hbound.
eapply Z.le_trans; [apply Z.le_min_r|exact (proj2 Hbound)].
Qed.
End Execution.
Print Assumptions call_nonsender_reserve_lower_bound.
Print Assumptions call_sender_reserve_lower_bound.