Library SetcodeConcrete.activityagreement

Public activity agrees with the concrete emptying counters

For any height at most until, an address recorded since the snapshot is ineligible for emptying. Every unrecorded address retains its snapshot counter. Both senders and recovered authorities count, even when an authorization was stale or its transaction's VM execution reverted.
From Stdlib Require Import List String NArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import SetcodeConcrete.transactionactivity SetcodeConcrete.consensuschecks
  SetcodeConcrete.nonceagreement SetcodeConcrete.supplysequence.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module B := State.BlockState.
Module W := State.WorldState.
Module T := Chain.Ethereum.Transaction.
Module Block := Chain.Ethereum.Block.
Definition emptyingAfter world a :=
  State.op_pointcaretlparenrparen world (W.next_emptying_transaction_block_for a).
Record ActivityAgreement snapshot activity world until : Prop := {
  activity_map : stringmaps.WellFormed (W.t.next_emptying_transaction_block world);
  activity_recent : ∀ a, In a activity → (until < emptyingAfter world a)%N;
  activity_absent : ∀ a, ¬ In a activity → emptyingAfter world a = emptyingAfter snapshot a
}.

Lemma initial_activity_agreement snapshot until :
  stringmaps.WellFormed (W.t.next_emptying_transaction_block snapshot) →
  ActivityAgreement snapshot [] snapshot until.
Proof. intro Hmap. constructor; [exact Hmap|intros a Hin; inversion Hin|reflexivity]. Qed.

Lemma activity_counter_eligibility snapshot activity world until a target :
  ActivityAgreement snapshot activity world until → (target ≤ until)%N →
  ((emptyingAfter world a ≤ target)%N ↔
    (emptyingAfter snapshot a ≤ target)%N ∧ ¬ In a activity).
Proof.
  intros [Hmap Hrecent Habsent] Htime.
  destruct (in_dec String.string_dec a activity) as [Hin|Hnot].
  - specialize (Hrecent a Hin). split; [lia|tauto].
  - rewrite Habsent by exact Hnot. tauto.
Qed.

Lemma existsb_address addresses a :
  existsb (fun addr ⇒ String.eqb addr a) addresses = true ↔ In a addresses.
Proof.
  rewrite existsb_exists. split.
  - intros [addr [Hin Heq]]. apply String.eqb_eq in Heq. subst addr. exact Hin.
  - intro Hin. ∃ a. split; [exact Hin|apply String.eqb_refl].
Qed.
Section Execution.
Context {F : E.FArgs}.
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).

Theorem transaction_activity_agreement snapshot activity until state tx validation post :
  ActivityAgreement snapshot activity (B.t.world_state state) until →
  (until < Block.Header.t.number (Block.t.header (B.t.current_block state)) + 3)%N →
  E.validate_transaction state tx = inl validation →
  E.process_transaction state tx = inl post →
  ActivityAgreement snapshot
    (E.transaction_validation.sender validation :: recoveredAuthorities tx ++ activity)
    (B.t.world_state post) until.
Proof.
  intros Hbefore Htime Hvalidation Hrun.
  destruct Hbefore as [Hmap Hrecent Habsent].
  destruct (process_transaction_activity cold warm access write reserve Hrevision Hcold Hwarm
    state tx validation post Hmap Hvalidation Hrun) as [_ [Hafter Hcounters]].
  change (∀ a, emptyingAfter (B.t.world_state post) a =
    if existsb (fun addr ⇒ String.eqb addr a)
      (E.transaction_validation.sender validation :: recoveredAuthorities tx)
    then (Block.Header.t.number (Block.t.header (B.t.current_block state)) + 3)%N
    else emptyingAfter (B.t.world_state state) a) in Hcounters.
  constructor; [exact Hafter| |].
  - intros a Hin. change (In a ((E.transaction_validation.sender validation :: recoveredAuthorities tx) ++ activity)) in Hin.
    apply in_app_iff in Hin as [Hnow|Hpast].
    + rewrite (Hcounters a). rewrite (proj2 (existsb_address _ _) Hnow). exact Htime.
    + rewrite (Hcounters a).
      destruct (existsb (fun addr ⇒ String.eqb addr a)
        (E.transaction_validation.sender validation :: recoveredAuthorities tx));
        [exact Htime|exact (Hrecent a Hpast)].
  - intros a Hnot. change (¬ In a ((E.transaction_validation.sender validation :: recoveredAuthorities tx) ++ activity)) in Hnot.
    rewrite (Hcounters a).
    destruct (existsb (fun addr ⇒ String.eqb addr a)
      (E.transaction_validation.sender validation :: recoveredAuthorities tx)) eqn:Hnow.
    + exfalso. apply Hnot, in_app_iff. left. apply existsb_address, Hnow.
    + apply Habsent. intro Hin. apply Hnot, in_app_iff. right. exact Hin.
