Skip to content

Commit

Permalink
fixing triedbmut lookup, added some testing in test. (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheme authored Aug 13, 2023
1 parent 2af70c2 commit c4be095
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
3 changes: 0 additions & 3 deletions trie-db/src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ where
NodeOwned::Leaf(slice, value) =>
return if partial == *slice {
let value = (*value).clone();
drop(node);
load_value_owned(
value,
nibble_key.original_data_as_prefix(),
Expand Down Expand Up @@ -395,7 +394,6 @@ where
NodeOwned::Branch(children, value) =>
if partial.is_empty() {
return if let Some(value) = value.clone() {
drop(node);
load_value_owned(
value,
nibble_key.original_data_as_prefix(),
Expand Down Expand Up @@ -433,7 +431,6 @@ where

if partial.len() == slice.len() {
return if let Some(value) = value.clone() {
drop(node);
load_value_owned(
value,
nibble_key.original_data_as_prefix(),
Expand Down
8 changes: 4 additions & 4 deletions trie-db/src/triedbmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ where
handle: &NodeHandle<TrieHash<L>>,
) -> Result<Option<DBValue>, TrieHash<L>, CError<L>> {
let mut handle = handle;
// prefix only use for value node access, so this is always correct.
let prefix = (full_key, None);
loop {
let (mid, child) = match handle {
Expand All @@ -871,8 +872,8 @@ where
},
NodeHandle::InMemory(handle) => match &self.storage[handle] {
Node::Empty => return Ok(None),
Node::Leaf(key, value) =>
if NibbleSlice::from_stored(key) == partial {
Node::Leaf(slice, value) =>
if NibbleSlice::from_stored(slice) == partial {
return Ok(value.in_memory_fetched_value(
prefix,
self.db,
Expand Down Expand Up @@ -923,7 +924,7 @@ where
None
})
} else if partial.starts_with(&slice) {
let idx = partial.at(0);
let idx = partial.at(slice.len());
match children[idx as usize].as_ref() {
Some(child) => (1 + slice.len(), child),
None => return Ok(None),
Expand Down Expand Up @@ -1911,7 +1912,6 @@ where
cache_child_values::<L>(&node, &mut values_to_cache, full_key.clone());
}

drop(node);
values_to_cache.into_iter().for_each(|(k, v)| cache.cache_value_for_key(&k, v));
}
}
Expand Down
54 changes: 54 additions & 0 deletions trie-db/test/src/triedbmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ fn playpen_internal<T: TrieLayout>() {
let mut root = Default::default();
let mut memtrie = populate_trie::<T>(&mut memdb, &mut root, &x);

// avoid duplicate
let value_set: std::collections::BTreeMap<&[u8], &[u8]> =
x.iter().map(|(k, v)| (k.as_slice(), v.as_slice())).collect();
for (k, v) in value_set {
assert_eq!(memtrie.get(k).unwrap().unwrap(), v);
}

memtrie.commit();
if *memtrie.root() != real {
println!("TRIE MISMATCH");
Expand Down Expand Up @@ -836,3 +843,50 @@ fn test_insert_remove_data_with_cache_internal<T: TrieLayout>() {
assert!(cache.lookup_value_for_key(key).is_none());
}
}

#[test]
fn test_two_assets_memory_db() {
test_two_assets_memory_db_inner_1::<HashedValueNoExtThreshold<1>>();
test_two_assets_memory_db_inner_2::<HashedValueNoExtThreshold<1>>();
}
fn test_two_assets_memory_db_inner_1<T: TrieLayout>() {
let mut memdb = PrefixedMemoryDB::<T>::new(&[0u8]);
let mut root = Default::default();
let mut state = TrieDBMutBuilder::<T>::new(&mut memdb, &mut root).build();

let key1 = [1u8; 3];
let data1 = [1u8; 2];
state.insert(key1.as_ref(), &data1).unwrap();
assert_eq!(state.get(key1.as_ref()).unwrap().unwrap(), data1); //PASSING
let key2 = [2u8; 3];
let data2 = [2u8; 2];
state.insert(key2.as_ref(), &data2).unwrap();
assert_eq!(state.get(key1.as_ref()).unwrap().unwrap(), data1);

state.commit();
}

fn test_two_assets_memory_db_inner_2<T: TrieLayout>() {
let mut memdb = PrefixedMemoryDB::<T>::new(&[0u8]);
let mut root = Default::default();
let mut state = TrieDBMutBuilder::<T>::new(&mut memdb, &mut root).build();

let key1 = [1u8];
let data1 = [1u8; 2];
state.insert(key1.as_ref(), &data1).unwrap();
assert_eq!(state.get(key1.as_ref()).unwrap().unwrap(), data1);
let key2 = [1u8, 2];
let data2 = [2u8; 2];
state.insert(key2.as_ref(), &data2).unwrap();
assert_eq!(state.get(key1.as_ref()).unwrap().unwrap(), data1);
assert_eq!(state.get(key2.as_ref()).unwrap().unwrap(), data2);

state.commit();

let key3 = [1u8, 3];
let data3 = [3u8; 2];
state.insert(key3.as_ref(), &data3).unwrap();
assert_eq!(state.get(key1.as_ref()).unwrap().unwrap(), data1);
assert_eq!(state.get(key2.as_ref()).unwrap().unwrap(), data2);
assert_eq!(state.get(key3.as_ref()).unwrap().unwrap(), data3);
}

0 comments on commit c4be095

Please sign in to comment.