Library SetcodeConcrete.migratednonce

Nonce bound for an entire migrated-sender transaction

This includes the sender update, authorization processing, beneficiary payment, and finalization of the actual generated transaction processor. The migrated-sender path does not run the VM. The general VM path and whole-window theorem remain separate obligations.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution
  QedkConcrete.worldaccounts SetcodeConcrete.nonceeffects.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module B := State.BlockState.
Module S := State.TransactionState.
Module W := State.WorldState.
Module T := Chain.Ethereum.Transaction.
Module A := Chain.Ethereum.Account.

Section Migrated.
Context {F : E.FArgs}.

Theorem migrated_transaction_nonce_bound state tx validation :
  let sender := E.transaction_validation.sender validation in
  Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true →
  E.validate_transaction state tx = inl validation →
  A.is_smart_contract (State.op_pointcaretlparenrparen state (B.account None sender)) = true →
  stringmaps.WellFormed (W.t.accounts (B.t.world_state state)) →
  0 ≤ noncebudget.nonceAt (B.t.world_state state) sender < 2^64-1 →
  0 ≤ T.nonce tx < 2^64-1 →
  ∃ post,
    E.process_transaction state tx = inl post ∧
    stringmaps.WellFormed (W.t.accounts (B.t.world_state post)) ∧
    0 ≤ noncebudget.nonceAt (B.t.world_state post) sender ≤
      Z.max (noncebudget.nonceAt (B.t.world_state state) sender) (T.nonce tx) +
      1 + Z.of_nat (Datatypes.length (T.authorization_list tx)).
