Skip to content

Commit

Permalink
Implement encoding proof node values (#2819)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tomaka authored Oct 4, 2022
1 parent 2d4a2d8 commit 1777378
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 78 deletions.
118 changes: 60 additions & 58 deletions bin/fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bin/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ test = false
doc = false

[[bin]]
name = "proof-node-decode"
path = "fuzz_targets/proof-node-decode.rs"
name = "proof-node-codec"
path = "fuzz_targets/proof-node-codec.rs"
test = false
doc = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,18 @@
#![no_main]

libfuzzer_sys::fuzz_target!(|data: &[u8]| {
let _ = smoldot::trie::proof_node_decode::decode(data);
if let Ok(decoded) = smoldot::trie::proof_node_codec::decode(data) {
assert_eq!(
smoldot::trie::proof_node_codec::encode(decoded.clone()).fold(
Vec::new(),
|mut a, b| {
a.extend_from_slice(b.as_ref());
a
}
),
data,
"{:?}",
decoded
);
}
});
4 changes: 3 additions & 1 deletion bin/light-base/src/runtime_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,9 @@ impl<'a> RuntimeCallLock<'a> {
| proof_verify::StorageValue::HashKnownValueMissing(_)
) {
assert_eq!(key.len() % 2, 0);
output.push(trie::nibbles_to_bytes_extend(key.iter().copied()).collect::<Vec<_>>());
output.push(
trie::nibbles_to_bytes_suffix_extend(key.iter().copied()).collect::<Vec<_>>(),
);
}

match node_info.children {
Expand Down
Loading

0 comments on commit 1777378

Please sign in to comment.