Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
add recording test
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier committed Aug 24, 2016
1 parent 94fc745 commit f0e94c6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
4 changes: 2 additions & 2 deletions ethcore/src/snapshot/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ impl StateProducer {
// modify existing accounts.
let mut accounts_to_modify: Vec<_> = {
let trie = TrieDB::new(&*db, &self.state_root).unwrap();
let regionck_bug = trie.iter()
let temp = trie.iter() // binding required due to complicated lifetime stuff
.filter(|_| rng.gen::<f32>() < ACCOUNT_CHURN)
.map(|(k, v)| (H256::from_slice(&k), v.to_owned()))
.collect();

regionck_bug
temp
};

// sweep once to alter storage tries.
Expand Down
82 changes: 77 additions & 5 deletions util/src/trie/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub trait Recorder {
/// The depth parameter is the depth of the visited node, with the root node having depth 0.
fn record(&mut self, hash: &H256, data: &[u8], depth: u32);

/// Drain all accepted records from the recorder in no particular order.
fn drain(self) -> Vec<Record> where Self: Sized;
/// Drain all accepted records from the recorder in ascending order by depth.
fn drain(&mut self) -> Vec<Record> where Self: Sized;
}

/// A no-op trie recorder. This ignores everything which is thrown at it.
Expand All @@ -53,7 +53,7 @@ impl Recorder for NoOp {
fn record(&mut self, _hash: &H256, _data: &[u8], _depth: u32) {}

#[inline]
fn drain(self) -> Vec<Record> { Vec::new() }
fn drain(&mut self) -> Vec<Record> { Vec::new() }
}

/// A simple recorder. Does nothing fancy but fulfills the `Recorder` interface
Expand Down Expand Up @@ -92,15 +92,16 @@ impl Recorder for BasicRecorder {
}
}

fn drain(self) -> Vec<Record> {
self.nodes
fn drain(&mut self) -> Vec<Record> {
::std::mem::replace(&mut self.nodes, Vec::new())
}
}

#[cfg(test)]
mod tests {
use super::*;
use sha3::Hashable;
use ::H256;

#[test]
fn no_op_does_nothing() {
Expand Down Expand Up @@ -161,4 +162,75 @@ mod tests {
depth: 456,
});
}

#[test]
fn trie_record() {
use trie::{TrieDB, TrieDBMut, Trie, TrieMut};
use memorydb::MemoryDB;

let mut db = MemoryDB::new();

let mut root = H256::default();

{
let mut x = TrieDBMut::new(&mut db, &mut root);

x.insert(b"dog", b"cat").unwrap();
x.insert(b"lunch", b"time").unwrap();
x.insert(b"notdog", b"notcat").unwrap();
x.insert(b"hotdog", b"hotcat").unwrap();
x.insert(b"letter", b"confusion").unwrap();
x.insert(b"insert", b"remove").unwrap();
x.insert(b"pirate", b"aargh!").unwrap();
x.insert(b"yo ho ho", b"and a bottle of rum").unwrap();
}

let trie = TrieDB::new(&db, &root).unwrap();
let mut recorder = BasicRecorder::new();

trie.get_recorded(b"pirate", &mut recorder).unwrap().unwrap();

let nodes: Vec<_> = recorder.drain().into_iter().map(|r| r.data).collect();
assert_eq!(nodes, vec![
vec![
248, 81, 128, 128, 128, 128, 128, 128, 160, 50, 19, 71, 57, 213, 63, 125, 149,
92, 119, 88, 96, 80, 126, 59, 11, 160, 142, 98, 229, 237, 200, 231, 224, 79, 118,
215, 93, 144, 246, 179, 176, 160, 118, 211, 171, 199, 172, 136, 136, 240, 221, 59,
110, 82, 86, 54, 23, 95, 48, 108, 71, 125, 59, 51, 253, 210, 18, 116, 79, 0, 236,
102, 142, 48, 128, 128, 128, 128, 128, 128, 128, 128, 128
],
vec![
248, 60, 206, 134, 32, 105, 114, 97, 116, 101, 134, 97, 97, 114, 103, 104, 33,
128, 128, 128, 128, 128, 128, 128, 128, 221, 136, 32, 111, 32, 104, 111, 32, 104,
111, 147, 97, 110, 100, 32, 97, 32, 98, 111, 116, 116, 108, 101, 32, 111, 102,
32, 114, 117, 109, 128, 128, 128, 128, 128, 128, 128
]
]);

trie.get_recorded(b"letter", &mut recorder).unwrap().unwrap();

let nodes: Vec<_> = recorder.drain().into_iter().map(|r| r.data).collect();
assert_eq!(nodes, vec![
vec![
248, 81, 128, 128, 128, 128, 128, 128, 160, 50, 19, 71, 57, 213, 63, 125, 149,
92, 119, 88, 96, 80, 126, 59, 11, 160, 142, 98, 229, 237, 200, 231, 224, 79, 118,
215, 93, 144, 246, 179, 176, 160, 118, 211, 171, 199, 172, 136, 136, 240, 221,
59, 110, 82, 86, 54, 23, 95, 48, 108, 71, 125, 59, 51, 253, 210, 18, 116, 79,
0, 236, 102, 142, 48, 128, 128, 128, 128, 128, 128, 128, 128, 128
],
vec![
248, 99, 128, 128, 128, 128, 200, 131, 32, 111, 103, 131, 99, 97, 116, 128, 128,
128, 206, 134, 32, 111, 116, 100, 111, 103, 134, 104, 111, 116, 99, 97, 116, 206,
134, 32, 110, 115, 101, 114, 116, 134, 114, 101, 109, 111, 118, 101, 128, 128,
160, 202, 250, 252, 153, 229, 63, 255, 13, 100, 197, 80, 120, 190, 186, 92, 5,
255, 135, 245, 205, 180, 213, 161, 8, 47, 107, 13, 105, 218, 1, 9, 5, 128,
206, 134, 32, 111, 116, 100, 111, 103, 134, 110, 111, 116, 99, 97, 116, 128, 128
],
vec![
235, 128, 128, 128, 128, 128, 128, 208, 133, 53, 116, 116, 101, 114, 137, 99,
111, 110, 102, 117, 115, 105, 111, 110, 202, 132, 53, 110, 99, 104, 132, 116,
105, 109, 101, 128, 128, 128, 128, 128, 128, 128, 128, 128
]
]);
}
}
6 changes: 2 additions & 4 deletions util/src/trie/triedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,13 @@ impl<'db> TrieDB<'db> {
/// value exists for the key.
///
/// Note: Not a public API; use Trie trait functions.
fn get_from_node<'key, R: 'key>
(
fn get_from_node<'key, R: 'key>(
&'db self,
node: &'db [u8],
key: &NibbleSlice<'key>,
r: &'key mut R,
d: u32
) -> super::Result<Option<&'db [u8]>> where 'db: 'key, R: Recorder
{
) -> super::Result<Option<&'db [u8]>> where 'db: 'key, R: Recorder {
match Node::decoded(node) {
Node::Leaf(ref slice, value) if key == slice => Ok(Some(value)),
Node::Extension(ref slice, item) if key.starts_with(slice) => {
Expand Down

0 comments on commit f0e94c6

Please sign in to comment.