Library SetcodeConcrete.noncefinalize
Final fees and deletion cannot increase nonces
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.State.
Require Import QedkConcrete.worldaccounts QedkConcrete.stringmaps
SetcodeConcrete.nonceeffects SetcodeConcrete.noncebudget.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module B := State.BlockState.
Lemma balance_write_nonce world keep addr balance query :
WellFormed (W.t.accounts world) →
nonceAt (State.op_pointcaretlparenrparenltminus world (W.account (Some keep) addr)
(A.t.with_balance balance (State.op_pointcaretlparenrparen world (W.account None addr)))) query =
nonceAt world query.
Proof.
intro Hmap. rewrite nonce_after_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq;
[apply String.eqb_eq in Heq; subst query|]; reflexivity.
Qed.
Lemma delete_accounts_nonce_bound block addresses query :
WellFormed (W.t.accounts (B.t.world_state block)) →
0 ≤ nonceAt (B.t.world_state block) query →
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)) ∧
0 ≤ nonceAt (B.t.world_state after) query ≤ nonceAt (B.t.world_state block) query.
Proof.
revert block. induction addresses as [|addr addresses IH]; intros block Hmap Hnonnegative.
- cbn [OCamlSeq.of_list OCamlSeq.fold_left]. split; [exact Hmap|lia].
- cbn [OCamlSeq.of_list OCamlSeq.fold_left].
set (next := State.op_pointcaretlparenrparenltminus block (B.account_opt None addr) None).
assert (HnextMap : WellFormed (W.t.accounts (B.t.world_state next))) by
(apply remove_wellformed; exact Hmap).
assert (Hnext : 0 ≤ nonceAt (B.t.world_state next) query ≤ nonceAt (B.t.world_state block) query).
{ change (0 ≤ A.t.nonce (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) ≤ nonceAt (B.t.world_state block) query).
destruct (String.string_dec addr query) as [Heq|Hneq].
- subst addr. rewrite find_remove_same by exact Hmap. change (0 ≤ 0 ≤ nonceAt (B.t.world_state block) query). lia.
- rewrite find_remove_other by assumption. change (0 ≤ nonceAt (B.t.world_state block) query ≤ nonceAt (B.t.world_state block) query). lia. }
destruct (IH next HnextMap (proj1 Hnext)) as [Hafter Hbound].
split; [exact Hafter|].
match type of Hbound with 0 ≤ ?n ≤ _ ⇒
change (0 ≤ n ≤ nonceAt (B.t.world_state block) query) end. lia.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.State.
Require Import QedkConcrete.worldaccounts QedkConcrete.stringmaps
SetcodeConcrete.nonceeffects SetcodeConcrete.noncebudget.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module B := State.BlockState.
Lemma balance_write_nonce world keep addr balance query :
WellFormed (W.t.accounts world) →
nonceAt (State.op_pointcaretlparenrparenltminus world (W.account (Some keep) addr)
(A.t.with_balance balance (State.op_pointcaretlparenrparen world (W.account None addr)))) query =
nonceAt world query.
Proof.
intro Hmap. rewrite nonce_after_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq;
[apply String.eqb_eq in Heq; subst query|]; reflexivity.
Qed.
Lemma delete_accounts_nonce_bound block addresses query :
WellFormed (W.t.accounts (B.t.world_state block)) →
0 ≤ nonceAt (B.t.world_state block) query →
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)) ∧
0 ≤ nonceAt (B.t.world_state after) query ≤ nonceAt (B.t.world_state block) query.
Proof.
revert block. induction addresses as [|addr addresses IH]; intros block Hmap Hnonnegative.
- cbn [OCamlSeq.of_list OCamlSeq.fold_left]. split; [exact Hmap|lia].
- cbn [OCamlSeq.of_list OCamlSeq.fold_left].
set (next := State.op_pointcaretlparenrparenltminus block (B.account_opt None addr) None).
assert (HnextMap : WellFormed (W.t.accounts (B.t.world_state next))) by
(apply remove_wellformed; exact Hmap).
assert (Hnext : 0 ≤ nonceAt (B.t.world_state next) query ≤ nonceAt (B.t.world_state block) query).
{ change (0 ≤ A.t.nonce (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) ≤ nonceAt (B.t.world_state block) query).
destruct (String.string_dec addr query) as [Heq|Hneq].
- subst addr. rewrite find_remove_same by exact Hmap. change (0 ≤ 0 ≤ nonceAt (B.t.world_state block) query). lia.
- rewrite find_remove_other by assumption. change (0 ≤ nonceAt (B.t.world_state block) query ≤ nonceAt (B.t.world_state block) query). lia. }
destruct (IH next HnextMap (proj1 Hnext)) as [Hafter Hbound].
split; [exact Hafter|].
match type of Hbound with 0 ≤ ?n ≤ _ ⇒
change (0 ≤ n ≤ nonceAt (B.t.world_state block) query) end. lia.
Qed.