2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
open Level_repr
|
|
|
|
|
|
|
|
let from_raw c ?offset l =
|
|
|
|
let l =
|
|
|
|
match offset with
|
|
|
|
| None -> l
|
|
|
|
| Some o -> Raw_level_repr.(of_int32_exn (Int32.add (to_int32 l) o)) in
|
|
|
|
let constants = Storage.constants c in
|
2017-04-10 15:01:22 +04:00
|
|
|
let first_level = Storage.first_level c in
|
2016-09-08 21:13:10 +04:00
|
|
|
Level_repr.from_raw
|
2017-04-10 15:01:22 +04:00
|
|
|
~first_level
|
2016-09-08 21:13:10 +04:00
|
|
|
~cycle_length:constants.Constants_repr.cycle_length
|
|
|
|
~voting_period_length:constants.Constants_repr.voting_period_length
|
|
|
|
l
|
|
|
|
|
2017-04-10 15:01:22 +04:00
|
|
|
let root c =
|
|
|
|
Level_repr.root (Storage.first_level c)
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let succ c l = from_raw c (Raw_level_repr.succ l.level)
|
|
|
|
let pred c l =
|
|
|
|
match Raw_level_repr.pred l.Level_repr.level with
|
|
|
|
| None -> None
|
|
|
|
| Some l -> Some (from_raw c l)
|
|
|
|
|
2017-04-10 15:01:22 +04:00
|
|
|
let current ctxt = Storage.current_level ctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let previous ctxt =
|
2017-04-10 15:01:22 +04:00
|
|
|
let l = current ctxt in
|
2016-09-08 21:13:10 +04:00
|
|
|
match pred ctxt l with
|
2017-04-10 15:01:22 +04:00
|
|
|
| None -> assert false (* We never validate the Genesis... *)
|
|
|
|
| Some p -> p
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let first_level_in_cycle ctxt c =
|
|
|
|
let constants = Storage.constants ctxt in
|
2017-09-22 19:35:12 +04:00
|
|
|
let first_level = Storage.first_level ctxt in
|
2016-09-08 21:13:10 +04:00
|
|
|
from_raw ctxt
|
|
|
|
(Raw_level_repr.of_int32_exn
|
2017-09-22 19:35:12 +04:00
|
|
|
(Int32.add
|
|
|
|
(Raw_level_repr.to_int32 first_level)
|
|
|
|
(Int32.mul
|
|
|
|
constants.Constants_repr.cycle_length
|
|
|
|
(Cycle_repr.to_int32 c))))
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let last_level_in_cycle ctxt c =
|
|
|
|
match pred ctxt (first_level_in_cycle ctxt (Cycle_repr.succ c)) with
|
|
|
|
| None -> assert false
|
|
|
|
| Some x -> x
|
|
|
|
|
|
|
|
let levels_in_cycle ctxt c =
|
|
|
|
let first = first_level_in_cycle ctxt c in
|
|
|
|
let rec loop n acc =
|
|
|
|
if Cycle_repr.(n.cycle = first.cycle)
|
|
|
|
then loop (succ ctxt n) (n :: acc)
|
|
|
|
else acc
|
|
|
|
in
|
|
|
|
loop first []
|