Source file proof.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
open! Import
include Proof_intf
module Make
(C : Type.S)
(H : Type.S) (S : sig
type step [@@deriving irmin]
end)
(M : Type.S) =
struct
type contents = C.t [@@deriving irmin]
type hash = H.t [@@deriving irmin]
type step = S.step [@@deriving irmin]
type metadata = M.t [@@deriving irmin]
type kinded_hash = [ `Contents of hash * metadata | `Node of hash ]
[@@deriving irmin]
type 'a inode = { length : int; proofs : (int * 'a) list } [@@deriving irmin]
type 'a inode_extender = { length : int; segments : int list; proof : 'a }
[@@deriving irmin]
type tree =
| Contents of contents * metadata
| Blinded_contents of hash * metadata
| Node of (step * tree) list
| Blinded_node of hash
| Inode of inode_tree inode
| Extender of inode_tree inode_extender
[@@deriving irmin]
and inode_tree =
| Blinded_inode of hash
| Inode_values of (step * tree) list
| Inode_tree of inode_tree inode
| Inode_extender of inode_tree inode_extender
[@@deriving irmin]
type elt =
| Contents of contents
| Node of (step * kinded_hash) list
| Inode of hash inode
| Inode_extender of hash inode_extender
[@@deriving irmin]
type stream = elt Seq.t [@@deriving irmin]
type 'a t = { before : kinded_hash; after : kinded_hash; state : 'a }
[@@deriving irmin]
let before t = t.before
let after t = t.after
let state t = t.state
let v ~before ~after state = { after; before; state }
end
type bad_stream_exn =
| Stream_too_long of { context : string; reason : string }
| Stream_too_short of { context : string; reason : string }
| Proof_mismatch of { context : string; reason : string }
exception Bad_proof of { context : string }
exception Bad_stream of bad_stream_exn
let bad_proof_exn context = raise (Bad_proof { context })
let bad_stream_too_long context reason =
raise (Bad_stream (Stream_too_long { context; reason }))
let bad_stream_too_short context reason =
raise (Bad_stream (Stream_too_short { context; reason }))
let bad_stream_exn context reason =
raise (Bad_stream (Proof_mismatch { context; reason }))
let bad_stream_exn_fmt s fmt = Fmt.kstr (bad_stream_exn ("Proof.Env." ^ s)) fmt
let bad_stream_too_short_fmt s fmt =
Fmt.kstr (bad_stream_too_short ("Proof.Env." ^ s)) fmt
module Env
(B : Backend.S)
(P : S
with type contents := B.Contents.Val.t
and type hash := B.Hash.t
and type step := B.Node.Val.step
and type metadata := B.Node.Val.metadata) =
struct
module H = B.Hash
module Hashes = struct
include Hashtbl.Make (struct
type t = H.t
let hash = H.short_hash
let equal = Type.(unstage (equal H.t))
end)
let of_list l = of_seq (List.to_seq l)
let to_list t = List.of_seq (to_seq t)
let t elt_t = Type.map [%typ: (H.t * elt) list] of_list to_list
end
type mode = Produce | Serialise | Deserialise | Consume [@@deriving irmin]
type kind = Set | Stream [@@deriving irmin]
module Set = struct
type produce = {
nodes : B.Node.Val.t Hashes.t;
contents : B.Contents.Val.t Hashes.t;
}
[@@deriving irmin]
type deserialise = {
nodes : B.Node_portable.t Hashes.t;
contents : B.Contents.Val.t Hashes.t;
}
[@@deriving irmin]
type t =
| Produce of produce
| Serialise of produce
| Deserialise of deserialise
| Consume of deserialise
[@@deriving irmin]
let producer () =
Produce { contents = Hashes.create 13; nodes = Hashes.create 13 }
let deserialiser () =
Deserialise { contents = Hashes.create 13; nodes = Hashes.create 13 }
end
module Stream = struct
let ref_t v = Type.map v ref ( ! )
type produce = {
set : unit Hashes.t;
singleton_inodes : (int * H.t) Hashes.t;
rev_elts : (H.t * P.elt) list ref;
rev_elts_size : int ref;
}
[@@deriving irmin]
type consume = {
nodes : B.Node_portable.t Hashes.t;
contents : B.Contents.Val.t Hashes.t;
stream : P.elt Seq.t ref;
}
[@@deriving irmin]
type t = Produce of produce | Consume of consume [@@deriving irmin]
let producer () =
let set = Hashes.create 13 in
let singleton_inodes = Hashes.create 13 in
let rev_elts = ref [] in
let rev_elts_size = ref 0 in
Produce { set; singleton_inodes; rev_elts; rev_elts_size }
let consumer stream =
let nodes = Hashes.create 13 in
let contents = Hashes.create 13 in
let stream = ref stream in
Consume { nodes; contents; stream }
let push { rev_elts; rev_elts_size; _ } h_elt index =
incr rev_elts_size;
rev_elts := List.insert_exn !rev_elts index h_elt
end
type v = Empty | Set of Set.t | Stream of Stream.t [@@deriving irmin]
type t = v ref
let t = Type.map v_t ref ( ! )
let empty () : t = ref Empty
let is_empty t = !t = Empty
let copy ~into t = into := !t
type hash = H.t [@@deriving irmin ~equal ~pp]
let rec forward_lookup h singleton_inodes : (int * hash) list option =
match Hashes.find_opt singleton_inodes h with
| None -> None
| Some (i', h') -> (
match forward_lookup h' singleton_inodes with
| None -> Some [ (i', h') ]
| Some l -> Some ((i', h') :: l))
let apply_extenders ~length singleton_inodes skips proofs =
let rec accumulate_segments ~(acc : int Reversed_list.t) h = function
| [] -> (Reversed_list.rev acc, h)
| (i', h') :: rest -> accumulate_segments ~acc:(i' :: acc) h' rest
in
let inode = P.Inode { length; proofs } in
match proofs with
| [ (i, h) ] -> (
match forward_lookup h singleton_inodes with
| None -> inode
| Some ls -> (
let () =
match List.rev ((i, h) :: ls) with
| [] | [ _ ] -> failwith "idk"
| _ :: tl -> List.iter (fun (_, h) -> Hashes.add skips h ()) tl
in
let i, h = accumulate_segments ~acc:[ i ] h ls in
match i with
| [] | [ _ ] -> assert false
| segments -> P.Inode_extender { length; segments; proof = h }))
| _ -> inode
let post_processing singleton_inodes (stream : (hash * P.elt) list) :
P.elt list =
let skips = Hashes.create 13 in
let rec aux rev_elts = function
| [] -> List.rev rev_elts
| (h, elt) :: rest ->
if Hashes.mem skips h then aux rev_elts rest
else
let elt' : P.elt =
match (elt : P.elt) with
| P.Inode { length; proofs } ->
apply_extenders ~length singleton_inodes skips proofs
| Node ls -> Node ls
| Contents c -> Contents c
| Inode_extender _ -> assert false
in
aux (elt' :: rev_elts) rest
in
aux [] stream
let to_stream t =
match !t with
| Stream (Produce { rev_elts; singleton_inodes; _ }) ->
List.rev !rev_elts |> post_processing singleton_inodes |> List.to_seq
| _ -> assert false
let is_empty_stream t =
match !t with
| Stream (Consume { stream; _ }) -> (
match !stream () with Seq.Nil -> true | _ -> false)
| _ -> false
let set_mode t (kind : kind) mode =
match kind with
| Set -> (
match (!t, mode) with
| Empty, Produce -> t := Set Set.(producer ())
| Empty, Deserialise -> t := Set Set.(deserialiser ())
| Set (Produce set), Serialise -> t := Set Set.(Serialise set)
| Set (Deserialise set), Consume -> t := Set Set.(Consume set)
| _ -> assert false)
| Stream -> (
match (!t, mode) with
| Empty, Produce -> t := Stream (Stream.producer ())
| _ -> assert false)
let with_set_consume f =
let t = ref Empty in
set_mode t Set Deserialise;
let stop_deserialise () = set_mode t Set Consume in
let+ res = f t ~stop_deserialise in
t := Empty;
res
let with_set_produce f =
let t = ref Empty in
set_mode t Set Produce;
let start_serialise () = set_mode t Set Serialise in
let+ res = f t ~start_serialise in
t := Empty;
res
let with_stream_produce f =
let t = ref Empty in
set_mode t Stream Produce;
let to_stream () = to_stream t in
let+ res = f t ~to_stream in
t := Empty;
res
let with_stream_consume stream f =
let t = Stream (Stream.consumer stream) |> ref in
let is_empty () = is_empty_stream t in
let+ res = f t ~is_empty in
t := Empty;
res
module Contents_hash = Hash.Typed (H) (B.Contents.Val)
let check_contents_integrity v h =
let h' = Contents_hash.hash v in
if not (equal_hash h' h) then
bad_stream_exn_fmt "check_contents_integrity" "got %a expected %a" pp_hash
h' pp_hash h
let check_node_integrity v h =
let h' =
try B.Node_portable.hash_exn ~force:false v
with Not_found ->
assert false
in
if not (equal_hash h' h) then
bad_stream_exn_fmt "check_node_integrity" "got %a expected %a" pp_hash h'
pp_hash h
let dehydrate_stream_node v =
match B.Node.Val.head v with
| `Node l ->
let l =
List.map
(function
| step, `Contents (k, m) ->
(step, `Contents (B.Contents.Key.to_hash k, m))
| step, `Node k -> (step, `Node (B.Node.Key.to_hash k)))
l
in
P.Node l
| `Inode (length, proofs) -> P.Inode { length; proofs }
let rehydrate_stream_node ~depth (elt : P.elt) h =
let bad_stream_exn_fmt = bad_stream_exn_fmt "rehydrate_stream_node" in
match elt with
| Contents _ ->
bad_stream_exn_fmt
"found contents at depth %d when looking for node with hash %a" depth
pp_hash h
| Node l -> (
match B.Node_portable.of_proof ~depth (`Values l) with
| Some v -> v
| None ->
bad_stream_exn_fmt
"could not deserialise Node at depth %d when looking for hash %a"
depth pp_hash h)
| Inode { length; proofs } ->
let proofs = List.map (fun (i, h) -> (i, `Blinded h)) proofs in
let inode = `Inode (length, proofs) in
let v =
match B.Node_portable.of_proof ~depth inode with
| Some v -> v
| None ->
bad_stream_exn_fmt
"could not deserialise Inode at depth %d when looking for hash \
%a"
depth pp_hash h
in
v
| Inode_extender { length; segments; proof } ->
let elt =
List.fold_left
(fun acc i -> `Inode (length, [ (i, acc) ]))
(`Blinded proof) (List.rev segments)
in
let v =
match B.Node_portable.of_proof ~depth elt with
| Some v -> v
| None ->
bad_stream_exn_fmt
"could not deserialise Inode at depth %d when looking for hash \
%a"
depth pp_hash h
in
v
let rehydrate_stream_contents (elt : P.elt) h =
let err k =
bad_stream_exn_fmt "find_contents"
"found %s when looking Contents with hash %a" k pp_hash h
in
match elt with
| Node _ -> err "Node"
| Inode _ -> err "Inode"
| Inode_extender _ -> err "Inode"
| Contents v -> v
let find_contents t h =
match !t with
| Empty -> None
| Set (Produce set) ->
Hashes.find_opt set.contents h
| Set (Serialise set) ->
Hashes.find_opt set.contents h
| Set (Deserialise _) ->
assert false
| Set (Consume set) ->
Hashes.find_opt set.contents h
| Stream (Produce _) ->
None
| Stream (Consume { contents; stream; _ }) -> (
match Hashes.find_opt contents h with
| Some v -> Some v
| None -> (
match !stream () with
| Seq.Nil ->
bad_stream_too_short_fmt "find_contents"
"empty stream when looking for hash %a" pp_hash h
| Cons (elt, rest) ->
let v = rehydrate_stream_contents elt h in
check_contents_integrity v h;
stream := rest;
Hashes.add contents h v;
Some v))
let add_contents_from_store t h v =
match !t with
| Empty -> ()
| Set (Produce set) ->
assert (not (Hashes.mem set.contents h));
Hashes.add set.contents h v
| Set (Serialise _) ->
assert false
| Set (Deserialise _) ->
assert false
| Set (Consume _) ->
assert false
| Stream (Produce ({ set; _ } as cache)) ->
if not @@ Hashes.mem set h then (
Hashes.add set h ();
let h_elt : hash * P.elt = (h, Contents v) in
Stream.push cache h_elt 0)
| Stream (Consume _) ->
assert false
let add_contents_from_proof t h v =
match !t with
| Set (Deserialise set) ->
Hashes.replace set.contents h v
| _ -> assert false
let find_node t h =
match !t with
| Empty -> None
| Set (Produce set) ->
Hashes.find_opt set.nodes h
| Set (Serialise set) ->
Hashes.find_opt set.nodes h
| Set (Deserialise _) ->
assert false
| Set (Consume _) ->
None
| Stream (Produce _) ->
None
| Stream (Consume _) ->
None
let find_recpnode t _find ~expected_depth h =
assert (expected_depth > 0);
match !t with
| Stream (Consume { nodes; stream; _ }) -> (
match Hashes.find_opt nodes h with
| Some v -> Some v
| None -> (
match !stream () with
| Seq.Nil ->
bad_stream_too_short_fmt "find_recnode"
"empty stream when looking for hash %a" pp_hash h
| Cons (v, rest) ->
let v = rehydrate_stream_node ~depth:expected_depth v h in
check_node_integrity v h;
stream := rest;
Hashes.add nodes h v;
Some v))
| _ -> assert false
let find_pnode t h =
match !t with
| Set (Consume set) ->
Hashes.find_opt set.nodes h
| Stream (Consume { nodes; stream; _ }) -> (
match Hashes.find_opt nodes h with
| Some v -> Some v
| None -> (
match !stream () with
| Seq.Nil ->
bad_stream_too_short_fmt "find_node"
"empty stream when looking for hash %a" pp_hash h
| Cons (v, rest) ->
stream := rest;
let v =
rehydrate_stream_node ~depth:0 v h
in
let v =
B.Node_portable.with_handler (find_recpnode t) v
in
let (_ : [ `Node of _ | `Inode of _ ]) =
B.Node_portable.head v
in
check_node_integrity v h;
Hashes.add nodes h v;
Some v))
| _ -> None
let add_recnode_from_store t find ~expected_depth k =
assert (expected_depth > 0);
match !t with
| Stream (Produce ({ set; singleton_inodes; _ } as cache)) -> (
match find ~expected_depth k with
| None -> None
| Some v ->
let h = B.Node.Key.to_hash k in
if not @@ Hashes.mem set h then (
Hashes.add set h ();
let elt = dehydrate_stream_node v in
let () =
match elt with
| P.Inode { proofs = [ bucket ]; _ } ->
Hashes.add singleton_inodes h bucket
| _ -> ()
in
Stream.push cache (h, elt) 0);
Some v)
| _ -> assert false
let add_node_from_store t h v =
match !t with
| Empty -> v
| Set (Produce set) ->
assert (not (Hashes.mem set.nodes h));
Hashes.add set.nodes h v;
v
| Set (Serialise _) ->
assert false
| Set (Deserialise _) ->
assert false
| Set (Consume _) ->
assert false
| Stream (Produce ({ set; rev_elts_size; singleton_inodes; _ } as cache)) ->
let new_hash = not @@ Hashes.mem set h in
let v =
B.Node.Val.with_handler (add_recnode_from_store t) v
in
if new_hash then (
Hashes.add set h ();
let len0 = !rev_elts_size in
let elt = dehydrate_stream_node v in
let len1 = !rev_elts_size in
let delta =
len1 - len0
in
let () =
match elt with
| P.Inode { proofs = [ bucket ]; _ } ->
Hashes.add singleton_inodes h bucket
| _ -> ()
in
Stream.push cache (h, elt) delta);
v
| Stream (Consume _) ->
assert false
let add_pnode_from_proof t h v =
match !t with
| Set (Deserialise set) ->
Hashes.replace set.nodes h v
| _ -> assert false
end