• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MinaProtocol / mina / 2863

05 Nov 2024 06:20PM UTC coverage: 30.754% (-16.6%) from 47.311%
2863

push

buildkite

web-flow
Merge pull request #16296 from MinaProtocol/dkijania/more_multi_jobs

more multi jobs in CI

20276 of 65930 relevant lines covered (30.75%)

8631.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.51
/src/lib/block_time/block_time.ml
1
open Core_kernel
9✔
2
open Snark_params
3
open Tick
4
open Unsigned_extended
5
open Snark_bits
6

7
(** See documentation of the {!Mina_wire_types} library *)
8
module Wire_types = Mina_wire_types.Block_time
9

10
module Make_sig (A : Wire_types.Types.S) = struct
11
  module type S = Intf.S with type Time.t = A.V1.t
12
end
13

14
module Make_str (_ : Wire_types.Concrete) = struct
15
  module Time = struct
16
    (* Milliseconds since epoch *)
17
    [%%versioned
18
    module Stable = struct
19
      module V1 = struct
20
        type t = UInt64.Stable.V1.t
×
21
        [@@deriving sexp, compare, equal, hash, yojson]
45✔
22

23
        let to_latest = Fn.id
24

25
        module T = struct
26
          type typ = t [@@deriving sexp, compare, hash]
×
27

28
          type t = typ [@@deriving sexp, compare, hash]
×
29
        end
30

31
        include Hashable.Make (T)
32
      end
33
    end]
34

35
    let max_value = UInt64.max_int
36

37
    let zero = UInt64.zero
38

39
    module Controller = struct
40
      type t = unit -> Time.Span.t [@@deriving sexp]
×
41

42
      (* NB: All instances are identical by construction (see basic below). *)
43
      let equal _ _ = true
×
44

45
      (* NB: All instances are identical by construction (see basic below). *)
46
      let compare _ _ = 0
×
47

48
      let time_offset = ref None
49

50
      let setting_enabled = ref None
51

52
      let disable_setting_offset () = setting_enabled := Some false
×
53

54
      let enable_setting_offset () =
55
        match !setting_enabled with
×
56
        | None ->
×
57
            setting_enabled := Some true
58
        | Some true ->
×
59
            ()
60
        | Some false ->
×
61
            failwith
62
              "Cannot enable time offset mutations; it has been explicitly \
63
               disabled"
64

65
      let set_time_offset offset =
66
        match !setting_enabled with
×
67
        | Some true ->
×
68
            time_offset := Some offset
69
        | None | Some false ->
×
70
            failwith "Cannot mutate the time offset"
71

72
      let create offset = offset
×
73

74
      let basic ~logger:_ () =
75
        match !time_offset with
200✔
76
        | Some offset ->
199✔
77
            offset
78
        | None ->
1✔
79
            let offset =
80
              let env = "MINA_TIME_OFFSET" in
81
              let env_offset =
82
                match Core_kernel.Sys.getenv_opt env with
83
                | Some tm ->
×
84
                    Int.of_string tm
×
85
                | None ->
1✔
86
                    let default = 0 in
87
                    eprintf
88
                      "Environment variable %s not found, using default of %d\n\
89
                       %!"
90
                      env default ;
91
                    default
1✔
92
              in
93
              Core_kernel.Time.Span.of_int_sec env_offset
1✔
94
            in
95
            time_offset := Some offset ;
96
            offset
97

98
      let get_time_offset ~logger = basic ~logger ()
×
99
    end
100

101
    module B = Bits
102
    module Bits = Bits.UInt64
103
    include B.Snarkable.UInt64 (Tick)
104
    module N = Mina_numbers.Nat.Make_checked (UInt64) (Bits)
105

106
    let to_input (t : t) =
107
      Random_oracle_input.Chunked.packed
1,612✔
108
        (Tick.Field.project (Bits.to_bits t), 64)
1,612✔
109

110
    module Checked = struct
111
      type t = N.var
112

113
      module Unsafe = N.Unsafe
114

115
      let to_input (t : t) = N.to_input t
92✔
116

117
      let to_field = N.to_field
118

119
      [%%define_locally N.(typ, ( = ), ( <= ), ( >= ), ( < ), ( > ))]
120
    end
121

122
    module Span = struct
123
      [%%versioned
124
      module Stable = struct
125
        module V1 = struct
126
          type t = UInt64.Stable.V1.t
×
127
          [@@deriving sexp, compare, equal, hash, yojson]
45✔
128

129
          let to_latest = Fn.id
130
        end
131
      end]
132

133
      module Bits = B.UInt64
134
      include B.Snarkable.UInt64 (Tick)
135

136
      let of_time_span s = UInt64.of_int64 (Int64.of_float (Time.Span.to_ms s))
×
137

138
      let to_time_span s = Time.Span.of_ms (Int64.to_float (UInt64.to_int64 s))
×
139

140
      let to_time_ns_span s =
