diff --git a/src/nibbleslice.rs b/src/nibbleslice.rs index 5866ed8f0..6ccf695c0 100644 --- a/src/nibbleslice.rs +++ b/src/nibbleslice.rs @@ -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); diff --git a/src/rlp.rs b/src/rlp.rs index 1703850bb..e8562828a 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -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 diff --git a/src/trie.rs b/src/trie.rs index 26eaafb2b..4aebc688f 100644 --- a/src/trie.rs +++ b/src/trie.rs @@ -88,6 +88,19 @@ impl TrieDB { unimplemented!(); }, Prototype::List(2) => { + let their_key_rlp = o.at(0); + let (them, _) = NibbleSlice::from_encoded(their_key_rlp.data()); + match partial_key.common_prefix(&them) { + 0 => { + // transmute to branch here + }, + cp if cp == them.len() => { + // fast-forward + }, + _ => { + // cleve into two + branch in the middle + }, + } // already have an extension. either fast_forward, cleve or transmute_to_branch. unimplemented!(); }, @@ -143,7 +156,5 @@ fn playpen() { assert!(t.is_empty()); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]); - assert_eq!(*t.root(), trie_root(vec![ - (vec![1u8, 0x23], vec![1u8, 0x23]) - ])); + assert_eq!(*t.root(), trie_root(vec![ (vec![1u8, 0x23], vec![1u8, 0x23]) ])); }