Library QedkConcrete.emptyinghistory

Concrete emptying history

Execution records activity by storing the next eligible block, rather than the last sender/authorization block used in the consensus proof. These updates touch neither balances nor reserve configuration storage. Only successfully applied authorities are passed to this operation.
From Stdlib Require Import List NArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.stringmaps.
Set Default Goal Selector "!".

Module W := MonadVM.State.WorldState.
Module S := MonadVM.State.TransactionState.
Module E := MonadVM.Execution.Make.

Section Counters.
Context {UN : Basics.Unreachable MonadVM.Support.UintN.t}.
Local Abbreviation readCounter := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.next_emptying_transaction_block_for addr)).
Local Abbreviation writeCounter := (fun world addr n ⇒
  State.op_pointcaretlparenrparenltminus world (@W.next_emptying_transaction_block_for addr) n).

Lemma counter_write_wellformed world addr n :
  WellFormed (W.t.next_emptying_transaction_block world) →
  WellFormed (W.t.next_emptying_transaction_block (writeCounter world addr n)).
Proof. intro Hwf. apply update_wellformed. exact Hwf. Qed.

Lemma counter_read_after_write world addr n query :
  WellFormed (W.t.next_emptying_transaction_block world) →
  readCounter (writeCounter world addr n) query =
    if String.eqb addr query then n else readCounter world query.
Proof.
  intro Hwf.
  match goal with |- _ = ?rhs ⇒
  change ((match OCamlMap.Make.find_opt stringOrder query
    (OCamlMap.Make.update stringOrder addr (fun _ ⇒ Some n)
      (W.t.next_emptying_transaction_block world)) with
    | Some value ⇒ value | None ⇒ 0%N end) = rhs) end.
  rewrite find_update by exact Hwf. destruct (String.eqb addr query); reflexivity.
Qed.

Lemma counter_writes world addresses n :
  WellFormed (W.t.next_emptying_transaction_block world) →
  let after := OCamlList.fold_left (fun w addr ⇒ writeCounter w addr n) world addresses in
  WellFormed (W.t.next_emptying_transaction_block after) ∧
  W.t.accounts after = W.t.accounts world ∧
  W.t.history after = W.t.history world ∧
  ∀ query, readCounter after query =
    if existsb (fun addr ⇒ String.eqb addr query) addresses then n else readCounter world query.
Proof.
  revert world. induction addresses as [|addr addresses IH]; intros world Hwf.
  { repeat split; try exact Hwf; reflexivity. }
  cbn [OCamlList.fold_left].
  specialize (IH (writeCounter world addr n) (counter_write_wellformed world addr n Hwf)).
  destruct IH as [Hwf' [Haccounts [Hhistory Hcounter]]].
  split; [exact Hwf'|]. split; [exact Haccounts|]. split; [exact Hhistory|].
  intro query. rewrite Hcounter. cbn beta. rewrite counter_read_after_write by exact Hwf.
  cbn [existsb]. destruct (String.eqb addr query),
    (existsb (fun a ⇒ String.eqb a query) addresses); reflexivity.
Qed.

Account preservation does not depend on the counter map being well formed.
Applying an authorization for the sender prevents emptying in this transaction, even if that authorization removes delegation. The counter records the activity independently of the resulting code.
Lemma bump_blocks_emptying s addresses :
  WellFormed (W.t.next_emptying_transaction_block (S.t.world_state s)) →
  List.In (S.t.tx_origin s) addresses →
  fst (@Reserve_balance.is_emptying_tx
    (@E.bump_emptying_transaction_counters F addresses s)) = false.
Proof.
  intros Hwf Hin.
  pose proof (bump_counters_exact s addresses Hwf) as [_ [_ [_ Hcounter]]].
  specialize (Hcounter (S.t.tx_origin s)).
  assert (Hmember : existsb (fun addr ⇒ String.eqb addr (S.t.tx_origin s)) addresses = true).
  { apply existsb_exists. ∃ (S.t.tx_origin s). split; [exact Hin|apply String.eqb_refl]. }
  rewrite Hmember in Hcounter.
  unfold Reserve_balance.is_emptying_tx.
  match goal with |- _ = ?rhs ⇒
  change ((negb (@Delegation.is_valid_delegation (Ethereum.Account.t.code (State.op_pointcaretlparenrparen
      (@E.bump_emptying_transaction_counters F addresses s)
      (@S.account None (S.t.tx_origin s))))) &&
    N.leb (State.op_pointcaretlparenrparen
      (S.t.world_state (@E.bump_emptying_transaction_counters F addresses s))
      (@W.next_emptying_transaction_block_for (S.t.tx_origin s)))
      (Ethereum.Block.Header.t.number (Ethereum.Block.t.header (S.t.current_block s)))) = rhs) end.
  rewrite Hcounter.
  assert (Hfuture : N.leb
    (Ethereum.Block.Header.t.number (Ethereum.Block.t.header (S.t.current_block s)) +
      @Reserve_balance.execution_consensus_delay )
    (Ethereum.Block.Header.t.number (Ethereum.Block.t.header (S.t.current_block s))) = false).
  { apply N.leb_gt. change (@Reserve_balance.execution_consensus_delay) with 3%N. lia. }
  rewrite Hfuture. apply Bool.andb_false_r.
Qed.

End Execution.

Print Assumptions bump_counters_exact.
Print Assumptions bump_blocks_emptying.