141
        Time_ns.Span.of_ms (Int64.to_float (UInt64.to_int64 s))
×
142

143
      let of_time_ns_span ns : t =
144
        let int64_ns = ns |> Time_ns.Span.to_int63_ns |> Int63.to_int64 in
×
145
        (* convert to milliseconds *)
146
        Int64.(int64_ns / 1_000_000L) |> UInt64.of_int64
×
147

148
      let to_string_hum s = to_time_ns_span s |> Time_ns.Span.to_string_hum
×
149

150
      let to_ms = UInt64.to_int64
151

152
      let of_ms = UInt64.of_int64
153

154
      [%%define_locally UInt64.Infix.(( + ), ( - ), ( * ))]
155

156
      [%%define_locally UInt64.(( < ), ( > ), ( = ), ( <= ), ( >= ), min, zero)]
157

158
      let to_input = to_input
159

160
      module Checked = Checked
161
    end
162

163
    include Comparable.Make (Stable.Latest)
164
    include Hashable.Make (Stable.Latest)
165

166
    let of_time t =
167
      UInt64.of_int64
209✔
168
        (Int64.of_float (Time.Span.to_ms (Time.to_span_since_epoch t)))
209✔
169

170
    (* TODO: Time.t can't hold the full uint64 range, so this can fail for large t *)
171
    let to_time_exn t =
172
      let t_int64 = UInt64.to_int64 t in
×
173
      if Int64.(t_int64 < zero) then failwith "converting to negative timestamp" ;
×
174
      Time.of_span_since_epoch (Time.Span.of_ms (Int64.to_float t_int64))
×
175

176
    let now offset = of_time (Time.sub (Time.now ()) (offset ()))
200✔
177

178
    let field_var_to_unpacked (x : Tick.Field.Var.t) =
179
      Tick.Field.Checked.unpack ~length:64 x
×
180

181
    let epoch = of_time Time.epoch
9✔
182

183
    let add x y = UInt64.add x y
×
184

185
    let diff x y = UInt64.sub x y
×
186

187
    let sub x y = UInt64.sub x y
×
188

189
    let to_span_since_epoch t = diff t epoch
×
190

191
    let of_span_since_epoch s = UInt64.add s epoch
3✔
192

193
    let diff_checked x y =
194
      let pack = Tick.Field.Var.project in
×
195
      Span.unpack_var Tick.Field.Checked.(pack x - pack y)
×
196

197
    let modulus t span = UInt64.rem t span
×
198

199
    let unpacked_to_number var =
200
      let bits = Span.Unpacked.var_to_bits var in
×
201
      Number.of_bits (bits :> Boolean.var list)
×
202

203
    let to_int64 = Fn.compose Span.to_ms to_span_since_epoch
9✔
204

205
    let of_int64 = Fn.compose of_span_since_epoch Span.of_ms
9✔
206

207
    let of_uint64 : UInt64.t -> t = of_span_since_epoch
208

209
    let to_uint64 : t -> UInt64.t = to_span_since_epoch
210

211
    (* TODO: this can fail if the input has more than 63 bits, because it would be serialized to a negative number string *)
212
    let to_string_exn t =
213
      let t_int64 = UInt64.to_int64 t in
181✔
214
      if Int64.(t_int64 < zero) then failwith "converting to negative timestamp" ;
×
215
      Int64.to_string t_int64
181✔
216

217
    let of_time_ns ns : t =
218
      let int64_ns = ns |> Time_ns.to_int63_ns_since_epoch |> Int63.to_int64 in
×
219
      (* convert to milliseconds *)
220
      Int64.(int64_ns / 1_000_000L) |> UInt64.of_int64
×
221

222
    let to_system_time (offset : Controller.t) (t : t) =
223
      of_span_since_epoch
×
224
        Span.(to_span_since_epoch t + of_time_span (offset ()))
×
225

226
    let to_string_system_time_exn (offset : Controller.t) (t : t) : string =
227
      to_system_time offset t |> to_string_exn
×
228

229
    let of_string_exn string =
230
      Int64.of_string string |> Span.of_ms |> of_span_since_epoch
×
231

232
    let gen_incl time_beginning time_end =
233
      let open Quickcheck.Let_syntax in
×
234
      let time_beginning_int64 = to_int64 time_beginning in
235
      let time_end_int64 = to_int64 time_end in
×
236
      let%map int64_time_span =
237
        Int64.(gen_incl time_beginning_int64 time_end_int64)
×
238
      in
239
      of_span_since_epoch @@ Span.of_ms int64_time_span
×
240

241
    let gen =
242
      let open Quickcheck.Let_syntax in
243
      let%map int64_time_span = Int64.(gen_incl zero max_value) in
9✔
244
      of_span_since_epoch @@ Span.of_ms int64_time_span
×
245
  end
246

247
  include Time
248
  module Timeout = Timeout_lib.Make (Time)
249
end
250

251
include Wire_types.Make (Make_sig) (Make_str)
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc