Merge branch 'master' of github.com:gavofyork/ethcore-util

This commit is contained in:
Gav Wood 2015-12-01 01:32:10 +01:00
commit eb8f04a3e6
5 changed files with 251 additions and 215 deletions

View File

@ -20,7 +20,7 @@ fn bench_stream_u64_value(b: &mut Bencher) {
// u64 // u64
let mut stream = RlpStream::new(); let mut stream = RlpStream::new();
stream.append(&0x1023456789abcdefu64); stream.append(&0x1023456789abcdefu64);
let _ = stream.out().unwrap(); let _ = stream.out();
}); });
} }
@ -42,7 +42,7 @@ fn bench_stream_u256_value(b: &mut Bencher) {
stream.append(&U256::from_str("8090a0b0c0d0e0f009102030405060770000000000000001000000000\ stream.append(&U256::from_str("8090a0b0c0d0e0f009102030405060770000000000000001000000000\
00012f0") 00012f0")
.unwrap()); .unwrap());
let _ = stream.out().unwrap(); let _ = stream.out();
}); });
} }
@ -66,7 +66,7 @@ fn bench_stream_nested_empty_lists(b: &mut Bencher) {
stream.append_list(0); stream.append_list(0);
stream.append_list(1).append_list(0); stream.append_list(1).append_list(0);
stream.append_list(2).append_list(0).append_list(1).append_list(0); stream.append_list(2).append_list(0).append_list(1).append_list(0);
let _ = stream.out().unwrap(); let _ = stream.out();
}); });
} }
@ -76,11 +76,11 @@ fn bench_decode_nested_empty_lists(b: &mut Bencher) {
// [ [], [[]], [ [], [[]] ] ] // [ [], [[]], [ [], [[]] ] ]
let data = vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]; let data = vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0];
let rlp = Rlp::new(&data); let rlp = Rlp::new(&data);
let _v0: Vec<u8> = Decodable::decode(&rlp.at(0)); let _v0: Vec<u16> = Decodable::decode(&rlp.at(0));
let _v1: Vec<Vec<u8>> = Decodable::decode(&rlp.at(1)); let _v1: Vec<Vec<u16>> = Decodable::decode(&rlp.at(1));
let nested_rlp = rlp.at(2); let nested_rlp = rlp.at(2);
let _v2a: Vec<u8> = Decodable::decode(&nested_rlp.at(0)); let _v2a: Vec<u16> = Decodable::decode(&nested_rlp.at(0));
let _v2b: Vec<Vec<u8>> = Decodable::decode(&nested_rlp.at(1)); let _v2b: Vec<Vec<u16>> = Decodable::decode(&nested_rlp.at(1));
}); });
} }
@ -91,6 +91,6 @@ fn bench_stream_1000_empty_lists(b: &mut Bencher) {
for _ in 0..1000 { for _ in 0..1000 {
stream.append_list(0); stream.append_list(0);
} }
let _ = stream.out().unwrap(); let _ = stream.out();
}); });
} }

View File

