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

[3/3] - Replace Legacy History Commitment Implementation #686

Merged
merged 102 commits into from
Oct 16, 2024

Conversation

rauljordan
Copy link
Collaborator

@rauljordan rauljordan commented Sep 16, 2024

Yay! For automated security checks. It found a potentially dangerous
memory aliasing in a for loop.

Also, I'm suppressing the complaints about errors which the
crypto.KeccakState implementations of Read and Write cannot throw.
The legacy in inclusion-proofs packages are only used from tests at
this point. And, those tests are largely only used for benchmarking
the optimized implementation against the legacy one.

There is one method from the prefix-proofs package which is used in
the layer2-state-provider for checking that the prefix proofs
generated from the optimized implementation are verifiable.
This change is mostly just suffling around code blocks, renaming
variables, and adding comments. But, it also adds a cursor and keeps
track of which node in the tree is actively being calculated.

This allows the historyCommitter to search for the values of specific
nodes by position in the tree while calculating the root.

The only reason for committing the code at this state is to benchmark
before and after adding the inclusion_proof calculation.
This allows us to remove the whole implementation from
inclusion_proof.go and the associated tests. Also, fun fact, that
existing implementation was not only much slower, but it was
incorrect.

I'll add some differential fuzzing to show that these inclusion proofs
match the results from the legacy inclusionproofs package.
The test was taking 26 seconds and always passing. Now, the whole
package's tests take < 1 second. :(
The previous code restructuring wasn't really "pure" and makes the PR
look much more complicated than it is.
Previously, fillers were being created for the entire depth of the
tree, but, they are only read up to the depth of a tree of size
virtual.
This version of the code should be easier to maintain and only
performs minutely slower because of having to copy the cursor in each
recursive stack frame.

This commit also rearranges the conditional logic on the left and
right sides of the tree to make it simpler to follow the recursion.
The history package doesn't need to let clients work directly with a
historyCommitter instance. It is really an implementation detail of
the history package.
@eljobe eljobe marked this pull request as ready for review October 10, 2024 06:19
Now that the virtual Merkle tree is fully-supported, it is okay if the
l2 state provider sends only the leaves it actually has values
for. The padding is not necessary.
It's not necessary. The traversal is simple to unwind using the shared
cursor at all levels of recursion. This gives back the ~10%
performance boost previously taken away with commit bafadd1.
Copy link

@gligneul gligneul left a comment

Choose a reason for hiding this comment

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

Looks good, just minor comments

layer2-state-provider/history_commitment_provider.go Outdated Show resolved Hide resolved
layer2-state-provider/history_commitment_provider.go Outdated Show resolved Hide resolved
state-commitments/history/history_commitment.go Outdated Show resolved Hide resolved
state-commitments/history/history_commitment.go Outdated Show resolved Hide resolved
state-commitments/history/history_commitment.go Outdated Show resolved Hide resolved
Specifically:
  1. Added a comment about History.Height
  2. Fixed some comment typos
  3. Extracted a virtualFrom helper-method
  4. Adopted slices.Clone
  5. Cleaned up a problem with the merge from main
@eljobe eljobe requested a review from gligneul October 14, 2024 21:59
The tiny bit of expressiveness it provides is not really worth the
confusion it could cause if someone uses the type to cast a 0 value
than it's worth.
@eljobe eljobe merged commit a85df38 into main Oct 16, 2024
5 checks passed
@eljobe eljobe deleted the replace-across-codebase-history-commit branch October 16, 2024 12:48
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.

3 participants