Library MonadVM.Reserve_balance

File generated by rocq-of-ocaml
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Settings.

Require Import MonadVM.Support.UintN.
Require MonadVM.Byte_string.
Require MonadVM.Chain.Ethereum.
Require MonadVM.Chain.Monad.
Require MonadVM.Crypto.
Require MonadVM.Delegation.
Require MonadVM.Gas.
Require MonadVM.Monad.
Require MonadVM.Numeric.
Require MonadVM.Option.
Require MonadVM.Reserve_config.
Require MonadVM.Staking.
Require MonadVM.State.
Require MonadVM.Traits.

Require Import MonadVM.Support.Variants.
Require MonadVM.Support.CryptoBlake2.
Require Import MonadVM.Support.TransactionStateNotation.
Local Open Scope transaction_state_scope.
Module TS := MonadVM.State.TransactionState.
Module WS := MonadVM.State.WorldState.
Module Account := MonadVM.Chain.Ethereum.Account.
Module Address := MonadVM.Chain.Ethereum.Address.
Module Header := MonadVM.Chain.Ethereum.Block.Header.
Module Block := MonadVM.Chain.Ethereum.Block.
Module U256 := MonadVM.Numeric.U256.
Module Uint := MonadVM.Support.UintN.
Module Revision := MonadVM.Chain.Monad.Revision.
Module SM := MonadVM.Monad.State.State_result.
Require Import RocqOfOCaml.Opaque.
Local Ltac _rocq_witness_type typ :=
  let typ := eval hnf in typ in
  lazymatch typ with
  | list ?a ⇒ let a := _rocq_witness_type a in constr:(list a)
  | option ?a ⇒ let a := _rocq_witness_type a in constr:(option a)
  | prod ?a ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(prod a b)
  | sum ?a ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(sum a b)
  | ?a → ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(a → b)
  | _ ⇒ typ
  end.
Monad §3: default_reserve_balance
Monad §3: k, the block delay between consensus and execution.
These slots live in the precompile account, so the existing Host journal rolls them back with the enclosing call or transaction.
Definition config_slot (addr : Address.t) : U256.t :=
  let key :=
    String.append
      (MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.to_bytes)
        (Address.to_bytes32 addr))
      (MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.to_bytes)
        MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.zeros))
    in
  let slot_hash := U256.of_repr (MonadVM.Crypto.keccak_256 key) in
  U256.logand slot_hash (U256.lognot (U256.op_tildedollar 127)).

Definition stored_config (world : WS.t) (addr : Address.t)
  : MonadVM.Reserve_config.t :=
  let storage :=
    (MonadVM.State.op_pointcaretlparenrparen world
      (WS.account None precompile_address)).(Account.t.storage) in
  let base := config_slot addr in
  let slot (offset : int) : MonadVM.Support.UintN.t :=
    let key := U256.to_repr (U256.op_plus base (U256.of_int offset)) in
    U256.to_uint
      (U256.of_repr
        ((fun x_1 ⇒
          MonadVM.Option.value x_1
            MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.zeros))
          ((MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.Map).(MonadVM.Byte_string.Fixed.Map.Map_signature.find_opt)
            (a := string)) key storage))) in
  {| MonadVM.Reserve_config.t.settled := slot 0;
    MonadVM.Reserve_config.t.pending := slot 1;
    MonadVM.Reserve_config.t.valid_from := slot 2; |}.

Definition user_reserve_balance
  (world : WS.t) (block : MonadVM.Support.UintN.t) (addr : Address.t)
  : U256.t :=
  U256.of_uint_exn
    (MonadVM.Reserve_config.active (stored_config world addr) block).

Activate the native account without overwriting its balance, code or storage.
Definition activate_precompile (world : WS.t) : WS.t :=
  let lens :=
    Lens.Infix.op_pipeminusminus (WS.account None precompile_address)
      Account.nonce in
  MonadVM.State.op_pointcaretdollarlparenrparenltminus world lens
    (fun (nonce : MonadVM.Numeric.U64.t) ⇒
      MonadVM.Numeric.U64.max nonce MonadVM.Numeric.U64.one).

Definition set_reserve_balance
  (world : WS.t) (block : MonadVM.Support.UintN.t) (addr : Address.t)
  (value : U256.t) : option WS.t :=
  match
    MonadVM.Reserve_config.set execution_consensus_delay block
      (U256.to_uint value) (stored_config world addr) with
  | None ⇒ None
  | Some record ⇒
    let base := config_slot addr in
    let lens :=
      Lens.Infix.op_pipeminusminus (WS.account None precompile_address)
        Account.storage in
    let set_slot
      (storage :
        MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.Map).(MonadVM.Byte_string.Fixed.Map.Map_signature.t)
          U256.Repr.t) (offset : int) (value : MonadVM.Support.UintN.t)
      : MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.Map).(MonadVM.Byte_string.Fixed.Map.Map_signature.t)
        U256.Repr.t :=
      let key := U256.to_repr (U256.op_plus base (U256.of_int offset)) in
      let value := U256.to_repr (U256.of_uint_exn value) in
      if
        MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.op_eq)
          value
          MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.zeros)
      then
        (MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.Map).(MonadVM.Byte_string.Fixed.Map.Map_signature.remove)
          (a := U256.Repr.t)) key storage
      else
        (MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.Map).(MonadVM.Byte_string.Fixed.Map.Map_signature.add)
          (a := U256.Repr.t)) key value storage in
    let world := activate_precompile world in
    let storage := MonadVM.State.op_pointcaretlparenrparen world lens in
    let storage := set_slot storage 0 record.(MonadVM.Reserve_config.t.settled)
      in
    let storage := set_slot storage 1 record.(MonadVM.Reserve_config.t.pending)
      in
    let storage :=
      set_slot storage 2 record.(MonadVM.Reserve_config.t.valid_from) in
    Some (MonadVM.State.op_pointcaretlparenrparenltminus world lens storage)
  end.

