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.Setcode_rules.
Require MonadVM.State.
Require MonadVM.Traits.

Require Import MonadVM.Support.Variants.
Require MonadVM.Support.CryptoBlake2.
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 : MonadVM.Chain.Ethereum.Address.t)
  : MonadVM.Numeric.U256.t :=
  let key :=
    String.append
      (MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.to_bytes)
        (MonadVM.Chain.Ethereum.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 := MonadVM.Numeric.U256.of_repr (MonadVM.Crypto.keccak_256 key)
    in
  MonadVM.Numeric.U256.logand slot_hash
    (MonadVM.Numeric.U256.lognot (MonadVM.Numeric.U256.op_tildedollar 127)).

Definition stored_config
  (world : MonadVM.State.WorldState.t) (addr : MonadVM.Chain.Ethereum.Address.t)
  : MonadVM.Reserve_config.t :=
  let storage :=
    (MonadVM.State.op_pointcaretlparenrparen world
      (MonadVM.State.WorldState.account None precompile_address)).(MonadVM.Chain.Ethereum.Account.t.storage)
    in
  let base := config_slot addr in
  let slot (offset : int) : MonadVM.Support.UintN.t :=
    let key :=
      MonadVM.Numeric.U256.to_repr
        (MonadVM.Numeric.U256.op_plus base (MonadVM.Numeric.U256.of_int offset))
      in
    MonadVM.Numeric.U256.to_uint
      (MonadVM.Numeric.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 : MonadVM.State.WorldState.t) (block : MonadVM.Support.UintN.t)
  (addr : MonadVM.Chain.Ethereum.Address.t) : MonadVM.Numeric.U256.t :=
  MonadVM.Numeric.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 : MonadVM.State.WorldState.t)
  : MonadVM.State.WorldState.t :=
  let lens :=
    Lens.Infix.op_pipeminusminus
      (MonadVM.State.WorldState.account None precompile_address)
      MonadVM.Chain.Ethereum.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 : MonadVM.State.WorldState.t) (block : MonadVM.Support.UintN.t)
  (addr : MonadVM.Chain.Ethereum.Address.t) (value : MonadVM.Numeric.U256.t)
  : option MonadVM.State.WorldState.t :=
  match
    MonadVM.Reserve_config.set execution_consensus_delay block
      (MonadVM.Numeric.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
        (MonadVM.State.WorldState.account None precompile_address)
        MonadVM.Chain.Ethereum.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)
          MonadVM.Numeric.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)
        MonadVM.Numeric.U256.Repr.t :=
      let key :=
        MonadVM.Numeric.U256.to_repr
          (MonadVM.Numeric.U256.op_plus base
            (MonadVM.Numeric.U256.of_int offset)) in
      let value :=
        MonadVM.Numeric.U256.to_repr (MonadVM.Numeric.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 := MonadVM.Numeric.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 := MonadVM.Numeric.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.
Only already executed transactions count. The full current block also contains future transactions and must not be scanned here.
Definition sent_in_window
  (state : MonadVM.State.TransactionState.t)
  (addr : MonadVM.Chain.Ethereum.Address.t) : bool :=
  let sent (tx : MonadVM.Chain.Ethereum.Transaction.t) : bool :=
    match
      MonadVM.Chain.Ethereum.Transaction.sender
        state.(MonadVM.State.TransactionState.t.tx_chain_id) tx with
    | Some sender ⇒ MonadVM.Chain.Ethereum.Address.op_eq sender addr
    | None ⇒ false
    end in
  orb
    (RocqOfOCaml.OCamlList._exists sent
      state.(MonadVM.State.TransactionState.t.prior_transactions))
    (RocqOfOCaml.OCamlList._exists
      (fun (block : MonadVM.Chain.Ethereum.Block.t) ⇒
        andb
          (andb
            (MonadVM.Support.UintN.op_lt
              block.(MonadVM.Chain.Ethereum.Block.t.header).(MonadVM.Chain.Ethereum.Block.Header.t.number)
              state.(MonadVM.State.TransactionState.t.current_block).(MonadVM.Chain.Ethereum.Block.t.header).(MonadVM.Chain.Ethereum.Block.Header.t.number))
            (MonadVM.Support.UintN.op_lt
              state.(MonadVM.State.TransactionState.t.current_block).(MonadVM.Chain.Ethereum.Block.t.header).(MonadVM.Chain.Ethereum.Block.Header.t.number)
              (MonadVM.Support.UintN.op_plus
                block.(MonadVM.Chain.Ethereum.Block.t.header).(MonadVM.Chain.Ethereum.Block.Header.t.number)
                execution_consensus_delay)))
          (RocqOfOCaml.OCamlList._exists sent
            block.(MonadVM.Chain.Ethereum.Block.t.transactions)))
      state.(MonadVM.State.TransactionState.t.world_state).(MonadVM.State.WorldState.t.history)).

Monad §6 Algorithm 3 (DippedIntoReserve).
Definition _rocq_fallback_dipped_into_reserve_0 :
  ltac:(let typ := constr:(ltac:(first [unshelve refine ((RocqOfOCaml.OCamlSeq.t
    (MonadVM.Chain.Ethereum.Address.Map.key × MonadVM.Chain.Ethereum.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 : MonadVM.Chain.Monad.Revision.active)
  : MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.t) bool :=
  MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
    ((MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_exclamation)
      (x := MonadVM.Chain.Ethereum.Address.t))
      MonadVM.State.TransactionState.tx_origin)
    (fun tx_sender ⇒
      MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
        ((MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_exclamation)
          (x := MonadVM.Gas.t)) MonadVM.State.TransactionState.tx_gas_limit)
        (fun tx_gas_limit ⇒
          MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
            ((MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_exclamation)
              (x := MonadVM.Gas.t)) MonadVM.State.TransactionState.tx_gas_price)
            (fun tx_effective_gas_price ⇒
              MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
                is_emptying_tx
                (fun is_emptying ⇒
                  MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
                    MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.get)
                    (fun state ⇒
                      let is_emptying :=
                        andb is_emptying
                          (orb (negb (MonadVM.Setcode_rules.enabled revision))
                            (MonadVM.Byte_string.Bytes.op_eq
                              (MonadVM.State.op_pointcaretlparenrparen state
                                (MonadVM.State.TransactionState.account None
                                  tx_sender)).(MonadVM.Chain.Ethereum.Account.t.code)
                              MonadVM.Byte_string.Bytes.empty)) in
                      MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
                        ((MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_exclamation)
                          (x := MonadVM.State.WorldState.t))
                          MonadVM.State.TransactionState.initial_world_state)
                        (fun original_world ⇒
                          MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
                            ((MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_exclamation)
                              (x := MonadVM.Support.UintN.t))
                              (Lens.Infix.op_pipeminusminus
                                (Lens.Infix.op_pipeminusminus
                                  MonadVM.State.TransactionState.current_block
                                  MonadVM.Chain.Ethereum.Block.header)
                                MonadVM.Chain.Ethereum.Block.Header.number))
                            (fun block ⇒
                              let original_balances :=
                                original_world.(MonadVM.State.WorldState.t.accounts)
                                in
                              MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
                                ((MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_exclamation)
                                  (x := MonadVM.Chain.Ethereum.Address.Map.t MonadVM.Chain.Ethereum.Account.t))
                                  (Lens.Infix.op_pipeminusminus
                                    MonadVM.State.TransactionState.world_state
                                    MonadVM.State.WorldState.accounts))
                                (fun new_state ⇒
                                  MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_letdollar)
                                    ((MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.op_exclamation)
                                      (x := MonadVM.Chain.Ethereum.Address._Set.(RocqOfOCaml.OCamlSet.S.t)))
                                      MonadVM.State.TransactionState.self_destruct)
                                    (fun self_destruct ⇒
                                      let account_reserve_violated
                                        (addr : MonadVM.Chain.Ethereum.Address.t)
                                        (orig_account :
                                          MonadVM.Chain.Ethereum.Account.t)
                                        (new_account :
                                          MonadVM.Chain.Ethereum.Account.t)
                                        : bool :=
                                        let configured :=
                                          match revision with
                                          |
                                            (MonadVM.Support.Variants.Revision.Eleven
                                            |
                                            MonadVM.Support.Variants.Revision.Setcode
                                              _) ⇒
                                            user_reserve_balance original_world
                                              block addr
                                          | _ ⇒ default_reserve_balance
                                          end in
                                        let reserve :=
                                          MonadVM.Numeric.U256.min configured
                                            orig_account.(MonadVM.Chain.Ethereum.Account.t.balance)
                                          in
                                        let current_balance :=
                                          new_account.(MonadVM.Chain.Ethereum.Account.t.balance)
                                          in
                                        if
                                          MonadVM.Chain.Ethereum.Address.op_eq
                                            addr
                                            tx_sender
                                        then
                                          if is_emptying then
                                            false
                                          else
                                            let tx_gas_fees :=
                                              MonadVM.Support.UintN.op_star
                                                tx_gas_limit
                                                tx_effective_gas_price in
                                            match revision with
                                            |
                                              (MonadVM.Support.Variants.Revision.Eleven
                                              |
                                              MonadVM.Support.Variants.Revision.Setcode
                                                _) ⇒
                                              orb
                                                (MonadVM.Support.UintN.op_gt
                                                  tx_gas_fees
                                                  (MonadVM.Numeric.U256.to_uint
                                                    reserve))
                                                (MonadVM.Numeric.U256.op_lt
                                                  current_balance
                                                  (MonadVM.Numeric.U256.op_minus
                                                    reserve
                                                    (MonadVM.Numeric.U256.of_uint_exn
                                                      tx_gas_fees)))
                                            | _ ⇒
                                              let threshold :=
                                                if
                                                  MonadVM.Support.UintN.op_gteq
                                                    (MonadVM.Numeric.U256.to_uint
                                                      reserve)
                                                    tx_gas_fees
                                                then
                                                  MonadVM.Numeric.U256.op_minus
                                                    reserve
                                                    (MonadVM.Numeric.U256.of_uint_exn
                                                      tx_gas_fees)
                                                else
                                                  MonadVM.Numeric.U256.zero in
                                              MonadVM.Numeric.U256.op_lt
                                                current_balance threshold
                                            end
                                        else
                                          MonadVM.Numeric.U256.op_lt
                                            current_balance reserve in
                                      let original_balances :=
                                        MonadVM.Chain.Ethereum.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
                                          |
                                          MonadVM.Support.Variants.Revision.Setcode
                                            _) ⇒
                                          MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.Seq_filter)
                                            (fun (function_parameter :
                                              string ×
                                                MonadVM.Chain.Ethereum.Account.t)
                                              ⇒
                                              let '(addr, _) :=
                                                function_parameter in
                                              negb
                                                (MonadVM.Chain.Ethereum.Address._Set.(RocqOfOCaml.OCamlSet.S.mem)
                                                  addr self_destruct))
                                            original_balances
                                        | _ ⇒
                                          _rocq_fallback_dipped_into_reserve_0
                                        end in
                                      (MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result._return)
                                        (a := bool))
                                        (MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.Seq_exists)
                                          (fun (function_parameter :
                                            MonadVM.Chain.Ethereum.Address.t ×
                                              MonadVM.Chain.Ethereum.Account.t ×
                                              MonadVM.Chain.Ethereum.Account.t)
                                            ⇒
                                            let
                                              '(addr, orig_account, new_account) :=
                                              function_parameter in
                                            account_reserve_violated addr
                                              orig_account new_account)
                                          (MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.Seq_filter_map)
                                            (fun (function_parameter :
                                              MonadVM.Chain.Ethereum.Address.Map.key
                                                ×
                                                MonadVM.Chain.Ethereum.Account.t)
                                              ⇒
                                              let '(addr, orig_account) :=
                                                function_parameter in
                                              let new_account :=
                                                (fun x_1 ⇒
                                                  MonadVM.State.TransactionState.M.(MonadVM.Monad.State.State_result.Option_value)
                                                    x_1
                                                    MonadVM.Chain.Ethereum.Account.empty)
                                                  (MonadVM.Chain.Ethereum.Address.Map.find_opt
                                                    addr new_state) in
                                              let exempt :=
                                                if
                                                  MonadVM.Setcode_rules.enabled
                                                    revision
                                                then
                                                  MonadVM.Setcode_rules.contract_exempt
                                                    (MonadVM.Chain.Ethereum.Account.is_smart_contract
                                                      new_account)
                                                    (MonadVM.Chain.Ethereum.Address.op_eq
                                                      addr tx_sender)
                                                    (sent_in_window state addr)
                                                else
                                                  MonadVM.Chain.Ethereum.Account.is_smart_contract
                                                    new_account in
                                              if exempt then
                                                None
                                              else
                                                Some
                                                  (addr, orig_account,
                                                    new_account))
                                            balances_to_check))))))))))).