-
Notifications
You must be signed in to change notification settings - Fork 740
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
Conversation
This reverts commit 1cfb29f.
Co-authored-by: Ankan <[email protected]>
…abrizi/polkadot-sdk into shawntabrizi-proving-trie
There was a problem hiding this 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.
Key: Encode, | ||
Value: Encode + Decode, | ||
{ | ||
/// Create a new instance of a `ProvingTrie` using an iterator of key/value pairs. |
There was a problem hiding this comment.
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.
// 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> { |
There was a problem hiding this comment.
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).
…tech/polkadot-sdk into shawntabrizi-binary-trie2
…tech/polkadot-sdk into shawntabrizi-binary-trie2
…tech/polkadot-sdk into shawntabrizi-binary-trie2
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: Ankan <[email protected]> Co-authored-by: Adrian Catangiu <[email protected]>
This is a refactor and improvement from: #3881
sp_runtime::proving_trie
now exposes aBasicProvingTrie
for bothbase2
andbase16
.base16
are more focused on single value proofs, also aligning their APIs with thebase2
trieProvingTrie
trait is included which wraps both thebase2
andbase16
trie, and exposes all APIs needed for an end to end scenario.ProofToHashes
trait is exposed which can allow us to write proper benchmarks for the merkle trie.