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

Snap/merge range and proof #1913

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open

Snap/merge range and proof #1913

wants to merge 39 commits into from

Conversation

asdacap
Copy link
Contributor

@asdacap asdacap commented Jun 20, 2024

Targeting #1901 not main.
Includes #1907

  • Maybe this will make it clearer on how the proof is used.
  • Add snap_support.go which contains more snap specific iteration.
  • Add IterateAndGenerateProof which iterate and generate the proof at the same time.
  • Add VerifyRange which is used to verify the output of IterateAndGenerateProof. Does not integrate the proof verification though.
    • VerifyRange also have the logic to reconstruct the keys of the proof and check if more leaf is available based on the proof.
  • Add some unit tests for verification, skipping some of it for more complete proof verification.

Copy link

codecov bot commented Jun 20, 2024

Codecov Report

Attention: Patch coverage is 74.18112% with 134 lines in your changes missing coverage. Please review.

Project coverage is 75.31%. Comparing base (4e6dcd7) to head (909914d).
Report is 24 commits behind head on main.

Files Patch % Lines
core/trie/proof.go 67.35% 31 Missing and 32 partials ⚠️
core/trie/trie.go 77.30% 19 Missing and 18 partials ⚠️
core/trie/snap_support.go 75.55% 11 Missing and 11 partials ⚠️
core/trie/node.go 69.23% 7 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1913      +/-   ##
==========================================
- Coverage   75.43%   75.31%   -0.13%     
==========================================
  Files          97       98       +1     
  Lines        8342     8802     +460     
==========================================
+ Hits         6293     6629     +336     
- Misses       1519     1582      +63     
- Partials      530      591      +61     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@asdacap asdacap requested review from rianhughes and pnowosie and removed request for rianhughes June 20, 2024 08:26
core/trie/key.go Outdated Show resolved Hide resolved
core/trie/key.go Outdated Show resolved Hide resolved
core/trie/snap_support.go Outdated Show resolved Hide resolved
asdacap and others added 4 commits June 25, 2024 20:55
Co-authored-by: Pawel Nowosielski <pnowosie@users.noreply.github.com>
Co-authored-by: Pawel Nowosielski <pnowosie@users.noreply.github.com>
return false, false, err
}

// TODO: Verify here proof here
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to 1) verify the left and right proofs here (assuming both exist), and, 2) calculate the root hash of the new trie built from the keys and boundary proofs right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly, I don't know what you have in mind for this. IMO, last time, I just add the leaves first, then add the proof one by one (key->hash, not key->proffnode), unless the exact key already have a node.

if !finished || startValue != nil {
feltBts := startValue.Bytes()
startKey := NewKey(t.height, feltBts[:])
// Yes, the left proof is actually for the start query, not the actual leaf. Very confusing, yea I know. Need to
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean that the left proof is for the start query, and not the actual leaf? I thought the left proof would be for the first key/leaf in the range?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually no. Its a very edge.. edge case. Can't remember at the top of my head why.

scenarios := []struct {
name string
startQuery *felt.Felt
skip bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we skip tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it will fail if its not skipped.

"github.com/NethermindEth/juno/core/felt"
)

func (t *Trie) IterateAndGenerateProof(startValue *felt.Felt, consumer func(key, value *felt.Felt) (bool, error),
Copy link
Contributor

@rianhughes rianhughes Jun 27, 2024

Choose a reason for hiding this comment

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

I'm curious why this approach was taken. Would it not have been simpler to check if there are any leaves to the left (/right) of the startQuery (/endQuery), and return a left (/right) proof if there were? I find the iteration and consumer logic a bit confusing tbh

Copy link
Contributor

Choose a reason for hiding this comment

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

The purpose of this function is to just get the proofs required for the given range and trie right?

Copy link
Contributor

@rianhughes rianhughes Jun 27, 2024

Choose a reason for hiding this comment

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

If we change the function signature to
func (t *Trie) GenerateProof(startRangeKey, endRangeKey *felt.Felt) ([]ProofNode, bool, error)
then we could remove the iteration and consumer logic, and simplify this a lot (by just checking if there are any nodes to the left of the startRangeKey, and the right of endRangeKey). What are your thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The purpose of the function is BOTH iterate and generate proof. Think higher level. The reason its a consumer function is that the condition to stop getting leaves is complicated. It need to stop on:

  • Context timeout
  • Buffer full
  • Max node reached
  • At least one node found
  • Limit node reached
    Also, these are relatively large array. Where possible you don't wanna have one []*felt.Felt buffer. EG: in case of class, you actually just want the []core.Class, not the KV, but you do need the proof and you do need the class in the exact order.

Base automatically changed from rianhughes/proof-range-test to main June 28, 2024 13:35
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.

None yet

3 participants