Library SetcodeConcrete.transactionnonceprediction

Public nonce prediction at transaction VM entry

For an account that has not migrated, the actual sender/authorization prefix agrees with the public nonce calculation. Other accounts may have migrated, and their predictions need not match execution.
From Stdlib Require Import List String ZArith Bool.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.authorization QedkConcrete.authorizationnoncecore
  QedkConcrete.accountcode QedkConcrete.accountnonces QedkConcrete.worldaccounts
  QedkConcrete.transactionframes QedkConcrete.admissionnonces.
Require Import SetcodeConcrete.transactionentry SetcodeConcrete.admissionnonceagreement.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module A := Chain.Ethereum.Account.
Module W := State.WorldState.
Module S := State.TransactionState.
Module B := State.BlockState.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Section Prediction.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun w a ⇒ State.op_pointcaretlparenrparen w (W.account None a)).
Lemma transaction_entry_nonce_prediction block tx validation query eligible nonces :
  stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) (T.authorization_list tx) →
  A.is_smart_contract (readAccount (B.t.world_state block) query) = false →
  eligible query = true →
  A.t.nonce (readAccount (B.t.world_state block) query) = nonces query →
  let entry := snd (transactionVmInput block tx validation) in
  stringmaps.WellFormed (W.t.accounts (S.t.world_state entry)) ∧
  A.is_smart_contract (readAccount (S.t.world_state entry) query) = false ∧
  A.t.nonce (readAccount (S.t.world_state entry) query) =
    admittedNoncesAfter eligible nonces (E.transaction_validation.sender validation) tx query.
Proof.
  intros Hmap Hwidth Hcode Heligible Hnonce entry.
  set (sender := E.transaction_validation.sender validation).
  set (consumed := fun a ⇒ if String.eqb sender a
    then Numeric.U64.op_plus (nonces a) Numeric.U64.one else nonces a).
  unfold transactionVmInput, transactionPrefix in entry.
  match goal with entry := context [Result.List.fold_left ?step (?applied, ?start) ?auths] |- _ ⇒
    set (charged := start) in × end.
  assert (HchargedWorld : S.t.world_state charged =
    State.op_pointcaretlparenrparenltminus (B.t.world_state block) (W.account (Some false) sender)
      (A.t.with_balance
        (Numeric.U256.op_minus (A.t.balance (readAccount (B.t.world_state block) sender))
          (E.transaction_validation.total_fee validation))
        (A.t.with_nonce (Setcode_rules.advance_sender_nonce (migratedSender block sender)
          (A.t.nonce (readAccount (B.t.world_state block) sender)) (T.nonce tx))
          (readAccount (B.t.world_state block) sender)))).
  { reflexivity. }
  assert (HchargedMap : stringmaps.WellFormed (W.t.accounts (S.t.world_state charged))) by
    (rewrite HchargedWorld; apply account_write_wellformed; exact Hmap).
  assert (HchargedCode : A.t.code (readAccount (S.t.world_state charged) query) =
    A.t.code (readAccount (B.t.world_state block) query)).
  { rewrite HchargedWorld, code_after_account_write by exact Hmap.
    destruct (String.eqb sender query) eqn:Heq; [apply String.eqb_eq in Heq; subst query|]; reflexivity. }
  assert (HchargedEligible : negb (A.is_smart_contract (readAccount (S.t.world_state charged) query)) = eligible query).
  { rewrite Heligible. apply Bool.negb_true_iff.
    change (andb (Byte_string.Bytes.op_ltgt (A.t.code (readAccount (S.t.world_state charged) query)) Byte_string.Bytes.empty)
      (negb (Delegation.is_valid_delegation (A.t.code (readAccount (S.t.world_state charged) query)))) = false).
    exact (eq_rect _ (fun code ⇒ andb (Byte_string.Bytes.op_ltgt code Byte_string.Bytes.empty)
      (negb (Delegation.is_valid_delegation code)) = false) Hcode _ (eq_sym HchargedCode)). }
  assert (HchargedNonce : A.t.nonce (readAccount (S.t.world_state charged) query) = consumed query).
  { rewrite HchargedWorld, nonce_after_account_write by exact Hmap.
    unfold consumed. destruct (String.eqb sender query) eqn:Heq; [|exact Hnonce].
    apply String.eqb_eq in Heq. subst query.
    cbn [A.t.with_nonce A.t.with_balance A.t.nonce].
    assert (Hmigrated : migratedSender block sender = false).
    { unfold migratedSender. change ((Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) &&
        A.is_smart_contract (readAccount (B.t.world_state block) sender)) = false).
      apply Bool.andb_false_iff. right. exact Hcode. }
    unfold Setcode_rules.advance_sender_nonce. rewrite Hmigrated, Hnonce. reflexivity. }
  pose proof (actual_authorizations_pointwise eligible consumed (T.authorization_list tx)
    nil nil charged query HchargedMap Hwidth HchargedEligible HchargedNonce) as Hprojection.
  pose proof (proj1 (@authorizations_preserve_balances F (T.authorization_list tx) nil charged HchargedMap)) as HauthMap.
  unfold entry.
  match goal with |- context [Result.List.fold_left ?step ?start ?auths] ⇒
    set (authorized := Result.List.fold_left step start auths) in × end.
  change (stringmaps.WellFormed (W.t.accounts (S.t.world_state (snd authorized)))) in HauthMap.
  change (negb (A.is_smart_contract (readAccount (S.t.world_state (snd authorized)) query)) = eligible query ∧
    A.t.nonce (readAccount (S.t.world_state (snd authorized)) query) =
      snd (authorizationNonces eligible (nil, consumed) (T.authorization_list tx)) query) in Hprojection.
  destruct authorized as [applied prepared]. cbn beta iota zeta delta [fst snd] in Hprojection, HauthMap |- ×.
  unfold State.op_pointcaretlparenrparen, W.account, W.account_opt.
  cbn -[E.bump_emptying_transaction_counters Addr.Map.find_opt].
  rewrite bump_counters_accounts.
  split; [exact HauthMap|]. destruct Hprojection as [He Hn]. split.
  - apply Bool.negb_true_iff. rewrite Heligible in He. exact He.
  - exact Hn.
Qed.
End Prediction.