Trie progress...

This commit is contained in:
Gav Wood 2015-11-30 16:06:29 +01:00
parent ddd0baa70a
commit d293bce22d
3 changed files with 28 additions and 5 deletions

View File

@ -38,6 +38,11 @@ impl<'a> NibbleSlice<'a> {
/// Create a new nibble slice with the given byte-slice with a nibble offset.
pub fn new_offset(data: &'a [u8], offset: usize) -> NibbleSlice { NibbleSlice{data: data, offset: offset} }
/// Create a new nibble slice from the given HPE encoded data (e.g. output of `encoded()`).
pub fn from_encoded(data: &'a [u8]) -> (NibbleSlice, bool) {
(Self::new_offset(data, if data[0] & 16 == 16 {1} else {2}), data[0] & 32 == 32)
}
/// Is this an empty slice?
pub fn is_empty(&self) -> bool { self.len() == 0 }
@ -148,6 +153,15 @@ mod tests {
assert_eq!(n.mid(1).encoded(true), &[0x31, 0x23, 0x45]);
}
#[test]
fn from_encoded() {
let n = NibbleSlice::new(D);
assert_eq!((n, false), NibbleSlice::from_encoded(&[0x00, 0x01, 0x23, 0x45]));
assert_eq!((n, true), NibbleSlice::from_encoded(&[0x20, 0x01, 0x23, 0x45]));
assert_eq!((n.mid(1), false), NibbleSlice::from_encoded(&[0x11, 0x23, 0x45]));
assert_eq!((n.mid(1), true), NibbleSlice::from_encoded(&[0x31, 0x23, 0x45]));
}
#[test]
fn shared() {
let n = NibbleSlice::new(D);

View File

@ -227,10 +227,6 @@ impl<'a> Rlp<'a> {
From::from(self.rlp.at(index).unwrap())
}
pub fn data_at(&self, _index: usize) -> &[u8] {
unimplemented!();
}
/// No value
///
/// ```rust

View File

@ -88,6 +88,19 @@ impl TrieDB {
unimplemented!();
},
Prototype::List(2) => {
let their_key_rlp = o.at(0);
let (them, _) = NibbleSlice::from_encoded(o.data());
match partial_key.common_prefix(&them) {
0 => {
// transmute to branch here
},
cp if cp == them.len() => {
// fast-forward
},
cp => {
// cleve into two + branch in the middle
},
}
// already have an extension. either fast_forward, cleve or transmute_to_branch.
unimplemented!();
},
@ -143,5 +156,5 @@ fn playpen() {
assert!(t.is_empty());
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
assert_eq!(*t.root(), hash256(&[ NibblePair::new_raw(vec![0x01u8, 0x23], vec![0x01u8, 0x23])]));
assert_eq!(*t.root(), hash256(&[NibblePair::new_raw(vec![0x01u8, 0x23], vec![0x01u8, 0x23])]));
}