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

Fix trie_root_calculator with state_version = 1 #711

Merged
merged 3 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/src/executor/trie_root_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,27 +327,27 @@ impl StorageValue {
// hash ahead of time if relevant.
let storage_value_hash =
if let Some((value, TrieEntryVersion::V1)) = maybe_storage_value {
Some(blake2_rfc::blake2b::blake2b(32, &[], value))
if value.len() >= 33 {
Some(blake2_rfc::blake2b::blake2b(32, &[], value))
} else {
None
}
} else {
None
};
let merkle_value = trie::trie_node::calculate_merkle_value(
trie::trie_node::Decoded {
children: array::from_fn(|n| calculated_elem.children[n].as_ref()),
partial_key: calculated_elem.partial_key.iter().copied(),
storage_value: match maybe_storage_value {
Some((value, TrieEntryVersion::V0)) => {
trie::trie_node::StorageValue::Unhashed(value)
}
Some((_, TrieEntryVersion::V1)) => {
trie::trie_node::StorageValue::Hashed(
<&[u8; 32]>::try_from(
storage_value_hash.as_ref().unwrap().as_bytes(),
)
storage_value: match (maybe_storage_value, storage_value_hash.as_ref()) {
(_, Some(storage_value_hash)) => trie::trie_node::StorageValue::Hashed(
<&[u8; 32]>::try_from(storage_value_hash.as_bytes())
.unwrap_or_else(|_| panic!()),
)
),
(Some((value, _)), None) => {
trie::trie_node::StorageValue::Unhashed(value)
}
None => trie::trie_node::StorageValue::None,
(None, _) => trie::trie_node::StorageValue::None,
},
},
parent_node.is_none(),
Expand Down
22 changes: 13 additions & 9 deletions lib/src/executor/trie_root_calculator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,23 @@ fn fuzzing() {
// checking difficulties.
let storage_value_hashed = match node_access.user_data().0.as_ref() {
Some((v, TrieEntryVersion::V1)) => {
Some(blake2_rfc::blake2b::blake2b(32, &[], v))
if v.len() >= 33 {
Some(blake2_rfc::blake2b::blake2b(32, &[], v))
} else {
None
}
}
_ => None,
};
let storage_value = match node_access.user_data().0.as_ref() {
Some((v, TrieEntryVersion::V0)) => {
trie::trie_node::StorageValue::Unhashed(&v[..])
}
Some((_, TrieEntryVersion::V1)) => trie::trie_node::StorageValue::Hashed(
<&[u8; 32]>::try_from(storage_value_hashed.as_ref().unwrap().as_bytes())
.unwrap(),
let storage_value = match (
node_access.user_data().0.as_ref(),
storage_value_hashed.as_ref(),
) {
(_, Some(storage_value_hashed)) => trie::trie_node::StorageValue::Hashed(
<&[u8; 32]>::try_from(storage_value_hashed.as_bytes()).unwrap(),
),
None => trie::trie_node::StorageValue::None,
(Some((v, _)), None) => trie::trie_node::StorageValue::Unhashed(&v[..]),
(None, _) => trie::trie_node::StorageValue::None,
};

let merkle_value = trie::trie_node::calculate_merkle_value(
Expand Down
4 changes: 4 additions & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Multiaddresses that can't be parsed during the discovery process are now silently ignored instead of causing the entire list of discovered nodes to be discarded. ([#705](https://github.com/smol-dot/smoldot/pull/705))

### Fixed

- Fix wrong trie root hash calculation with `state_version = 1`. ([#711](https://github.com/smol-dot/smoldot/pull/711))

## 1.0.9 - 2023-06-08

### Added
Expand Down