Library SetcodeConcrete.executionconfig

Only delayed native settings can change reserve configuration

The opcode and every recursive VM/Host call preserve the reserve precompile's isolation. Its actual native setter is the only allowed writer. Settings made in block b leave all thresholds at heights from b through b+2 unchanged, including when two accounts share a hash page.
From Stdlib Require Import List String ZArith NArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragenative
  QedkConcrete.assemblychecked QedkConcrete.precompilesframe
  QedkConcrete.reserveencoding QedkConcrete.hoststoragewords QedkConcrete.hostframe
  QedkConcrete.vmhostframe.
Require Import SetcodeConcrete.executionconfigstorage.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Definition configStorage block initial :=
  protectedStorage Reserve_balance.precompile_address (reachableReserveStorage block initial).

Section Dispatch.
Context {HF : Host.Instantiate.FArgs}.
Lemma native_config_storage vm block initial msg addr action cold warm access write reserve :
  HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  @nativeDispatch HF vm msg addr = Some action →
  preservesCallback (configStorage block initial) action.
Proof.
  intros Hrevision Hselected. unfold nativeDispatch, Precompiles.Make.try_precompile in Hselected.
  match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
    destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
  - destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action;
      intros s Hs; exact Hs.
  - unfold Precompiles.Make.monad_precompiles in Hselected.
    change (@Precompiles.Make.Revision (@H.Precompiles_fargs
      (H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm))).(Precompiles.Make_Revision_signature.revision)
      with HF.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) in Hselected.
    rewrite Hrevision in Hselected.
    match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
      destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [action'|] eqn:Hnative in Hselected end.
    2: discriminate.
    apply precompilesframe.find_singleton_value in Hnative. subst action'.
    match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
    + inversion Hselected; subst action. intros s Hs. exact Hs.
    + inversion Hselected; subst action. apply native_preserves_reachable_reserve_storage.
Qed.
End Dispatch.

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).

Theorem transaction_call_only_delayed_settings block initial tx msg :
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
    (Evmc.Message.t.recipient msg = Reserve_balance.precompile_address →
      Evmc.Message.t.code_address msg = Reserve_balance.precompile_address)) →
  preservesCallback (configStorage block initial) (@E.Host.call_from_eoa F tx msg).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
  intros Hgas Htarget.
  apply (@transaction_call_protects_config_storage F Reserve_balance.precompile_address
    (reachableReserveStorage block initial) cold warm access write reserve Hrevision Hcold Hwarm).
  - intros vm nested addr action Haction.
    exact (@native_config_storage (@E.Instantiation_fargs F) vm block initial nested addr action
      cold warm access write reserve Hrevision Haction).
  - exact Hgas.
  - exact Htarget.
Qed.

Theorem system_call_only_delayed_settings block initial msg :
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  Evmc.Message.t.sender msg ≠ Reserve_balance.precompile_address →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  (Evmc.Message.t.recipient msg = Reserve_balance.precompile_address →
    Evmc.Message.t.code_address msg = Reserve_balance.precompile_address) →
  preservesCallback (configStorage block initial) (@E.Host.call F msg).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
  intros Hgas Hsender Hkind Htarget.
  apply (@system_call_protects_config_storage F Reserve_balance.precompile_address
    (reachableReserveStorage block initial) cold warm access write reserve Hrevision Hcold Hwarm).
  - intros vm nested addr action Haction.
    exact (@native_config_storage (@E.Instantiation_fargs F) vm block initial nested addr action
      cold warm access write reserve Hrevision Haction).
  - exact Hgas.
  - exact Hsender.
  - exact Hkind.
  - exact Htarget.
Qed.

initial is the reserve-storage byte map before this transaction's VM call. target is a height no more than two above block. This conclusion is about the actual post-call configuration, not a simulated update list.
Theorem transaction_call_preserves_configured_threshold block initial tx msg state query target :
  stringmaps.WellFormed initial → storageWords initial →
  (block ≤ target < block+3)%N →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
    (Evmc.Message.t.recipient msg = Reserve_balance.precompile_address →
      Evmc.Message.t.code_address msg = Reserve_balance.precompile_address)) →
  configStorage block initial state →
  let post := snd (@E.Host.call_from_eoa F tx msg state) in
  Reserve_balance.user_reserve_balance (S.t.world_state post) target query =
    Numeric.U256.of_uint_exn (Reserve_config.active
      {| Reserve_config.t.settled := readConfigField initial query 0;
         Reserve_config.t.pending := readConfigField initial query 1;
         Reserve_config.t.valid_from := readConfigField initial query 2 |} target).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
  intros Hmap Hwords Htime Hgas Htarget Hinitial post.
  pose proof (transaction_call_only_delayed_settings block initial tx msg Hgas Htarget state Hinitial) as Hafter.
  destruct (protected_storage_contents _ _ _ Hafter) as [_ [updates Hwrites]].
  pose proof (reserve_storage_writes_threshold_any_account _ _ _ _ query target
    Hmap Hwords Hwrites Htime) as Hthreshold.
  unfold Reserve_balance.user_reserve_balance, Reserve_balance.stored_config.
  f_equal. exact Hthreshold.
Qed.
End Execution.
Print Assumptions transaction_call_preserves_configured_threshold.