Skip to content

Commit a8a6a0b

Browse files
committed
merkle_ledger_test: constrain all modules with canonical module types from intf, functorize Base_types in prep for tests
1 parent 6c675fd commit a8a6a0b

File tree

4 files changed

+120
-67
lines changed

4 files changed

+120
-67
lines changed

src/lib/merkle_ledger_tests/test.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ let test_db () =
5454
Quickcheck.test ~trials:5 ~sexp_of:[%sexp_of: Balance.t list]
5555
gen_non_zero_balances ~f:(fun balances ->
5656
let account_ids = Account_id.gen_accounts num_accounts in
57+
let T = Account_id.eq in
5758
let accounts = List.map2_exn account_ids balances ~f:Account.create in
5859
DB.with_ledger ~depth:Depth.depth ~f:(fun db ->
5960
let enumerate_dir_combinations max_depth =

src/lib/merkle_ledger_tests/test_database.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ module Make (Test : Test_intf) = struct
130130
Quickcheck.random_value ~seed:(`Deterministic "balance 1")
131131
Balance.gen
132132
in
133+
let T = Account_id.eq in
133134
let account = Account.create account_id balance in
134135
let balance' =
135136
Quickcheck.random_value ~seed:(`Deterministic "balance 2")
@@ -437,6 +438,7 @@ module Make (Test : Test_intf) = struct
437438
Quickcheck.random_value
438439
(Quickcheck.Generator.list_with_length num_accounts Balance.gen)
439440
in
441+
let T = Account_id.eq in
440442
let accounts = List.map2_exn account_ids balances ~f:Account.create in
441443
Test.with_instance (fun mdb ->
442444
List.iter accounts ~f:(fun account ->
@@ -462,6 +464,7 @@ module Make (Test : Test_intf) = struct
462464
List.fold balances ~init:0 ~f:(fun accum balance ->
463465
Balance.to_nanomina_int balance + accum )
464466
in
467+
let T = Account_id.eq in
465468
let accounts =
466469
List.map2_exn account_ids balances ~f:Account.create
467470
in
@@ -492,6 +495,7 @@ module Make (Test : Test_intf) = struct
492495
List.fold some_balances ~init:0 ~f:(fun accum balance ->
493496
Balance.to_int balance + accum )
494497
in
498+
let T = Account_id.eq in
495499
let accounts =
496500
List.map2_exn account_ids balances ~f:Account.create
497501
in

src/lib/merkle_ledger_tests/test_mask.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ module Make (Test : Test_intf) = struct
320320
in
321321
let account_ids = Account_id.gen_accounts num_accounts in
322322
let balances = gen_values Balance.gen in
323+
let T = Account_id.eq in
323324
let accounts =
324325
List.map2_exn account_ids balances ~f:(fun public_key balance ->
325326
Account.create public_key balance )
@@ -350,6 +351,7 @@ module Make (Test : Test_intf) = struct
350351
in
351352
let account_ids = Account_id.gen_accounts num_accounts in
352353
let balances = gen_values Balance.gen num_accounts in
354+
let T = Account_id.eq in
353355
let base_accounts =
354356
List.map2_exn account_ids balances ~f:Account.create
355357
in
@@ -409,6 +411,7 @@ module Make (Test : Test_intf) = struct
409411
Quickcheck.random_value
410412
(Quickcheck.Generator.list_with_length num_accounts Balance.gen)
411413
in
414+
let T = Account_id.eq in
412415
let accounts =
413416
List.map2_exn account_ids balances ~f:Account.create
414417
in
@@ -444,6 +447,7 @@ module Make (Test : Test_intf) = struct
444447
List.init num_accounts ~f:(fun n ->
445448
Balance.of_nanomina_int_exn (n + 1) )
446449
in
450+
let T = Account_id.eq in
447451
let parent_accounts =
448452
List.map2_exn account_ids balances ~f:Account.create
449453
in
@@ -486,6 +490,7 @@ module Make (Test : Test_intf) = struct
486490
List.init num_accounts ~f:(fun n ->
487491
Balance.of_nanomina_int_exn (n + 1) )
488492
in
493+
let T = Account_id.eq in
489494
let parent_accounts =
490495
List.map2_exn account_ids balances ~f:Account.create
491496
in
@@ -539,6 +544,7 @@ module Make (Test : Test_intf) = struct
539544
Quickcheck.random_value
540545
(Quickcheck.Generator.list_with_length num_accounts Balance.gen)
541546
in
547+
let T = Account_id.eq in
542548
let accounts =
543549
List.map2_exn account_ids balances ~f:Account.create
544550
in
@@ -571,6 +577,7 @@ module Make (Test : Test_intf) = struct
571577
Quickcheck.random_value
572578
(Quickcheck.Generator.list_with_length num_accounts Balance.gen)
573579
in
580+
let T = Account_id.eq in
574581
let accounts =
575582
List.map2_exn account_ids balances ~f:Account.create
576583
in
@@ -612,6 +619,7 @@ module Make (Test : Test_intf) = struct
612619
Test.with_instances (fun maskable mask ->
613620
let attached_mask = Maskable.register_mask maskable mask in
614621
let k = Account_id.gen_accounts 1 |> List.hd_exn in
622+
let T = Account_id.eq in
615623
let acct1 = Account.create k (Balance.of_nanomina_int_exn 10) in
616624
let loc =
617625
Mask.Attached.get_or_create_account attached_mask k acct1

src/lib/merkle_ledger_tests/test_stubs.ml

Lines changed: 107 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,7 @@ module Balance = struct
88
let of_int = of_nanomina_int_exn
99
end
1010

11-
module Account = struct
12-
(* want bin_io, not available with Account.t *)
13-
type t = Mina_base.Account.Stable.Latest.t
14-
[@@deriving bin_io_unversioned, sexp, equal, compare, hash, yojson]
15-
16-
type key = Mina_base.Account.Key.Stable.Latest.t
17-
[@@deriving bin_io_unversioned, sexp, equal, compare, hash]
18-
19-
(* use Account items needed *)
20-
let empty = Mina_base.Account.empty
21-
22-
let public_key = Mina_base.Account.public_key
23-
24-
let identifier = Mina_base.Account.identifier
25-
26-
let key_gen = Mina_base.Account.key_gen
27-
28-
let gen = Mina_base.Account.gen
29-
30-
let create = Mina_base.Account.create
31-
32-
let balance Mina_base.Account.{ balance; _ } = balance
33-
34-
let update_balance t bal = { t with Mina_base.Account.balance = bal }
35-
36-
let token Mina_base.Account.{ token_id; _ } = token_id
37-
end
38-
3911
module Receipt = Mina_base.Receipt
40-
41-
module Hash = struct
42-
module T = struct
43-
type t = Md5.t [@@deriving sexp, hash, compare, bin_io_unversioned, equal]
44-
end
45-
46-
include T
47-
48-
include Codable.Make_base58_check (struct
49-
type t = T.t [@@deriving bin_io_unversioned]
50-
51-
let description = "Ledger test hash"
52-
53-
let version_byte = Base58_check.Version_bytes.ledger_test_hash
54-
end)
55-
56-
include Hashable.Make_binable (T)
57-
58-
(* to prevent pre-image attack,
59-
* important impossible to create an account such that (merge a b = hash_account account) *)
60-
61-
let hash_account account =
62-
Md5.digest_string (Format.sprintf !"0%{sexp: Account.t}" account)
63-
64-
let merge ~height a b =
65-
let res =
66-
Md5.digest_string
67-
(sprintf "test_ledger_%d:%s%s" height (Md5.to_hex a) (Md5.to_hex b))
68-
in
69-
res
70-
71-
let empty_account = hash_account Account.empty
72-
end
73-
7412
module Intf = Merkle_ledger.Intf
7513

7614
module In_memory_kvdb : Intf.Key_value_database with type config := string =
@@ -146,7 +84,13 @@ module Storage_locations : Intf.Storage_locations = struct
14684
let key_value_db_dir = ""
14785
end
14886

149-
module Key = struct
87+
module Key : sig
88+
include Merkle_ledger.Intf.Key with type t = Mina_base.Account.Key.t
89+
90+
val gen : t Base_quickcheck.Generator.t
91+
92+
val gen_keys : int -> t list
93+
end = struct
15094
[%%versioned
15195
module Stable = struct
15296
module V1 = struct
@@ -159,9 +103,9 @@ module Key = struct
159103

160104
let to_string = Signature_lib.Public_key.Compressed.to_base58_check
161105

162-
let gen = Account.key_gen
106+
let gen = Mina_base.Account.key_gen
163107

164-
let empty : t = Account.empty.public_key
108+
let empty : t = Mina_base.Account.empty.public_key
165109

166110
let gen_keys num_keys =
167111
Quickcheck.random_value (Quickcheck.Generator.list_with_length num_keys gen)
@@ -172,7 +116,19 @@ end
172116

173117
module Token_id = Mina_base.Token_id
174118

175-
module Account_id = struct
119+
module Account_id : sig
120+
include
121+
Merkle_ledger.Intf.Account_id
122+
with type token_id := Mina_base.Token_id.t
123+
and type key := Key.t
124+
125+
val gen : t Base_quickcheck.Generator.t
126+
127+
val gen_accounts : int -> t list
128+
129+
val eq :
130+
(Mina_wire_types.Mina_base_account_id.M.V2.t, t) Core_kernel.Type_equal.t
131+
end = struct
176132
[%%versioned
177133
module Stable = struct
178134
module V2 = struct
@@ -183,6 +139,8 @@ module Account_id = struct
183139
end
184140
end]
185141

142+
type t = Mina_base.Account_id.t
143+
186144
include Hashable.Make_binable (Stable.Latest)
187145
include Comparable.Make (Stable.Latest)
188146

@@ -203,9 +161,89 @@ module Account_id = struct
203161
let gen_accounts num_accounts =
204162
Quickcheck.random_value
205163
(Quickcheck.Generator.list_with_length num_accounts gen)
164+
165+
let eq = Core_kernel.Type_equal.T
166+
end
167+
168+
module Account : sig
169+
include
170+
Merkle_ledger.Intf.Account
171+
with type token_id := Token_id.t
172+
and type account_id := Account_id.t
173+
and type balance := Balance.t
174+
and type t = Mina_base.Account.t
175+
176+
val gen : t Base_quickcheck.Generator.t
177+
178+
val create : Mina_base.Account_id.t -> Balance.t -> t
179+
180+
val update_balance : t -> Balance.t -> t
181+
182+
val public_key : t -> Mina_base.Account.Key.t
183+
end = struct
184+
(* want bin_io, not available with Account.t *)
185+
type t = Mina_base.Account.Stable.Latest.t
186+
[@@deriving bin_io_unversioned, sexp, equal, compare, hash, yojson]
187+
188+
(* use Account items needed *)
189+
let empty = Mina_base.Account.empty
190+
191+
let public_key = Mina_base.Account.public_key
192+
193+
let gen = Mina_base.Account.gen
194+
195+
let create = Mina_base.Account.create
196+
197+
let balance Mina_base.Account.{ balance; _ } = balance
198+
199+
let update_balance t bal = { t with Mina_base.Account.balance = bal }
200+
201+
let token Mina_base.Account.{ token_id; _ } = token_id
202+
203+
let identifier ({ public_key; token_id; _ } : t) =
204+
Account_id.create public_key token_id
205+
end
206+
207+
module Hash = struct
208+
module T = struct
209+
type t = Md5.t [@@deriving sexp, hash, compare, bin_io_unversioned, equal]
210+
end
211+
212+
include T
213+
214+
include Codable.Make_base58_check (struct
215+
type t = T.t [@@deriving bin_io_unversioned]
216+
217+
let description = "Ledger test hash"
218+
219+
let version_byte = Base58_check.Version_bytes.ledger_test_hash
220+
end)
221+
222+
include Hashable.Make_binable (T)
223+
224+
(* to prevent pre-image attack,
225+
* important impossible to create an account such that (merge a b = hash_account account) *)
226+
227+
let hash_account account =
228+
Md5.digest_string (Format.sprintf !"0%{sexp: Account.t}" account)
229+
230+
let merge ~height a b =
231+
let res =
232+
Md5.digest_string
233+
(sprintf "test_ledger_%d:%s%s" height (Md5.to_hex a) (Md5.to_hex b))
234+
in
235+
res
236+
237+
let empty_account = hash_account Account.empty
206238
end
207239

208-
module Base_inputs = struct
240+
module Make_base_inputs
241+
(Account : Merkle_ledger.Intf.Account
242+
with type account_id := Account_id.t
243+
and type token_id := Token_id.t
244+
and type balance := Balance.t)
245+
(Hash : Merkle_ledger.Intf.Hash with type account := Account.t) =
246+
struct
209247
module Key = Key
210248
module Account_id = Account_id
211249
module Token_id = Token_id
@@ -221,3 +259,5 @@ module Base_inputs = struct
221259
module Account = Account
222260
module Hash = Hash
223261
end
262+
263+
module Base_inputs = Make_base_inputs (Account) (Hash)

0 commit comments

Comments
 (0)