Qed.

Theorem checked_transaction_activity_agreement snapshot previous next until state tx post :
  ActivityAgreement snapshot (admissionActivity previous) (B.t.world_state state) until →
  (until < Block.Header.t.number (Block.t.header (B.t.current_block state)) + 3)%N →
  checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
  E.process_transaction state tx = inl post →
  ActivityAgreement snapshot (admissionActivity next) (B.t.world_state post) until.
Proof.
  intros Hbefore Htime Hcheck Hrun.
  assert (Hv : ∃ validation, E.validate_transaction state tx = inl validation).
  { unfold E.process_transaction in Hrun. destruct (E.validate_transaction state tx) as [v|error];
      [eexists; reflexivity|discriminate]. }
  destruct Hv as [validation Hv].
  pose proof (validated_sender _ _ _ Hv) as Hsender.
  unfold checkTransaction in Hcheck. rewrite Hsender in Hcheck.
  destruct (Gas.tx_effective_gas_price _ tx); [|discriminate].
  match type of Hcheck with context [if ?test then _ else _] ⇒ destruct test end; [|discriminate].
  cbn zeta in Hcheck.
  match type of Hcheck with context [if ?test then _ else _] ⇒ destruct test end; [|discriminate].
  injection Hcheck as <-.
  exact (transaction_activity_agreement snapshot (admissionActivity previous) until
    state tx validation post Hbefore Htime Hv Hrun).
Qed.

Theorem transactions_activity_agreement snapshot previous next until state txs post :
  ActivityAgreement snapshot (admissionActivity previous) (B.t.world_state state) until →
  (until < Block.Header.t.number (Block.t.header (B.t.current_block state)) + 3)%N →
  checkTransactions snapshot (Block.t.header (B.t.current_block state)) previous txs = Some next →
  Result.List.fold_leftM E.process_transaction state txs = inl post →
  ActivityAgreement snapshot (admissionActivity next) (B.t.world_state post) until.
Proof.
  revert previous state. induction txs as [|tx rest IH];
    intros previous state Hbefore Htime Hcheck Hrun.
  - cbn [checkTransactions] in Hcheck. injection Hcheck as <-.
    injection Hrun as <-. exact Hbefore.
  - cbn [checkTransactions] in Hcheck.
    destruct (checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx)
      as [middle|] eqn:Htx; [|discriminate].
    change (Result.op_letdollar (E.process_transaction state tx)
      (fun after ⇒ Result.List.fold_leftM E.process_transaction after rest) = inl post) in Hrun.
    destruct (E.process_transaction state tx) as [after|error] eqn:Hexec; [|discriminate].
    apply (IH middle after).
    + exact (checked_transaction_activity_agreement snapshot previous middle until state tx after
        Hbefore Htime Htx Hexec).
    + rewrite (process_transaction_current_block state tx after Hexec). exact Htime.
    + rewrite (process_transaction_current_block state tx after Hexec). exact Hcheck.
    + exact Hrun.
Qed.
End Execution.
Print Assumptions transactions_activity_agreement.