From e327e182892f59d6d6f5519c1522e9cdf8ff494b Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Wed, 14 Feb 2024 12:37:21 +0100 Subject: [PATCH 01/30] Merkle_ledger: simplify functions --- src/lib/merkle_ledger/database.ml | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/lib/merkle_ledger/database.ml b/src/lib/merkle_ledger/database.ml index 863fcbcdf54..09d984401d6 100644 --- a/src/lib/merkle_ledger/database.ml +++ b/src/lib/merkle_ledger/database.ml @@ -290,25 +290,13 @@ module Make (Inputs : Inputs_intf) : Result.map location_result ~f:(fun location -> set mdb key location ; location ) - let last_location_address mdb = - match - last_location_key () |> get_raw mdb |> Result.of_option ~error:() - |> Result.bind ~f:(Location.parse ~ledger_depth:mdb.depth) - with - | Error () -> - None - | Ok parsed_location -> - Some (Location.to_path_exn parsed_location) - let last_location mdb = - match - last_location_key () |> get_raw mdb |> Result.of_option ~error:() - |> Result.bind ~f:(Location.parse ~ledger_depth:mdb.depth) - with - | Error () -> - None - | Ok parsed_location -> - Some parsed_location + last_location_key () |> get_raw mdb + |> Option.bind ~f:(fun data -> + Location.parse ~ledger_depth:mdb.depth data |> Result.ok ) + + let last_location_address mdb = + Option.map (last_location mdb) ~f:Location.to_path_exn end let get_at_index_exn mdb index = From efd8125066aa04abbd79946e2154a14df182a8fc Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 15:35:50 +0100 Subject: [PATCH 02/30] Merkle_ledger: interface for Util --- src/lib/merkle_ledger/util.mli | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/lib/merkle_ledger/util.mli diff --git a/src/lib/merkle_ledger/util.mli b/src/lib/merkle_ledger/util.mli new file mode 100644 index 00000000000..7acf5cd1b6e --- /dev/null +++ b/src/lib/merkle_ledger/util.mli @@ -0,0 +1,68 @@ +module type Inputs_intf = sig + module Location : Location_intf.S + + module Location_binable : + Core_kernel.Hashable.S_binable with type t := Location.t + + module Key : Intf.Key + + module Token_id : Intf.Token_id + + module Account_id : + Intf.Account_id with type key := Key.t and type token_id := Token_id.t + + module Balance : Intf.Balance + + module Account : + Intf.Account + with type balance := Balance.t + and type account_id := Account_id.t + and type token_id := Token_id.t + + module Hash : Intf.Hash with type account := Account.t + + module Base : sig + type t + + val get : t -> Location.t -> Account.t option + + val last_filled : t -> Location.t option + end + + val get_hash : Base.t -> Location.t -> Hash.t + + val location_of_account_addr : Location.Addr.t -> Location.t + + val location_of_hash_addr : Location.Addr.t -> Location.t + + val ledger_depth : Base.t -> int + + val set_raw_hash_batch : Base.t -> (Location.t * Hash.t) list -> unit + + val set_raw_account_batch : Base.t -> (Location.t * Account.t) list -> unit + + val set_location_batch : + last_location:Location.t + -> Base.t + -> (Account_id.t * Location.t) Mina_stdlib.Nonempty_list.t + -> unit +end + +module Make (Inputs : Inputs_intf) : sig + val get_all_accounts_rooted_at_exn : + Inputs.Base.t + -> Inputs.Location.Addr.t + -> (Inputs.Location.Addr.t * Inputs.Account.t) list + + val set_hash_batch : + Inputs.Base.t -> (Inputs.Location.t * Inputs.Hash.t) list -> unit + + val set_batch : + Inputs.Base.t -> (Inputs.Location.t * Inputs.Account.t) list -> unit + + val set_batch_accounts : + Inputs.Base.t -> (Inputs.Location.Addr.t * Inputs.Account.t) list -> unit + + val set_all_accounts_rooted_at_exn : + Inputs.Base.t -> Inputs.Location.Addr.t -> Inputs.Account.t list -> unit +end From 7c971639bbcfb03afc178c956fb4b9ca4d05b39b Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 16:29:55 +0100 Subject: [PATCH 03/30] Merkle_ledger: interface for Location --- src/lib/merkle_ledger/location.mli | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/lib/merkle_ledger/location.mli diff --git a/src/lib/merkle_ledger/location.mli b/src/lib/merkle_ledger/location.mli new file mode 100644 index 00000000000..2feda1c9061 --- /dev/null +++ b/src/lib/merkle_ledger/location.mli @@ -0,0 +1,18 @@ +module Bigstring : sig + [%%versioned: + module Stable : sig + module V1 : sig + type t = Core_kernel.Bigstring.Stable.V1.t [@@deriving sexp, compare] + + include Core_kernel.Binable.S with type t := t + + val hash_fold_t : Core_kernel.Hash.state -> t -> Core_kernel.Hash.state + + val hash : t -> Core_kernel.Hash.hash_value + end + end] + + include Core_kernel.Hashable.S with type t := t +end + +module T : Location_intf.S From 69116d8b1b1480b7611bf71a0444a750f31adc71 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 16:30:11 +0100 Subject: [PATCH 04/30] Merkle_ledger: remove unused equal function --- src/lib/merkle_ledger/location.ml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lib/merkle_ledger/location.ml b/src/lib/merkle_ledger/location.ml index ee8e1c61e33..494aa5489e4 100644 --- a/src/lib/merkle_ledger/location.ml +++ b/src/lib/merkle_ledger/location.ml @@ -10,8 +10,6 @@ module Bigstring = struct let to_latest = Fn.id - let equal = Bigstring.equal - let hash t = Bigstring.to_string t |> String.hash let hash_fold_t hash_state t = @@ -27,8 +25,7 @@ module Bigstring = struct end end] - [%%define_locally - Bigstring.(get, length, equal, create, to_string, set, blit, sub)] + [%%define_locally Bigstring.(get, length, create, to_string, set, blit, sub)] include Hashable.Make (Stable.Latest) end From 967626f2f91cbe8007167e744108d88b9f2307a4 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 16:37:35 +0100 Subject: [PATCH 05/30] Merkle_ledger: remove unused Merkle_path_intf module --- src/lib/merkle_ledger/merkle_path_intf.ml | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/lib/merkle_ledger/merkle_path_intf.ml diff --git a/src/lib/merkle_ledger/merkle_path_intf.ml b/src/lib/merkle_ledger/merkle_path_intf.ml deleted file mode 100644 index 4627b1ddd3d..00000000000 --- a/src/lib/merkle_ledger/merkle_path_intf.ml +++ /dev/null @@ -1,11 +0,0 @@ -module type S = sig - type hash - - type elem = [ `Left of hash | `Right of hash ] [@@deriving sexp, equal] - - val elem_hash : elem -> hash - - type t = elem list [@@deriving sexp, equal] - - val implied_root : t -> hash -> hash -end From 5dd4c1122d892e58ac0a824a8e5763831de01384 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 16:53:17 +0100 Subject: [PATCH 06/30] Merkle_ledger: actual interface for Merkle_path --- src/lib/merkle_ledger/merkle_path.mli | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/lib/merkle_ledger/merkle_path.mli diff --git a/src/lib/merkle_ledger/merkle_path.mli b/src/lib/merkle_ledger/merkle_path.mli new file mode 100644 index 00000000000..ee77bd9812f --- /dev/null +++ b/src/lib/merkle_ledger/merkle_path.mli @@ -0,0 +1,21 @@ +module type S = sig + type hash + + type elem = [ `Left of hash | `Right of hash ] [@@deriving sexp, equal] + + val elem_hash : elem -> hash + + type t = elem list [@@deriving sexp, equal] + + val implied_root : t -> hash -> hash + + val check_path : t -> hash -> hash -> bool +end + +module Make (Hash : sig + type t [@@deriving sexp, equal] + + val merge : height:int -> t -> t -> t + + val equal : t -> t -> bool +end) : S with type hash := Hash.t From 0529763bf620ecabb1aaae5279631873a70aa671 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 16:53:56 +0100 Subject: [PATCH 07/30] Merkle_ledger: rm unused check_path in Merkle_path --- src/lib/merkle_ledger/merkle_path.ml | 5 ----- src/lib/merkle_ledger/merkle_path.mli | 2 -- 2 files changed, 7 deletions(-) diff --git a/src/lib/merkle_ledger/merkle_path.ml b/src/lib/merkle_ledger/merkle_path.ml index a2a5acd294d..2b14ad8a12c 100644 --- a/src/lib/merkle_ledger/merkle_path.ml +++ b/src/lib/merkle_ledger/merkle_path.ml @@ -10,8 +10,6 @@ module type S = sig type t = elem list [@@deriving sexp, equal] val implied_root : t -> hash -> hash - - val check_path : t -> hash -> hash -> bool end module Make (Hash : sig @@ -38,7 +36,4 @@ end) : S with type hash := Hash.t = struct in (acc, height + 1) ) |> fst - - let check_path t leaf_hash root_hash = - Hash.equal (implied_root t leaf_hash) root_hash end diff --git a/src/lib/merkle_ledger/merkle_path.mli b/src/lib/merkle_ledger/merkle_path.mli index ee77bd9812f..fc7d744a766 100644 --- a/src/lib/merkle_ledger/merkle_path.mli +++ b/src/lib/merkle_ledger/merkle_path.mli @@ -8,8 +8,6 @@ module type S = sig type t = elem list [@@deriving sexp, equal] val implied_root : t -> hash -> hash - - val check_path : t -> hash -> hash -> bool end module Make (Hash : sig From dd18e9d037c4a7fbaf05447b715504be8f41ff56 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 16:58:40 +0100 Subject: [PATCH 08/30] Merkle_ledger: interface for Null_ledger --- src/lib/merkle_ledger/null_ledger.mli | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/lib/merkle_ledger/null_ledger.mli diff --git a/src/lib/merkle_ledger/null_ledger.mli b/src/lib/merkle_ledger/null_ledger.mli new file mode 100644 index 00000000000..b4e8196de16 --- /dev/null +++ b/src/lib/merkle_ledger/null_ledger.mli @@ -0,0 +1,22 @@ +module type Inputs_intf = sig + include Base_inputs_intf.S + + module Location : Location_intf.S +end + +module Make (Inputs : Inputs_intf) : sig + include + Base_ledger_intf.S + with module Addr = Inputs.Location.Addr + with module Location = Inputs.Location + with type key := Inputs.Key.t + and type token_id := Inputs.Token_id.t + and type token_id_set := Inputs.Token_id.Set.t + and type account_id := Inputs.Account_id.t + and type account_id_set := Inputs.Account_id.Set.t + and type hash := Inputs.Hash.t + and type root_hash := Inputs.Hash.t + and type account := Inputs.Account.t + + val create : depth:int -> unit -> t +end From e2ef0ce384d53c40b5cf4c4e9b8e3a3d6f5be573 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 17:05:15 +0100 Subject: [PATCH 09/30] Merkle_ledger: interface for Database --- src/lib/merkle_ledger/database.mli | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/lib/merkle_ledger/database.mli diff --git a/src/lib/merkle_ledger/database.mli b/src/lib/merkle_ledger/database.mli new file mode 100644 index 00000000000..a2283e247f0 --- /dev/null +++ b/src/lib/merkle_ledger/database.mli @@ -0,0 +1,25 @@ +module type Inputs_intf = sig + include Base_inputs_intf.S + + module Location : Location_intf.S + + module Location_binable : + Core_kernel.Hashable.S_binable with type t := Location.t + + module Kvdb : Intf.Key_value_database with type config := string + + module Storage_locations : Intf.Storage_locations +end + +module Make (Inputs : Inputs_intf) : + Database_intf.S + with module Location = Inputs.Location + and module Addr = Inputs.Location.Addr + and type key := Inputs.Key.t + and type token_id := Inputs.Token_id.t + and type token_id_set := Inputs.Token_id.Set.t + and type account := Inputs.Account.t + and type root_hash := Inputs.Hash.t + and type hash := Inputs.Hash.t + and type account_id := Inputs.Account_id.t + and type account_id_set := Inputs.Account_id.Set.t From 77a8178e914bc91371342f42ee7a573f09d42050 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 21:16:38 +0100 Subject: [PATCH 10/30] Merkle_ledger: remove unused *_intf module `Merkle_ledger_intf` and `Ledger_extras_intf` were never used --- src/lib/merkle_ledger/ledger_extras_intf.ml | 15 --------------- src/lib/merkle_ledger/merkle_ledger_intf.ml | 5 ----- 2 files changed, 20 deletions(-) delete mode 100644 src/lib/merkle_ledger/ledger_extras_intf.ml delete mode 100644 src/lib/merkle_ledger/merkle_ledger_intf.ml diff --git a/src/lib/merkle_ledger/ledger_extras_intf.ml b/src/lib/merkle_ledger/ledger_extras_intf.ml deleted file mode 100644 index 9f2bb5bd930..00000000000 --- a/src/lib/merkle_ledger/ledger_extras_intf.ml +++ /dev/null @@ -1,15 +0,0 @@ -(* ledger_extras_intf.ml -- adds functionality to Base_ledger_intf.S *) - -module type S = sig - include Merkle_ledger_intf.S - - val with_ledger : f:(t -> 'a) -> 'a - - val set_at_addr_exn : t -> Addr.t -> account -> unit - - val account_id_of_index : t -> index -> account_id option - - val account_id_of_index_exn : t -> index -> account_id - - val recompute_tree : t -> unit -end diff --git a/src/lib/merkle_ledger/merkle_ledger_intf.ml b/src/lib/merkle_ledger/merkle_ledger_intf.ml deleted file mode 100644 index 7b85045befb..00000000000 --- a/src/lib/merkle_ledger/merkle_ledger_intf.ml +++ /dev/null @@ -1,5 +0,0 @@ -module type S = sig - type t [@@deriving bin_io, sexp] - - include Base_ledger_intf.S with type t := t -end From e97f1c1a8eba92a2785fc3064014735df9b3550d Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 21:22:01 +0100 Subject: [PATCH 11/30] Merkle_ledger: interface for Any_ledger --- src/lib/merkle_ledger/any_ledger.mli | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/lib/merkle_ledger/any_ledger.mli diff --git a/src/lib/merkle_ledger/any_ledger.mli b/src/lib/merkle_ledger/any_ledger.mli new file mode 100644 index 00000000000..3a2a11c98fc --- /dev/null +++ b/src/lib/merkle_ledger/any_ledger.mli @@ -0,0 +1,55 @@ +module type S = sig + type key + + type token_id + + type token_id_set + + type account_id + + type account_id_set + + type account + + type hash + + module Location : Location_intf.S + + (** The type of the witness for a base ledger exposed here so that it can + * be easily accessed from outside this module *) + type witness [@@deriving sexp_of] + + module type Base_intf = + Base_ledger_intf.S + with module Addr = Location.Addr + with module Location = Location + with type key := key + and type token_id := token_id + and type token_id_set := token_id_set + and type account_id := account_id + and type account_id_set := account_id_set + and type hash := hash + and type root_hash := hash + and type account := account + + val cast : (module Base_intf with type t = 'a) -> 'a -> witness + + module M : Base_intf with type t = witness +end + +module type Inputs_intf = sig + include Base_inputs_intf.S + + module Location : Location_intf.S +end + +module Make_base (Inputs : Inputs_intf) : + S + with module Location = Inputs.Location + with type key := Inputs.Key.t + and type token_id := Inputs.Token_id.t + and type token_id_set := Inputs.Token_id.Set.t + and type account_id := Inputs.Account_id.t + and type hash := Inputs.Hash.t + and type account_id_set := Inputs.Account_id.Set.t + and type account := Inputs.Account.t From ead1a2570f679dd9f4595138ebc921827dcab239 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 21:40:53 +0100 Subject: [PATCH 12/30] Merkle_ledger: interface for Graphviz --- src/lib/merkle_ledger/graphviz.mli | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/lib/merkle_ledger/graphviz.mli diff --git a/src/lib/merkle_ledger/graphviz.mli b/src/lib/merkle_ledger/graphviz.mli new file mode 100644 index 00000000000..3616d6bf9fd --- /dev/null +++ b/src/lib/merkle_ledger/graphviz.mli @@ -0,0 +1,50 @@ +(** Visualizable_ledger shows a subgraph of a merkle_ledger using Graphviz *) +module type S = sig + type addr + + type ledger + + type t + + (* Visualize will enumerate through all edges of a subtree with a + initial_address. It will then interpret all of the edges and nodes into an + intermediate form that will be easy to write into a dot file *) + val visualize : ledger -> initial_address:addr -> t + + (* Write will transform the intermediate form generate by visualize and save + the results into a dot file *) + val write : path:string -> name:string -> t -> unit Async.Deferred.t +end + +module type Inputs_intf = sig + module Key : Intf.Key + + module Token_id : Intf.Token_id + + module Account_id : + Intf.Account_id with type key := Key.t and type token_id := Token_id.t + + module Balance : Intf.Balance + + module Account : + Intf.Account + with type account_id := Account_id.t + and type balance := Balance.t + + module Hash : Intf.Hash with type account := Account.t + + module Location : Location_intf.S + + module Ledger : + Base_ledger_intf.S + with module Addr = Location.Addr + and module Location = Location + and type account_id := Account_id.t + and type account_id_set := Account_id.Set.t + and type hash := Hash.t + and type root_hash := Hash.t + and type account := Account.t +end + +module Make (Inputs : Inputs_intf) : + S with type addr := Inputs.Location.Addr.t and type ledger := Inputs.Ledger.t From c32f5a75b4a80a42406a9e3bb008e1cef203713c Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 22:05:37 +0100 Subject: [PATCH 13/30] Merkle_ledger: make Database_intf a pure interface --- .../{database_intf.ml => database_intf.mli} | 0 src/lib/merkle_ledger/dune | 55 ++++++++++--------- 2 files changed, 28 insertions(+), 27 deletions(-) rename src/lib/merkle_ledger/{database_intf.ml => database_intf.mli} (100%) diff --git a/src/lib/merkle_ledger/database_intf.ml b/src/lib/merkle_ledger/database_intf.mli similarity index 100% rename from src/lib/merkle_ledger/database_intf.ml rename to src/lib/merkle_ledger/database_intf.mli diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index 70339fc26c5..07b4829c103 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -2,35 +2,36 @@ (name merkle_ledger) (public_name merkle_ledger) (library_flags -linkall) + (modules_without_implementation database_intf) (libraries ;; opam libraries - bitstring - async_kernel - core_kernel.uuid - bin_prot.shape - sexplib0 - integers - core.uuid - async - core - extlib - rocks - core_kernel - base.caml - base.base_internalhash_types - async_unix - ;; local libraries - merkle_address - immutable_array - direction - cache_dir - empty_hashes - key_value_database - mina_stdlib - visualization - ppx_version.runtime - bounded_types - ) + bitstring + async_kernel + core_kernel.uuid + bin_prot.shape + sexplib0 + integers + core.uuid + async + core + extlib + rocks + core_kernel + base.caml + base.base_internalhash_types + async_unix + ;; local libraries + merkle_address + immutable_array + direction + cache_dir + empty_hashes + key_value_database + mina_stdlib + visualization + ppx_version.runtime + bounded_types + ) (preprocess (pps ppx_mina ppx_version ppx_jane ppx_compare ppx_deriving.show ppx_deriving_yojson)) (instrumentation (backend bisect_ppx)) From 7fe1a32c31fb9780c13f1b9f83c7e6c6793ac300 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Thu, 15 Feb 2024 22:11:45 +0100 Subject: [PATCH 14/30] Merkle_ledger: minimizing module type declaration for database --- src/lib/merkle_ledger/database.ml | 25 +------------------------ src/lib/merkle_ledger/database.mli | 15 +-------------- src/lib/merkle_ledger/database_intf.mli | 13 +++++++++++++ 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/src/lib/merkle_ledger/database.ml b/src/lib/merkle_ledger/database.ml index 09d984401d6..847d6890313 100644 --- a/src/lib/merkle_ledger/database.ml +++ b/src/lib/merkle_ledger/database.ml @@ -1,29 +1,6 @@ open Core -module type Inputs_intf = sig - include Base_inputs_intf.S - - module Location : Location_intf.S - - module Location_binable : Hashable.S_binable with type t := Location.t - - module Kvdb : Intf.Key_value_database with type config := string - - module Storage_locations : Intf.Storage_locations -end - -module Make (Inputs : Inputs_intf) : - Database_intf.S - with module Location = Inputs.Location - and module Addr = Inputs.Location.Addr - and type key := Inputs.Key.t - and type token_id := Inputs.Token_id.t - and type token_id_set := Inputs.Token_id.Set.t - and type account := Inputs.Account.t - and type root_hash := Inputs.Hash.t - and type hash := Inputs.Hash.t - and type account_id := Inputs.Account_id.t - and type account_id_set := Inputs.Account_id.Set.t = struct +module Make (Inputs : Database_intf.Inputs_intf) = struct (* The max depth of a merkle tree can never be greater than 253. *) open Inputs diff --git a/src/lib/merkle_ledger/database.mli b/src/lib/merkle_ledger/database.mli index a2283e247f0..36b03dad1da 100644 --- a/src/lib/merkle_ledger/database.mli +++ b/src/lib/merkle_ledger/database.mli @@ -1,17 +1,4 @@ -module type Inputs_intf = sig - include Base_inputs_intf.S - - module Location : Location_intf.S - - module Location_binable : - Core_kernel.Hashable.S_binable with type t := Location.t - - module Kvdb : Intf.Key_value_database with type config := string - - module Storage_locations : Intf.Storage_locations -end - -module Make (Inputs : Inputs_intf) : +module Make (Inputs : Database_intf.Inputs_intf) : Database_intf.S with module Location = Inputs.Location and module Addr = Inputs.Location.Addr diff --git a/src/lib/merkle_ledger/database_intf.mli b/src/lib/merkle_ledger/database_intf.mli index 40f40195b40..31ad3b18982 100644 --- a/src/lib/merkle_ledger/database_intf.mli +++ b/src/lib/merkle_ledger/database_intf.mli @@ -16,3 +16,16 @@ module type S = sig ledger_depth:int -> Location.t Core.Quickcheck.Generator.t end end + +module type Inputs_intf = sig + include Base_inputs_intf.S + + module Location : Location_intf.S + + module Location_binable : + Core_kernel.Hashable.S_binable with type t := Location.t + + module Kvdb : Intf.Key_value_database with type config := string + + module Storage_locations : Intf.Storage_locations +end From 34f5973c766f16bdfc050335f4ae5dae04c0b6d1 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 11:35:06 +0100 Subject: [PATCH 15/30] Merkle_ledger: activate warnings --- src/lib/merkle_ledger/dune | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index 07b4829c103..d7d9b85f1a6 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -1,6 +1,17 @@ (library (name merkle_ledger) (public_name merkle_ledger) + (flags + ; Deactivated warnings + ; 40: name-out-scope (activate later) + ; + ; 41: ambiguous name (too many of them for now, activate later) + ; + ; 42: disambiguated-name (rely on type disambiguation ,not too bad but closer + ; module openings may both solve the warning *and* help the reader) + ; + ; 44: open-shadow-identifier (operation overloading is common in the codebase) + (:standard -w +a-40..42-44 -warn-error +a)) (library_flags -linkall) (modules_without_implementation database_intf) (libraries From 96baa2a59e3c4f720af797958dcfec506b058534 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 11:36:47 +0100 Subject: [PATCH 16/30] Merkle_ledger: sort libraries in dune file --- src/lib/merkle_ledger/dune | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index d7d9b85f1a6..115428cfa89 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -16,32 +16,32 @@ (modules_without_implementation database_intf) (libraries ;; opam libraries - bitstring + async async_kernel - core_kernel.uuid + async_unix + base.base_internalhash_types + base.caml bin_prot.shape - sexplib0 - integers - core.uuid - async + bitstring core + core.uuid + core_kernel + core_kernel.uuid extlib + integers rocks - core_kernel - base.caml - base.base_internalhash_types - async_unix + sexplib0 ;; local libraries - merkle_address - immutable_array - direction + bounded_types cache_dir + direction empty_hashes + immutable_array key_value_database + merkle_address mina_stdlib - visualization ppx_version.runtime - bounded_types + visualization ) (preprocess (pps ppx_mina ppx_version ppx_jane ppx_compare ppx_deriving.show ppx_deriving_yojson)) From 6fa38f15e30ab282cd7ca8a0e8d047a212ef722e Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 11:47:20 +0100 Subject: [PATCH 17/30] Merkle_ledger: make intf files actual interfaces --- .../{base_inputs_intf.ml => base_inputs_intf.mli} | 0 .../{base_ledger_intf.ml => base_ledger_intf.mli} | 0 src/lib/merkle_ledger/dune | 8 +++++++- src/lib/merkle_ledger/{intf.ml => intf.mli} | 0 .../merkle_ledger/{location_intf.ml => location_intf.mli} | 0 .../merkle_ledger/{syncable_intf.ml => syncable_intf.mli} | 0 6 files changed, 7 insertions(+), 1 deletion(-) rename src/lib/merkle_ledger/{base_inputs_intf.ml => base_inputs_intf.mli} (100%) rename src/lib/merkle_ledger/{base_ledger_intf.ml => base_ledger_intf.mli} (100%) rename src/lib/merkle_ledger/{intf.ml => intf.mli} (100%) rename src/lib/merkle_ledger/{location_intf.ml => location_intf.mli} (100%) rename src/lib/merkle_ledger/{syncable_intf.ml => syncable_intf.mli} (100%) diff --git a/src/lib/merkle_ledger/base_inputs_intf.ml b/src/lib/merkle_ledger/base_inputs_intf.mli similarity index 100% rename from src/lib/merkle_ledger/base_inputs_intf.ml rename to src/lib/merkle_ledger/base_inputs_intf.mli diff --git a/src/lib/merkle_ledger/base_ledger_intf.ml b/src/lib/merkle_ledger/base_ledger_intf.mli similarity index 100% rename from src/lib/merkle_ledger/base_ledger_intf.ml rename to src/lib/merkle_ledger/base_ledger_intf.mli diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index 115428cfa89..b8e67604562 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -13,7 +13,13 @@ ; 44: open-shadow-identifier (operation overloading is common in the codebase) (:standard -w +a-40..42-44 -warn-error +a)) (library_flags -linkall) - (modules_without_implementation database_intf) + (modules_without_implementation + base_inputs_intf + base_ledger_intf + database_intf + intf + location_intf + syncable_intf) (libraries ;; opam libraries async diff --git a/src/lib/merkle_ledger/intf.ml b/src/lib/merkle_ledger/intf.mli similarity index 100% rename from src/lib/merkle_ledger/intf.ml rename to src/lib/merkle_ledger/intf.mli diff --git a/src/lib/merkle_ledger/location_intf.ml b/src/lib/merkle_ledger/location_intf.mli similarity index 100% rename from src/lib/merkle_ledger/location_intf.ml rename to src/lib/merkle_ledger/location_intf.mli diff --git a/src/lib/merkle_ledger/syncable_intf.ml b/src/lib/merkle_ledger/syncable_intf.mli similarity index 100% rename from src/lib/merkle_ledger/syncable_intf.ml rename to src/lib/merkle_ledger/syncable_intf.mli From bdc78efc4d1ba85eb3b30ea6479f1dedca78915c Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 12:17:04 +0100 Subject: [PATCH 18/30] Merkle_ledger: fixing fragile pattern-matching warning --- src/lib/merkle_ledger/database.ml | 6 ++++-- src/lib/merkle_ledger/location.ml | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib/merkle_ledger/database.ml b/src/lib/merkle_ledger/database.ml index 847d6890313..c76ba3a264c 100644 --- a/src/lib/merkle_ledger/database.ml +++ b/src/lib/merkle_ledger/database.ml @@ -5,6 +5,8 @@ module Make (Inputs : Database_intf.Inputs_intf) = struct open Inputs module Db_error = struct + [@@@warning "-4"] (* due to deriving sexp below *) + type t = Account_location_not_found | Out_of_leaves | Malformed_database [@@deriving sexp] end @@ -555,7 +557,7 @@ module Make (Inputs : Database_intf.Inputs_intf) = struct let get_or_create_account mdb account_id account = match Account_location.get mdb account_id with - | Error Account_location_not_found -> ( + | Error Db_error.Account_location_not_found -> ( match Account_location.allocate mdb account_id with | Ok location -> set mdb location account ; @@ -564,7 +566,7 @@ module Make (Inputs : Database_intf.Inputs_intf) = struct | Error err -> Error (Error.create "get_or_create_account" err Db_error.sexp_of_t) ) - | Error err -> + | Error ((Db_error.Malformed_database | Db_error.Out_of_leaves) as err) -> Error (Error.create "get_or_create_account" err Db_error.sexp_of_t) | Ok location -> Ok (`Existed, location) diff --git a/src/lib/merkle_ledger/location.ml b/src/lib/merkle_ledger/location.ml index 494aa5489e4..aa3ec1f7e01 100644 --- a/src/lib/merkle_ledger/location.ml +++ b/src/lib/merkle_ledger/location.ml @@ -50,14 +50,18 @@ module T = struct let hash ~ledger_depth depth = UInt8.of_int (ledger_depth - depth) end + [@@@warning "-4"] (* disabled because of deriving sexp *) + type t = Generic of Bigstring.t | Account of Addr.t | Hash of Addr.t [@@deriving hash, sexp, compare] - let is_generic = function Generic _ -> true | _ -> false + [@@@warning "+4"] + + let is_generic = function Generic _ -> true | Account _ | Hash _ -> false - let is_account = function Account _ -> true | _ -> false + let is_account = function Account _ -> true | Generic _ | Hash _ -> false - let is_hash = function Hash _ -> true | _ -> false + let is_hash = function Hash _ -> true | Account _ | Generic _ -> false let height ~ledger_depth : t -> int = function | Generic _ -> @@ -169,7 +173,7 @@ module T = struct match location with | Hash addr -> loop addr - | _ -> + | Account _ | Generic _ -> failwith "can only get merkle path dependencies of a hash location" type location = t [@@deriving sexp, compare] From ef81113c8b5850a34dfe304660330b2a2c12cd0f Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 14:19:30 +0100 Subject: [PATCH 19/30] Merkle_ledger: apply intf trick for Graphviz See https://www.craigfe.io/posts/the-intf-trick --- src/lib/merkle_ledger/dune | 3 +- src/lib/merkle_ledger/graphviz.ml | 55 ++----------------------- src/lib/merkle_ledger/graphviz.mli | 52 ++--------------------- src/lib/merkle_ledger/graphviz_intf.mli | 45 ++++++++++++++++++++ 4 files changed, 55 insertions(+), 100 deletions(-) create mode 100644 src/lib/merkle_ledger/graphviz_intf.mli diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index b8e67604562..ad6e0937ef6 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -11,12 +11,13 @@ ; module openings may both solve the warning *and* help the reader) ; ; 44: open-shadow-identifier (operation overloading is common in the codebase) - (:standard -w +a-40..42-44 -warn-error +a)) + (:standard -w +a-40..42-44 -warn-error +a -open Core)) (library_flags -linkall) (modules_without_implementation base_inputs_intf base_ledger_intf database_intf + graphviz_intf intf location_intf syncable_intf) diff --git a/src/lib/merkle_ledger/graphviz.ml b/src/lib/merkle_ledger/graphviz.ml index 95204555772..15f32f4fe3e 100644 --- a/src/lib/merkle_ledger/graphviz.ml +++ b/src/lib/merkle_ledger/graphviz.ml @@ -1,57 +1,10 @@ open Core open Async -(** Visualizable_ledger shows a subgraph of a merkle_ledger using Graphviz *) -module type S = sig - type addr - - type ledger - - type t - - (* Visualize will enumerate through all edges of a subtree with a - initial_address. It will then interpret all of the edges and nodes into an - intermediate form that will be easy to write into a dot file *) - val visualize : ledger -> initial_address:addr -> t - - (* Write will transform the intermediate form generate by visualize and save - the results into a dot file *) - val write : path:string -> name:string -> t -> unit Deferred.t -end - -module type Inputs_intf = sig - module Key : Intf.Key - - module Token_id : Intf.Token_id - - module Account_id : - Intf.Account_id with type key := Key.t and type token_id := Token_id.t - - module Balance : Intf.Balance - - module Account : - Intf.Account - with type account_id := Account_id.t - and type balance := Balance.t - - module Hash : Intf.Hash with type account := Account.t - - module Location : Location_intf.S - - module Ledger : - Base_ledger_intf.S - with module Addr = Location.Addr - and module Location = Location - and type account_id := Account_id.t - and type account_id_set := Account_id.Set.t - and type hash := Hash.t - and type root_hash := Hash.t - and type account := Account.t -end - -module Make (Inputs : Inputs_intf) : - S with type addr := Inputs.Location.Addr.t and type ledger := Inputs.Ledger.t = -struct +module Make (Inputs : Graphviz_intf.Inputs_intf) : + Graphviz_intf.S + with type addr := Inputs.Location.Addr.t + and type ledger := Inputs.Ledger.t = struct open Inputs module Account = struct diff --git a/src/lib/merkle_ledger/graphviz.mli b/src/lib/merkle_ledger/graphviz.mli index 3616d6bf9fd..c1148e59471 100644 --- a/src/lib/merkle_ledger/graphviz.mli +++ b/src/lib/merkle_ledger/graphviz.mli @@ -1,50 +1,6 @@ (** Visualizable_ledger shows a subgraph of a merkle_ledger using Graphviz *) -module type S = sig - type addr - type ledger - - type t - - (* Visualize will enumerate through all edges of a subtree with a - initial_address. It will then interpret all of the edges and nodes into an - intermediate form that will be easy to write into a dot file *) - val visualize : ledger -> initial_address:addr -> t - - (* Write will transform the intermediate form generate by visualize and save - the results into a dot file *) - val write : path:string -> name:string -> t -> unit Async.Deferred.t -end - -module type Inputs_intf = sig - module Key : Intf.Key - - module Token_id : Intf.Token_id - - module Account_id : - Intf.Account_id with type key := Key.t and type token_id := Token_id.t - - module Balance : Intf.Balance - - module Account : - Intf.Account - with type account_id := Account_id.t - and type balance := Balance.t - - module Hash : Intf.Hash with type account := Account.t - - module Location : Location_intf.S - - module Ledger : - Base_ledger_intf.S - with module Addr = Location.Addr - and module Location = Location - and type account_id := Account_id.t - and type account_id_set := Account_id.Set.t - and type hash := Hash.t - and type root_hash := Hash.t - and type account := Account.t -end - -module Make (Inputs : Inputs_intf) : - S with type addr := Inputs.Location.Addr.t and type ledger := Inputs.Ledger.t +module Make (Inputs : Graphviz_intf.Inputs_intf) : + Graphviz_intf.S + with type addr := Inputs.Location.Addr.t + and type ledger := Inputs.Ledger.t diff --git a/src/lib/merkle_ledger/graphviz_intf.mli b/src/lib/merkle_ledger/graphviz_intf.mli new file mode 100644 index 00000000000..4b553fed915 --- /dev/null +++ b/src/lib/merkle_ledger/graphviz_intf.mli @@ -0,0 +1,45 @@ +module type S = sig + type addr + + type ledger + + type t + + (* Visualize will enumerate through all edges of a subtree with a + initial_address. It will then interpret all of the edges and nodes into an + intermediate form that will be easy to write into a dot file *) + val visualize : ledger -> initial_address:addr -> t + + (* Write will transform the intermediate form generate by visualize and save + the results into a dot file *) + val write : path:string -> name:string -> t -> unit Async.Deferred.t +end + +module type Inputs_intf = sig + module Key : Intf.Key + + module Token_id : Intf.Token_id + + module Account_id : + Intf.Account_id with type key := Key.t and type token_id := Token_id.t + + module Balance : Intf.Balance + + module Account : + Intf.Account + with type account_id := Account_id.t + and type balance := Balance.t + + module Hash : Intf.Hash with type account := Account.t + + module Location : Location_intf.S + + module Ledger : + Base_ledger_intf.S + with module Addr = Location.Addr + and module Location = Location + and type account_id := Account_id.t + and type account_id_set := Account_id.Set.t + and type hash := Hash.t + and type account := Account.t +end From a62ac4abd2c1afad7f2954ce5ac95c273ef16c45 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 14:55:01 +0100 Subject: [PATCH 20/30] Merkle_ledger: factorize inputs interface --- src/lib/merkle_ledger/any_ledger.ml | 8 +------- src/lib/merkle_ledger/any_ledger.mli | 8 +------- src/lib/merkle_ledger/base_inputs_intf.mli | 6 ++++++ src/lib/merkle_ledger/null_ledger.ml | 8 +------- src/lib/merkle_ledger/null_ledger.mli | 8 +------- 5 files changed, 10 insertions(+), 28 deletions(-) diff --git a/src/lib/merkle_ledger/any_ledger.ml b/src/lib/merkle_ledger/any_ledger.ml index 147fa13e03a..0f936c1ca10 100644 --- a/src/lib/merkle_ledger/any_ledger.ml +++ b/src/lib/merkle_ledger/any_ledger.ml @@ -54,13 +54,7 @@ module type S = sig module M : Base_intf with type t = witness end -module type Inputs_intf = sig - include Base_inputs_intf.S - - module Location : Location_intf.S -end - -module Make_base (Inputs : Inputs_intf) : +module Make_base (Inputs : Base_inputs_intf.Intf) : S with module Location = Inputs.Location with type key := Inputs.Key.t diff --git a/src/lib/merkle_ledger/any_ledger.mli b/src/lib/merkle_ledger/any_ledger.mli index 3a2a11c98fc..3f4c81393b5 100644 --- a/src/lib/merkle_ledger/any_ledger.mli +++ b/src/lib/merkle_ledger/any_ledger.mli @@ -37,13 +37,7 @@ module type S = sig module M : Base_intf with type t = witness end -module type Inputs_intf = sig - include Base_inputs_intf.S - - module Location : Location_intf.S -end - -module Make_base (Inputs : Inputs_intf) : +module Make_base (Inputs : Base_inputs_intf.Intf) : S with module Location = Inputs.Location with type key := Inputs.Key.t diff --git a/src/lib/merkle_ledger/base_inputs_intf.mli b/src/lib/merkle_ledger/base_inputs_intf.mli index cda6d078c74..13135d08d27 100644 --- a/src/lib/merkle_ledger/base_inputs_intf.mli +++ b/src/lib/merkle_ledger/base_inputs_intf.mli @@ -16,3 +16,9 @@ module type S = sig module Hash : Intf.Hash with type account := Account.t end + +module type Intf = sig + include S + + module Location : Location_intf.S +end diff --git a/src/lib/merkle_ledger/null_ledger.ml b/src/lib/merkle_ledger/null_ledger.ml index e9e511f15c2..986a8ca194e 100644 --- a/src/lib/merkle_ledger/null_ledger.ml +++ b/src/lib/merkle_ledger/null_ledger.ml @@ -1,12 +1,6 @@ open Core_kernel -module type Inputs_intf = sig - include Base_inputs_intf.S - - module Location : Location_intf.S -end - -module Make (Inputs : Inputs_intf) : sig +module Make (Inputs : Base_inputs_intf.Intf) : sig include Base_ledger_intf.S with module Addr = Inputs.Location.Addr diff --git a/src/lib/merkle_ledger/null_ledger.mli b/src/lib/merkle_ledger/null_ledger.mli index b4e8196de16..b9741c69aab 100644 --- a/src/lib/merkle_ledger/null_ledger.mli +++ b/src/lib/merkle_ledger/null_ledger.mli @@ -1,10 +1,4 @@ -module type Inputs_intf = sig - include Base_inputs_intf.S - - module Location : Location_intf.S -end - -module Make (Inputs : Inputs_intf) : sig +module Make (Inputs : Base_inputs_intf.Intf) : sig include Base_ledger_intf.S with module Addr = Inputs.Location.Addr From b7c4af0eb4348a7afcb2abfb0eddb0635427f5d1 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 14:57:37 +0100 Subject: [PATCH 21/30] Merkle_ledger: db-specific input module type --- src/lib/merkle_ledger/base_inputs_intf.mli | 11 +++++++++++ src/lib/merkle_ledger/database.ml | 2 +- src/lib/merkle_ledger/database.mli | 2 +- src/lib/merkle_ledger/database_intf.mli | 13 ------------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/lib/merkle_ledger/base_inputs_intf.mli b/src/lib/merkle_ledger/base_inputs_intf.mli index 13135d08d27..8f25bcd5a33 100644 --- a/src/lib/merkle_ledger/base_inputs_intf.mli +++ b/src/lib/merkle_ledger/base_inputs_intf.mli @@ -22,3 +22,14 @@ module type Intf = sig module Location : Location_intf.S end + +module type DATABASE = sig + include Intf + + module Location_binable : + Core_kernel.Hashable.S_binable with type t := Location.t + + module Kvdb : Intf.Key_value_database with type config := string + + module Storage_locations : Intf.Storage_locations +end diff --git a/src/lib/merkle_ledger/database.ml b/src/lib/merkle_ledger/database.ml index c76ba3a264c..356185f3553 100644 --- a/src/lib/merkle_ledger/database.ml +++ b/src/lib/merkle_ledger/database.ml @@ -1,6 +1,6 @@ open Core -module Make (Inputs : Database_intf.Inputs_intf) = struct +module Make (Inputs : Base_inputs_intf.DATABASE) = struct (* The max depth of a merkle tree can never be greater than 253. *) open Inputs diff --git a/src/lib/merkle_ledger/database.mli b/src/lib/merkle_ledger/database.mli index 36b03dad1da..e55b6c9fc49 100644 --- a/src/lib/merkle_ledger/database.mli +++ b/src/lib/merkle_ledger/database.mli @@ -1,4 +1,4 @@ -module Make (Inputs : Database_intf.Inputs_intf) : +module Make (Inputs : Base_inputs_intf.DATABASE) : Database_intf.S with module Location = Inputs.Location and module Addr = Inputs.Location.Addr diff --git a/src/lib/merkle_ledger/database_intf.mli b/src/lib/merkle_ledger/database_intf.mli index 31ad3b18982..40f40195b40 100644 --- a/src/lib/merkle_ledger/database_intf.mli +++ b/src/lib/merkle_ledger/database_intf.mli @@ -16,16 +16,3 @@ module type S = sig ledger_depth:int -> Location.t Core.Quickcheck.Generator.t end end - -module type Inputs_intf = sig - include Base_inputs_intf.S - - module Location : Location_intf.S - - module Location_binable : - Core_kernel.Hashable.S_binable with type t := Location.t - - module Kvdb : Intf.Key_value_database with type config := string - - module Storage_locations : Intf.Storage_locations -end From 7c96972c8e8b253881e9f4ef506efdec349050f7 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 14:59:32 +0100 Subject: [PATCH 22/30] Merkle_ledger: inline module type Base_inputs_intf.S --- src/lib/merkle_ledger/base_inputs_intf.mli | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/lib/merkle_ledger/base_inputs_intf.mli b/src/lib/merkle_ledger/base_inputs_intf.mli index 8f25bcd5a33..30a5f037f14 100644 --- a/src/lib/merkle_ledger/base_inputs_intf.mli +++ b/src/lib/merkle_ledger/base_inputs_intf.mli @@ -1,4 +1,4 @@ -module type S = sig +module type Intf = sig module Key : Intf.Key module Token_id : Intf.Token_id @@ -15,10 +15,6 @@ module type S = sig and type balance := Balance.t module Hash : Intf.Hash with type account := Account.t -end - -module type Intf = sig - include S module Location : Location_intf.S end From b15aeeb133072e66b6beae76d6446bc634e4872c Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 15:02:09 +0100 Subject: [PATCH 23/30] Merkle_ledger: define module type Ledger.NULL --- src/lib/merkle_ledger/base_ledger_intf.mli | 6 ++++++ src/lib/merkle_ledger/null_ledger.ml | 4 +--- src/lib/merkle_ledger/null_ledger.mli | 4 +--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lib/merkle_ledger/base_ledger_intf.mli b/src/lib/merkle_ledger/base_ledger_intf.mli index 74c13db4376..f2e609b02dd 100644 --- a/src/lib/merkle_ledger/base_ledger_intf.mli +++ b/src/lib/merkle_ledger/base_ledger_intf.mli @@ -148,3 +148,9 @@ module type S = sig *) val detached_signal : t -> unit Async_kernel.Deferred.t end + +module type NULL = sig + include S + + val create : depth:int -> unit -> t +end diff --git a/src/lib/merkle_ledger/null_ledger.ml b/src/lib/merkle_ledger/null_ledger.ml index 986a8ca194e..bc89a839c2b 100644 --- a/src/lib/merkle_ledger/null_ledger.ml +++ b/src/lib/merkle_ledger/null_ledger.ml @@ -2,7 +2,7 @@ open Core_kernel module Make (Inputs : Base_inputs_intf.Intf) : sig include - Base_ledger_intf.S + Base_ledger_intf.NULL with module Addr = Inputs.Location.Addr with module Location = Inputs.Location with type key := Inputs.Key.t @@ -13,8 +13,6 @@ module Make (Inputs : Base_inputs_intf.Intf) : sig and type hash := Inputs.Hash.t and type root_hash := Inputs.Hash.t and type account := Inputs.Account.t - - val create : depth:int -> unit -> t end = struct open Inputs diff --git a/src/lib/merkle_ledger/null_ledger.mli b/src/lib/merkle_ledger/null_ledger.mli index b9741c69aab..9c95e4d4632 100644 --- a/src/lib/merkle_ledger/null_ledger.mli +++ b/src/lib/merkle_ledger/null_ledger.mli @@ -1,6 +1,6 @@ module Make (Inputs : Base_inputs_intf.Intf) : sig include - Base_ledger_intf.S + Base_ledger_intf.NULL with module Addr = Inputs.Location.Addr with module Location = Inputs.Location with type key := Inputs.Key.t @@ -11,6 +11,4 @@ module Make (Inputs : Base_inputs_intf.Intf) : sig and type hash := Inputs.Hash.t and type root_hash := Inputs.Hash.t and type account := Inputs.Account.t - - val create : depth:int -> unit -> t end From 5e8186337e7b2f8cc0b056e5f912b8b225fa376d Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 15:11:51 +0100 Subject: [PATCH 24/30] Merkle_ledger: moving ledger module types to own file --- src/lib/merkle_ledger/any_ledger.ml | 41 +-------------- src/lib/merkle_ledger/any_ledger.mli | 41 +-------------- src/lib/merkle_ledger/base_ledger_intf.mli | 58 ++++++++++++++++++++++ src/lib/merkle_ledger/database.mli | 2 +- src/lib/merkle_ledger/database_intf.mli | 18 ------- src/lib/merkle_ledger/dune | 1 - src/lib/mina_ledger/ledger.ml | 4 +- src/lib/mina_ledger/ledger.mli | 4 +- 8 files changed, 65 insertions(+), 104 deletions(-) delete mode 100644 src/lib/merkle_ledger/database_intf.mli diff --git a/src/lib/merkle_ledger/any_ledger.ml b/src/lib/merkle_ledger/any_ledger.ml index 0f936c1ca10..7727c02edf1 100644 --- a/src/lib/merkle_ledger/any_ledger.ml +++ b/src/lib/merkle_ledger/any_ledger.ml @@ -15,47 +15,8 @@ open Core_kernel -module type S = sig - type key - - type token_id - - type token_id_set - - type account_id - - type account_id_set - - type account - - type hash - - module Location : Location_intf.S - - (** The type of the witness for a base ledger exposed here so that it can - * be easily accessed from outside this module *) - type witness [@@deriving sexp_of] - - module type Base_intf = - Base_ledger_intf.S - with module Addr = Location.Addr - with module Location = Location - with type key := key - and type token_id := token_id - and type token_id_set := token_id_set - and type account_id := account_id - and type account_id_set := account_id_set - and type hash := hash - and type root_hash := hash - and type account := account - - val cast : (module Base_intf with type t = 'a) -> 'a -> witness - - module M : Base_intf with type t = witness -end - module Make_base (Inputs : Base_inputs_intf.Intf) : - S + Base_ledger_intf.ANY with module Location = Inputs.Location with type key := Inputs.Key.t and type token_id := Inputs.Token_id.t diff --git a/src/lib/merkle_ledger/any_ledger.mli b/src/lib/merkle_ledger/any_ledger.mli index 3f4c81393b5..54fd866ed7f 100644 --- a/src/lib/merkle_ledger/any_ledger.mli +++ b/src/lib/merkle_ledger/any_ledger.mli @@ -1,44 +1,5 @@ -module type S = sig - type key - - type token_id - - type token_id_set - - type account_id - - type account_id_set - - type account - - type hash - - module Location : Location_intf.S - - (** The type of the witness for a base ledger exposed here so that it can - * be easily accessed from outside this module *) - type witness [@@deriving sexp_of] - - module type Base_intf = - Base_ledger_intf.S - with module Addr = Location.Addr - with module Location = Location - with type key := key - and type token_id := token_id - and type token_id_set := token_id_set - and type account_id := account_id - and type account_id_set := account_id_set - and type hash := hash - and type root_hash := hash - and type account := account - - val cast : (module Base_intf with type t = 'a) -> 'a -> witness - - module M : Base_intf with type t = witness -end - module Make_base (Inputs : Base_inputs_intf.Intf) : - S + Base_ledger_intf.ANY with module Location = Inputs.Location with type key := Inputs.Key.t and type token_id := Inputs.Token_id.t diff --git a/src/lib/merkle_ledger/base_ledger_intf.mli b/src/lib/merkle_ledger/base_ledger_intf.mli index f2e609b02dd..fecef9b5233 100644 --- a/src/lib/merkle_ledger/base_ledger_intf.mli +++ b/src/lib/merkle_ledger/base_ledger_intf.mli @@ -154,3 +154,61 @@ module type NULL = sig val create : depth:int -> unit -> t end + +module type ANY = sig + type key + + type token_id + + type token_id_set + + type account_id + + type account_id_set + + type account + + type hash + + module Location : Location_intf.S + + (** The type of the witness for a base ledger exposed here so that it can + * be easily accessed from outside this module *) + type witness [@@deriving sexp_of] + + module type Base_intf = + S + with module Addr = Location.Addr + with module Location = Location + with type key := key + and type token_id := token_id + and type token_id_set := token_id_set + and type account_id := account_id + and type account_id_set := account_id_set + and type hash := hash + and type root_hash := hash + and type account := account + + val cast : (module Base_intf with type t = 'a) -> 'a -> witness + + module M : Base_intf with type t = witness +end + +module type DATABASE = sig + include S + + val create : ?directory_name:string -> depth:int -> unit -> t + + (** create_checkpoint would create the checkpoint and open a db connection to that checkpoint *) + val create_checkpoint : t -> directory_name:string -> unit -> t + + (** make_checkpoint would only create the checkpoint *) + val make_checkpoint : t -> directory_name:string -> unit + + val with_ledger : depth:int -> f:(t -> 'a) -> 'a + + module For_tests : sig + val gen_account_location : + ledger_depth:int -> Location.t Core.Quickcheck.Generator.t + end +end diff --git a/src/lib/merkle_ledger/database.mli b/src/lib/merkle_ledger/database.mli index e55b6c9fc49..6aba1a6ef34 100644 --- a/src/lib/merkle_ledger/database.mli +++ b/src/lib/merkle_ledger/database.mli @@ -1,5 +1,5 @@ module Make (Inputs : Base_inputs_intf.DATABASE) : - Database_intf.S + Base_ledger_intf.DATABASE with module Location = Inputs.Location and module Addr = Inputs.Location.Addr and type key := Inputs.Key.t diff --git a/src/lib/merkle_ledger/database_intf.mli b/src/lib/merkle_ledger/database_intf.mli deleted file mode 100644 index 40f40195b40..00000000000 --- a/src/lib/merkle_ledger/database_intf.mli +++ /dev/null @@ -1,18 +0,0 @@ -module type S = sig - include Base_ledger_intf.S - - val create : ?directory_name:string -> depth:int -> unit -> t - - (** create_checkpoint would create the checkpoint and open a db connection to that checkpoint *) - val create_checkpoint : t -> directory_name:string -> unit -> t - - (** make_checkpoint would only create the checkpoint *) - val make_checkpoint : t -> directory_name:string -> unit - - val with_ledger : depth:int -> f:(t -> 'a) -> 'a - - module For_tests : sig - val gen_account_location : - ledger_depth:int -> Location.t Core.Quickcheck.Generator.t - end -end diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index ad6e0937ef6..cc823c36c28 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -16,7 +16,6 @@ (modules_without_implementation base_inputs_intf base_ledger_intf - database_intf graphviz_intf intf location_intf diff --git a/src/lib/mina_ledger/ledger.ml b/src/lib/mina_ledger/ledger.ml index fe1ed65e6ec..933472a3838 100644 --- a/src/lib/mina_ledger/ledger.ml +++ b/src/lib/mina_ledger/ledger.ml @@ -106,7 +106,7 @@ module Ledger_inner = struct end module Db : - Merkle_ledger.Database_intf.S + Merkle_ledger.Base_ledger_intf.DATABASE with module Location = Location_at_depth with module Addr = Location_at_depth.Addr with type root_hash := Ledger_hash.t @@ -122,7 +122,7 @@ module Ledger_inner = struct module Null = Null_ledger.Make (Inputs) module Any_ledger : - Merkle_ledger.Any_ledger.S + Merkle_ledger.Base_ledger_intf.ANY with module Location = Location_at_depth with type account := Account.t and type key := Public_key.Compressed.t diff --git a/src/lib/mina_ledger/ledger.mli b/src/lib/mina_ledger/ledger.mli index adc8643d2cc..f1a7a604c49 100644 --- a/src/lib/mina_ledger/ledger.mli +++ b/src/lib/mina_ledger/ledger.mli @@ -5,7 +5,7 @@ open Mina_base module Location : Merkle_ledger.Location_intf.S module Db : - Merkle_ledger.Database_intf.S + Merkle_ledger.Base_ledger_intf.DATABASE with module Location = Location with module Addr = Location.Addr with type root_hash := Ledger_hash.t @@ -18,7 +18,7 @@ module Db : and type account_id_set := Account_id.Set.t module Any_ledger : - Merkle_ledger.Any_ledger.S + Merkle_ledger.Base_ledger_intf.ANY with module Location = Location with type account := Account.t and type key := Public_key.Compressed.t From c82ae70247a1b4462667f0771c610c9d9ec8a8b5 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 15:40:12 +0100 Subject: [PATCH 25/30] Merkle_ledger: put input/ledger module types in Intf --- src/lib/merkle_ledger/any_ledger.ml | 6 +- src/lib/merkle_ledger/any_ledger.mli | 4 +- src/lib/merkle_ledger/base_inputs_intf.mli | 31 -- src/lib/merkle_ledger/base_ledger_intf.mli | 214 --------- src/lib/merkle_ledger/database.ml | 2 +- src/lib/merkle_ledger/database.mli | 4 +- src/lib/merkle_ledger/dune | 11 +- src/lib/merkle_ledger/graphviz_intf.mli | 2 +- src/lib/merkle_ledger/intf.ml | 408 ++++++++++++++++++ src/lib/merkle_ledger/intf.mli | 143 ------ src/lib/merkle_ledger/null_ledger.ml | 4 +- src/lib/merkle_ledger/null_ledger.mli | 4 +- src/lib/merkle_mask/base_merkle_tree_intf.mli | 2 +- src/lib/mina_ledger/ledger.ml | 4 +- src/lib/mina_ledger/ledger.mli | 4 +- src/lib/mina_ledger/ledger_transfer.ml | 2 +- 16 files changed, 431 insertions(+), 414 deletions(-) delete mode 100644 src/lib/merkle_ledger/base_inputs_intf.mli delete mode 100644 src/lib/merkle_ledger/base_ledger_intf.mli create mode 100644 src/lib/merkle_ledger/intf.ml delete mode 100644 src/lib/merkle_ledger/intf.mli diff --git a/src/lib/merkle_ledger/any_ledger.ml b/src/lib/merkle_ledger/any_ledger.ml index 7727c02edf1..cc8aa98d6f6 100644 --- a/src/lib/merkle_ledger/any_ledger.ml +++ b/src/lib/merkle_ledger/any_ledger.ml @@ -15,8 +15,8 @@ open Core_kernel -module Make_base (Inputs : Base_inputs_intf.Intf) : - Base_ledger_intf.ANY +module Make_base (Inputs : Intf.Inputs.Intf) : + Intf.Ledger.ANY with module Location = Inputs.Location with type key := Inputs.Key.t and type token_id := Inputs.Token_id.t @@ -29,7 +29,7 @@ module Make_base (Inputs : Base_inputs_intf.Intf) : module Location = Location module type Base_intf = - Base_ledger_intf.S + Intf.Ledger.S with module Addr = Location.Addr with module Location = Location with type key := Inputs.Key.t diff --git a/src/lib/merkle_ledger/any_ledger.mli b/src/lib/merkle_ledger/any_ledger.mli index 54fd866ed7f..8a0c50a527a 100644 --- a/src/lib/merkle_ledger/any_ledger.mli +++ b/src/lib/merkle_ledger/any_ledger.mli @@ -1,5 +1,5 @@ -module Make_base (Inputs : Base_inputs_intf.Intf) : - Base_ledger_intf.ANY +module Make_base (Inputs : Intf.Inputs.Intf) : + Intf.Ledger.ANY with module Location = Inputs.Location with type key := Inputs.Key.t and type token_id := Inputs.Token_id.t diff --git a/src/lib/merkle_ledger/base_inputs_intf.mli b/src/lib/merkle_ledger/base_inputs_intf.mli deleted file mode 100644 index 30a5f037f14..00000000000 --- a/src/lib/merkle_ledger/base_inputs_intf.mli +++ /dev/null @@ -1,31 +0,0 @@ -module type Intf = sig - module Key : Intf.Key - - module Token_id : Intf.Token_id - - module Account_id : - Intf.Account_id with type key := Key.t and type token_id := Token_id.t - - module Balance : Intf.Balance - - module Account : - Intf.Account - with type token_id := Token_id.t - and type account_id := Account_id.t - and type balance := Balance.t - - module Hash : Intf.Hash with type account := Account.t - - module Location : Location_intf.S -end - -module type DATABASE = sig - include Intf - - module Location_binable : - Core_kernel.Hashable.S_binable with type t := Location.t - - module Kvdb : Intf.Key_value_database with type config := string - - module Storage_locations : Intf.Storage_locations -end diff --git a/src/lib/merkle_ledger/base_ledger_intf.mli b/src/lib/merkle_ledger/base_ledger_intf.mli deleted file mode 100644 index fecef9b5233..00000000000 --- a/src/lib/merkle_ledger/base_ledger_intf.mli +++ /dev/null @@ -1,214 +0,0 @@ -open Core - -module type S = sig - (** a Merkle hash associated with the root node *) - type root_hash - - (** a Merkle hash associated any non-root node *) - type hash - - type account - - type key - - type token_id - - type token_id_set - - type account_id - - type account_id_set - - type index = int - - (** no deriving, purposely; signatures that include this one may add deriving *) - type t - - module Addr : module type of Merkle_address - - module Path : Merkle_path.S with type hash := hash - - module Location : sig - type t [@@deriving sexp, compare, hash] - - include Comparable.S with type t := t - end - - include - Syncable_intf.S - with type root_hash := root_hash - and type hash := hash - and type account := account - and type addr := Addr.t - and type path = Path.t - and type t := t - - (** list of accounts in the ledger *) - val to_list : t -> account list Async.Deferred.t - - (** list of accounts via slower sequential mechanism *) - val to_list_sequential : t -> account list - - (** iterate over all indexes and accounts *) - val iteri : t -> f:(index -> account -> unit) -> unit - - (** fold over accounts in the ledger, passing the Merkle address *) - val foldi : - t -> init:'accum -> f:(Addr.t -> 'accum -> account -> 'accum) -> 'accum - - (** the set of [account_id]s are ledger elements to skip during the fold, - because they're in a mask - *) - val foldi_with_ignored_accounts : - t - -> account_id_set - -> init:'accum - -> f:(Addr.t -> 'accum -> account -> 'accum) - -> 'accum - - (** fold over accounts until stop condition reached when calling [f]; calls [finish] for - result - *) - val fold_until : - t - -> init:'accum - -> f:('accum -> account -> ('accum, 'stop) Base.Continue_or_stop.t) - -> finish:('accum -> 'stop) - -> 'stop Async.Deferred.t - - (** set of account ids associated with accounts *) - val accounts : t -> account_id_set Async.Deferred.t - - (** Get the account id that owns a token. *) - val token_owner : t -> token_id -> account_id option - - (** Get the set of all accounts which own a token. *) - val token_owners : t -> account_id_set - - (** Get all of the tokens for which a public key has accounts. *) - val tokens : t -> key -> token_id_set - - val location_of_account : t -> account_id -> Location.t option - - val location_of_account_batch : - t -> account_id list -> (account_id * Location.t option) list - - (** This may return an error if the ledger is full. *) - val get_or_create_account : - t - -> account_id - -> account - -> ([ `Added | `Existed ] * Location.t) Or_error.t - - (** the ledger should not be used after calling [close] *) - val close : t -> unit - - (** for account locations in the ledger, the last (rightmost) filled location *) - val last_filled : t -> Location.t option - - val get_uuid : t -> Uuid.t - - (** return Some [directory] for ledgers that use a file system, else None *) - val get_directory : t -> string option - - val get : t -> Location.t -> account option - - val get_batch : t -> Location.t list -> (Location.t * account option) list - - val set : t -> Location.t -> account -> unit - - val set_batch : t -> (Location.t * account) list -> unit - - val get_at_index_exn : t -> int -> account - - val set_at_index_exn : t -> int -> account -> unit - - val index_of_account_exn : t -> account_id -> int - - (** meant to be a fast operation: the root hash is stored, rather - than calculated dynamically - *) - val merkle_root : t -> root_hash - - val merkle_path : t -> Location.t -> Path.t - - val merkle_path_at_index_exn : t -> int -> Path.t - - val merkle_path_batch : t -> Location.t list -> Path.t list - - val wide_merkle_path_batch : - t - -> Location.t list - -> [ `Left of hash * hash | `Right of hash * hash ] list list - - val get_hash_batch_exn : t -> Location.t list -> hash list - - (** Triggers when the ledger has been detached and should no longer be - accessed. - *) - val detached_signal : t -> unit Async_kernel.Deferred.t -end - -module type NULL = sig - include S - - val create : depth:int -> unit -> t -end - -module type ANY = sig - type key - - type token_id - - type token_id_set - - type account_id - - type account_id_set - - type account - - type hash - - module Location : Location_intf.S - - (** The type of the witness for a base ledger exposed here so that it can - * be easily accessed from outside this module *) - type witness [@@deriving sexp_of] - - module type Base_intf = - S - with module Addr = Location.Addr - with module Location = Location - with type key := key - and type token_id := token_id - and type token_id_set := token_id_set - and type account_id := account_id - and type account_id_set := account_id_set - and type hash := hash - and type root_hash := hash - and type account := account - - val cast : (module Base_intf with type t = 'a) -> 'a -> witness - - module M : Base_intf with type t = witness -end - -module type DATABASE = sig - include S - - val create : ?directory_name:string -> depth:int -> unit -> t - - (** create_checkpoint would create the checkpoint and open a db connection to that checkpoint *) - val create_checkpoint : t -> directory_name:string -> unit -> t - - (** make_checkpoint would only create the checkpoint *) - val make_checkpoint : t -> directory_name:string -> unit - - val with_ledger : depth:int -> f:(t -> 'a) -> 'a - - module For_tests : sig - val gen_account_location : - ledger_depth:int -> Location.t Core.Quickcheck.Generator.t - end -end diff --git a/src/lib/merkle_ledger/database.ml b/src/lib/merkle_ledger/database.ml index 356185f3553..eaa89ba1c39 100644 --- a/src/lib/merkle_ledger/database.ml +++ b/src/lib/merkle_ledger/database.ml @@ -1,6 +1,6 @@ open Core -module Make (Inputs : Base_inputs_intf.DATABASE) = struct +module Make (Inputs : Intf.Inputs.DATABASE) = struct (* The max depth of a merkle tree can never be greater than 253. *) open Inputs diff --git a/src/lib/merkle_ledger/database.mli b/src/lib/merkle_ledger/database.mli index 6aba1a6ef34..b399d3e09f6 100644 --- a/src/lib/merkle_ledger/database.mli +++ b/src/lib/merkle_ledger/database.mli @@ -1,5 +1,5 @@ -module Make (Inputs : Base_inputs_intf.DATABASE) : - Base_ledger_intf.DATABASE +module Make (Inputs : Intf.Inputs.DATABASE) : + Intf.Ledger.DATABASE with module Location = Inputs.Location and module Addr = Inputs.Location.Addr and type key := Inputs.Key.t diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index cc823c36c28..64a0c3e9208 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -11,15 +11,12 @@ ; module openings may both solve the warning *and* help the reader) ; ; 44: open-shadow-identifier (operation overloading is common in the codebase) - (:standard -w +a-40..42-44 -warn-error +a -open Core)) + (:standard -w +a-40..42-44 -warn-error +a-70 -open Core)) (library_flags -linkall) (modules_without_implementation - base_inputs_intf - base_ledger_intf - graphviz_intf - intf - location_intf - syncable_intf) + graphviz_intf + location_intf + syncable_intf) (libraries ;; opam libraries async diff --git a/src/lib/merkle_ledger/graphviz_intf.mli b/src/lib/merkle_ledger/graphviz_intf.mli index 4b553fed915..9a529ac3ffe 100644 --- a/src/lib/merkle_ledger/graphviz_intf.mli +++ b/src/lib/merkle_ledger/graphviz_intf.mli @@ -35,7 +35,7 @@ module type Inputs_intf = sig module Location : Location_intf.S module Ledger : - Base_ledger_intf.S + Intf.Ledger.S with module Addr = Location.Addr and module Location = Location and type account_id := Account_id.t diff --git a/src/lib/merkle_ledger/intf.ml b/src/lib/merkle_ledger/intf.ml new file mode 100644 index 00000000000..5995f574877 --- /dev/null +++ b/src/lib/merkle_ledger/intf.ml @@ -0,0 +1,408 @@ +open Core + +module type Key = sig + type t [@@deriving sexp] + + module Stable : sig + module V1 : sig + type t [@@deriving sexp, bin_io] + end + + module Latest = V1 + end + with type V1.t = t + + val empty : t + + val to_string : t -> string + + include Hashable.S_binable with type t := t + + include Comparable.S with type t := t +end + +module type Token_id = sig + type t [@@deriving sexp] + + module Stable : sig + module Latest : sig + type t [@@deriving bin_io] + end + end + with type Latest.t = t + + val default : t + + include Hashable.S_binable with type t := t + + include Comparable.S_binable with type t := t +end + +module type Account_id = sig + type key + + type token_id + + [%%versioned: + module Stable : sig + module V2 : sig + type t [@@deriving sexp] + end + end] + + val public_key : t -> key + + val token_id : t -> token_id + + val create : key -> token_id -> t + + val derive_token_id : owner:t -> token_id + + include Hashable.S_binable with type t := t + + include Comparable.S with type t := t +end + +module type Balance = sig + type t [@@deriving equal] + + val zero : t + + val to_int : t -> int +end + +module type Account = sig + type t [@@deriving bin_io, equal, sexp, compare] + + type token_id + + type account_id + + type balance + + val token : t -> token_id + + val identifier : t -> account_id + + val balance : t -> balance + + val empty : t +end + +module type Hash = sig + type t [@@deriving bin_io, compare, equal, sexp, yojson] + + val to_base58_check : t -> string + + include Hashable.S_binable with type t := t + + type account + + val merge : height:int -> t -> t -> t + + val hash_account : account -> t + + val empty_account : t +end + +module type Depth = sig + val depth : int +end + +module type Key_value_database = sig + type t [@@deriving sexp] + + type config + + include + Key_value_database.Intf.Ident + with type t := t + and type key := Bigstring.t + and type value := Bigstring.t + and type config := config + + val create_checkpoint : t -> string -> t + + val make_checkpoint : t -> string -> unit + + val get_uuid : t -> Uuid.t + + val set_batch : + t + -> ?remove_keys:Bigstring.t list + -> key_data_pairs:(Bigstring.t * Bigstring.t) list + -> unit + + (** An association list, sorted by key *) + val to_alist : t -> (Bigstring.t * Bigstring.t) list + + val foldi : + t + -> init:'a + -> f:(int -> 'a -> key:Bigstring.t -> data:Bigstring.t -> 'a) + -> 'a + + val fold_until : + t + -> init:'a + -> f: + ( 'a + -> key:Bigstring.t + -> data:Bigstring.t + -> ('a, 'b) Continue_or_stop.t ) + -> finish:('a -> 'b) + -> 'b +end + +module type Storage_locations = sig + val key_value_db_dir : string +end + +module Inputs = struct + module type Intf = sig + module Key : Key + + module Token_id : Token_id + + module Account_id : + Account_id with type key := Key.t and type token_id := Token_id.t + + module Balance : Balance + + module Account : + Account + with type token_id := Token_id.t + and type account_id := Account_id.t + and type balance := Balance.t + + module Hash : Hash with type account := Account.t + + module Location : Location_intf.S + end + + module type DATABASE = sig + include Intf + + module Location_binable : + Core_kernel.Hashable.S_binable with type t := Location.t + + module Kvdb : Key_value_database with type config := string + + module Storage_locations : Storage_locations + end +end + +module Ledger = struct + module type S = sig + (** a Merkle hash associated with the root node *) + type root_hash + + (** a Merkle hash associated any non-root node *) + type hash + + type account + + type key + + type token_id + + type token_id_set + + type account_id + + type account_id_set + + type index = int + + (** no deriving, purposely; signatures that include this one may add deriving *) + type t + + module Addr : module type of Merkle_address + + module Path : Merkle_path.S with type hash := hash + + module Location : sig + type t [@@deriving sexp, compare, hash] + + include Comparable.S with type t := t + end + + include + Syncable_intf.S + with type root_hash := root_hash + and type hash := hash + and type account := account + and type addr := Addr.t + and type path = Path.t + and type t := t + + (** list of accounts in the ledger *) + val to_list : t -> account list Async.Deferred.t + + (** list of accounts via slower sequential mechanism *) + val to_list_sequential : t -> account list + + (** iterate over all indexes and accounts *) + val iteri : t -> f:(index -> account -> unit) -> unit + + (** fold over accounts in the ledger, passing the Merkle address *) + val foldi : + t -> init:'accum -> f:(Addr.t -> 'accum -> account -> 'accum) -> 'accum + + (** the set of [account_id]s are ledger elements to skip during the fold, + because they're in a mask + *) + val foldi_with_ignored_accounts : + t + -> account_id_set + -> init:'accum + -> f:(Addr.t -> 'accum -> account -> 'accum) + -> 'accum + + (** fold over accounts until stop condition reached when calling [f]; calls [finish] for + result + *) + val fold_until : + t + -> init:'accum + -> f:('accum -> account -> ('accum, 'stop) Base.Continue_or_stop.t) + -> finish:('accum -> 'stop) + -> 'stop Async.Deferred.t + + (** set of account ids associated with accounts *) + val accounts : t -> account_id_set Async.Deferred.t + + (** Get the account id that owns a token. *) + val token_owner : t -> token_id -> account_id option + + (** Get the set of all accounts which own a token. *) + val token_owners : t -> account_id_set + + (** Get all of the tokens for which a public key has accounts. *) + val tokens : t -> key -> token_id_set + + val location_of_account : t -> account_id -> Location.t option + + val location_of_account_batch : + t -> account_id list -> (account_id * Location.t option) list + + (** This may return an error if the ledger is full. *) + val get_or_create_account : + t + -> account_id + -> account + -> ([ `Added | `Existed ] * Location.t) Or_error.t + + (** the ledger should not be used after calling [close] *) + val close : t -> unit + + (** for account locations in the ledger, the last (rightmost) filled location *) + val last_filled : t -> Location.t option + + val get_uuid : t -> Uuid.t + + (** return Some [directory] for ledgers that use a file system, else None *) + val get_directory : t -> string option + + val get : t -> Location.t -> account option + + val get_batch : t -> Location.t list -> (Location.t * account option) list + + val set : t -> Location.t -> account -> unit + + val set_batch : t -> (Location.t * account) list -> unit + + val get_at_index_exn : t -> int -> account + + val set_at_index_exn : t -> int -> account -> unit + + val index_of_account_exn : t -> account_id -> int + + (** meant to be a fast operation: the root hash is stored, rather + than calculated dynamically + *) + val merkle_root : t -> root_hash + + val merkle_path : t -> Location.t -> Path.t + + val merkle_path_at_index_exn : t -> int -> Path.t + + val merkle_path_batch : t -> Location.t list -> Path.t list + + val wide_merkle_path_batch : + t + -> Location.t list + -> [ `Left of hash * hash | `Right of hash * hash ] list list + + val get_hash_batch_exn : t -> Location.t list -> hash list + + (** Triggers when the ledger has been detached and should no longer be + accessed. + *) + val detached_signal : t -> unit Async_kernel.Deferred.t + end + + module type NULL = sig + include S + + val create : depth:int -> unit -> t + end + + module type ANY = sig + type key + + type token_id + + type token_id_set + + type account_id + + type account_id_set + + type account + + type hash + + module Location : Location_intf.S + + (** The type of the witness for a base ledger exposed here so that it can + * be easily accessed from outside this module *) + type witness [@@deriving sexp_of] + + module type Base_intf = + S + with module Addr = Location.Addr + with module Location = Location + with type key := key + and type token_id := token_id + and type token_id_set := token_id_set + and type account_id := account_id + and type account_id_set := account_id_set + and type hash := hash + and type root_hash := hash + and type account := account + + val cast : (module Base_intf with type t = 'a) -> 'a -> witness + + module M : Base_intf with type t = witness + end + + module type DATABASE = sig + include S + + val create : ?directory_name:string -> depth:int -> unit -> t + + (** create_checkpoint would create the checkpoint and open a db connection to that checkpoint *) + val create_checkpoint : t -> directory_name:string -> unit -> t + + (** make_checkpoint would only create the checkpoint *) + val make_checkpoint : t -> directory_name:string -> unit + + val with_ledger : depth:int -> f:(t -> 'a) -> 'a + + module For_tests : sig + val gen_account_location : + ledger_depth:int -> Location.t Core.Quickcheck.Generator.t + end + end +end diff --git a/src/lib/merkle_ledger/intf.mli b/src/lib/merkle_ledger/intf.mli deleted file mode 100644 index df9c2769ff3..00000000000 --- a/src/lib/merkle_ledger/intf.mli +++ /dev/null @@ -1,143 +0,0 @@ -open Core - -module type Key = sig - type t [@@deriving sexp] - - module Stable : sig - module V1 : sig - type t [@@deriving sexp, bin_io] - end - - module Latest = V1 - end - with type V1.t = t - - val empty : t - - val to_string : t -> string - - include Hashable.S_binable with type t := t - - include Comparable.S with type t := t -end - -module type Token_id = sig - type t [@@deriving sexp] - - module Stable : sig - module Latest : sig - type t [@@deriving bin_io] - end - end - with type Latest.t = t - - val default : t - - include Hashable.S_binable with type t := t - - include Comparable.S_binable with type t := t -end - -module type Account_id = sig - type key - - type token_id - - [%%versioned: - module Stable : sig - module V2 : sig - type t [@@deriving sexp] - end - end] - - val public_key : t -> key - - val token_id : t -> token_id - - val create : key -> token_id -> t - - val derive_token_id : owner:t -> token_id - - include Hashable.S_binable with type t := t - - include Comparable.S with type t := t -end - -module type Balance = sig - type t [@@deriving equal] - - val zero : t - - val to_int : t -> int -end - -module type Account = sig - type t [@@deriving bin_io, equal, sexp, compare] - - type token_id - - type account_id - - type balance - - val token : t -> token_id - - val identifier : t -> account_id - - val balance : t -> balance - - val empty : t -end - -module type Hash = sig - type t [@@deriving bin_io, compare, equal, sexp, yojson] - - val to_base58_check : t -> string - - include Hashable.S_binable with type t := t - - type account - - val merge : height:int -> t -> t -> t - - val hash_account : account -> t - - val empty_account : t -end - -module type Depth = sig - val depth : int -end - -module type Key_value_database = sig - type t [@@deriving sexp] - - type config - - include - Key_value_database.Intf.Ident - with type t := t - and type key := Bigstring.t - and type value := Bigstring.t - and type config := config - - val create_checkpoint : t -> string -> t - - val make_checkpoint : t -> string -> unit - - val get_uuid : t -> Uuid.t - - val set_batch : - t - -> ?remove_keys:Bigstring.t list - -> key_data_pairs:(Bigstring.t * Bigstring.t) list - -> unit - - val to_alist : t -> (Bigstring.t * Bigstring.t) list - - (* an association list, sorted by key *) -end - -module type Storage_locations = sig - val key_value_db_dir : string -end diff --git a/src/lib/merkle_ledger/null_ledger.ml b/src/lib/merkle_ledger/null_ledger.ml index bc89a839c2b..69cba669e72 100644 --- a/src/lib/merkle_ledger/null_ledger.ml +++ b/src/lib/merkle_ledger/null_ledger.ml @@ -1,8 +1,8 @@ open Core_kernel -module Make (Inputs : Base_inputs_intf.Intf) : sig +module Make (Inputs : Intf.Inputs.Intf) : sig include - Base_ledger_intf.NULL + Intf.Ledger.NULL with module Addr = Inputs.Location.Addr with module Location = Inputs.Location with type key := Inputs.Key.t diff --git a/src/lib/merkle_ledger/null_ledger.mli b/src/lib/merkle_ledger/null_ledger.mli index 9c95e4d4632..e7a3b77c377 100644 --- a/src/lib/merkle_ledger/null_ledger.mli +++ b/src/lib/merkle_ledger/null_ledger.mli @@ -1,6 +1,6 @@ -module Make (Inputs : Base_inputs_intf.Intf) : sig +module Make (Inputs : Intf.Inputs.Intf) : sig include - Base_ledger_intf.NULL + Intf.Ledger.NULL with module Addr = Inputs.Location.Addr with module Location = Inputs.Location with type key := Inputs.Key.t diff --git a/src/lib/merkle_mask/base_merkle_tree_intf.mli b/src/lib/merkle_mask/base_merkle_tree_intf.mli index 8c0266524ae..be6db18f7ea 100644 --- a/src/lib/merkle_mask/base_merkle_tree_intf.mli +++ b/src/lib/merkle_mask/base_merkle_tree_intf.mli @@ -1,4 +1,4 @@ (* base_merkle_tree_intf.ml *) (** base module type for masking and masked Merkle trees *) -module type S = Merkle_ledger.Base_ledger_intf.S +module type S = Merkle_ledger.Intf.Ledger.S diff --git a/src/lib/mina_ledger/ledger.ml b/src/lib/mina_ledger/ledger.ml index 933472a3838..470452519b0 100644 --- a/src/lib/mina_ledger/ledger.ml +++ b/src/lib/mina_ledger/ledger.ml @@ -106,7 +106,7 @@ module Ledger_inner = struct end module Db : - Merkle_ledger.Base_ledger_intf.DATABASE + Merkle_ledger.Intf.Ledger.DATABASE with module Location = Location_at_depth with module Addr = Location_at_depth.Addr with type root_hash := Ledger_hash.t @@ -122,7 +122,7 @@ module Ledger_inner = struct module Null = Null_ledger.Make (Inputs) module Any_ledger : - Merkle_ledger.Base_ledger_intf.ANY + Merkle_ledger.Intf.Ledger.ANY with module Location = Location_at_depth with type account := Account.t and type key := Public_key.Compressed.t diff --git a/src/lib/mina_ledger/ledger.mli b/src/lib/mina_ledger/ledger.mli index f1a7a604c49..483b4222d81 100644 --- a/src/lib/mina_ledger/ledger.mli +++ b/src/lib/mina_ledger/ledger.mli @@ -5,7 +5,7 @@ open Mina_base module Location : Merkle_ledger.Location_intf.S module Db : - Merkle_ledger.Base_ledger_intf.DATABASE + Merkle_ledger.Intf.Ledger.DATABASE with module Location = Location with module Addr = Location.Addr with type root_hash := Ledger_hash.t @@ -18,7 +18,7 @@ module Db : and type account_id_set := Account_id.Set.t module Any_ledger : - Merkle_ledger.Base_ledger_intf.ANY + Merkle_ledger.Intf.Ledger.ANY with module Location = Location with type account := Account.t and type key := Public_key.Compressed.t diff --git a/src/lib/mina_ledger/ledger_transfer.ml b/src/lib/mina_ledger/ledger_transfer.ml index 22efdb917a8..9efd4643d35 100644 --- a/src/lib/mina_ledger/ledger_transfer.ml +++ b/src/lib/mina_ledger/ledger_transfer.ml @@ -2,7 +2,7 @@ open Core_kernel open Mina_base module type Base_ledger_intf = - Merkle_ledger.Base_ledger_intf.S + Merkle_ledger.Intf.Ledger.S with type account := Account.t and type key := Signature_lib.Public_key.Compressed.t and type token_id := Token_id.t From caa09509ee8994c068c8dd009aaecb963e62a9fd Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 15:43:26 +0100 Subject: [PATCH 26/30] Merkle_ledger: mv Syncable_intf to Intf --- src/lib/merkle_ledger/dune | 3 +- src/lib/merkle_ledger/intf.ml | 36 +++++++++++++++++++++- src/lib/merkle_ledger/syncable_intf.mli | 33 -------------------- src/lib/syncable_ledger/syncable_ledger.ml | 2 +- 4 files changed, 37 insertions(+), 37 deletions(-) delete mode 100644 src/lib/merkle_ledger/syncable_intf.mli diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index 64a0c3e9208..59ca1a7be2f 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -15,8 +15,7 @@ (library_flags -linkall) (modules_without_implementation graphviz_intf - location_intf - syncable_intf) + location_intf) (libraries ;; opam libraries async diff --git a/src/lib/merkle_ledger/intf.ml b/src/lib/merkle_ledger/intf.ml index 5995f574877..2e506c82e87 100644 --- a/src/lib/merkle_ledger/intf.ml +++ b/src/lib/merkle_ledger/intf.ml @@ -158,6 +158,40 @@ module type Storage_locations = sig val key_value_db_dir : string end +module type SYNCABLE = sig + type root_hash + + type hash + + type account + + type addr + + type t [@@deriving sexp] + + type path + + val depth : t -> int + + val num_accounts : t -> int + + val merkle_path_at_addr_exn : t -> addr -> path + + val get_inner_hash_at_addr_exn : t -> addr -> hash + + val set_inner_hash_at_addr_exn : t -> addr -> hash -> unit + + val set_all_accounts_rooted_at_exn : t -> addr -> account list -> unit + + val set_batch_accounts : t -> (addr * account) list -> unit + + (** Get all of the accounts that are in a subtree of the underlying Merkle + tree rooted at `address`. The accounts are ordered by their addresses. *) + val get_all_accounts_rooted_at_exn : t -> addr -> (addr * account) list + + val merkle_root : t -> root_hash +end + module Inputs = struct module type Intf = sig module Key : Key @@ -228,7 +262,7 @@ module Ledger = struct end include - Syncable_intf.S + SYNCABLE with type root_hash := root_hash and type hash := hash and type account := account diff --git a/src/lib/merkle_ledger/syncable_intf.mli b/src/lib/merkle_ledger/syncable_intf.mli deleted file mode 100644 index 7665106793d..00000000000 --- a/src/lib/merkle_ledger/syncable_intf.mli +++ /dev/null @@ -1,33 +0,0 @@ -module type S = sig - type root_hash - - type hash - - type account - - type addr - - type t [@@deriving sexp] - - type path - - val depth : t -> int - - val num_accounts : t -> int - - val merkle_path_at_addr_exn : t -> addr -> path - - val get_inner_hash_at_addr_exn : t -> addr -> hash - - val set_inner_hash_at_addr_exn : t -> addr -> hash -> unit - - val set_all_accounts_rooted_at_exn : t -> addr -> account list -> unit - - val set_batch_accounts : t -> (addr * account) list -> unit - - (** Get all of the accounts that are in a subtree of the underlying Merkle - tree rooted at `address`. The accounts are ordered by their addresses. *) - val get_all_accounts_rooted_at_exn : t -> addr -> (addr * account) list - - val merkle_root : t -> root_hash -end diff --git a/src/lib/syncable_ledger/syncable_ledger.ml b/src/lib/syncable_ledger/syncable_ledger.ml index a5db250cda5..44f1f972064 100644 --- a/src/lib/syncable_ledger/syncable_ledger.ml +++ b/src/lib/syncable_ledger/syncable_ledger.ml @@ -69,7 +69,7 @@ module type Inputs_intf = sig end module MT : - Merkle_ledger.Syncable_intf.S + Merkle_ledger.Intf.SYNCABLE with type hash := Hash.t and type root_hash := Root_hash.t and type addr := Addr.t From b0008f51ea83b8ceb5f078ca127ef384fdbce48a Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 15:47:19 +0100 Subject: [PATCH 27/30] Merkle_ledger: mv Graphviz_intf to Intf --- src/lib/merkle_ledger/dune | 1 - src/lib/merkle_ledger/graphviz.ml | 4 +-- src/lib/merkle_ledger/graphviz.mli | 4 +-- src/lib/merkle_ledger/graphviz_intf.mli | 45 ------------------------- src/lib/merkle_ledger/intf.ml | 32 ++++++++++++++++++ 5 files changed, 36 insertions(+), 50 deletions(-) delete mode 100644 src/lib/merkle_ledger/graphviz_intf.mli diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index 59ca1a7be2f..ee3fe493fb9 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -14,7 +14,6 @@ (:standard -w +a-40..42-44 -warn-error +a-70 -open Core)) (library_flags -linkall) (modules_without_implementation - graphviz_intf location_intf) (libraries ;; opam libraries diff --git a/src/lib/merkle_ledger/graphviz.ml b/src/lib/merkle_ledger/graphviz.ml index 15f32f4fe3e..950ad5eb88b 100644 --- a/src/lib/merkle_ledger/graphviz.ml +++ b/src/lib/merkle_ledger/graphviz.ml @@ -1,8 +1,8 @@ open Core open Async -module Make (Inputs : Graphviz_intf.Inputs_intf) : - Graphviz_intf.S +module Make (Inputs : Intf.Graphviz.I) : + Intf.Graphviz.S with type addr := Inputs.Location.Addr.t and type ledger := Inputs.Ledger.t = struct open Inputs diff --git a/src/lib/merkle_ledger/graphviz.mli b/src/lib/merkle_ledger/graphviz.mli index c1148e59471..86a52fa54c5 100644 --- a/src/lib/merkle_ledger/graphviz.mli +++ b/src/lib/merkle_ledger/graphviz.mli @@ -1,6 +1,6 @@ (** Visualizable_ledger shows a subgraph of a merkle_ledger using Graphviz *) -module Make (Inputs : Graphviz_intf.Inputs_intf) : - Graphviz_intf.S +module Make (Inputs : Intf.Graphviz.I) : + Intf.Graphviz.S with type addr := Inputs.Location.Addr.t and type ledger := Inputs.Ledger.t diff --git a/src/lib/merkle_ledger/graphviz_intf.mli b/src/lib/merkle_ledger/graphviz_intf.mli deleted file mode 100644 index 9a529ac3ffe..00000000000 --- a/src/lib/merkle_ledger/graphviz_intf.mli +++ /dev/null @@ -1,45 +0,0 @@ -module type S = sig - type addr - - type ledger - - type t - - (* Visualize will enumerate through all edges of a subtree with a - initial_address. It will then interpret all of the edges and nodes into an - intermediate form that will be easy to write into a dot file *) - val visualize : ledger -> initial_address:addr -> t - - (* Write will transform the intermediate form generate by visualize and save - the results into a dot file *) - val write : path:string -> name:string -> t -> unit Async.Deferred.t -end - -module type Inputs_intf = sig - module Key : Intf.Key - - module Token_id : Intf.Token_id - - module Account_id : - Intf.Account_id with type key := Key.t and type token_id := Token_id.t - - module Balance : Intf.Balance - - module Account : - Intf.Account - with type account_id := Account_id.t - and type balance := Balance.t - - module Hash : Intf.Hash with type account := Account.t - - module Location : Location_intf.S - - module Ledger : - Intf.Ledger.S - with module Addr = Location.Addr - and module Location = Location - and type account_id := Account_id.t - and type account_id_set := Account_id.Set.t - and type hash := Hash.t - and type account := Account.t -end diff --git a/src/lib/merkle_ledger/intf.ml b/src/lib/merkle_ledger/intf.ml index 2e506c82e87..e71faadcdfa 100644 --- a/src/lib/merkle_ledger/intf.ml +++ b/src/lib/merkle_ledger/intf.ml @@ -440,3 +440,35 @@ module Ledger = struct end end end + +module Graphviz = struct + module type S = sig + type addr + + type ledger + + type t + + (* Visualize will enumerate through all edges of a subtree with a + initial_address. It will then interpret all of the edges and nodes into an + intermediate form that will be easy to write into a dot file *) + val visualize : ledger -> initial_address:addr -> t + + (* Write will transform the intermediate form generate by visualize and save + the results into a dot file *) + val write : path:string -> name:string -> t -> unit Async.Deferred.t + end + + module type I = sig + include Inputs.Intf + + module Ledger : + Ledger.S + with module Addr = Location.Addr + and module Location = Location + and type account_id := Account_id.t + and type account_id_set := Account_id.Set.t + and type hash := Hash.t + and type account := Account.t + end +end From da8931a6746746910c2aa7766f76c2cf788ffddf Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Fri, 16 Feb 2024 16:32:59 +0100 Subject: [PATCH 28/30] merkle_ledger: auto open Core_Kernel in dune --- src/lib/merkle_ledger/any_ledger.ml | 2 - src/lib/merkle_ledger/database.ml | 4 +- src/lib/merkle_ledger/dune | 2 +- src/lib/merkle_ledger/graphviz.ml | 1 - src/lib/merkle_ledger/intf.ml | 60 ++++++++++++++++++++++--- src/lib/merkle_ledger/location.ml | 7 ++- src/lib/merkle_ledger/location.mli | 10 ++--- src/lib/merkle_ledger/location_intf.mli | 2 - src/lib/merkle_ledger/merkle_path.ml | 2 - src/lib/merkle_ledger/null_ledger.ml | 2 - src/lib/merkle_ledger/util.ml | 2 - src/lib/merkle_ledger/util.mli | 3 +- 12 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/lib/merkle_ledger/any_ledger.ml b/src/lib/merkle_ledger/any_ledger.ml index cc8aa98d6f6..e74ac391d90 100644 --- a/src/lib/merkle_ledger/any_ledger.ml +++ b/src/lib/merkle_ledger/any_ledger.ml @@ -13,8 +13,6 @@ * Props to @nholland for showing me this trick. * *) -open Core_kernel - module Make_base (Inputs : Intf.Inputs.Intf) : Intf.Ledger.ANY with module Location = Inputs.Location diff --git a/src/lib/merkle_ledger/database.ml b/src/lib/merkle_ledger/database.ml index eaa89ba1c39..7488725511c 100644 --- a/src/lib/merkle_ledger/database.ml +++ b/src/lib/merkle_ledger/database.ml @@ -1,5 +1,3 @@ -open Core - module Make (Inputs : Intf.Inputs.DATABASE) = struct (* The max depth of a merkle tree can never be greater than 253. *) open Inputs @@ -46,6 +44,8 @@ module Make (Inputs : Intf.Inputs.DATABASE) = struct let depth t = t.depth let create ?directory_name ~depth () = + let open Core in + (* for ^/ and Unix below *) assert (depth < 0xfe) ; let uuid = Uuid_unix.create () in let directory = diff --git a/src/lib/merkle_ledger/dune b/src/lib/merkle_ledger/dune index ee3fe493fb9..c85fcfa76c4 100644 --- a/src/lib/merkle_ledger/dune +++ b/src/lib/merkle_ledger/dune @@ -11,7 +11,7 @@ ; module openings may both solve the warning *and* help the reader) ; ; 44: open-shadow-identifier (operation overloading is common in the codebase) - (:standard -w +a-40..42-44 -warn-error +a-70 -open Core)) + (:standard -w +a-40..42-44 -warn-error +a-70 -open Core_kernel)) (library_flags -linkall) (modules_without_implementation location_intf) diff --git a/src/lib/merkle_ledger/graphviz.ml b/src/lib/merkle_ledger/graphviz.ml index 950ad5eb88b..894514cf361 100644 --- a/src/lib/merkle_ledger/graphviz.ml +++ b/src/lib/merkle_ledger/graphviz.ml @@ -1,4 +1,3 @@ -open Core open Async module Make (Inputs : Intf.Graphviz.I) : diff --git a/src/lib/merkle_ledger/intf.ml b/src/lib/merkle_ledger/intf.ml index e71faadcdfa..39de10c4c5e 100644 --- a/src/lib/merkle_ledger/intf.ml +++ b/src/lib/merkle_ledger/intf.ml @@ -1,4 +1,53 @@ -open Core +module type LOCATION = sig + module Addr : module type of Merkle_address + + module Prefix : sig + val generic : Unsigned.UInt8.t + + val account : Unsigned.UInt8.t + + val hash : ledger_depth:int -> int -> Unsigned.UInt8.t + end + + type t = Generic of Bigstring.t | Account of Addr.t | Hash of Addr.t + [@@deriving sexp, hash, compare] + + val is_generic : t -> bool + + val is_account : t -> bool + + val is_hash : t -> bool + + val height : ledger_depth:int -> t -> int + + val root_hash : t + + val last_direction : Addr.t -> Direction.t + + val build_generic : Bigstring.t -> t + + val parse : ledger_depth:int -> Bigstring.t -> (t, unit) Result.t + + val prefix_bigstring : Unsigned.UInt8.t -> Bigstring.t -> Bigstring.t + + val to_path_exn : t -> Addr.t + + val serialize : ledger_depth:int -> t -> Bigstring.t + + val parent : t -> t + + val next : t -> t Option.t + + val prev : t -> t Option.t + + val sibling : t -> t + + val order_siblings : t -> 'a -> 'a -> 'a * 'a + + val merkle_path_dependencies_exn : t -> (t * Direction.t) list + + include Comparable.S with type t := t +end module type Key = sig type t [@@deriving sexp] @@ -211,14 +260,13 @@ module Inputs = struct module Hash : Hash with type account := Account.t - module Location : Location_intf.S + module Location : LOCATION end module type DATABASE = sig include Intf - module Location_binable : - Core_kernel.Hashable.S_binable with type t := Location.t + module Location_binable : Hashable.S_binable with type t := Location.t module Kvdb : Key_value_database with type config := string @@ -397,7 +445,7 @@ module Ledger = struct type hash - module Location : Location_intf.S + module Location : LOCATION (** The type of the witness for a base ledger exposed here so that it can * be easily accessed from outside this module *) @@ -436,7 +484,7 @@ module Ledger = struct module For_tests : sig val gen_account_location : - ledger_depth:int -> Location.t Core.Quickcheck.Generator.t + ledger_depth:int -> Location.t Quickcheck.Generator.t end end end diff --git a/src/lib/merkle_ledger/location.ml b/src/lib/merkle_ledger/location.ml index aa3ec1f7e01..cee243a14f4 100644 --- a/src/lib/merkle_ledger/location.ml +++ b/src/lib/merkle_ledger/location.ml @@ -1,4 +1,3 @@ -open Core open Unsigned (* add functions to library module Bigstring so we can derive hash for the type t below *) @@ -6,7 +5,7 @@ module Bigstring = struct [%%versioned_binable module Stable = struct module V1 = struct - type t = Core_kernel.Bigstring.Stable.V1.t [@@deriving sexp, compare] + type t = Bigstring.Stable.V1.t [@@deriving sexp, compare] let to_latest = Fn.id @@ -18,9 +17,9 @@ module Bigstring = struct include Bounded_types.String.Of_stringable (struct type nonrec t = t - let of_string s = Core_kernel.Bigstring.of_string s + let of_string s = Bigstring.of_string s - let to_string s = Core_kernel.Bigstring.to_string s + let to_string s = Bigstring.to_string s end) end end] diff --git a/src/lib/merkle_ledger/location.mli b/src/lib/merkle_ledger/location.mli index 2feda1c9061..7fc11b37d21 100644 --- a/src/lib/merkle_ledger/location.mli +++ b/src/lib/merkle_ledger/location.mli @@ -2,17 +2,17 @@ module Bigstring : sig [%%versioned: module Stable : sig module V1 : sig - type t = Core_kernel.Bigstring.Stable.V1.t [@@deriving sexp, compare] + type t = Bigstring.Stable.V1.t [@@deriving sexp, compare] - include Core_kernel.Binable.S with type t := t + include Binable.S with type t := t - val hash_fold_t : Core_kernel.Hash.state -> t -> Core_kernel.Hash.state + val hash_fold_t : Hash.state -> t -> Hash.state - val hash : t -> Core_kernel.Hash.hash_value + val hash : t -> Hash.hash_value end end] - include Core_kernel.Hashable.S with type t := t + include Hashable.S with type t := t end module T : Location_intf.S diff --git a/src/lib/merkle_ledger/location_intf.mli b/src/lib/merkle_ledger/location_intf.mli index 15cbe61d40e..19fb518e2e7 100644 --- a/src/lib/merkle_ledger/location_intf.mli +++ b/src/lib/merkle_ledger/location_intf.mli @@ -1,7 +1,5 @@ (* location_intf.ml -- interface file for Location *) -open Core - module type S = sig module Addr : module type of Merkle_address diff --git a/src/lib/merkle_ledger/merkle_path.ml b/src/lib/merkle_ledger/merkle_path.ml index 2b14ad8a12c..fcea8eb6dcf 100644 --- a/src/lib/merkle_ledger/merkle_path.ml +++ b/src/lib/merkle_ledger/merkle_path.ml @@ -1,5 +1,3 @@ -open Core_kernel - module type S = sig type hash diff --git a/src/lib/merkle_ledger/null_ledger.ml b/src/lib/merkle_ledger/null_ledger.ml index 69cba669e72..157244e8c10 100644 --- a/src/lib/merkle_ledger/null_ledger.ml +++ b/src/lib/merkle_ledger/null_ledger.ml @@ -1,5 +1,3 @@ -open Core_kernel - module Make (Inputs : Intf.Inputs.Intf) : sig include Intf.Ledger.NULL diff --git a/src/lib/merkle_ledger/util.ml b/src/lib/merkle_ledger/util.ml index f2061c85e73..af51910d7c5 100644 --- a/src/lib/merkle_ledger/util.ml +++ b/src/lib/merkle_ledger/util.ml @@ -1,5 +1,3 @@ -open Core_kernel - module type Inputs_intf = sig module Location : Location_intf.S diff --git a/src/lib/merkle_ledger/util.mli b/src/lib/merkle_ledger/util.mli index 7acf5cd1b6e..5473f664eb8 100644 --- a/src/lib/merkle_ledger/util.mli +++ b/src/lib/merkle_ledger/util.mli @@ -1,8 +1,7 @@ module type Inputs_intf = sig module Location : Location_intf.S - module Location_binable : - Core_kernel.Hashable.S_binable with type t := Location.t + module Location_binable : Hashable.S_binable with type t := Location.t module Key : Intf.Key From b4035b1a5622bb5f86a90ecc8f4a29f79d3f27b0 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Mon, 26 Feb 2024 16:50:46 +0100 Subject: [PATCH 29/30] Merkle_ledger: reintegrate check_path for tests --- src/lib/merkle_ledger/merkle_path.ml | 5 +++++ src/lib/merkle_ledger/merkle_path.mli | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/lib/merkle_ledger/merkle_path.ml b/src/lib/merkle_ledger/merkle_path.ml index fcea8eb6dcf..c837c8a9149 100644 --- a/src/lib/merkle_ledger/merkle_path.ml +++ b/src/lib/merkle_ledger/merkle_path.ml @@ -8,6 +8,8 @@ module type S = sig type t = elem list [@@deriving sexp, equal] val implied_root : t -> hash -> hash + + val check_path : t -> hash -> hash -> bool end module Make (Hash : sig @@ -34,4 +36,7 @@ end) : S with type hash := Hash.t = struct in (acc, height + 1) ) |> fst + + let check_path t leaf_hash root_hash = + Hash.equal (implied_root t leaf_hash) root_hash end diff --git a/src/lib/merkle_ledger/merkle_path.mli b/src/lib/merkle_ledger/merkle_path.mli index fc7d744a766..158b5489511 100644 --- a/src/lib/merkle_ledger/merkle_path.mli +++ b/src/lib/merkle_ledger/merkle_path.mli @@ -8,6 +8,10 @@ module type S = sig type t = elem list [@@deriving sexp, equal] val implied_root : t -> hash -> hash + + (** [check_path path leaf_hash root_hash] is used in tests to check that + [leaf_hash] along with [path] actually corresponds to [root_hash]. *) + val check_path : t -> hash -> hash -> bool end module Make (Hash : sig From 8377269b6897f487fb3b816338e0ff1481e5d752 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Mon, 26 Feb 2024 17:01:26 +0100 Subject: [PATCH 30/30] Merkle_ledger_tests: use new interfaces in tests --- src/lib/merkle_ledger_tests/test_database.ml | 2 +- src/lib/merkle_ledger_tests/test_mask.ml | 2 +- src/lib/merkle_ledger_tests/test_stubs.ml | 11 +++++++++++ src/lib/syncable_ledger/test.ml | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/merkle_ledger_tests/test_database.ml b/src/lib/merkle_ledger_tests/test_database.ml index 70c21a828d3..e6ac59a406f 100644 --- a/src/lib/merkle_ledger_tests/test_database.ml +++ b/src/lib/merkle_ledger_tests/test_database.ml @@ -7,7 +7,7 @@ let%test_module "test functor on in memory databases" = module Database = Merkle_ledger.Database module type DB = - Merkle_ledger.Database_intf.S + Intf.Ledger.DATABASE with type key := Key.t and type token_id := Token_id.t and type token_id_set := Token_id.Set.t diff --git a/src/lib/merkle_ledger_tests/test_mask.ml b/src/lib/merkle_ledger_tests/test_mask.ml index eea869a5753..a7455679115 100644 --- a/src/lib/merkle_ledger_tests/test_mask.ml +++ b/src/lib/merkle_ledger_tests/test_mask.ml @@ -667,7 +667,7 @@ module Make_maskable_and_mask_with_depth (Depth : Depth_S) = struct (* underlying Merkle tree *) module Base_db : - Merkle_ledger.Database_intf.S + Merkle_ledger.Intf.Ledger.DATABASE with module Location = Location and module Addr = Location.Addr and type account := Account.t diff --git a/src/lib/merkle_ledger_tests/test_stubs.ml b/src/lib/merkle_ledger_tests/test_stubs.ml index 3c47f97f083..61d26b19d0a 100644 --- a/src/lib/merkle_ledger_tests/test_stubs.ml +++ b/src/lib/merkle_ledger_tests/test_stubs.ml @@ -128,6 +128,17 @@ struct let remove t ~key = Bigstring_frozen.Table.remove t.table key let make_checkpoint _ _ = () + + let foldi t ~init ~f = + let i = ref (-1) in + let f ~key ~data accum = incr i ; f !i accum ~key ~data in + Bigstring_frozen.Table.fold t.table ~init ~f + + (* Relying on {!val:to_alist} is probably enough for testing purposes. *) + let fold_until t ~init ~f ~finish = + let f accum (key, data) = f accum ~key ~data in + let alist = to_alist t in + List.fold_until alist ~init ~f ~finish end module Storage_locations : Intf.Storage_locations = struct diff --git a/src/lib/syncable_ledger/test.ml b/src/lib/syncable_ledger/test.ml index 4f32857daea..0d076706b12 100644 --- a/src/lib/syncable_ledger/test.ml +++ b/src/lib/syncable_ledger/test.ml @@ -4,7 +4,7 @@ open Pipe_lib open Network_peer module type Ledger_intf = sig - include Merkle_ledger.Syncable_intf.S + include Merkle_ledger.Intf.SYNCABLE type account_id