···4949 let%lwt result = Lazy.force lazy_opt_lwt in
5050 f result
51515252-(* produces a cid and cbor-encoded bytes for a given tree *)
5353-let serialize node : (Cid.t * bytes) Lwt.t =
5454- let sorted_entries =
5555- List.sort (fun a b -> String.compare a.key b.key) node.entries
5656- in
5757- let rec aux node =
5858- let%lwt left =
5959- node.left
6060- >>? function
6161- | Some l ->
6262- let%lwt cid, _ = aux l in
6363- Lwt.return_some cid
6464- | None ->
6565- Lwt.return_none
6666- in
6767- let last_key = ref "" in
6868- let%lwt mst_entries =
6969- Lwt_list.map_s
7070- (fun entry ->
7171- let%lwt right =
7272- entry.right
7373- >>? function
7474- | Some r ->
7575- let%lwt cid, _ = aux r in
7676- Lwt.return (Some cid)
7777- | None ->
7878- Lwt.return None
7979- in
8080- let prefix_len = Util.shared_prefix_length !last_key entry.key in
8181- last_key := entry.key ;
8282- Lwt.return
8383- { k=
8484- Bytes.of_string
8585- (String.sub entry.key prefix_len
8686- (String.length entry.key - prefix_len) )
8787- ; p= prefix_len
8888- ; v= entry.value
8989- ; t= right } )
9090- node.entries
9191- in
9292- let encoded = Dag_cbor.encode (encode_node_raw {l= left; e= mst_entries}) in
9393- let cid = Cid.create Dcbor encoded in
9494- Lwt.return (cid, encoded)
9595- in
9696- aux {node with entries= sorted_entries}
9797-9852module Make (Store : Writable_blockstore) = struct
9953 type bs = Store.t
10054···432386 let to_car t : bytes Lwt.t =
433387 t |> to_blocks_stream |> Car.blocks_to_car (Some t.root)
434388389389+ (* produces a cid and cbor-encoded bytes for a given tree *)
390390+ let serialize t node : (Cid.t * bytes, exn) Lwt_result.t =
391391+ let sorted_entries =
392392+ List.sort (fun a b -> String.compare a.key b.key) node.entries
393393+ in
394394+ let rec aux node : (Cid.t * bytes) Lwt.t =
395395+ let%lwt left =
396396+ node.left
397397+ >>? function
398398+ | Some l ->
399399+ let%lwt cid, _ = aux l in
400400+ Lwt.return_some cid
401401+ | None ->
402402+ Lwt.return_none
403403+ in
404404+ let last_key = ref "" in
405405+ let%lwt mst_entries =
406406+ Lwt_list.map_s
407407+ (fun entry ->
408408+ let%lwt right =
409409+ entry.right
410410+ >>? function
411411+ | Some r ->
412412+ let%lwt cid, _ = aux r in
413413+ Lwt.return_some cid
414414+ | None ->
415415+ Lwt.return_none
416416+ in
417417+ let prefix_len = Util.shared_prefix_length !last_key entry.key in
418418+ last_key := entry.key ;
419419+ Lwt.return
420420+ { k=
421421+ Bytes.of_string
422422+ (String.sub entry.key prefix_len
423423+ (String.length entry.key - prefix_len) )
424424+ ; p= prefix_len
425425+ ; v= entry.value
426426+ ; t= right } )
427427+ node.entries
428428+ in
429429+ let encoded =
430430+ Dag_cbor.encode (encode_node_raw {l= left; e= mst_entries})
431431+ in
432432+ let cid = Cid.create Dcbor encoded in
433433+ match%lwt Store.put_block t.blockstore cid encoded with
434434+ | Ok _ ->
435435+ Lwt.return (cid, encoded)
436436+ | Error e ->
437437+ raise e
438438+ in
439439+ try%lwt Lwt.map Result.ok (aux {node with entries= sorted_entries})
440440+ with e -> Lwt.return_error e
441441+435442 (* raw-node helpers for covering proofs: operate on stored bytes, not re-serialization *)
436443 type interleaved_entry =
437444 | Tree of Cid.t
···728735 Lwt.return sorted
729736730737 (* creates and persists an empty mst *)
731731- let create_empty blockstore : t Lwt.t =
738738+ let create_empty blockstore : (t, exn) Lwt_result.t =
732739 let encoded = Dag_cbor.encode (encode_node_raw {l= None; e= []}) in
733740 let cid = Cid.create Dcbor encoded in
734734- let%lwt () = Store.put_block blockstore cid encoded in
735735- Lwt.return {blockstore; root= cid}
741741+ Lwt_result.bind (Store.put_block blockstore cid encoded) (fun _ ->
742742+ Lwt.return_ok {blockstore; root= cid} )
736743737744 (* returns the cid for a given key, if it exists *)
738745 let get_cid t key : Cid.t option Lwt.t =
···795802 | [] ->
796803 let encoded = Dag_cbor.encode (encode_node_raw {l= None; e= []}) in
797804 let cid = Cid.create Dcbor encoded in
798798- Store.put_block blockstore cid encoded >|= fun () -> (cid, 0)
805805+ Store.put_block blockstore cid encoded >|= fun _ -> (cid, 0)
799806 | _ ->
800807 let with_layers =
801808 List.map (fun (k, v) -> (k, v, Util.leading_zeros_on_hash k)) pairs
···833840 in
834841 let cid' = Cid.create Dcbor encoded in
835842 Store.put_block blockstore cid' encoded
836836- >>= fun () -> wrap cid' (layer + 1)
843843+ >>= fun _ -> wrap cid' (layer + 1)
837844 in
838845 wrap cid child_layer >|= fun c -> Some c
839846 in
···874881 in
875882 let cid' = Cid.create Dcbor encoded in
876883 Store.put_block blockstore cid' encoded
877877- >>= fun () -> wrap cid' (layer + 1)
884884+ >>= fun _ -> wrap cid' (layer + 1)
878885 in
879886 wrap cid child_layer >|= fun c -> Some c )
880887 rights
···895902 let node_raw = {l= l_cid; e= entries_raw} in
896903 let encoded = Dag_cbor.encode (encode_node_raw node_raw) in
897904 let cid = Cid.create Dcbor encoded in
898898- Store.put_block blockstore cid encoded >|= fun () -> (cid, root_layer)
905905+ Store.put_block blockstore cid encoded >|= fun _ -> (cid, root_layer)
899906 in
900907 persist_from_sorted sorted >|= fun (root, _) -> {blockstore; root}
901908
+4-4
mist/lib/storage/blockstore.ml
···13131414 include Readable with type t := t
15151616- val put_block : t -> Cid.t -> bytes -> unit Lwt.t
1616+ val put_block : t -> Cid.t -> bytes -> (bool, exn) Lwt_result.t
17171818- val put_many : t -> Block_map.t -> unit Lwt.t
1818+ val put_many : t -> Block_map.t -> (int, exn) Lwt_result.t
19192020- val delete_block : t -> Cid.t -> unit Lwt.t
2020+ val delete_block : t -> Cid.t -> (bool, exn) Lwt_result.t
21212222- val delete_many : t -> Cid.t list -> unit Lwt.t
2222+ val delete_many : t -> Cid.t list -> (int, exn) Lwt_result.t
2323end