Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Merkle_ledger #15141

Merged
merged 31 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e327e18
Merkle_ledger: simplify functions
rbonichon Feb 14, 2024
efd8125
Merkle_ledger: interface for Util
rbonichon Feb 15, 2024
7c97163
Merkle_ledger: interface for Location
rbonichon Feb 15, 2024
69116d8
Merkle_ledger: remove unused equal function
rbonichon Feb 15, 2024
967626f
Merkle_ledger: remove unused Merkle_path_intf module
rbonichon Feb 15, 2024
5dd4c11
Merkle_ledger: actual interface for Merkle_path
rbonichon Feb 15, 2024
0529763
Merkle_ledger: rm unused check_path in Merkle_path
rbonichon Feb 15, 2024
dd18e9d
Merkle_ledger: interface for Null_ledger
rbonichon Feb 15, 2024
e2ef0ce
Merkle_ledger: interface for Database
rbonichon Feb 15, 2024
77a8178
Merkle_ledger: remove unused *_intf module
rbonichon Feb 15, 2024
e97f1c1
Merkle_ledger: interface for Any_ledger
rbonichon Feb 15, 2024
ead1a25
Merkle_ledger: interface for Graphviz
rbonichon Feb 15, 2024
c32f5a7
Merkle_ledger: make Database_intf a pure interface
rbonichon Feb 15, 2024
7fe1a32
Merkle_ledger: minimizing module type declaration for database
rbonichon Feb 15, 2024
34f5973
Merkle_ledger: activate warnings
rbonichon Feb 16, 2024
96baa2a
Merkle_ledger: sort libraries in dune file
rbonichon Feb 16, 2024
6fa38f1
Merkle_ledger: make intf files actual interfaces
rbonichon Feb 16, 2024
bdc78ef
Merkle_ledger: fixing fragile pattern-matching warning
rbonichon Feb 16, 2024
ef81113
Merkle_ledger: apply intf trick for Graphviz
rbonichon Feb 16, 2024
a62ac4a
Merkle_ledger: factorize inputs interface
rbonichon Feb 16, 2024
b7c4af0
Merkle_ledger: db-specific input module type
rbonichon Feb 16, 2024
7c96972
Merkle_ledger: inline module type Base_inputs_intf.S
rbonichon Feb 16, 2024
b15aeeb
Merkle_ledger: define module type Ledger.NULL
rbonichon Feb 16, 2024
5e81863
Merkle_ledger: moving ledger module types to own file
rbonichon Feb 16, 2024
c82ae70
Merkle_ledger: put input/ledger module types in Intf
rbonichon Feb 16, 2024
caa0950
Merkle_ledger: mv Syncable_intf to Intf
rbonichon Feb 16, 2024
b0008f5
Merkle_ledger: mv Graphviz_intf to Intf
rbonichon Feb 16, 2024
da8931a
merkle_ledger: auto open Core_Kernel in dune
rbonichon Feb 16, 2024
b801353
Merge branch 'develop' into rb/merkle_ledger_intf
Feb 26, 2024
b4035b1
Merkle_ledger: reintegrate check_path for tests
rbonichon Feb 26, 2024
8377269
Merkle_ledger_tests: use new interfaces in tests
rbonichon Feb 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 3 additions & 50 deletions src/lib/merkle_ledger/any_ledger.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,8 @@
* Props to @nholland for showing me this trick.
* *)

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 type Inputs_intf = sig
include Base_inputs_intf.S

module Location : Location_intf.S
end

module Make_base (Inputs : Inputs_intf) :
S
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
Expand All @@ -74,7 +27,7 @@ module Make_base (Inputs : Inputs_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
Expand Down
10 changes: 10 additions & 0 deletions src/lib/merkle_ledger/any_ledger.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
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
18 changes: 0 additions & 18 deletions src/lib/merkle_ledger/base_inputs_intf.ml

This file was deleted.

150 changes: 0 additions & 150 deletions src/lib/merkle_ledger/base_ledger_intf.ml

This file was deleted.

59 changes: 13 additions & 46 deletions src/lib/merkle_ledger/database.ml
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
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 : Intf.Inputs.DATABASE) = struct
(* The max depth of a merkle tree can never be greater than 253. *)
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
Expand Down Expand Up @@ -67,6 +44,8 @@ module Make (Inputs : Inputs_intf) :
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 =
Expand Down Expand Up @@ -290,25 +269,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 =
Expand Down Expand Up @@ -590,7 +557,7 @@ module Make (Inputs : Inputs_intf) :

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 ;
Expand All @@ -599,7 +566,7 @@ module Make (Inputs : Inputs_intf) :
| 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)
Expand Down
12 changes: 12 additions & 0 deletions src/lib/merkle_ledger/database.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
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
18 changes: 0 additions & 18 deletions src/lib/merkle_ledger/database_intf.ml

This file was deleted.

Loading