Library MonadVM.Reserve_config

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

Require Import MonadVM.Support.UintN.
Require MonadVM.Numeric.

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.
QEDK's three persistent reserve slots. valid_from = 0 means unconfigured; a matured record is read logically, without clearing its stored slots.

Module t.
  Record record : Set := Build {
    settled : MonadVM.Support.UintN.t;
    pending : MonadVM.Support.UintN.t;
    valid_from : MonadVM.Support.UintN.t;
  }.
  Definition with_settled settled (r : record) :=
    Build settled r.(pending) r.(valid_from).
  Definition with_pending pending (r : record) :=
    Build r.(settled) pending r.(valid_from).
  Definition with_valid_from valid_from (r : record) :=
    Build r.(settled) r.(pending) valid_from.
End t.
Definition t := t.record.

Definition default : MonadVM.Support.UintN.t :=
  MonadVM.Support.UintN.of_string "10000000000000000000".

Definition limit : MonadVM.Support.UintN.t :=
  MonadVM.Support.UintN.shift_left MonadVM.Support.UintN.one 256.

Definition active (record : t) (block : MonadVM.Support.UintN.t)
  : MonadVM.Support.UintN.t :=
  if
    MonadVM.Support.UintN.op_eq record.(MonadVM.Reserve_config.t.valid_from)
      MonadVM.Support.UintN.zero
  then
    default
  else
    if
      MonadVM.Support.UintN.op_lt block
        record.(MonadVM.Reserve_config.t.valid_from)
    then
      record.(MonadVM.Reserve_config.t.settled)
    else
      record.(MonadVM.Reserve_config.t.pending).

Definition read (record : t) (block : MonadVM.Support.UintN.t) : t :=
  let settled := active record block in
  if
    orb
      (MonadVM.Support.UintN.op_eq record.(MonadVM.Reserve_config.t.valid_from)
        MonadVM.Support.UintN.zero)
      (MonadVM.Support.UintN.op_lteq
        record.(MonadVM.Reserve_config.t.valid_from) block)
  then
    {| MonadVM.Reserve_config.t.settled := settled;
      MonadVM.Reserve_config.t.pending := settled;
      MonadVM.Reserve_config.t.valid_from := MonadVM.Support.UintN.zero; |}
  else
    record.

Reject a still-pending request from a different block. Repeated settings in the requesting block replace its pending value, not its active value.
Definition set
  (delay : MonadVM.Support.UintN.t) (block : MonadVM.Support.UintN.t)
  (requested : MonadVM.Support.UintN.t) (record : t) : option t :=
  let valid_from := MonadVM.Support.UintN.op_plus block delay in
  if
    orb (MonadVM.Support.UintN.op_eq delay MonadVM.Support.UintN.zero)
      (orb (MonadVM.Support.UintN.op_gteq valid_from limit)
        (MonadVM.Support.UintN.op_gteq requested limit))
  then
    None
  else
    if
      andb
        (MonadVM.Support.UintN.op_gt
          record.(MonadVM.Reserve_config.t.valid_from) block)
        (MonadVM.Support.UintN.op_ltgt
          record.(MonadVM.Reserve_config.t.valid_from) valid_from)
    then
      None
    else
      Some
        {| MonadVM.Reserve_config.t.settled := active record block;
          MonadVM.Reserve_config.t.pending := requested;
          MonadVM.Reserve_config.t.valid_from := valid_from; |}.