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

Batch merkle_path lookups in Sparse_ledger #14528

Merged

Conversation

mrmr1993
Copy link
Member

@mrmr1993 mrmr1993 commented Nov 7, 2023

Checklist:

  • Dependency versions are unchanged
    • Notify Velocity team if dependencies must change in CI
  • Modified the current draft of release notes with details on what is completed or incomplete within this project
  • Document code purpose, how to use it
    • Mention expected invariants, implicit constraints
  • Tests were added for the new behavior
    • Document test purpose, significance of failures
    • Test names should reflect their purpose
  • All tests pass (CI will check this if you didn't)
  • Serialized types are in stable-versioned modules
  • Does this close issues? List them

@mrmr1993
Copy link
Member Author

mrmr1993 commented Nov 7, 2023

!ci-build-me

@mrmr1993
Copy link
Member Author

mrmr1993 commented Nov 7, 2023

!ci-nightly-me

Copy link
Contributor

mergify bot commented Nov 7, 2023

⚠️ The sha of the head commit of this PR conflicts with #14527. Mergify cannot evaluate rules on this PR. ⚠️

1 similar comment
Copy link
Contributor

mergify bot commented Nov 8, 2023

⚠️ The sha of the head commit of this PR conflicts with #14527. Mergify cannot evaluate rules on this PR. ⚠️

@mrmr1993
Copy link
Member Author

mrmr1993 commented Nov 8, 2023

!ci-build-me

@mrmr1993
Copy link
Member Author

mrmr1993 commented Nov 8, 2023

!ci-nightly-me

@mrmr1993 mrmr1993 marked this pull request as ready for review November 21, 2023 18:20
@mrmr1993 mrmr1993 requested a review from a team as a code owner November 21, 2023 18:20
@georgeee georgeee force-pushed the feature/batch-account-lookups branch from eb3db50 to 2fbe1c5 Compare November 27, 2023 15:51
@georgeee georgeee force-pushed the feature/batch-merkle-path-lookups branch from 15a4df4 to b9c0b92 Compare November 27, 2023 16:35
@@ -700,6 +700,58 @@ module Make (Inputs : Inputs_intf) :
List.map2_exn dependency_dirs dependency_hashes ~f:(fun dir hash ->
Direction.map dir ~left:(`Left hash) ~right:(`Right hash) )

let merkle_path_batch mdb locations =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems over-complicated. If we just want to batch the get of hashes, I think we should keep the original structure of merkle_path function. I would suggest change the definition of the function like following:

  let merkle_path_batch mdb locations =
    let locations =
      List.map locations ~f:(fun location ->
          if Location.is_account location then
            Location.Hash (Location.to_path_exn location)
          else (
            assert (Location.is_hash location) ;
            location ) )
    in
    let list_of_dependencies = List.map locations ~f:(Location.merkle_path_dependencies_exn) in 
    let dependency_locs = List.concat list_of_dependencies |> List.map ~f:fst in
    let dependency_hashes = get_hash_batch mdb dependency_locs in 
    let rec go list_of_dependencies hashes acc =
      match list_of_dependencies with 
      | [] -> acc 
      | deps :: rest_of_deps ->
        let locs, dirs = List.unzip deps in
        let hashes, rest_of_hashes = List.(split_n hashes (length locs)) in 
        let path =
        List.map2_exn dirs hashes ~f:(fun dir hash ->
          Direction.map dir ~left:(`Left hash) ~right:(`Right hash) )
        in 
        go rest_of_deps rest_of_hashes (path :: acc)
      in 
      go list_of_dependencies dependency_hashes []
      |> List.rev

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghost-not-in-the-shell could you check the version I have in #14594

I also wanted to refactor it but did that at a higher layer of PR chain to avoid conflicts

Copy link
Member

@georgeee georgeee Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But your version is even easier to read

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind if I use your code at PR #14594 and keep this one as it is?

Copy link
Member

@georgeee georgeee Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact version in that PR becomes a bit more general because of wide paths. Anyway, I suggest having a discussion on final version there as here is only an intermediate step and overall this function as here seems to be correct.

@georgeee georgeee force-pushed the feature/batch-account-lookups branch from 2fbe1c5 to daa60aa Compare November 28, 2023 17:21
@georgeee georgeee force-pushed the feature/batch-merkle-path-lookups branch from b9c0b92 to 07af60b Compare November 28, 2023 17:23
@georgeee georgeee force-pushed the feature/batch-account-lookups branch from daa60aa to 4271394 Compare November 28, 2023 20:06
@georgeee georgeee force-pushed the feature/batch-merkle-path-lookups branch from 07af60b to f644690 Compare November 28, 2023 20:06
@georgeee georgeee force-pushed the feature/batch-merkle-path-lookups branch from f644690 to 5147f71 Compare November 28, 2023 20:12
@nholland94
Copy link
Member

!ci-build-me

loop locations loc_acc dir_acc (0 :: length :: length_acc)
else
let sibling = Location.sibling k in
let sibling_dir =
Copy link
Member

@nholland94 nholland94 Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: rename to something other than sibling_dir. This can be done a layer above this in the PR chain given we are doing the larger refactor there.

, length :: lengths
, acc_hd :: acc_tl ) ->
let dir =
Direction.map direction ~left:(`Left hash) ~right:(`Right hash)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be flipped? It appears from the function above that we are returning the directions we traverse, and then returning the locations to the sibling hashes. So I would think we want to flip the directions here in order to build the proper merkle path.

Copy link
Member

@georgeee georgeee Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merkle path convention is that in each element there is a direction to the node itself (from its parent) and hash of sibling.

Because direction = Location.last_direction (Location.to_path_exn k), Direction.map direction ~left:(Left hash) ~right:(Right hash) returns direction to the node itself with hash of a sibling, following the convention.

@deepthiskumar deepthiskumar merged commit 1ba7954 into feature/batch-account-lookups Dec 5, 2023
2 checks passed
@deepthiskumar deepthiskumar deleted the feature/batch-merkle-path-lookups branch December 5, 2023 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants