Library QedkConcrete.transactionfinalize
Account bounds survive transaction finalization
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.State QedkConcrete.stringmaps QedkConcrete.worldaccounts
QedkConcrete.supply QedkConcrete.credits.
Set Default Goal Selector "!".
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.
Module U := Numeric.U256.
Section Finalization.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Lemma delete_accounts_preserves_other block addresses query :
WellFormed (W.t.accounts (B.t.world_state block)) →
¬ List.In query addresses →
let after := OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(@B.account_opt None addr) None) block (OCamlSeq.of_list addresses) in
WellFormed (W.t.accounts (B.t.world_state after)) ∧
readAccount (B.t.world_state after) query = readAccount (B.t.world_state block) query.
Proof.
revert block. induction addresses as [|addr addresses IH]; intros block Hwf Habsent.
- split; [exact Hwf|reflexivity].
- cbn [OCamlSeq.of_list OCamlSeq.fold_left].
set (next := State.op_pointcaretlparenrparenltminus block
(@B.account_opt None addr) None).
assert (Hnext : WellFormed (W.t.accounts (B.t.world_state next))).
{ apply remove_wellformed. exact Hwf. }
destruct (IH next Hnext ltac:(intro Hin; apply Habsent; right; exact Hin)) as [Hafter Hread].
split; [exact Hafter|]. etransitivity; [exact Hread|].
match goal with |- _ = ?rhs ⇒
change ((match OCamlMap.Make.find_opt stringOrder query
(OCamlMap.Make.remove stringOrder addr (W.t.accounts (B.t.world_state block))) with
| Some account ⇒ account | None ⇒ @A.empty end) = rhs)
end.
rewrite find_remove_other by (first [exact Hwf|intro Heq; subst; apply Habsent; left; reflexivity]).
reflexivity.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.State QedkConcrete.stringmaps QedkConcrete.worldaccounts
QedkConcrete.supply QedkConcrete.credits.
Set Default Goal Selector "!".
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.
Module U := Numeric.U256.
Section Finalization.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable U.t}.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (@W.account None addr)).
Lemma delete_accounts_preserves_other block addresses query :
WellFormed (W.t.accounts (B.t.world_state block)) →
¬ List.In query addresses →
let after := OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(@B.account_opt None addr) None) block (OCamlSeq.of_list addresses) in
WellFormed (W.t.accounts (B.t.world_state after)) ∧
readAccount (B.t.world_state after) query = readAccount (B.t.world_state block) query.
Proof.
revert block. induction addresses as [|addr addresses IH]; intros block Hwf Habsent.
- split; [exact Hwf|reflexivity].
- cbn [OCamlSeq.of_list OCamlSeq.fold_left].
set (next := State.op_pointcaretlparenrparenltminus block
(@B.account_opt None addr) None).
assert (Hnext : WellFormed (W.t.accounts (B.t.world_state next))).
{ apply remove_wellformed. exact Hwf. }
destruct (IH next Hnext ltac:(intro Hin; apply Habsent; right; exact Hin)) as [Hafter Hread].
split; [exact Hafter|]. etransitivity; [exact Hread|].
match goal with |- _ = ?rhs ⇒
change ((match OCamlMap.Make.find_opt stringOrder query
(OCamlMap.Make.remove stringOrder addr (W.t.accounts (B.t.world_state block))) with
| Some account ⇒ account | None ⇒ @A.empty end) = rhs)
end.
rewrite find_remove_other by (first [exact Hwf|intro Heq; subst; apply Habsent; left; reflexivity]).
reflexivity.
Qed.
The credit cannot wrap when backed by the aggregate budget. Deleting
unrelated selfdestructed accounts then leaves the lower bound intact,
including when the protected account is itself the fee beneficiary.
Lemma credit_and_finalize_nondecreasing block beneficiary amount addresses query :
balanceInvariant (B.t.world_state block) →
(0 ≤ U.to_z amount)%Z →
(totalBalance (B.t.world_state block) + U.to_z amount < 2^256)%Z →
¬ List.In query addresses →
let credited := State.op_pointcaretdollarlparenrparenltminus block
(Lens.Infix.op_pipeminusminus (@B.account None beneficiary) A.balance)
(fun balance ⇒ @U.op_plus balance amount) in
let after := OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(@B.account_opt None addr) None) credited (OCamlSeq.of_list addresses) in
(U.to_z (A.t.balance (readAccount (B.t.world_state block) query)) ≤
U.to_z (A.t.balance (readAccount (B.t.world_state after) query)))%Z.
Proof.
intros Hinv Hamount Hbudget Habsent credited.
assert (HcreditInv : balanceInvariant (B.t.world_state credited)).
{ apply credit_preserves_invariant; [exact (proj1 Hinv)|exact (proj1 (proj2 Hinv))|exact Hamount|exact Hbudget]. }
pose proof (@credit_nondecreasing_from_supply (B.t.world_state block)
beneficiary amount query (proj1 Hinv) (proj1 (proj2 Hinv)) Hamount Hbudget) as Hcredit.
destruct (delete_accounts_preserves_other credited addresses query (proj1 HcreditInv) Habsent)
as [_ Hread].
cbn zeta. rewrite Hread. exact Hcredit.
Qed.
End Finalization.
Print Assumptions credit_and_finalize_nondecreasing.
balanceInvariant (B.t.world_state block) →
(0 ≤ U.to_z amount)%Z →
(totalBalance (B.t.world_state block) + U.to_z amount < 2^256)%Z →
¬ List.In query addresses →
let credited := State.op_pointcaretdollarlparenrparenltminus block
(Lens.Infix.op_pipeminusminus (@B.account None beneficiary) A.balance)
(fun balance ⇒ @U.op_plus balance amount) in
let after := OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(@B.account_opt None addr) None) credited (OCamlSeq.of_list addresses) in
(U.to_z (A.t.balance (readAccount (B.t.world_state block) query)) ≤
U.to_z (A.t.balance (readAccount (B.t.world_state after) query)))%Z.
Proof.
intros Hinv Hamount Hbudget Habsent credited.
assert (HcreditInv : balanceInvariant (B.t.world_state credited)).
{ apply credit_preserves_invariant; [exact (proj1 Hinv)|exact (proj1 (proj2 Hinv))|exact Hamount|exact Hbudget]. }
pose proof (@credit_nondecreasing_from_supply (B.t.world_state block)
beneficiary amount query (proj1 Hinv) (proj1 (proj2 Hinv)) Hamount Hbudget) as Hcredit.
destruct (delete_accounts_preserves_other credited addresses query (proj1 HcreditInv) Habsent)
as [_ Hread].
cbn zeta. rewrite Hread. exact Hcredit.
Qed.
End Finalization.
Print Assumptions credit_and_finalize_nondecreasing.