Skip to content

Commit

Permalink
Perform account hashing in finalize_hashes call
Browse files Browse the repository at this point in the history
Defer computation of account hashes to the moment the hashes of the mask
will actually be accessed.

This is useful if the same account gets overwritten a few times in the
same block.
  • Loading branch information
georgeee committed Aug 26, 2024
1 parent d65141c commit 03e7683
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/lib/merkle_mask/masking_merkle_tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ module Make (Inputs : Inputs_intf.S) = struct
Manual definitions of sexp are needed to avoid warning 4 in autogenerated code
*)
type unhashed_account_t = Hash.t * Location.t
type unhashed_account_t = Account.t option * Location.t

let sexp_of_unhashed_account_t =
Tuple2.sexp_of_t Hash.sexp_of_t Location.sexp_of_t
Tuple2.sexp_of_t (Option.sexp_of_t Account.sexp_of_t) Location.sexp_of_t

let unhashed_account_t_of_sexp =
Tuple2.t_of_sexp Hash.t_of_sexp Location.t_of_sexp
Tuple2.t_of_sexp (Option.t_of_sexp Account.t_of_sexp) Location.t_of_sexp

type t =
{ uuid : Uuid.Stable.V1.t
Expand Down Expand Up @@ -291,7 +291,10 @@ module Make (Inputs : Inputs_intf.S) = struct
Fn.compose snd @@ List.fold_map ~init ~f

let compute_merge_hashes :
(Hash.t * Addr.t * [ `Left of Hash.t | `Right of Hash.t ] list) list
( Account.t option
* Addr.t
* [ `Left of Hash.t | `Right of Hash.t ] list )
list
-> (Addr.t * Hash.t) list =
let process_pair height = function
| (lh, laddr, `Left _ :: lpath), (rh, _, `Right _ :: _rpath) ->
Expand Down Expand Up @@ -340,7 +343,10 @@ module Make (Inputs : Inputs_intf.S) = struct
| _ ->
impl acc' (height + 1) (converge height task)
in
impl [] 0
let hash_account =
Option.value_map ~default:Hash.empty_account ~f:Hash.hash_account
in
Fn.compose (impl [] 0) (List.map ~f:(Tuple3.map_fst ~f:hash_account))

let finalize_hashes_do t unhashed_accounts =
let merkle_path_batch =
Expand All @@ -355,7 +361,7 @@ module Make (Inputs : Inputs_intf.S) = struct
in
(* let _task = *)
List.map2_exn
~f:(fun (h, loc) p -> (h, Location.to_path_exn loc, p))
~f:(fun (a, loc) p -> (a, Location.to_path_exn loc, p))
unhashed_accounts merkle_path_batch
|> List.stable_sort ~compare:(fun (_, a, _) (_, b, _) ->
Addr.compare a b )
Expand Down Expand Up @@ -608,9 +614,7 @@ module Make (Inputs : Inputs_intf.S) = struct
t.current_location <- Some prev_loc
| None ->
t.current_location <- None ) ;
(* update hashes *)
let account_hash = Hash.empty_account in
t.unhashed_accounts <- (account_hash, location) :: t.unhashed_accounts
t.unhashed_accounts <- (None, location) :: t.unhashed_accounts

let set_account_unsafe t location account =
assert_is_attached t ;
Expand All @@ -626,9 +630,7 @@ module Make (Inputs : Inputs_intf.S) = struct
let set t location account =
assert_is_attached t ;
set_account_unsafe t location account ;
(* Update merkle path. *)
let account_hash = Hash.hash_account account in
t.unhashed_accounts <- (account_hash, location) :: t.unhashed_accounts
t.unhashed_accounts <- (Some account, location) :: t.unhashed_accounts

(* if the mask's parent sets an account, we can prune an entry in the mask
if the account in the parent is the same in the mask
Expand Down

0 comments on commit 03e7683

Please sign in to comment.