package tezos-protocol-013-PtJakart

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file period_repr.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

(* `Permanent *)
type error += Malformed_period of int64 | Invalid_arg | Period_overflow

let () =
  let open Data_encoding in
  (* Malformed period *)
  register_error_kind
    `Permanent
    ~id:"malformed_period"
    ~title:"Malformed period"
    ~description:"Period is negative."
    ~pp:(fun ppf period ->
      Format.fprintf ppf "The given period '%Ld' is negative " period)
    (obj1 (req "malformed_period" int64))
    (function Malformed_period n -> Some n | _ -> None)
    (fun n -> Malformed_period n) ;
  (* Invalid arg *)
  register_error_kind
    `Permanent
    ~id:"invalid_arg"
    ~title:"Invalid arg"
    ~description:"Negative multiple of periods are not allowed."
    ~pp:(fun ppf () -> Format.fprintf ppf "Invalid arg")
    empty
    (function Invalid_arg -> Some () | _ -> None)
    (fun () -> Invalid_arg) ;
  let title = "Period overflow" in
  register_error_kind
    `Permanent
    ~id:"period_overflow"
    ~title
    ~description:"Last operation generated an integer overflow."
    ~pp:(fun ppf () -> Format.fprintf ppf "%s" title)
    empty
    (function Period_overflow -> Some () | _ -> None)
    (fun () -> Period_overflow)

module type INTERNAL = sig
  type t = private int64

  val create : int64 -> t option

  val zero : t

  val one : t

  val mult_ : t -> t -> t option

  val add_ : t -> t -> t option

  val encoding : t Data_encoding.t

  val rpc_arg : t RPC_arg.arg

  val pp : Format.formatter -> t -> unit

  include Compare.S with type t := t
end

(* Internal module implementing natural numbers using int64. These are different
   from usual (wrapping up) unsigned integers in that if one overflows the
   representation bounds for int64 through [add] or [mul], a [None] value is
   returned *)
module Internal : INTERNAL = struct
  type t = Int64.t

  let encoding =
    Data_encoding.(
      with_decoding_guard
        (fun t ->
          if Compare.Int64.(t >= 0L) then Ok ()
          else Error "Positive int64 required")
        int64)

  let rpc_arg = RPC_arg.uint63

  let pp ppf v = Format.fprintf ppf "%Ld" v

  include (Compare.Int64 : Compare.S with type t := t)

  let zero = 0L

  let one = 1L

  let create t = if t >= zero then Some t else None

  (* The create function is not used in the [mul_] and [add_] below to not add
      extra Some | None pattern matching to handle since the overflow checks are
      generic and apply as well to negative as positive integers .

     To handle overflows, both [add_] and [mult_] return option types. [None] is
      returned on detected overflow, [Some value] when everything went well. *)
  let mult_ a b =
    if a <> zero then
      let res = Int64.mul a b in
      if Compare.Int64.(Int64.div res a <> b) then None else Some res
    else Some zero

  let add_ a b =
    let res = Int64.add a b in
    if Compare.Int64.(res < a || res < b) then None else Some res
end

include Internal

type period = Internal.t

let to_seconds (t : Internal.t) = (t :> int64)

let of_seconds secs =
  match Internal.create secs with
  | Some v -> ok v
  | None -> error (Malformed_period secs)

let of_seconds_exn t =
  match Internal.create t with
  | Some t -> t
  | None -> invalid_arg "Period.of_seconds_exn"

let mult i p =
  match Internal.create (Int64.of_int32 i) with
  | None -> error Invalid_arg
  | Some iper -> (
      match Internal.mult_ iper p with
      | None -> error Period_overflow
      | Some res -> ok res)

let add p1 p2 =
  match Internal.add_ p1 p2 with
  | None -> error Period_overflow
  | Some res -> ok res

let ( +? ) = add

let one_second = Internal.one

let one_minute = of_seconds_exn 60L

let one_hour = of_seconds_exn 3600L
OCaml

Innovation. Community. Security.