Library SetcodeConcrete.blocknonceagreement

Nonce prediction through complete admitted blocks

System calls preserve code and nonces because their initial prescribed bytecode has neither calls nor adoption. Withdrawals change balances only. The admission snapshot and nonce calculation are retained across blocks; they are not restarted after an account migrates.
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.blockentry QedkConcrete.systemcode QedkConcrete.worldaccounts.
Require Import SetcodeConcrete.nonceagreementsequence SetcodeConcrete.nonceagreement
  SetcodeConcrete.consensuschecks SetcodeConcrete.transactionaccountfinalize
  SetcodeConcrete.codebytes SetcodeConcrete.systemcodeframe SetcodeConcrete.codeframe SetcodeConcrete.blocknonce
  SetcodeConcrete.blockcode SetcodeConcrete.noncebudget SetcodeConcrete.supplysequence.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Import ListNotations.
Module E := Execution.Make.
Module W := State.WorldState.
Module B := State.BlockState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Block := Chain.Ethereum.Block.
Local Abbreviation codeAt := (fun world a ⇒ A.t.code (accountAt world a)).
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).

Lemma block_entry_code world block query :
  prescribedSystemCode world →
  codeAt (B.t.world_state (blockTransactionEntry world block)) query = codeAt world query.
Proof.
  intros [[Hmap Hbeacon] [_ Hhistory]].
  set (activated := Reserve_balance.activate_precompile world).
  assert (Hactive : stringmaps.WellFormed (W.t.accounts activated))
    by (apply account_write_wellformed; exact Hmap).
  assert (Hb : codeAt activated E.beacon_roots_address = beaconRootsCode)
    by (exact (eq_trans (activation_preserves_code world _ Hmap) Hbeacon)).
  assert (Hh : codeAt activated E.history_storage_address = historyStorageCode)
    by (exact (eq_trans (activation_preserves_code world _ Hmap) Hhistory)).
  unfold blockTransactionEntry. cbn zeta. fold activated.
  match goal with |- context [E.process_system_message (B.make activated block) ?addr ?data] ⇒
    set (beaconData := data) end.
  assert (Hbc : callFreeBytes (codeAt activated E.beacon_roots_address) = true)
    by (rewrite Hb; exact beaconRootsCode_call_free).
  assert (Hba : adoptionFreeBytes (codeAt activated E.beacon_roots_address) = true)
    by (rewrite Hb; exact beacon_roots_no_adoption).
  destruct (system_message_code cold warm access write reserve Hrevision Hcold Hwarm
    (B.make activated block) E.beacon_roots_address beaconData E.history_storage_address
    historyStorageCode Hactive Hh Hbc Hba) as [Hmap' Hhistory'].
  pose proof (proj2 (system_message_code cold warm access write reserve Hrevision Hcold Hwarm
    (B.make activated block) E.beacon_roots_address beaconData query (codeAt world query)
    Hactive (activation_preserves_code world query Hmap) Hbc Hba)) as Hquery.
  match goal with |- context [E.process_system_message ?beacon E.history_storage_address ?data] ⇒
    apply (proj2 (system_message_code cold warm access write reserve Hrevision Hcold Hwarm
      beacon E.history_storage_address data query (codeAt world query) Hmap' Hquery
      (eq_rect _ (fun code ⇒ callFreeBytes code = true) historyStorageCode_call_free _ (eq_sym Hhistory'))
      (eq_rect _ (fun code ⇒ adoptionFreeBytes code = true) history_storage_no_adoption _ (eq_sym Hhistory')))) end.
Qed.

Lemma block_entry_nonce_agreement world block eligible predicted senders :
  (∀ a, In a senders → a ≠ Reserve_balance.precompile_address) →
  prescribedSystemCode world → NonceAgreement eligible predicted world senders →
  NonceAgreement eligible predicted (B.t.world_state (blockTransactionEntry world block)) senders.
Proof.
  intros Hnative Hsystem Hbefore a Ha.
  destruct (Hbefore a Ha) as [He [Hmap HP]]. split; [exact He|].
  destruct Hsystem as [[Hwf Hb] [_ Hh]].
  destruct (block_entry_nonce cold warm access write reserve Hrevision Hcold Hwarm
    world block a (Hnative a Ha) Hmap Hb Hh) as [Hafter Hnonce].
  split; [exact Hafter|].
  pose proof (block_entry_code world block a (conj (conj Hwf Hb) (conj Hwf Hh))) as Hcode.
  exact (eq_rect _ (fun value : Prop ⇒ value) HP _
    (eq_sym (f_equal2 (fun code nonce ⇒ transactioncode.regularCode code ∨ nonce = predicted a)
      Hcode Hnonce))).
Qed.

Lemma block_entry_current world block : B.t.current_block (blockTransactionEntry world block) = block.
Proof.
  unfold blockTransactionEntry. cbn zeta.
  rewrite !system_message_current_block. reflexivity.
Qed.

Lemma withdrawals_nonce_agreement eligible predicted senders withdrawals state :
  NonceAgreement eligible predicted (B.t.world_state state) senders →
  NonceAgreement eligible predicted (B.t.world_state
    (Result.List.fold_left E.process_withdrawal state withdrawals)) senders.
Proof.
  revert state. induction withdrawals as [|wd rest IH]; intros state Hbefore; [exact Hbefore|].
  change (NonceAgreement eligible predicted (B.t.world_state
    (Result.List.fold_left E.process_withdrawal (E.process_withdrawal state wd) rest)) senders).
  apply IH. intros a Ha. destruct (Hbefore a Ha) as [He HP]. split; [exact He|].
  unfold E.process_withdrawal. apply credit_world_account. exact HP.
Qed.

Theorem block_nonce_agreement verify snapshot previous next world block post senders :
  (∀ a, In a senders → a ≠ Reserve_balance.precompile_address) →
  Forall (fun tx ⇒ Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20%Z)
    (T.authorization_list tx)) (Block.t.transactions block) →
  prescribedSystemCode world →
  NonceAgreement (eligibleAt snapshot) (admissionNonces previous) world senders →
  transactionsAvoidSenderCreation senders (blockTransactionEntry world block) (Block.t.transactions block) →
  checkTransactions snapshot (Block.t.header block) previous (Block.t.transactions block) = Some next →
  E.process_block verify world block = inl post →
  NonceAgreement (eligibleAt snapshot) (admissionNonces next) post senders.
Proof.
  intros Hnative Hwidth Hsystem Hbefore Hcreation Hcheck Hrun.
  pose proof (block_entry_nonce_agreement world block _ _ senders Hnative Hsystem Hbefore) as Hentry.
  unfold E.process_block in Hrun. rewrite Hrevision in Hrun.
  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.
  destruct (E.validate_block world block) as [[]|error]; [|discriminate].
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
    change entry with (blockTransactionEntry world block) in Hrun end.
  destruct (Result.List.fold_leftM E.process_transaction (blockTransactionEntry world block)
    (Block.t.transactions block)) as [after|error] eqn:Htxs; [|discriminate].
  pose proof (transactions_nonce_agreement cold warm access write reserve Hrevision Hcold Hwarm
    snapshot previous next (blockTransactionEntry world block) (Block.t.transactions block) after senders
    Hnative Hwidth Hentry Hcreation ltac:(rewrite block_entry_current; exact Hcheck) Htxs) as Htransactions.
  pose proof (withdrawals_nonce_agreement _ _ senders (Block.t.withdrawals block) after Htransactions) as Hwithdrawals.
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [E.validate_block ?w ?b] ⇒ destruct (E.validate_block w b) as [[]|error] end;
    [|discriminate].
  destruct verify; [|injection Hrun as <-; exact Hwithdrawals].
  match type of Hrun with context [match ?check with inl _ ⇒ _ | inr _ ⇒ _ end] ⇒
    destruct check as [[]|error] end;
    [injection Hrun as <-; exact Hwithdrawals|discriminate].
Qed.
End Execution.
Print Assumptions block_nonce_agreement.