Skip to main content

🍲 Dal_endorsement_repr.v

Translated OCaml

See proofs, Gitlab , OCaml

File generated by coq-of-ocaml
Require Import CoqOfOCaml.CoqOfOCaml.
Require Import CoqOfOCaml.Settings.

Require Import TezosOfOCaml.Environment.V7.
Require TezosOfOCaml.Proto_alpha.Bitset.
Require TezosOfOCaml.Proto_alpha.Dal_slot_repr.
Require TezosOfOCaml.Proto_alpha.Misc.

Definition t : Set := Bitset.t.

Definition available_slots : Set := t.

Definition encoding : Data_encoding.t Bitset.t := Bitset.encoding.

Definition empty : Bitset.t := Bitset.empty.

Definition is_available
  (t_value : Bitset.t) (index_value : Dal_slot_repr.Index.t) : bool :=
  match Bitset.mem t_value (Dal_slot_repr.Index.to_int index_value) with
  | Pervasives.Ok b_valueb_value
  | Pervasives.Error _false
  end.

Definition commit (t_value : Bitset.t) (index_value : Dal_slot_repr.Index.t)
  : Bitset.t :=
  match Bitset.add t_value (Dal_slot_repr.Index.to_int index_value) with
  | Pervasives.Ok t_valuet_value
  | Pervasives.Error _t_value
  end.

Definition occupied_size_in_bits : Bitset.t int :=
  Bitset.occupied_size_in_bits.

Definition expected_size_in_bits (max_index : Dal_slot_repr.Index.t) : int :=
  match Bitset.add Bitset.empty (Dal_slot_repr.Index.to_int max_index) with
  | Pervasives.Error _ ⇒ 0
  | Pervasives.Ok t_valueBitset.occupied_size_in_bits t_value
  end.

Module Accountability.
  Definition t : Set := list Bitset.t.

  Definition shard : Set := int.

  Definition init_value (length : int) : list Bitset.t :=
    let l_value :=
      List.init_value
        "Dal_endorsement_repr.Accountability.init: length cannot be negative"
        length
        (fun (function_parameter : int) ⇒
          let '_ := function_parameter in
          Bitset.empty) in
    match l_value with
    | Pervasives.Error msgPervasives.invalid_arg msg
    | Pervasives.Ok l_valuel_value
    end.

  Definition record_slot_shard_availability
    (bitset : Bitset.t) (shards : list int) : Bitset.t :=
    List.fold_left
      (fun (bitset : Bitset.t) ⇒
        fun (shard : int) ⇒
          (fun x_1Result.value_value x_1 bitset) (Bitset.add bitset shard))
      bitset shards.

  Definition record_shards_availability
    (shard_bitset_per_slot : list Bitset.t) (slots : Bitset.t)
    (shards : list int) : list Bitset.t :=
    List.mapi
      (fun (slot : int) ⇒
        fun (bitset : Bitset.t) ⇒
          match Bitset.mem slots slot with
          | Pervasives.Error _bitset
          | Pervasives.Ok slot_available
            if slot_available then
              record_slot_shard_availability bitset shards
            else
              bitset
          end) shard_bitset_per_slot.

  Definition is_slot_available
    (shard_bitset_per_slot : list Bitset.t) (threshold : int)
    (number_of_shards : int) (index_value : Dal_slot_repr.Index.t) : bool :=
    match
      List.nth shard_bitset_per_slot (Dal_slot_repr.Index.to_int index_value)
      with
    | Nonefalse
    | Some bitset
      let acc_value := Pervasives.ref_value 0 in
      let '_ :=
        List.iter
          (fun (x_value : int) ⇒
            match Bitset.mem bitset x_value with
            | (Pervasives.Error _ | Pervasives.Ok false) ⇒ tt
            | Pervasives.Ok truePervasives.incr acc_value
            end) (Misc.op_minusminusgt 0 (number_of_shards -i 1)) in
      (Pervasives.op_exclamation acc_value) i
      ((threshold ×i number_of_shards) /i 100)
    end.
End Accountability.