Skip to content

Commit

Permalink
Merge pull request #15141 from MinaProtocol/rb/merkle_ledger_intf
Browse files Browse the repository at this point in the history
Refactoring `Merkle_ledger`
  • Loading branch information
Richard Bonichon authored Feb 27, 2024
2 parents 6cae7cd + 8377269 commit e035129
Show file tree
Hide file tree
Showing 33 changed files with 626 additions and 467 deletions.
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

0 comments on commit e035129

Please sign in to comment.