Proof.
  cbn zeta. intros Henabled Hvalidation Hcode Hwf Ha Hp.
  unfold E.process_transaction. rewrite Hvalidation.
  destruct validation as [sender fee price intrinsic].
  cbn [E.transaction_validation.sender] in Hcode, Ha.
  change (@Result.op_letdollar) with
    (fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
      match first with inl value ⇒ next value | inr error ⇒ inr error end).
  cbn beta iota zeta in Hcode |- ×.
  rewrite Henabled, Hcode. cbn [andb].
  match goal with |- context [Result.List.fold_left ?step ([], ?initial) ?auths] ⇒
    set (prepared := initial) end.
  assert (HpreparedWorld : S.t.world_state prepared =
    State.op_pointcaretlparenrparenltminus (B.t.world_state state) (W.account (Some false) sender)
      (A.t.with_nonce
        (Setcode_rules.advance_sender_nonce true (noncebudget.nonceAt (B.t.world_state state) sender) (T.nonce tx))
        (A.t.with_balance
          (Numeric.U256.op_minus
            (A.t.balance (State.op_pointcaretlparenrparen (B.t.world_state state) (W.account None sender))) fee)
          (State.op_pointcaretlparenrparen (B.t.world_state state) (W.account None sender))))).
  { reflexivity. }
  assert (HpreparedMap : stringmaps.WellFormed (W.t.accounts (S.t.world_state prepared))).
  { rewrite HpreparedWorld. apply account_write_wellformed. exact Hwf. }
  assert (HpreparedNonce : noncebudget.nonceAt (S.t.world_state prepared) sender =
      Z.max (noncebudget.nonceAt (B.t.world_state state) sender) (T.nonce tx) + 1).
  { rewrite HpreparedWorld, nonce_after_write by exact Hwf.
    rewrite String.eqb_refl. cbn [A.t.with_nonce A.t.nonce].
    unfold Setcode_rules.advance_sender_nonce.
    rewrite rules.u64_max, rules.u64_increment; [reflexivity|lia]. }
  pose proof (authorizations_nonce_bound (T.authorization_list tx) prepared [] sender
    HpreparedMap ltac:(rewrite HpreparedNonce; lia)) as HauthBound.
  pose proof (authorizations_selfdestruct (T.authorization_list tx) prepared []) as Hself.
  change (S.t.self_destruct prepared) with (@nil Chain.Ethereum.Address.t) in Hself.
  match goal with |- context [Result.List.fold_left ?step ([], prepared) ?auths] ⇒
    change (Result.List.fold_left step ([], prepared) auths)
      with (processAuthorizations prepared [] auths) end.
  destruct (processAuthorizations prepared [] (T.authorization_list tx))
    as [authorities authorized] eqn:Hauth.
  cbn beta iota zeta delta [fst snd] in HauthBound, Hself |- ×.
  destruct HauthBound as [HauthorizedMap HauthorizedNonce].
  rewrite HpreparedNonce in HauthorizedNonce.
  match goal with |- context [Evmc.Message.CallKind.is_create ?kind] ⇒
    destruct (Evmc.Message.CallKind.is_create kind) end.
  all: cbn beta iota zeta.
  all: eexists; split; [reflexivity|].
  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 E.transaction_validation.sender].
  all: match goal with |- context [E.bump_emptying_transaction_counters [?address] ?before] ⇒
    set (final := E.bump_emptying_transaction_counters [address] before) end.
  all: assert (HfinalAccounts : W.t.accounts (S.t.world_state final) =
      W.t.accounts (S.t.world_state authorized)) by
    (unfold final; rewrite bump_counters_accounts;
     change (W.t.accounts (S.t.world_state (E.bump_emptying_transaction_counters
       (Result.List.filter_map T.Authorization.authority (T.authorization_list tx)) authorized)) =
       W.t.accounts (S.t.world_state authorized)); apply bump_counters_accounts).
  all: assert (HfinalMap : stringmaps.WellFormed (W.t.accounts (S.t.world_state final))) by
    (rewrite HfinalAccounts; exact HauthorizedMap).
  all: match goal with |- context [Result.Seq.fold_left ?step ?initial ?addresses] ⇒
    assert (Haddresses : addresses = OCamlSeq.empty) by
      (change (Chain.Ethereum.Address._Set.(OCamlSet.S.to_seq)
        (S.t.self_destruct authorized) = OCamlSeq.empty); rewrite Hself; reflexivity);
    rewrite Haddresses;
    change (Result.Seq.fold_left step initial OCamlSeq.empty) with initial
  end.
  all: split; [apply account_write_wellformed; exact HfinalMap|].
  all: match goal with |- context [State.op_pointcaretdollarlparenrparenltminus _
      (Lens.Infix.op_pipeminusminus (B.account None ?beneficiary) _) ?update] ⇒
    change (0 ≤ noncebudget.nonceAt
      (State.op_pointcaretlparenrparenltminus (S.t.world_state final)
        (W.account (Some false) beneficiary)
        (A.t.with_balance
          (update (A.t.balance (State.op_pointcaretlparenrparen (S.t.world_state final)
            (W.account None beneficiary))))
          (State.op_pointcaretlparenrparen (S.t.world_state final) (W.account None beneficiary)))) sender ≤
      Z.max (noncebudget.nonceAt (B.t.world_state state) sender) (T.nonce tx) + 1 +
        Z.of_nat (Datatypes.length (T.authorization_list tx)))
  end.
  all: rewrite nonce_after_write by exact HfinalMap.
  all: match goal with |- context [if String.eqb ?beneficiary ?query then _ else _] ⇒
    destruct (String.eqb beneficiary query) eqn:Heq
  end.
  all: try (apply String.eqb_eq in Heq; rewrite Heq).
  all: cbn [A.t.with_balance A.t.nonce].
  all: unfold noncebudget.nonceAt, State.op_pointcaretlparenrparen, W.account, W.account_opt.
  all: cbn -[Chain.Ethereum.Address.Map.find_opt final].
  all: rewrite HfinalAccounts; exact HauthorizedNonce.
Qed.
End Migrated.

Print Assumptions migrated_transaction_nonce_bound.