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

Implement encoding proof node values #2819

Merged
merged 9 commits into from
Oct 4, 2022

Conversation

tomaka
Copy link
Contributor

@tomaka tomaka commented Oct 4, 2022

Necessary in order to finish #1166

Right now we can decode items from Merkle proofs. This PR implements encoding back these items.

While implementing encoding, I've discovered several cases where the decoding was too lenient and was accepting as valid things that shouldn't really be valid. I've fixed this as part of this PR. See the changes in src/util.rs and at the bottom of src/trie/proof_node_codec.rs.

When I say "shouldn't really be valid" I mean that there were multiple different sequences of bytes that could lead to the same decoded struct. Since we're hashing things a lot here and there, it is important that there is a bijection of decoded <-> encoded. The Substrate implementation does the same. cc paritytech/substrate#12417

Copy link
Contributor

@mergify mergify bot left a comment

Choose a reason for hiding this comment

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

Automatically approving tomaka's pull requests. This auto-approval will be removed once more maintainers are active.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2022

twiggy diff report

Difference in .wasm size before and after this pull request.


 Delta Bytes │ Item
─────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       +1453 ┊ smoldot::trie::proof_node_codec::decode::h8c2a3387e8fd00db
       -1398 ┊ smoldot::trie::proof_node_decode::decode::h3c802fc74e097b79
        +309 ┊ <&T as core::fmt::Display>::fmt::hc61d54e835a5e55a
        +306 ┊ <smoldot::trie::proof_node_codec::Error as core::fmt::Debug>::fmt::h771dc1f7d38ceb18
        -267 ┊ <&T as core::fmt::Display>::fmt::hede33647e967f354
        -264 ┊ <smoldot::trie::proof_node_decode::Error as core::fmt::Debug>::fmt::h52238c6df391e999
        +257 ┊ <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter::h24d08d1fb54a1500
        -257 ┊ <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter::h505ccd594b2d1511
        -176 ┊ alloc::vec::Vec<T,A>::extend_desugared::h2508a4c0bc10f95c
        +176 ┊ alloc::vec::Vec<T,A>::extend_desugared::hdafe000385f163e9
        -119 ┊ <smoldot::trie::nibble::nibbles_to_bytes_extend::Iter<I> as core::iter::traits::iterator::Iterator>::next::hcceeebdd458373a3
        +119 ┊ <smoldot::trie::nibble::nibbles_to_bytes_suffix_extend::Iter<I> as core::iter::traits::iterator::Iterator>::next::hfb9289ced9322d6a
        +111 ┊ <smoldot::trie::proof_node_codec::PartialKey as core::iter::traits::iterator::Iterator>::next::hf090f35ddcd91518
        -111 ┊ <smoldot::trie::proof_node_decode::PartialKey as core::iter::traits::iterator::Iterator>::next::h7efb59290db7c70f
        +107 ┊ smoldot::trie::proof_node_codec::Decoded::children_bitmap::h47da02d2a0a90f55
        -107 ┊ smoldot::trie::proof_node_decode::Decoded::children_bitmap::hebf4eaa59ed5d772
         +98 ┊ smoldot::util::nom_scale_compact_usize::h78e936afad2da3eb
         +98 ┊ smoldot::util::nom_scale_compact_usize::hf6e357ea1a4d52cf
         +83 ┊ <smoldot::trie::proof_node_codec::PartialKey as core::clone::Clone>::clone::he6bae938fa7cc5af
         -83 ┊ <smoldot::trie::proof_node_decode::PartialKey as core::clone::Clone>::clone::h2e2a851a709e4791
         +97 ┊ ... and 14 more.
        +453 ┊ Σ [34 Total Rows]

Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

👍

@tomaka tomaka added the automerge Automatically merge pull request as soon as possible label Oct 4, 2022
@mergify mergify bot merged commit 1777378 into paritytech:main Oct 4, 2022
@tomaka tomaka deleted the encode-proof-node branch October 4, 2022 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Automatically merge pull request as soon as possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants