diff --git a/lib/src/executor/trie_root_calculator.rs b/lib/src/executor/trie_root_calculator.rs index 4ea6f5ed98..dd57369624 100644 --- a/lib/src/executor/trie_root_calculator.rs +++ b/lib/src/executor/trie_root_calculator.rs @@ -327,7 +327,11 @@ 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 }; @@ -335,19 +339,15 @@ impl StorageValue { 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(), diff --git a/lib/src/executor/trie_root_calculator/tests.rs b/lib/src/executor/trie_root_calculator/tests.rs index a4b4d914ba..5c61cddc3b 100644 --- a/lib/src/executor/trie_root_calculator/tests.rs +++ b/lib/src/executor/trie_root_calculator/tests.rs @@ -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( diff --git a/wasm-node/CHANGELOG.md b/wasm-node/CHANGELOG.md index 88534d8d27..e1a8c687f2 100644 --- a/wasm-node/CHANGELOG.md +++ b/wasm-node/CHANGELOG.md @@ -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