Library SetcodeConcrete.transactionactivity

Recovered-authority activity in complete SETCODE transactions

The actual executor records the sender and every recovered authority, not just applied authorizations. VM rollback, code adoption, the migrated-sender failed-receipt path and finalization do not erase those activity updates. This is needed to compare public emptying checks with the native execution check.
From Stdlib Require Import List NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.emptyinghistory QedkConcrete.stringmaps
  QedkConcrete.validation QedkConcrete.hostframe.
Require Import SetcodeConcrete.transactionentry SetcodeConcrete.contextframe
  SetcodeConcrete.executioncontext SetcodeConcrete.consensuschecks.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module T := Chain.Ethereum.Transaction.

Lemma native_recovered_authorities tx :
  Result.List.filter_map T.Authorization.authority (T.authorization_list tx) =
  recoveredAuthorities tx.
Proof.
  unfold recoveredAuthorities. change (@Result.List.filter_map) with (@OCamlList.filter_map).
  induction (T.authorization_list tx) as [|auth rest IH]; [reflexivity|].
  cbn [OCamlList.filter_map Stdlib.Lists.List.fold_right].
  destruct (T.Authorization.authority auth); now rewrite IH.
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).
Local Abbreviation readCounter := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (W.next_emptying_transaction_block_for addr)).

Lemma delete_accounts_history block addresses :
  W.t.history (B.t.world_state (OCamlSeq.fold_left
    (fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
      (B.account_opt None addr) None) block addresses)) =
    W.t.history (B.t.world_state block).
Proof.
  revert block. induction addresses as [|addr addresses IH]; intro block;
    [reflexivity|cbn [OCamlSeq.fold_left]; rewrite IH; reflexivity].
Qed.

Lemma delete_accounts_counters block addresses :
  W.t.next_emptying_transaction_block (B.t.world_state (OCamlSeq.fold_left
    (fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
      (B.account_opt None addr) None) block addresses)) =
    W.t.next_emptying_transaction_block (B.t.world_state block).
Proof.
  revert block. induction addresses as [|addr addresses IH]; intro block;
    [reflexivity|cbn [OCamlSeq.fold_left]; rewrite IH; reflexivity].
Qed.

block and post are the actual states before and after tx. For each address, return its old next-emptying height unless it is the sender or a recovered authority; those addresses get current block height + 3. A successful VM result is not required.
Theorem process_transaction_activity block tx validation post :
  WellFormed (W.t.next_emptying_transaction_block (B.t.world_state block)) →
  E.validate_transaction block tx = inl validation →
  E.process_transaction block tx = inl post →
  W.t.history (B.t.world_state post) = W.t.history (B.t.world_state block) ∧
  WellFormed (W.t.next_emptying_transaction_block (B.t.world_state post)) ∧
  ∀ query, readCounter (B.t.world_state post) query =
    if existsb (fun addr ⇒ String.eqb addr query)
      (E.transaction_validation.sender validation :: recoveredAuthorities tx)
    then (Chain.Ethereum.Block.Header.t.number
      (Chain.Ethereum.Block.t.header (B.t.current_block block)) +
      Reserve_balance.execution_consensus_delay)%N
    else readCounter (B.t.world_state block) query.