@ -31,7 +31,7 @@ pub struct NibbleSlice<'a> {
offset: usize, offset: usize,
} }
impl<'a> NibbleSlice<'a> { impl<'a, 'view> NibbleSlice<'a> where 'a: 'view {
/// Create a new nibble slice with the given byte-slice. /// Create a new nibble slice with the given byte-slice.
pub fn new(data: &[u8]) -> NibbleSlice { NibbleSlice::new_offset(data, 0) } pub fn new(data: &[u8]) -> NibbleSlice { NibbleSlice::new_offset(data, 0) }
@ -60,7 +60,7 @@ impl<'a> NibbleSlice<'a> {
} }
/// Return object which represents a view on to this slice (further) offset by `i` nibbles. /// Return object which represents a view on to this slice (further) offset by `i` nibbles.
pub fn mid(&self, i: usize) -> Self { NibbleSlice{ data: self.data, offset: self.offset + i} } pub fn mid(&'view self, i: usize) -> NibbleSlice<'a> { NibbleSlice{ data: self.data, offset: self.offset + i} }
/// Do we start with the same nibbles as the whole of `them`? /// Do we start with the same nibbles as the whole of `them`?
pub fn starts_with(&self, them: &Self) -> bool { self.common_prefix(them) == them.len() } pub fn starts_with(&self, them: &Self) -> bool { self.common_prefix(them) == them.len() }

View File

@ -132,7 +132,7 @@ pub enum Prototype {
List(usize), List(usize),
} }
impl<'a> Rlp<'a> { impl<'a, 'view> Rlp<'a> where 'a: 'view {
/// Create a new instance of `Rlp` /// Create a new instance of `Rlp`
pub fn new(bytes: &'a [u8]) -> Rlp<'a> { pub fn new(bytes: &'a [u8]) -> Rlp<'a> {
Rlp { Rlp {
@ -153,7 +153,7 @@ impl<'a> Rlp<'a> {
} }
} }
/// The bare data of the rlp. /// The raw data of the RLP.
/// ///
/// ```rust /// ```rust
/// extern crate ethcore_util as util; /// extern crate ethcore_util as util;
@ -162,15 +162,18 @@ impl<'a> Rlp<'a> {
/// fn main () { /// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = Rlp::new(&data); /// let rlp = Rlp::new(&data);
/// let view = rlp.at(1); /// let dog = rlp.at(1).raw();
/// let dog = view.data();
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']); /// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
/// } /// }
/// ``` /// ```
pub fn raw(&self) -> &[u8] { pub fn raw(&'view self) -> &'a [u8] {
self.rlp.raw() self.rlp.raw()
} }
pub fn data(&'view self) -> &'a [u8] {
self.rlp.data()
}
/// Returns number of rlp items. /// Returns number of rlp items.
/// ///
/// ```rust /// ```rust
@ -223,7 +226,7 @@ impl<'a> Rlp<'a> {
/// assert_eq!(dog, "dog".to_string()); /// assert_eq!(dog, "dog".to_string());
/// } /// }
/// ``` /// ```
pub fn at(&self, index: usize) -> Rlp<'a> { pub fn at(&'view self, index: usize) -> Rlp<'a> {
From::from(self.rlp.at(index).unwrap()) From::from(self.rlp.at(index).unwrap())
} }
@ -325,7 +328,7 @@ impl<'a> Rlp<'a> {
} }
} }
impl<'a> UntrustedRlp<'a> { impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view {
/// returns new instance of `UntrustedRlp` /// returns new instance of `UntrustedRlp`
pub fn new(bytes: &'a [u8]) -> UntrustedRlp<'a> { pub fn new(bytes: &'a [u8]) -> UntrustedRlp<'a> {
UntrustedRlp { UntrustedRlp {
@ -343,15 +346,18 @@ impl<'a> UntrustedRlp<'a> {
/// fn main () { /// fn main () {
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
/// let rlp = UntrustedRlp::new(&data); /// let rlp = UntrustedRlp::new(&data);
/// let view = rlp.at(1).unwrap(); /// let dog = rlp.at(1).unwrap().raw();
/// let dog = view.data();
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']); /// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
/// } /// }
/// ``` /// ```
pub fn raw(&self) -> &[u8] { pub fn raw(&'view self) -> &'a [u8] {
self.bytes self.bytes
} }
pub fn data(&'view self) -> &'a [u8] {
unimplemented!();
}
/// Returns number of rlp items. /// Returns number of rlp items.
/// ///
/// ```rust /// ```rust
@ -410,7 +416,7 @@ impl<'a> UntrustedRlp<'a> {
/// assert_eq!(dog, "dog".to_string()); /// assert_eq!(dog, "dog".to_string());
/// } /// }
/// ``` /// ```
pub fn at(&self, index: usize) -> Result<UntrustedRlp<'a>, DecoderError> { pub fn at(&'view self, index: usize) -> Result<UntrustedRlp<'a>, DecoderError> {
if !self.is_list() { if !self.is_list() {
return Err(DecoderError::RlpExpectedToBeList); return Err(DecoderError::RlpExpectedToBeList);
} }
@ -1452,4 +1458,31 @@ mod tests {
vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])]; vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])];
run_decode_tests(tests); run_decode_tests(tests);
} }
#[test]
fn test_view() {
struct View<'a> {
bytes: &'a [u8]
}
impl <'a, 'view> View<'a> where 'a: 'view {
fn new(bytes: &'a [u8]) -> View<'a> {
View {
bytes: bytes
}
}
fn offset(&'view self, len: usize) -> View<'a> {
View::new(&self.bytes[len..])
}
fn data(&'view self) -> &'a [u8] {
self.bytes
}
}
let data = vec![0, 1, 2, 3];
let view = View::new(&data);
let _data_slice = view.offset(1).data();
}
} }

View File

@ -1,4 +1,5 @@
use memorydb::*; use memorydb::*;
use sha3::*;
use hashdb::*; use hashdb::*;
use hash::*; use hash::*;
use nibbleslice::*; use nibbleslice::*;
@ -25,51 +26,51 @@ pub trait Trie {
fn remove(&mut self, key: &[u8]); fn remove(&mut self, key: &[u8]);
} }
pub struct TrieDB {
db: Box<HashDB>,
root: H256,
}
enum Operation { enum Operation {
New(H256, Bytes), New(H256, Bytes),
Delete(H256), Delete(H256),
} }
struct Diff (Vec<Operation>) struct Diff (Vec<Operation>);
impl Diff { impl Diff {
fn new() -> Diff { Diff(vec![]) } fn new() -> Diff { Diff(vec![]) }
/// Given the RLP that encodes a node, append a reference to that node `out` and leave `diff` /// Given the RLP that encodes a node, append a reference to that node `out` and leave `diff`
/// such that the reference is valid, once applied. /// such that the reference is valid, once applied.
fn new_node(&mut self, Bytes rlp, out: &mut RlpStream) { fn new_node(&mut self, rlp: Bytes, out: &mut RlpStream) {
if (rlp.len() >= 32) { if (rlp.len() >= 32) {
let rlp_sha3 = rlp.sha3(); let rlp_sha3 = rlp.sha3();
out.append(&rlp_sha3); out.append(&rlp_sha3);
self.operations.push(Operation::New(rlp_sha3, rlp)); self.0.push(Operation::New(rlp_sha3, rlp));
} }
else { else {
out.append_raw(&rlp); out.append_raw(&rlp, 1);
} }
} }
/// Given the RLP that encodes a now-unused node, leave `diff` in such a state that it is noted. /// Given the RLP that encodes a now-unused node, leave `diff` in such a state that it is noted.
fn delete_node_sha3(&mut self, old_sha3: H256) { fn delete_node_sha3(&mut self, old_sha3: H256) {
self.operations.push(Operation::Delete(old_sha3)); self.0.push(Operation::Delete(old_sha3));
} }
fn delete_node(&mut self, old: &Rlp) { fn delete_node(&mut self, old: &Rlp) {
if (old.is_data() && old.size() == 32) { if (old.is_data() && old.size() == 32) {
self.operations.push(Operation::Delete(H256::decode(old))); self.0.push(Operation::Delete(H256::decode(old)));
} }
} }
fn replace_node(&mut self, old: &Rlp, Bytes rlp, out: &mut RlpStream) { fn replace_node(&mut self, old: &Rlp, rlp: Bytes, out: &mut RlpStream) {
self.delete_node(old); self.delete_node(old);
self.new_node(rlp, &mut out); self.new_node(rlp, out);
} }
} }
pub struct TrieDB {
db: Box<HashDB>,
root: H256,
}
impl TrieDB { impl TrieDB {
pub fn new<T>(db: T) -> Self where T: HashDB + 'static { TrieDB{ db: Box::new(db), root: H256::new() } } pub fn new<T>(db: T) -> Self where T: HashDB + 'static { TrieDB{ db: Box::new(db), root: H256::new() } }
@ -87,16 +88,8 @@ impl TrieDB {
println!("set_root_rlp {:?} {:?}", root_data, self.root); println!("set_root_rlp {:?} {:?}", root_data, self.root);
} }
fn add(&mut self, key: &NibbleSlice, value: &[u8]) {
// determine what the new root is, insert new nodes and remove old as necessary.
let todo: Diff = Diff::new();
let root_rlp = self.inject(self.db.lookup(&self.root).expect("Trie root not found!"), key, value, &mut todo);
self.apply(todo);
self.set_root_rlp(&root_rlp);
}
fn apply(&mut self, diff: Diff) { fn apply(&mut self, diff: Diff) {
for d in diff.operations.into_iter() { for d in diff.0.into_iter() {
match d { match d {
Operation::Delete(h) => { Operation::Delete(h) => {
trace!("TrieDB::apply --- {:?}", &h); trace!("TrieDB::apply --- {:?}", &h);
@ -109,176 +102,20 @@ impl TrieDB {
} }
} }
} }
/*
/// Return the bytes encoding the node represented by `rlp`. It will be unlinked from fn add(&mut self, key: &NibbleSlice, value: &[u8]) {
/// the trie. // determine what the new root is, insert new nodes and remove old as necessary.
fn take_node(&self, rlp: &Rlp, &mut diff) -> Bytes { let todo: Diff = Diff::new();
if (rlp.is_data()) { let root_rlp = self.inject(self.db.lookup(&self.root).expect("Trie root not found!"), key, value, &mut todo);
Bytes::decode(rlp) self.apply(todo);
} self.set_root_rlp(&root_rlp);
else {
let h = H256::decode(rlp);
let r = self.db.lookup(&h).as_vec();
diff.delete_node(h);
r
}
}
fn inject_and_replace(&self, old: &[u8], old_sha3: H256, partial: &NibbleSlice, value: &[u8], diff: &mut Diff, out: &mut RlpStream) {
diff.new_node(self.inject(old, partial, value, diff), &mut out);
diff.delete_node(old, old_sha3);
}
/// Transform an existing extension or leaf node plus a new partial/value to a two-entry branch.
///
/// **This operation will not insert the new node nor destroy the original.**
fn transmute_to_branch_and_inject(&self, orig_is_leaf: bool, orig_partial: &NibbleSlice, orig_raw_payload: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
let intermediate = match orig_is_leaf {
true => Self::transmute_leaf_to_branch(orig_partial, orig_raw_payload, &mut diff),
false => Self::transmute_extension_to_branch(orig_partial, orig_raw_payload, &mut diff),
};
self.inject(&intermediate, partial, value, &mut diff)
// TODO: implement without having to make an intermediate representation.
}
/// Transform an existing extension or leaf node to an invalid single-entry branch.
///
/// **This operation will not insert the new node nor destroy the original.**
fn transmute_extension_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
let mut s = RLPStream::new_list(17);
assert!(!orig_partial.is_empty()); // extension nodes are not allowed to have empty partial keys.
let index = orig_partial.at(0);
// orig is extension - orig_payload is a node itself.
for i in 0..17 {
if index == i {
if orig_partial.len() > 1 {
// still need an extension
diff.new_node(compose_extension(orig_partial.mid(1), orig_raw_payload), &mut s);
} else {
// was an extension of length 1 - just redirect the payload into here.
s.append_raw(orig_payload.raw());
}
} else {
s.append_null_data();
}
}
s.out()
}
fn transmute_leaf_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
let mut s = RLPStream::new_list(17);
let index = orig_partial.is_empty() ? 16 : orig_partial.at(0);
// orig is leaf - orig_payload is data representing the actual value.
for i in 0..17 {
if index == i {
// this is our node.
diff.new_node(compose_raw(orig_partial.mid(if i == 16 {0} else {1}), orig_raw_payload, true), &mut s);
} else {
s.append_null_data();
}
}
s.out()
}
/// Given a branch node's RLP `orig` together with a `partial` key and `value`, return the
/// RLP-encoded node that accomodates the trie with the new entry. Mutate `diff` so that
/// once applied the returned node is valid.
fn injected_into_branch(&self, orig: &Rlp, partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
RlpStream s;
let index = partial.is_empty() ? 16 : partial.at(0);
for i in 0..17 {
if index == i && {
// this is our node.
if (orig.at(i).is_empty()) {
// easy - original had empty slot.
diff.new_node(compose_leaf(partial.mid(if i == 16 {0} else {1}), value), &mut s);
} else if (i == 16) {
// leaf entry - just replace.
let new = compose_leaf(partial.mid(if i == 16 {0} else {1}), value);
diff.replace_node(orig.at(i).raw(), new, &mut s),
} else {
// harder - original has something there already
let new = self.inject(orig.at(i).raw(), partial.mid(1), value, &mut diff);
diff.replace_node(orig.at(i).raw(), new, &mut s)
}
} else {
s.append_raw(orig.at(i).raw());
}
}
s
}
/// Determine the RLP of the node, assuming we're inserting `partial` into the
/// node currently of data `old`. This will *not* delete any hash of `old` from the database;
/// it will just return the new RLP that includes the new node.
///
/// The database will be updated so as to make the returned RLP valid through inserting
/// and deleting nodes as necessary.
///
/// **This operation will not insert the new node now destroy the original.**
fn inject(&self, old: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
// already have an extension. either fast_forward, cleve or transmute_to_branch.
let old_rlp = Rlp::new(old);
match old_rlp.prototype() {
Prototype::List(17) => {
// already have a branch. route and inject.
self.injected_into_branch(old_rlp, partial, value, &mut diff)
},
Prototype::List(2) => {
let their_key_rlp = old_rlp.at(0);
let (them, is_leaf) = NibbleSlice::from_encoded(their_key_rlp.data());
match partial.common_prefix(&them) {
0 if partial.is_empty() && them.is_empty() => {
// both empty: just replace.
compose_leaf(partial, value)
},
0 => {
// one of us isn't empty: transmute to branch here
transmute_to_branch_and_inject(is_leaf, them, old_rlp.at(1).raw())
},
cp if cp == them.len() => {
// fully-shared prefix for this extension:
// skip to the end of this extension and continue the inject there.
let n = self.take_node(old_rlp.at(1).raw());
let downstream_node = self.inject(&n, partial.mid(cp), value, &mut diff);
let mut s = RlpStream::new_list(2);
s.append_raw(old_rlp.at(0).raw());
diff.new_node(downstream_node, &mut s);
s.out()
},
cp => {
// partially-shared prefix for this extension:
// split into two extensions, high and low, pass the
// low through inject with the value before inserting the result
// into high to create the new.
// TODO: optimise by doing this without creating injected_low.
// low (farther from root)
let low = Self::compose_raw(them.mid(cp), old_rlp.at(1).raw(), is_leaf);
let injected_low = self.inject(&low, partial.mid(cp), value, &mut diff);
// high (closer to root)
let mut s = RlpStream::new_list(2);
s.append(them.encoded_leftmost(cp, false));
diff.new_node(injected_low, &mut s);
s.out()
},
}
},
Prototype::Data(0) => {
(Self::compose_leaf(partial, value, true), Diff::new())
},
_ => panic!("Invalid RLP for node."),
}
} }
fn compose_raw(partial: &NibbleSlice, raw_payload: &[u8], bool is_leaf) -> Bytes { fn compose_raw(partial: &NibbleSlice, raw_payload: &[u8], bool is_leaf) -> Bytes {
println!("compose_raw {:?} {:?} {:?} ({:?})", partial, value, is_leaf, partial.encoded(is_leaf)); println!("compose_raw {:?} {:?} {:?} ({:?})", partial, value, is_leaf, partial.encoded(is_leaf));
let mut s = RlpStream::new_list(2); let mut s = RlpStream::new_list(2);
s.append(&partial.encoded(is_leaf)); s.append(&partial.encoded(is_leaf));
s.append_raw(raw_payload); s.append_raw(raw_payload, 1);
let r = s.out(); let r = s.out();
println!("output: -> {:?}", &r); println!("output: -> {:?}", &r);
r r
@ -297,6 +134,171 @@ impl TrieDB {
fn compose_extension(partial: &NibbleSlice, raw_payload: &[u8]) -> Bytes { fn compose_extension(partial: &NibbleSlice, raw_payload: &[u8]) -> Bytes {
Self::compose_raw(partial, raw_payload, false) Self::compose_raw(partial, raw_payload, false)
} }
/// Return the bytes encoding the node represented by `rlp`. It will be unlinked from
/// the trie.
fn take_node(&self, rlp: &Rlp, &mut diff) -> Bytes {
if (rlp.is_data()) {
Bytes::decode(rlp)
}
else {
let h = H256::decode(rlp);
let r = self.db.lookup(&h).expect("Trie root not found!").as_vec();
diff.delete_node(h);
r
}
}
/// Transform an existing extension or leaf node plus a new partial/value to a two-entry branch.
///
/// **This operation will not insert the new node nor destroy the original.**
fn transmute_to_branch_and_inject(&self, orig_is_leaf: bool, orig_partial: &NibbleSlice, orig_raw_payload: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
let intermediate = match orig_is_leaf {
true => Self::transmute_leaf_to_branch(orig_partial, orig_raw_payload, diff),
false => Self::transmute_extension_to_branch(orig_partial, orig_raw_payload, diff),
};
self.inject(&intermediate, partial, value, diff)
// TODO: implement without having to make an intermediate representation.
}
/// Transform an existing extension or leaf node to an invalid single-entry branch.
///
/// **This operation will not insert the new node nor destroy the original.**
fn transmute_extension_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
let mut s = RLPStream::new_list(17);
assert!(!orig_partial.is_empty()); // extension nodes are not allowed to have empty partial keys.
let index = orig_partial.at(0);
// orig is extension - orig_payload is a node itself.
for i in 0..17 {
if index == i {
if orig_partial.len() > 1 {
// still need an extension
diff.new_node(compose_extension(orig_partial.mid(1), orig_raw_payload), s);
} else {
// was an extension of length 1 - just redirect the payload into here.
s.append_raw(orig_payload.raw(), 1);
}
} else {
s.append_null_data();
}
}
s.out()
}
fn transmute_leaf_to_branch(orig_partial: &NibbleSlice, orig_raw_payload: &[u8], diff: &mut Diff) -> Bytes {
let mut s = RLPStream::new_list(17);
let index = orig_partial.is_empty() ? 16 : orig_partial.at(0);
// orig is leaf - orig_payload is data representing the actual value.
for i in 0..17 {
if index == i {
// this is our node.
diff.new_node(compose_raw(orig_partial.mid(if i == 16 {0} else {1}), orig_raw_payload, true), s);
} else {
s.append_null_data();
}
}
s.out()
}
/// Given a branch node's RLP `orig` together with a `partial` key and `value`, return the
/// RLP-encoded node that accomodates the trie with the new entry. Mutate `diff` so that
/// once applied the returned node is valid.
fn injected_into_branch(&self, orig: &Rlp, partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
RlpStream s;
let index = partial.is_empty() ? 16 : partial.at(0);
for i in 0..17 {
if index == i && {
// this is our node.
if (orig.at(i).is_empty()) {
// easy - original had empty slot.
diff.new_node(compose_leaf(partial.mid(if i == 16 {0} else {1}), value), s);
} else if (i == 16) {
// leaf entry - just replace.
let new = compose_leaf(partial.mid(if i == 16 {0} else {1}), value);
diff.replace_node(orig.at(i).raw(), new, s),
} else {
// harder - original has something there already
let new = self.inject(orig.at(i).raw(), partial.mid(1), value, diff);
diff.replace_node(orig.at(i).raw(), new, s)
}
} else {
s.append_raw(orig.at(i).raw(), 1);
}
}
s
}
fn inject_and_replace(&self, old: &[u8], old_sha3: H256, partial: &NibbleSlice, value: &[u8], diff: &mut Diff, out: &mut RlpStream) {
diff.new_node(self.inject(old, partial, value, diff), &mut out);
diff.delete_node(old, old_sha3);
}
/// Determine the RLP of the node, assuming we're inserting `partial` into the
/// node currently of data `old`. This will *not* delete any hash of `old` from the database;
/// it will just return the new RLP that includes the new node.
///
/// The database will be updated so as to make the returned RLP valid through inserting
/// and deleting nodes as necessary.
///
/// **This operation will not insert the new node now destroy the original.**
fn inject(&self, old: &[u8], partial: &NibbleSlice, value: &[u8], diff: &mut Diff) -> Bytes {
// already have an extension. either fast_forward, cleve or transmute_to_branch.
let old_rlp = Rlp::new(old);
match old_rlp.prototype() {
Prototype::List(17) => {
// already have a branch. route and inject.
self.injected_into_branch(old_rlp, partial, value, diff)
},
Prototype::List(2) => {
let their_key_rlp = old_rlp.at(0);
let (them, is_leaf) = NibbleSlice::from_encoded(their_key_rlp.data());
match partial.common_prefix(&them) {
0 if partial.is_empty() && them.is_empty() => {
// both empty: just replace.
compose_leaf(partial, value)
},
0 => {
// one of us isn't empty: transmute to branch here
transmute_to_branch_and_inject(is_leaf, them, old_rlp.at(1).raw())
},
cp if cp == them.len() => {
// fully-shared prefix for this extension:
// skip to the end of this extension and continue the inject there.
let n = self.take_node(old_rlp.at(1).raw());
let downstream_node = self.inject(&n, partial.mid(cp), value, diff);
let mut s = RlpStream::new_list(2);
s.append_raw(old_rlp.at(0).raw(), 1);
diff.new_node(downstream_node, s);
s.out()
},
cp => {
// partially-shared prefix for this extension:
// split into two extensions, high and low, pass the
// low through inject with the value before inserting the result
// into high to create the new.
// TODO: optimise by doing this without creating injected_low.
// low (farther from root)
let low = Self::compose_raw(them.mid(cp), old_rlp.at(1).raw(), is_leaf);
let injected_low = self.inject(&low, partial.mid(cp), value, diff);
// high (closer to root)
let mut s = RlpStream::new_list(2);
s.append(them.encoded_leftmost(cp, false));
diff.new_node(injected_low, s);
s.out()
},
}
},
Prototype::Data(0) => {
(Self::compose_leaf(partial, value, true), Diff::new())
},
_ => panic!("Invalid RLP for node."),
}
}
*/
} }
impl Trie for TrieDB { impl Trie for TrieDB {
@ -311,7 +313,8 @@ impl Trie for TrieDB {
} }
fn insert(&mut self, key: &[u8], value: &[u8]) { fn insert(&mut self, key: &[u8], value: &[u8]) {
(self as &mut TrieDB).add(&NibbleSlice::new(key), value); unimplemented!();
// (self as &mut TrieDB).add(&NibbleSlice::new(key), value);
} }
fn remove(&mut self, _key: &[u8]) { fn remove(&mut self, _key: &[u8]) {
@ -330,7 +333,7 @@ fn playpen() {
t.init(); t.init();
assert_eq!(*t.root(), SHA3_NULL_RLP); assert_eq!(*t.root(), SHA3_NULL_RLP);
assert!(t.is_empty()); assert!(t.is_empty());
/*
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]); 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]) ]));*/
} }

View File

@ -138,9 +138,9 @@ fn as_nibbles(bytes: &[u8]) -> Vec<u8> {
fn hash256rlp(input: &[(Vec<u8>, Vec<u8>)], pre_len: usize, stream: &mut RlpStream) { fn hash256rlp(input: &[(Vec<u8>, Vec<u8>)], pre_len: usize, stream: &mut RlpStream) {
let inlen = input.len(); let inlen = input.len();
// in case of empty slice, just append null // in case of empty slice, just append empty data
if inlen == 0 { if inlen == 0 {
stream.append(&""); stream.append_empty_data();
return; return;
} }
@ -196,7 +196,7 @@ fn hash256rlp(input: &[(Vec<u8>, Vec<u8>)], pre_len: usize, stream: &mut RlpStre
// if at least 1 successive element has the same nibble // if at least 1 successive element has the same nibble
// append their suffixes // append their suffixes
match len { match len {
0 => { stream.append(&""); }, 0 => { stream.append_empty_data(); },
_ => hash256aux(&input[begin..(begin + len)], pre_len + 1, stream) _ => hash256aux(&input[begin..(begin + len)], pre_len + 1, stream)
} }
begin += len; begin += len;
@ -205,7 +205,7 @@ fn hash256rlp(input: &[(Vec<u8>, Vec<u8>)], pre_len: usize, stream: &mut RlpStre
// if fist key len is equal prefix, append it's value // if fist key len is equal prefix, append it's value
match pre_len == key.len() { match pre_len == key.len() {
true => { stream.append(&value); }, true => { stream.append(&value); },
false => { stream.append(&""); } false => { stream.append_empty_data(); }
}; };
} }