Source file tezos_tree_encoding.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
include Tree
exception Uninitialized_self_ref
type key = string list
module E = Encoding
module D = Decoding
exception Key_not_found = D.Key_not_found
type 'a encoding = 'a E.t
type 'a decoding = 'a D.t
type 'a t = {encode : 'a encoding; decode : 'a decoding}
let return x = {encode = E.ignore; decode = D.Syntax.return x}
let conv d e {encode; decode} =
{encode = E.contramap e encode; decode = D.map d decode}
let conv_lwt d e {encode; decode} =
{encode = E.contramap_lwt e encode; decode = D.map_lwt d decode}
let scope key {encode; decode} =
{encode = E.scope key encode; decode = D.scope key decode}
let tup2_ a b =
{encode = E.tup2 a.encode b.encode; decode = D.Syntax.both a.decode b.decode}
let tup3_ a b c =
conv
(fun (a, (b, c)) -> (a, b, c))
(fun (a, b, c) -> (a, (b, c)))
(tup2_ a (tup2_ b c))
let tup4_ a b c d =
conv
(fun (a, (b, c, d)) -> (a, b, c, d))
(fun (a, b, c, d) -> (a, (b, c, d)))
(tup2_ a (tup3_ b c d))
let tup5_ a b c d e =
conv
(fun (a, (b, c, d, e)) -> (a, b, c, d, e))
(fun (a, b, c, d, e) -> (a, (b, c, d, e)))
(tup2_ a (tup4_ b c d e))
let tup6_ a b c d e f =
conv
(fun (a, (b, c, d, e, f)) -> (a, b, c, d, e, f))
(fun (a, b, c, d, e, f) -> (a, (b, c, d, e, f)))
(tup2_ a (tup5_ b c d e f))
let tup7_ a b c d e f g =
conv
(fun (a, (b, c, d, e, f, g)) -> (a, b, c, d, e, f, g))
(fun (a, b, c, d, e, f, g) -> (a, (b, c, d, e, f, g)))
(tup2_ a (tup6_ b c d e f g))
let tup8_ a b c d e f g h =
conv
(fun (a, (b, c, d, e, f, g, h)) -> (a, b, c, d, e, f, g, h))
(fun (a, b, c, d, e, f, g, h) -> (a, (b, c, d, e, f, g, h)))
(tup2_ a (tup7_ b c d e f g h))
let tup9_ a b c d e f g h i =
conv
(fun (a, (b, c, d, e, f, g, h, i)) -> (a, b, c, d, e, f, g, h, i))
(fun (a, b, c, d, e, f, g, h, i) -> (a, (b, c, d, e, f, g, h, i)))
(tup2_ a (tup8_ b c d e f g h i))
let tup10_ a b c d e f g h i j =
conv
(fun (a, (b, c, d, e, f, g, h, i, j)) -> (a, b, c, d, e, f, g, h, i, j))
(fun (a, b, c, d, e, f, g, h, i, j) -> (a, (b, c, d, e, f, g, h, i, j)))
(tup2_ a (tup9_ b c d e f g h i j))
let flat_or_wrap ~flatten ix enc =
if flatten then enc else scope [string_of_int ix] enc
let tup2 ~flatten a b =
tup2_ (flat_or_wrap ~flatten 1 a) (flat_or_wrap ~flatten 2 b)
let tup3 ~flatten a b c =
tup3_
(flat_or_wrap ~flatten 1 a)
(flat_or_wrap ~flatten 2 b)
(flat_or_wrap ~flatten 3 c)
let tup4 ~flatten a b c d =
tup4_
(flat_or_wrap ~flatten 1 a)
(flat_or_wrap ~flatten 2 b)
(flat_or_wrap ~flatten 3 c)
(flat_or_wrap ~flatten 4 d)
let tup5 ~flatten a b c d e =
tup5_
(flat_or_wrap ~flatten 1 a)
(flat_or_wrap ~flatten 2 b)
(flat_or_wrap ~flatten 3 c)
(flat_or_wrap ~flatten 4 d)
(flat_or_wrap ~flatten 5 e)
let tup6 ~flatten a b c d e f =
tup6_
(flat_or_wrap ~flatten 1 a)
(flat_or_wrap ~flatten 2 b)
(flat_or_wrap ~flatten 3 c)
(flat_or_wrap ~flatten 4 d)
(flat_or_wrap ~flatten 5 e)
(flat_or_wrap ~flatten 6 f)
let tup7 ~flatten a b c d e f g =
tup7_
(flat_or_wrap ~flatten 1 a)
(flat_or_wrap ~flatten 2 b)
(flat_or_wrap ~flatten 3 c)
(flat_or_wrap ~flatten 4 d)
(flat_or_wrap ~flatten 5 e)
(flat_or_wrap ~flatten 6 f)
(flat_or_wrap ~flatten 7 g)
let tup8 ~flatten a b c d e f g h =
tup8_
(flat_or_wrap ~flatten 1 a)
(flat_or_wrap ~flatten 2 b)
(flat_or_wrap ~flatten 3 c)
(flat_or_wrap ~flatten 4 d)
(flat_or_wrap ~flatten 5 e)
(flat_or_wrap ~flatten 6 f)
(flat_or_wrap ~flatten 7 g)
(flat_or_wrap ~flatten 8 h)
let tup9 ~flatten a b c d e f g h i =
tup9_
(flat_or_wrap ~flatten 1 a)
(flat_or_wrap ~flatten 2 b)
(flat_or_wrap ~flatten 3 c)
(flat_or_wrap ~flatten 4 d)
(flat_or_wrap ~flatten 5 e)
(flat_or_wrap ~flatten 6 f)
(flat_or_wrap ~flatten 7 g)
(flat_or_wrap ~flatten 8 h)
(flat_or_wrap ~flatten 9 i)
let tup10 ~flatten a b c d e f g h i j =
tup10_
(flat_or_wrap ~flatten 1 a)
(flat_or_wrap ~flatten 2 b)
(flat_or_wrap ~flatten 3 c)
(flat_or_wrap ~flatten 4 d)
(flat_or_wrap ~flatten 5 e)
(flat_or_wrap ~flatten 6 f)
(flat_or_wrap ~flatten 7 g)
(flat_or_wrap ~flatten 8 h)
(flat_or_wrap ~flatten 9 i)
(flat_or_wrap ~flatten 10 j)
let raw key = {encode = E.raw key; decode = D.raw key}
let value ?default key de =
{encode = E.value key de; decode = D.value ?default key de}
module Lazy_map_encoding = struct
module type Lazy_map_sig = sig
type key
type 'a t
type 'a producer = key -> 'a Lwt.t
module Map : Stdlib.Map.S with type key = key
val origin : 'a t -> wrapped_tree option
val string_of_key : key -> string
val loaded_bindings : 'a t -> (key * 'a option) list
val create :
?values:'a Map.t ->
?produce_value:'a producer ->
?origin:wrapped_tree ->
unit ->
'a t
end
module type S = sig
type 'a map
val lazy_map : 'a t -> 'a map t
end
module Make (Map : Lazy_map_sig) = struct
let lazy_map value =
let to_key k = [Map.string_of_key k] in
let encode =
E.contramap
(fun map -> (Map.origin map, Map.loaded_bindings map))
(E.lazy_mapping to_key value.encode)
in
let decode =
D.map
(fun (origin, produce_value) -> Map.create ?origin ~produce_value ())
(let open D.Syntax in
let+ produce_value = D.lazy_mapping to_key value.decode in
produce_value)
in
{encode; decode}
end
end
module Lazy_vector_encoding = struct
module type Lazy_vector_sig = sig
type 'a t
type key
type 'a producer = key -> 'a Lwt.t
module Map : Lazy_map_encoding.Lazy_map_sig with type key = key
val origin : 'a t -> wrapped_tree option
val string_of_key : key -> string
val loaded_bindings : 'a t -> (key * 'a option) list
val create :
?first_key:key ->
?values:'a Map.Map.t ->
?produce_value:'a producer ->
?origin:wrapped_tree ->
key ->
'a t
val num_elements : 'a t -> key
val first_key : 'a t -> key
end
module type S = sig
type 'a vector
type key
val lazy_vector : key t -> 'a t -> 'a vector t
end
module Make (Vector : Lazy_vector_sig) = struct
let lazy_vector with_key value =
let to_key k = [Vector.string_of_key k] in
let encode =
E.contramap
(fun vector ->
( (Vector.origin vector, Vector.loaded_bindings vector),
Vector.num_elements vector,
Vector.first_key vector ))
(E.tup3
(E.scope ["contents"] (E.lazy_mapping to_key value.encode))
(E.scope ["length"] with_key.encode)
(E.scope ["head"] with_key.encode))
in
let decode =
D.map
(fun ((origin, produce_value), len, head) ->
Vector.create ~produce_value ~first_key:head ?origin len)
(let open D.Syntax in
let+ x = D.scope ["contents"] (D.lazy_mapping to_key value.decode)
and+ y = D.scope ["length"] with_key.decode
and+ z = D.scope ["head"] with_key.decode in
(x, y, z))
in
{encode; decode}
end
end
module CBV_encoding = struct
module type CBV_sig = sig
type t
type chunk
val origin : t -> wrapped_tree option
val loaded_chunks : t -> (int64 * chunk option) list
val length : t -> int64
val create :
?origin:wrapped_tree -> ?get_chunk:(int64 -> chunk Lwt.t) -> int64 -> t
end
module type S = sig
type cbv
type chunk
val cbv : chunk t -> cbv t
end
module Make (CBV : CBV_sig) = struct
let cbv chunk =
let to_key k = [Int64.to_string k] in
let encode =
E.contramap
(fun vector ->
((CBV.origin vector, CBV.loaded_chunks vector), CBV.length vector))
(E.tup2
(E.scope ["contents"] @@ E.lazy_mapping to_key chunk.encode)
(E.value ["length"] Data_encoding.int64))
in
let decode =
D.map
(fun ((origin, get_chunk), len) -> CBV.create ?origin ~get_chunk len)
(let open D.Syntax in
let+ x = D.scope ["contents"] @@ D.lazy_mapping to_key chunk.decode
and+ y = D.value ["length"] Data_encoding.int64 in
(x, y))
in
{encode; decode}
end
end
type ('tag, 'a) case =
| Case : {
tag : 'tag;
probe : 'a -> 'b Lwt.t option;
extract : 'b -> 'a Lwt.t;
delegate : 'b t;
}
-> ('tag, 'a) case
let case_lwt tag delegate probe = Case {tag; delegate; probe; extract}
let case tag delegate probe =
case_lwt
tag
delegate
(fun x -> Option.map Lwt.return @@ probe x)
(fun x -> Lwt.return @@ extract x)
let tagged_union ?default {encode; decode} cases =
let to_encode_case (Case {tag; delegate; probe; extract = _}) =
E.case_lwt tag delegate.encode probe
in
let to_decode_case (Case {tag; delegate; ; probe = _}) =
D.case_lwt tag delegate.decode extract
in
let encode = E.tagged_union encode (List.map to_encode_case cases) in
let decode = D.tagged_union ?default decode (List.map to_decode_case cases) in
{encode; decode}
let value_option key encoding =
let encode = E.value_option key encoding in
let decode = D.value_option key encoding in
{encode; decode}
let option enc =
tagged_union
~default:(fun () -> None)
(value [] Data_encoding.string)
[
case "Some" enc Fun.id Option.some;
case
"None"
(return ())
(function None -> Some () | _ -> None)
(fun () -> None);
]
let delayed f =
let enc = lazy (f ()) in
let encode =
E.delayed (fun () ->
let {encode; _} = Lazy.force enc in
encode)
in
let decode =
D.delayed (fun () ->
let {decode; _} = Lazy.force enc in
decode)
in
{encode; decode}
let either enc_a enc_b =
tagged_union
(value [] Data_encoding.string)
[
case
"Left"
enc_a
(function Either.Left x -> Some x | _ -> None)
(function x -> Left x);
case
"Right"
enc_b
(function Either.Right x -> Some x | _ -> None)
(function x -> Right x);
]
module type TREE = S
type wrapped_tree = Tree.wrapped_tree
module Wrapped : TREE with type tree = wrapped_tree = Tree.Wrapped
let wrapped_tree : wrapped_tree t =
{encode = E.wrapped_tree; decode = D.wrapped_tree}
module Runner = struct
module type S = sig
type tree
val encode : 'a t -> 'a -> tree -> tree Lwt.t
val decode : 'a t -> tree -> 'a Lwt.t
end
module Make (T : TREE) = struct
type tree = T.tree
let encode {encode; _} value tree = E.run (module T) encode value tree
let decode {decode; _} tree = D.run (module T) decode tree
end
end
module Encodings_util = struct
module type Bare_tezos_context_sig = sig
type t
type tree
type index
module Tree :
Tezos_context_sigs.Context.TREE
with type t := t
and type key := string list
and type value := bytes
and type tree := tree
val init :
?patch_context:(t -> t tzresult Lwt.t) ->
?readonly:bool ->
?index_log_size:int ->
string ->
index Lwt.t
val empty : index -> t
end
module Make (Ctx : Bare_tezos_context_sig) = struct
type Tree.tree_instance += Tree of Ctx.tree
module Tree = struct
type tree = Ctx.tree
include Ctx.Tree
let select = function Tree t -> t | _ -> raise Incorrect_tree_type
let wrap t = Tree t
end
module Tree_encoding_runner = Runner.Make (Tree)
let empty_tree () =
let open Lwt_syntax in
let* index = Ctx.init "/tmp" in
let empty_store = Ctx.empty index in
return @@ Ctx.Tree.empty empty_store
end
end