Proof.
  intros Hwf Hvalidation Hrun.
  rewrite <- native_recovered_authorities.
  pose proof (transaction_prefix_context block tx validation) as Hprefix.
  unfold E.process_transaction in Hrun. rewrite Hvalidation in Hrun.
  destruct validation as [sender fee price intrinsic].
  change (@Result.op_letdollar) with
    (fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
      match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
  cbn beta iota zeta delta [E.transaction_validation.sender E.transaction_validation.total_fee
    E.transaction_validation.intrinsic_gas E.transaction_validation.effective_gas_price] in Hrun |- ×.
  match type of Hprefix with context [transactionPrefix block tx ?v] ⇒
    match type of Hrun with context [Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
      change (Result.List.fold_left f start (T.authorization_list tx)) with (transactionPrefix block tx v) in Hrun;
      destruct (transactionPrefix block tx v) as [applied prepared] eqn:Hprepared
    end
  end.
  cbn beta iota zeta delta [fst snd] in Hprefix, Hrun |- ×.
  unfold migrationContext, transactionContext in Hprefix.
  injection Hprefix as Hinitial Hblock Horigin Hgas Hprice Hhistory Hcounters Hprior Hchain.
  assert (HpreparedWf : WellFormed (W.t.next_emptying_transaction_block (S.t.world_state prepared))).
  { rewrite Hcounters. exact Hwf. }
  set (activity := Result.List.filter_map T.Authorization.authority (T.authorization_list tx)) in ×.
  assert (Henabled : Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true).
  { rewrite Hrevision. reflexivity. }
  rewrite Henabled in Hrun. cbn beta iota zeta delta [andb] in Hrun.
  pose proof (@bump_counters_exact F prepared activity HpreparedWf)
    as [HauthWf [_ [HauthHistory HauthCounters]]].
  match type of Hrun with context [E.Host.call_from_eoa tx ?message ?initial] ⇒
    set (beforeVm := initial) in ×
  end.
  match type of Hrun with context [E.Host.call_from_eoa tx ?message beforeVm] ⇒
    set (msg := message) in ×
  end.
  pose proof (validated_message_gas block tx _ (S.t.world_state beforeVm) Hvalidation) as Hmsg.
  change (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z in Hmsg.
  pose proof (@transaction_call_preserves_migration_context F
    cold warm access write reserve Hrevision Hcold Hwarm tx msg Hmsg beforeVm) as Hvm.
  destruct (E.Host.call_from_eoa tx msg beforeVm) as [result afterVm] eqn:Hcall.
  cbn [snd] in Hvm. unfold migrationContext, transactionContext in Hvm.
  injection Hvm as Hvminitial Hvmblock Hvmorigin Hvmgas Hvmprice Hvmhistory Hvmcounters Hvmprior Hvmchain.
  match type of Hrun with context [if Chain.Ethereum.Account.is_smart_contract ?account then _ else _] ⇒
    destruct (Chain.Ethereum.Account.is_smart_contract account)
  end.
  all: cbn beta iota zeta in Hrun.
  all: try (destruct (Evmc.Message.CallKind.is_create (Evmc.Message.t.kind msg)); cbn beta iota zeta in Hrun).
  all: match type of Hrun with context [E.bump_emptying_transaction_counters (?who :: nil) ?state] ⇒
    set (finished := state) in × end.
  all: assert (HfinishedBlock : S.t.current_block finished = S.t.current_block prepared) by
    (unfold finished; first [exact Hvmblock|reflexivity]).
  all: assert (HfinishedHistory : W.t.history (S.t.world_state finished) =
    W.t.history (S.t.world_state prepared)) by
    (unfold finished; etransitivity; [first [exact Hvmhistory|reflexivity]|exact HauthHistory]).
  all: assert (HfinishedCounters : W.t.next_emptying_transaction_block (S.t.world_state finished) =
    W.t.next_emptying_transaction_block (S.t.world_state (E.bump_emptying_transaction_counters activity prepared))) by
    (unfold finished; first [exact Hvmcounters|reflexivity]).
  all: assert (HfinishedWf : WellFormed (W.t.next_emptying_transaction_block (S.t.world_state finished))) by
    (rewrite HfinishedCounters; exact HauthWf).
  all: pose proof (@bump_counters_exact F finished [sender] HfinishedWf)
    as [HfinalWf [_ [HfinalHistory HfinalCounters]]].
  all: change (@Result._return) with (fun (A B : Set) (value : A) ⇒ @inl A B value) in Hrun.
  all: injection Hrun as <-.
  all: cbn beta iota zeta delta [B.t.with_transactions_processed B.t.with_transaction_frames B.t.with_gas_used B.t.world_state].
  all: change (@Result.Seq.fold_left) with (@OCamlSeq.fold_left).
  all: split.
  all: try (rewrite delete_accounts_history; change
    (W.t.history (S.t.world_state (E.bump_emptying_transaction_counters [sender] finished)) =
      W.t.history (B.t.world_state block)); rewrite HfinalHistory, HfinishedHistory; exact Hhistory).
  all: split; [rewrite delete_accounts_counters; exact HfinalWf|].
  all: intro query.
  all: match goal with |- State.op_pointcaretlparenrparen ?world _ = ?rhs ⇒
    change ((match OCamlMap.Make.find_opt stringOrder query
      (W.t.next_emptying_transaction_block world) with
      | Some value ⇒ value | None ⇒ 0%N end) = rhs)
  end.
  all: rewrite delete_accounts_counters.
  all: match goal with |- _ = ?rhs ⇒
    change (readCounter (S.t.world_state (E.bump_emptying_transaction_counters [sender] finished)) query = rhs)
  end.
  all: cbv beta; rewrite HfinalCounters, HfinishedBlock, Hblock; cbn [existsb E.transaction_validation.sender].
  all: destruct (String.eqb sender query) eqn:Hsender; [reflexivity|]; cbn [orb].
  all: match goal with |- State.op_pointcaretlparenrparen ?world _ = ?rhs ⇒
    change ((match OCamlMap.Make.find_opt stringOrder query
      (W.t.next_emptying_transaction_block world) with
      | Some value ⇒ value | None ⇒ 0%N end) = rhs)
  end.
  all: rewrite HfinishedCounters.
  all: match goal with |- _ = ?rhs ⇒
    change (readCounter (S.t.world_state (E.bump_emptying_transaction_counters activity prepared)) query = rhs)
  end.
  all: cbv beta; rewrite HauthCounters, Hblock.
  all: destruct (existsb (fun addr ⇒ String.eqb addr query) activity); [reflexivity|].
  all: change ((match OCamlMap.Make.find_opt stringOrder query
    (W.t.next_emptying_transaction_block (S.t.world_state prepared)) with
    | Some value ⇒ value | None ⇒ 0%N end) = readCounter (B.t.world_state block) query).
  all: rewrite Hcounters; reflexivity.
Qed.
End Execution.
Print Assumptions process_transaction_activity.