Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
54 changes: 40 additions & 14 deletions primitives/trie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ pub fn delta_trie_root<L: TrieConfiguration, I, A, B, DB, V>(
{
let mut trie = TrieDBMut::<L>::from_existing(&mut *db, &mut root)?;

let mut delta = delta.into_iter().collect::<Vec<_>>();
delta.sort_by(|l, r| l.0.borrow().cmp(r.0.borrow()));

for (key, change) in delta {
match change.borrow() {
Some(val) => trie.insert(key.borrow(), val.borrow())?,
Expand Down Expand Up @@ -259,19 +262,12 @@ pub fn child_delta_trie_root<L: TrieConfiguration, I, A, B, DB, RD, V>(
// root is fetched from DB, not writable by runtime, so it's always valid.
root.as_mut().copy_from_slice(root_data.as_ref());

{
let mut db = KeySpacedDBMut::new(&mut *db, keyspace);
let mut trie = TrieDBMut::<L>::from_existing(&mut db, &mut root)?;

for (key, change) in delta {
match change.borrow() {
Some(val) => trie.insert(key.borrow(), val.borrow())?,
None => trie.remove(key.borrow())?,
};
}
}

Ok(root)
let mut db = KeySpacedDBMut::new(&mut *db, keyspace);
delta_trie_root::<L, _, _, _, _, _>(
&mut db,
root,
delta,
)
}

/// Call `f` for all keys in a child trie.
Expand Down Expand Up @@ -468,7 +464,7 @@ mod trie_constants {
#[cfg(test)]
mod tests {
use super::*;
use codec::{Encode, Compact};
use codec::{Encode, Decode, Compact};
use sp_core::Blake2Hasher;
use hash_db::{HashDB, Hasher};
use trie_db::{DBValue, TrieMut, Trie, NodeCodec as NodeCodecT};
Expand Down Expand Up @@ -856,4 +852,34 @@ mod tests {
).is_err()
);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not totally sure if we really need this test, but having test is better.

Copy link
Member Author

Choose a reason for hiding this comment

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

We need this test, clearly! I don't want to spend another two weeks chasing the same bug again :P

#[test]
fn generate_storage_root_with_proof_works_independently_from_the_delta_order() {
let proof = StorageProof::decode(&mut &include_bytes!("../test-res/proof")[..]).unwrap();
let storage_root = sp_core::H256::decode(
&mut &include_bytes!("../test-res/storage_root")[..],
).unwrap();
// Delta order that is "invalid" so that it would require a different proof.
let invalid_delta = Vec::<(Vec<u8>, Option<Vec<u8>>)>::decode(
&mut &include_bytes!("../test-res/invalid-delta-order")[..],
).unwrap();
// Delta order that is "valid"
let valid_delta = Vec::<(Vec<u8>, Option<Vec<u8>>)>::decode(
&mut &include_bytes!("../test-res/valid-delta-order")[..],
).unwrap();

let proof_db = proof.into_memory_db::<Blake2Hasher>();
let first_storage_root = delta_trie_root::<Layout, _, _, _, _, _>(
&mut proof_db.clone(),
storage_root,
valid_delta,
).unwrap();
let second_storage_root = delta_trie_root::<Layout, _, _, _, _, _>(
&mut proof_db.clone(),
storage_root,
invalid_delta,
).unwrap();

assert_eq!(first_storage_root, second_storage_root);
}
}
Binary file added primitives/trie/test-res/invalid-delta-order
Binary file not shown.
Binary file added primitives/trie/test-res/proof
Binary file not shown.
1 change: 1 addition & 0 deletions primitives/trie/test-res/storage_root
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�=�[�42%�JP ��h�Kw�)R� 0�ԭu��
Binary file added primitives/trie/test-res/valid-delta-order
Binary file not shown.