package octez-protocol-alpha-libs

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

Source file assert.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2021-2022 Trili Tech, <contact@trili.tech>                  *)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Protocol

let error ~loc v f =
  let open Lwt_result_syntax in
  match v with
  | Error err when List.exists f err -> return_unit
  | Ok _ -> failwith "Unexpected successful result (%s)" loc
  | Error err -> failwith "@[Unexpected error (%s): %a@]" loc pp_print_trace err

let join_errors e1 e2 =
  let open Lwt_result_syntax in
  match (e1, e2) with
  | Ok (), Ok () -> return_unit
  | Error e, Ok () | Ok (), Error e -> fail e
  | Error e1, Error e2 -> fail (e1 @ e2)

let test_error_encodings e =
  let module E = Environment.Error_monad in
  ignore (E.pp Format.str_formatter e) ;
  let e' = E.json_of_error e |> E.error_of_json in
  assert (e = e')

let proto_error ~loc v f =
  error ~loc v (function
      | Environment.Ecoproto_error err ->
          test_error_encodings err ;
          f err
      | _ -> false)

let proto_error_with_info ?(error_info_field = `Title) ~loc v
    expected_error_info =
  let info err =
    let i = Error_monad.find_info_of_error (Environment.wrap_tzerror err) in
    match error_info_field with
    | `Title -> i.title
    | `Id -> i.id
    | `Description -> i.description
    | `Message -> Format.asprintf "%a" Environment.Error_monad.pp err
  in
  proto_error ~loc v (function err ->
      Format.printf
        "@[<v 4>THE ERROR IS: %s@,EXPECTED: %s@]@."
        (info err)
        expected_error_info ;
      let info = info err in
      String.equal info expected_error_info)

let equal ~loc (cmp : 'a -> 'a -> bool) msg pp a b =
  let open Lwt_result_syntax in
  if not (cmp a b) then
    failwith "@[@[[%s]@] - @[%s : %a is not equal to %a@]@]" loc msg pp a pp b
  else return_unit

let leq ~loc (cmp : 'a -> 'a -> int) msg pp a b =
  let open Lwt_result_syntax in
  if cmp a b > 0 then
    failwith
      "@[@[[%s]@] - @[%s : %a is not less or equal to %a@]@]"
      loc
      msg
      pp
      a
      pp
      b
  else return_unit

let lt ~loc (cmp : 'a -> 'a -> int) msg pp a b =
  let open Lwt_result_syntax in
  if cmp a b >= 0 then
    failwith "@[@[[%s]@] - @[%s : %a is not less than %a@]@]" loc msg pp a pp b
  else return_unit

let not_equal ~loc (cmp : 'a -> 'a -> bool) msg pp a b =
  let open Lwt_result_syntax in
  if cmp a b then
    failwith "@[@[[%s]@] - @[%s : %a is equal to %a@]@]" loc msg pp a pp b
  else return_unit

module Int32 = struct
  include Int32

  let pp pp v = Format.pp_print_int pp (Int32.to_int v)
end

module Int64 = struct
  include Int64

  let pp pp v = Format.pp_print_int pp (Int64.to_int v)
end

(* char *)
let equal_char ~loc a b =
  equal ~loc Char.equal "Characters aren't equal" Format.pp_print_char a b

(* int *)
let equal_int ~loc (a : int) (b : int) =
  equal ~loc Int.equal "Integers aren't equal" Format.pp_print_int a b

let not_equal_int ~loc (a : int) (b : int) =
  not_equal ~loc Int.equal "Integers are equal" Format.pp_print_int a b

let leq_int ~loc (a : int) (b : int) =
  leq ~loc Compare.Int.compare "Integer comparison" Format.pp_print_int a b

(* int32 *)
let equal_int32 ~loc (a : int32) (b : int32) =
  equal ~loc Int32.equal "Int32 aren't equal" Int32.pp a b

let leq_int32 ~loc (a : int32) (b : int32) =
  leq ~loc Compare.Int32.compare "Int32 comparison" Int32.pp a b

let lt_int32 ~loc (a : int32) (b : int32) =
  lt ~loc Compare.Int32.compare "Int32 comparison" Int32.pp a b

(* int64 *)
let equal_int64 ~loc (a : int64) (b : int64) =
  equal ~loc Compare.Int64.( = ) "Int64 aren't equal" Int64.pp a b

let not_equal_int64 ~loc (a : int64) (b : int64) =
  not_equal ~loc Int64.equal "Int64 are equal" Int64.pp a b

let leq_int64 ~loc (a : int64) (b : int64) =
  leq ~loc Compare.Int64.compare "Int64 comparison" Int64.pp a b

let equal_z ~loc (a : Z.t) (b : Z.t) =
  equal ~loc Compare.Z.( = ) "Z are not equal" Z.pp_print a b

let equal_q ~loc (a : Q.t) (b : Q.t) =
  equal ~loc Compare.Q.( = ) "Q are not equal" Q.pp_print a b

(* bool *)
let equal_bool ~loc (a : bool) (b : bool) =
  equal ~loc Bool.equal "Booleans aren't equal" Format.pp_print_bool a b

let not_equal_bool ~loc (a : bool) (b : bool) =
  not_equal ~loc Bool.equal "Booleans are equal" Format.pp_print_bool a b

let is_true ~loc (a : bool) =
  equal ~loc Bool.equal "Boolean is not true" Format.pp_print_bool a true

(* string *)
let equal_string ~loc (a : string) (b : string) =
  equal ~loc String.equal "Strings aren't equal" Format.pp_print_string a b

let not_equal_string ~loc (a : string) (b : string) =
  not_equal ~loc String.equal "Strings are equal" Format.pp_print_string a b

(* tez *)
let equal_tez ~loc (a : Alpha_context.Tez.t) (b : Alpha_context.Tez.t) =
  let open Alpha_context in
  equal ~loc Tez.( = ) "Tez aren't equal" Tez.pp a b

let not_equal_tez ~loc (a : Alpha_context.Tez.t) (b : Alpha_context.Tez.t) =
  let open Alpha_context in
  not_equal ~loc Tez.( = ) "Tez are equal" Tez.pp a b

(* pkh *)
let equal_pkh ~loc (a : Signature.Public_key_hash.t)
    (b : Signature.Public_key_hash.t) =
  let module PKH = Signature.Public_key_hash in
  equal ~loc PKH.equal "Public key hashes aren't equal" PKH.pp a b

let not_equal_pkh ~loc (a : Signature.Public_key_hash.t)
    (b : Signature.Public_key_hash.t) =
  let module PKH = Signature.Public_key_hash in
  not_equal ~loc PKH.equal "Public key hashes are equal" PKH.pp a b

(* protocol hash *)
let equal_protocol_hash ~loc (a : Protocol_hash.t) (b : Protocol_hash.t) =
  equal
    ~loc
    Protocol_hash.equal
    "Protocol hashes aren't equal"
    Protocol_hash.pp
    a
    b

let not_equal_protocol_hash ~loc (a : Protocol_hash.t) (b : Protocol_hash.t) =
  not_equal
    ~loc
    Protocol_hash.equal
    "Protocol hashes are equal"
    Protocol_hash.pp
    a
    b

let get_some ~loc =
  let open Lwt_result_syntax in
  function Some x -> return x | None -> failwith "Unexpected None (%s)" loc

let is_none ~loc ~pp =
  let open Lwt_result_syntax in
  function
  | Some x -> failwith "Unexpected (Some %a) (%s)" pp x loc
  | None -> return_unit

let equal_result ~loc ~pp_ok ~pp_error eq_ok eq_error a b =
  equal
    ~loc
    (Result.equal ~ok:eq_ok ~error:eq_error)
    "Results are not equal"
    (Format.pp_print_result ~ok:pp_ok ~error:pp_error)
    a
    b

let is_error ~loc ~pp =
  let open Lwt_result_syntax in
  function
  | Ok x -> failwith "Unexpected (Ok %a) (%s)" pp x loc | Error _ -> return_unit

let get_ok ~__LOC__ =
  let open Lwt_result_syntax in
  function
  | Ok r -> return r
  | Error err ->
      failwith "@[Unexpected error (%s): %a@]" __LOC__ pp_print_trace err

open Context

(* Some asserts for account operations *)

let contract_property_is property ~loc b contract expected =
  let open Lwt_result_syntax in
  let* balance = property b contract in
  equal_tez ~loc balance expected

(** [balance_is b c amount] checks that the current balance [b] of contract [c]
    is [amount].
*)
let balance_is = contract_property_is Contract.balance

(** [frozen_bonds_is b c amount] checks that the current frozen bonds of
    contract [c] is [amount].
*)
let frozen_bonds_is = contract_property_is Contract.frozen_bonds

let balance_or_frozen_bonds_was_operated ~is_balance ~operand ~loc b contract
    old_balance amount =
  let open Lwt_result_wrap_syntax in
  let*?@ expected = operand old_balance amount in
  let f = if is_balance then balance_is else frozen_bonds_is in
  f ~loc b contract expected

(** [balance_was_credited ~loc ctxt contract old_balance amount] checks
    that [contract]'s balance was credited [amount] tez in comparison to
    [old_balance].
*)
let balance_was_credited =
  balance_or_frozen_bonds_was_operated
    ~is_balance:true
    ~operand:Alpha_context.Tez.( +? )

(** [balance_was_credited ~loc ctxt contract old_balance amount] checks
    that [contract]'s balance was debited [amount] tez in comparison to
    [old_balance].
*)
let balance_was_debited =
  balance_or_frozen_bonds_was_operated
    ~is_balance:true
    ~operand:Alpha_context.Tez.( -? )

(** [frozen_bonds_was_credited ~loc ctxt contract old_balance amount] checks
    that [contract]'s frozen bonds was credited [amount] tez in comparison to
    [old_balance].
*)
let frozen_bonds_was_credited =
  balance_or_frozen_bonds_was_operated
    ~is_balance:false
    ~operand:Alpha_context.Tez.( +? )

(** [frozen_bonds_was_credited ~loc ctxt contract old_balance amount] checks
    that [contract]'s frozen bonds was credited [amount] tez in comparison to
    [old_balance].
*)
let frozen_bonds_was_debited =
  balance_or_frozen_bonds_was_operated
    ~is_balance:false
    ~operand:Alpha_context.Tez.( -? )

let pp_print_list pp out xs =
  let list_pp fmt =
    Format.pp_print_list ~pp_sep:(fun fmt () -> Format.fprintf fmt ";@.") fmt
  in
  Format.fprintf out "[%a]" (list_pp pp) xs

let assert_equal_list ~loc eq msg pp =
  equal ~loc (List.equal eq) msg (pp_print_list pp)

let assert_equal_list_opt ~loc eq msg pp =
  equal
    ~loc
    (Option.equal (List.equal eq))
    msg
    (Format.pp_print_option (pp_print_list pp))

(** Checks that both lists have the same elements, not taking the
    order of these elements into account, but taking their
    multiplicity into account. *)
let equal_list_any_order ~loc ~compare msg pp list1 list2 =
  let ordered_list1 = List.sort compare list1 in
  let ordered_list2 = List.sort compare list2 in
  equal
    ~loc
    (List.equal (fun a b -> compare a b = 0))
    msg
    (pp_print_list pp)
    ordered_list1
    ordered_list2

let to_json_string encoding x =
  x
  |> Data_encoding.Json.construct encoding
  |> Format.asprintf "\n%a\n" Data_encoding.Json.pp

let equal_with_encoding ~loc encoding a b =
  equal_string ~loc (to_json_string encoding a) (to_json_string encoding b)

let not_equal_with_encoding ~loc encoding a b =
  not_equal_string ~loc (to_json_string encoding a) (to_json_string encoding b)
OCaml

Innovation. Community. Security.