Monad §6 Algorithm 4 (IsEmptying). Check whether the current transaction is emptying. The check of delegated_in_state is exactly as in the spec. The comparison with the next emptying transaction counter subsumes both auth_condition and prior_sender_condition. This function must be called after processing the transaction's authorization list. Note that authority processing bumps the next emptying transaction block counter before the transaction is processed, but the transaction itself only bumps its sender's counter after it finishes, so while processing it this function will return true.
Monad §6 Algorithm 3 (DippedIntoReserve).
Definition _rocq_fallback_dipped_into_reserve_0 :
  ltac:(let typ := constr:(ltac:(first [unshelve refine ((RocqOfOCaml.OCamlSeq.t
    (Address.Map.key × Account.t))%type
  )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

Definition dipped_into_reserve (revision : Revision.active)
  : TS.M.(SM.t) bool :=
  let* tx_sender := read TS.tx_origin in
  let* tx_gas_limit := read TS.tx_gas_limit in
  let* tx_effective_gas_price := read TS.tx_gas_price in
  let* is_emptying := is_emptying_tx in
  let* original_world := read TS.initial_world_state in
  let* block :=
    read
      (Lens.Infix.op_pipeminusminus
        (Lens.Infix.op_pipeminusminus TS.current_block Block.header)
        Header.number) in
  let original_balances := original_world.(WS.t.accounts) in
  let* new_state :=
    read (Lens.Infix.op_pipeminusminus TS.world_state WS.accounts) in
  let* self_destruct := read TS.self_destruct in
  let account_reserve_violated
    (addr : Address.t) (orig_account : Account.t) (new_account : Account.t)
    : bool :=
    let configured :=
      match revision with
      | MonadVM.Support.Variants.Revision.Eleven ⇒
        user_reserve_balance original_world block addr
      | _ ⇒ default_reserve_balance
      end in
    let reserve := U256.min configured orig_account.(Account.t.balance) in
    let current_balance := new_account.(Account.t.balance) in
    let tx_gas_fees :=
      MonadVM.Support.UintN.op_star tx_gas_limit tx_effective_gas_price in
    let violation_threshold :=
      if Address.op_eq addr tx_sender then
        if
          MonadVM.Support.UintN.op_gteq (U256.to_uint reserve) tx_gas_fees
        then
          U256.op_minus reserve (U256.of_uint_exn tx_gas_fees)
        else
          U256.zero
      else
        reserve in
    andb (orb (Address.op_ltgt addr tx_sender) (negb is_emptying))
      (orb
        (andb (Address.op_eq addr tx_sender)
          (MonadVM.Support.UintN.op_gt tx_gas_fees (U256.to_uint reserve)))
        (U256.op_lt current_balance violation_threshold)) in
  let original_balances := Address.Map.to_seq original_balances in
  let balances_to_check :=
    match revision with
    | MonadVM.Support.Variants.Revision.Eight ⇒ original_balances
    |
      (MonadVM.Support.Variants.Revision.Nine |
      MonadVM.Support.Variants.Revision.Eleven) ⇒
      TS.M.(SM.Seq_filter)
        (fun (function_parameter : string × Account.t) ⇒
          let '(addr, _) := function_parameter in
          negb (Address._Set.(RocqOfOCaml.OCamlSet.S.mem) addr self_destruct))
        original_balances
    | _ ⇒ _rocq_fallback_dipped_into_reserve_0
    end in
  return
    (TS.M.(SM.Seq_exists)
      (fun (function_parameter : Address.t × Account.t × Account.t) ⇒
        let '(addr, orig_account, new_account) := function_parameter in
        account_reserve_violated addr orig_account new_account)
      (TS.M.(SM.Seq_filter_map)
        (fun (function_parameter : Address.Map.key × Account.t) ⇒
          let '(addr, orig_account) := function_parameter in
          let new_account :=
            (fun x_1 ⇒ TS.M.(SM.Option_value) x_1 Account.empty)
              (Address.Map.find_opt addr new_state) in
          if
            orb (Account.is_smart_contract new_account)
              (Address.op_eq addr MonadVM.Staking.staking_address)
          then
            None
          else
            Some (addr, orig_account, new_account)) balances_to_check)).