1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Record<HO> {
pub depth: u32,
pub data: Vec<u8>,
pub hash: HO,
}
#[derive(Debug)]
pub struct Recorder<HO> {
nodes: Vec<Record<HO>>,
min_depth: u32,
}
impl<HO: Copy> Default for Recorder<HO> {
fn default() -> Self {
Recorder::new()
}
}
impl<HO: Copy> Recorder<HO> {
#[inline]
pub fn new() -> Self {
Recorder::with_depth(0)
}
pub fn with_depth(depth: u32) -> Self {
Recorder {
nodes: Vec::new(),
min_depth: depth,
}
}
pub fn record(&mut self, hash: &HO, data: &[u8], depth: u32) {
if depth >= self.min_depth {
self.nodes.push(Record {
depth: depth,
data: data.into(),
hash: *hash,
})
}
}
pub fn drain(&mut self) -> Vec<Record<HO>> {
::std::mem::replace(&mut self.nodes, Vec::new())
}
}
#[cfg(test)]
mod tests {
use memory_db::MemoryDB;
use hash_db::Hasher;
use keccak_hasher::KeccakHasher;
use reference_trie::{RefTrieDB, RefTrieDBMut, Trie, TrieMut, Recorder, Record};
#[test]
fn basic_recorder() {
let mut basic = Recorder::new();
let node1 = vec![1, 2, 3, 4];
let node2 = vec![4, 5, 6, 7, 8, 9, 10];
let (hash1, hash2) = (KeccakHasher::hash(&node1), KeccakHasher::hash(&node2));
basic.record(&hash1, &node1, 0);
basic.record(&hash2, &node2, 456);
let record1 = Record {
data: node1,
hash: hash1,
depth: 0,
};
let record2 = Record {
data: node2,
hash: hash2,
depth: 456,
};
assert_eq!(basic.drain(), vec![record1, record2]);
}
#[test]
fn basic_recorder_min_depth() {
let mut basic = Recorder::with_depth(400);
let node1 = vec![1, 2, 3, 4];
let node2 = vec![4, 5, 6, 7, 8, 9, 10];
let hash1 = KeccakHasher::hash(&node1);
let hash2 = KeccakHasher::hash(&node2);
basic.record(&hash1, &node1, 0);
basic.record(&hash2, &node2, 456);
let records = basic.drain();
assert_eq!(records.len(), 1);
assert_eq!(records[0].clone(), Record {
data: node2,
hash: hash2,
depth: 456,
});
}
#[test]
fn trie_record() {
let mut db = MemoryDB::default();
let mut root = Default::default();
{
let mut x = RefTrieDBMut::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 = RefTrieDB::new(&db, &root).unwrap();
let mut recorder = Recorder::new();
trie.get_with(b"pirate", &mut recorder).unwrap().unwrap();
let nodes: Vec<_> = recorder.drain().into_iter().map(|r| r.data).collect();
assert_eq!(nodes, vec![
vec![
254, 192, 0, 128, 32, 27, 87, 5, 125, 163, 0, 90, 117, 142, 28, 67, 189, 82, 249,
72, 103, 181, 28, 167, 216, 106, 191, 152, 9, 255, 42, 59, 75, 199, 172, 190, 128,
227, 98, 5, 56, 103, 215, 106, 0, 144, 78, 159, 78, 163, 198, 13, 159, 226, 112, 82,
132, 211, 79, 143, 4, 16, 109, 253, 182, 34, 196, 39, 13
],
vec![
254, 1, 2, 52, 11, 105, 114, 97, 116, 101, 24, 97, 97, 114, 103, 104, 33, 112, 15,
111, 32, 104, 111, 32, 104, 111, 76, 97, 110, 100, 32, 97, 32, 98, 111, 116, 116,
108, 101, 32, 111, 102, 32, 114, 117, 109
]
]);
trie.get_with(b"letter", &mut recorder).unwrap().unwrap();
let nodes: Vec<_> = recorder.drain().into_iter().map(|r| r.data).collect();
assert_eq!(nodes, vec![
vec![
254, 192, 0, 128, 32, 27, 87, 5, 125, 163, 0, 90, 117, 142, 28, 67, 189, 82, 249,
72, 103, 181, 28, 167, 216, 106, 191, 152, 9, 255, 42, 59, 75, 199, 172, 190, 128,
227, 98, 5, 56, 103, 215, 106, 0, 144, 78, 159, 78, 163, 198, 13, 159, 226, 112, 82,
132, 211, 79, 143, 4, 16, 109, 253, 182, 34, 196, 39, 13
],
vec![
254, 16, 83, 28, 5, 111, 103, 12, 99, 97, 116, 52, 11, 111, 116, 100, 111, 103, 24,
104, 111, 116, 99, 97, 116, 52, 11, 110, 115, 101, 114, 116, 24, 114, 101, 109, 111,
118, 101, 124, 254, 192, 0, 64, 10, 5, 116, 116, 101, 114, 36, 99, 111, 110, 102,
117, 115, 105, 111, 110, 40, 8, 5, 110, 99, 104, 16, 116, 105, 109, 101, 52, 11, 111,
116, 100, 111, 103, 24, 110, 111, 116, 99, 97, 116
]
]);
}
}