Library SetcodeConcrete.nonceagreement

Actual nonce agreement before migration

Consensus continues to use the designated snapshot's eligibility and its public nonce calculation. Every protected address either has regular code now, or still has exactly the publicly predicted nonce. The regular case is permanent; the EOA case uses the cryptographic CREATE exclusion only along the actual execution, not arbitrary states.
From Stdlib Require Import List String ZArith Bool.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.admissionnonces QedkConcrete.validation.
Require Import SetcodeConcrete.transactioneoanonces SetcodeConcrete.transactionregular
  SetcodeConcrete.transactionaccountfinalize SetcodeConcrete.transactionentry
  SetcodeConcrete.transactioncode SetcodeConcrete.codeevolution SetcodeConcrete.consensuschecks.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Module W := State.WorldState.
Module B := State.BlockState.
Module S := State.TransactionState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.

Definition NonceAgreement (eligible : Addr.t → bool) (predicted : Addr.t → Numeric.U64.t)
    world senders : Prop :=
  ∀ a, In a senders → eligible a = true ∧
    worldAccount a (fun code nonce ⇒ regularCode code ∨ nonce = predicted a) world.

Lemma initial_nonce_agreement snapshot senders :
  stringmaps.WellFormed (W.t.accounts snapshot) →
  (∀ a, In a senders → A.is_smart_contract (accountAt snapshot a) = false) →
  NonceAgreement (eligibleAt snapshot) (fun a ⇒ A.t.nonce (accountAt snapshot a)) snapshot senders.
Proof.
  intros Hmap Hcode a Ha. split.
  - unfold eligibleAt. rewrite Hcode by exact Ha. reflexivity.
  - split; [exact Hmap|right; reflexivity].
Qed.
Section Transaction.
Context {F : E.FArgs}.
Lemma validated_sender state tx validation :
  E.validate_transaction state tx = inl validation →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx =
    Some (E.transaction_validation.sender validation).
Proof.
  intro Hv. unfold E.validate_transaction in Hv. cbn zeta in Hv.
  repeat (lazymatch type of Hv with Result.op_letdollar _ _ = _ ⇒ idtac end;
    apply validation.bind_success in Hv;
    let step := fresh "step" in let Hstep := fresh "Hstep" in
    destruct Hv as [step [Hstep Hv]]; cbn beta iota zeta in Hv).
  injection Hv as Heq. subst validation.
  match goal with
    H : context [T.sender _ tx] |- _ ⇒
      destruct (T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx) as [sender|] eqn:Hs;
      [injection H as <-; reflexivity|discriminate H]
  end.
Qed.

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 process_transaction_nonce_agreement state tx post eligible predicted senders sender :
  (∀ a, In a senders → a ≠ Reserve_balance.precompile_address) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20%Z) (T.authorization_list tx) →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
  NonceAgreement eligible predicted (B.t.world_state state) senders →
  (∀ validation, E.validate_transaction state tx = inl validation →
    ∀ a, In a senders → A.is_smart_contract (accountAt (B.t.world_state state) a) = false →
    ¬ In a (S.t.accounts_created_in_current_transaction (transactionFinished state tx validation))) →
  E.process_transaction state tx = inl post →
  NonceAgreement eligible (admittedNoncesAfter eligible predicted sender tx) (B.t.world_state post) senders.
Proof.
  intros Hnative Hwidth Hsender Hbefore Hcreation Hrun.
  assert (Hv : ∃ validation, E.validate_transaction state tx = inl validation).
  { unfold E.process_transaction in Hrun.
    destruct (E.validate_transaction state tx) as [validation|error] eqn:Hvalidation;
      [eexists; reflexivity|discriminate]. }
  destruct Hv as [validation Hv].
  assert (Hidentity : E.transaction_validation.sender validation = sender).
  { pose proof (validated_sender _ _ _ Hv) as Hrecovered. rewrite Hsender in Hrecovered. congruence. }
  intros a Ha. destruct (Hbefore a Ha) as [Heligible [Hmap Haccount]].
  split; [exact Heligible|].
  destruct (A.is_smart_contract (accountAt (B.t.world_state state) a)) eqn:Hcode.
  - assert (Hregular : worldAccount a (fun code _ ⇒ regularCode code) (B.t.world_state state)).
    { split; [exact Hmap|apply regular_code_contract; exact Hcode]. }
    destruct (process_transaction_preserves_regular cold warm access write reserve
      Hrevision Hcold Hwarm state tx validation post a Hv Hrun Hregular) as [Hwf HP].
    split; [exact Hwf|left; exact HP].
  - assert (Hnonce : A.t.nonce (accountAt (B.t.world_state state) a) = predicted a).
    { destruct Haccount as [Hregular|Hnonce]; [|exact Hnonce].
      apply regular_code_contract in Hregular.
      exfalso. exact (Bool.diff_true_false (eq_trans (eq_sym Hregular) Hcode)). }
    pose proof (process_transaction_eoa_nonce_prediction cold warm access write reserve
      Hrevision Hcold Hwarm state tx validation post a eligible predicted
      (Hnative a Ha) Hmap Hwidth Hcode Heligible Hnonce Hv Hrun
      (Hcreation validation Hv a Ha Hcode)) as Hafter.
    rewrite Hidentity in Hafter. exact Hafter.
Qed.
End Transaction.
Print Assumptions process_transaction_nonce_agreement.