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

Improve APIs for Tries in Runtime #5756

Merged
merged 104 commits into from
Sep 30, 2024
Merged

Conversation

shawntabrizi
Copy link
Member

@shawntabrizi shawntabrizi commented Sep 18, 2024

This is a refactor and improvement from: #3881

  • sp_runtime::proving_trie now exposes a BasicProvingTrie for both base2 and base16.
  • APIs for base16 are more focused on single value proofs, also aligning their APIs with the base2 trie
  • A ProvingTrie trait is included which wraps both the base2 and base16 trie, and exposes all APIs needed for an end to end scenario.
  • A ProofToHashes trait is exposed which can allow us to write proper benchmarks for the merkle trie.

Co-authored-by: Adrian Catangiu <[email protected]>
Copy link
Contributor

@cheme cheme left a comment

Choose a reason for hiding this comment

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

Had a quick look of base 16 (I don't know the base 2 lib). proof_to_hashes is not really good, but approach could still work (we encode node but a large part of the size is indeed hashes, but sometime it contains value hashes too or inline value (less than 32 byte for V1 so generally it 16-17 hashes per branch with an overhead of generally less than 10 byte from encoding). Also if the value access is big ( included in the proof), the result will be totally wrong. I mention alternative in the comments.

substrate/primitives/runtime/src/proving_trie/base16.rs Outdated Show resolved Hide resolved
Key: Encode,
Value: Encode + Decode,
{
/// Create a new instance of a `ProvingTrie` using an iterator of key/value pairs.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the method where the trie version really matter.

substrate/primitives/runtime/src/proving_trie/base16.rs Outdated Show resolved Hide resolved
// This base 16 trie does not directly expose the depth of the trie, so we can roughly calculate
// it assuming the data in the proof are hashes, and the number of hashes present will tell us
// the depth of the trie.
fn proof_to_hashes(proof: &[u8]) -> Result<u32, DispatchError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

So the method takes a proof and return the depth of this proof?
The proof contains touched encoded touched existing node (that themselves contain hashes), so counting the number of those node for a proof return the exact depth (since proof are Vec<Vec>, with inner being encoded node, you probably just have to decode the compact len at the first position), actually should check each node since some are just value (when size > 32), but it should be just a 1 depth error. This is only if there is a single value access in the proof.
For compact proof it is mostly the same.
If we want to be correct (don't assume the proof contains a single value), then we need to parse the sparse contain trie: for proof the method would need to calculate all hash and root hash in param: very costy. For compact proof the root is always the first and we can go down the trie without recalculating hashes (we know the structure of node in proof from node that are not include : so it is quite doable).

substrate/primitives/runtime/src/proving_trie/base16.rs Outdated Show resolved Hide resolved
substrate/primitives/runtime/src/proving_trie/base16.rs Outdated Show resolved Hide resolved
@shawntabrizi shawntabrizi added this pull request to the merge queue Sep 29, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 29, 2024
@shawntabrizi shawntabrizi added this pull request to the merge queue Sep 29, 2024
Merged via the queue into master with commit 05b5fb2 Sep 30, 2024
217 checks passed
@shawntabrizi shawntabrizi deleted the shawntabrizi-binary-trie2 branch September 30, 2024 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T1-FRAME This PR/Issue is related to core FRAME, the framework.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants