Merge branch 'master' of https://github.com/gavofyork/ethcore-util
This commit is contained in:
commit
8e1af0e7ca
@ -1,4 +1,5 @@
|
||||
use hash::*;
|
||||
use bytes::*;
|
||||
|
||||
pub trait HashDB {
|
||||
/// Look up a given hash into the bytes that hash to it, returning None if the
|
||||
@ -56,6 +57,9 @@ pub trait HashDB {
|
||||
/// ```
|
||||
fn insert(&mut self, value: &[u8]) -> H256;
|
||||
|
||||
/// Like `insert()` , except you provide the key and the data is all moved.
|
||||
fn emplace(&mut self, key: H256, value: Bytes);
|
||||
|
||||
/// Remove a datum previously inserted. Insertions can be "owed" such that the same number of `insert()`s may
|
||||
/// happen without the data being eventually being inserted into the DB.
|
||||
///
|
||||
|
@ -37,6 +37,12 @@ use std::collections::HashMap;
|
||||
/// m.kill(&k);
|
||||
/// assert!(!m.exists(&k));
|
||||
///
|
||||
/// m.kill(&k);
|
||||
/// assert!(!m.exists(&k));
|
||||
///
|
||||
/// m.insert(d);
|
||||
/// assert!(!m.exists(&k));
|
||||
|
||||
/// m.insert(d);
|
||||
/// assert!(m.exists(&k));
|
||||
/// assert_eq!(m.lookup(&k).unwrap(), d);
|
||||
@ -130,9 +136,9 @@ impl HashDB for MemoryDB {
|
||||
fn insert(&mut self, value: &[u8]) -> H256 {
|
||||
let key = value.sha3();
|
||||
if match self.data.get_mut(&key) {
|
||||
Some(&mut (ref mut old_value, ref mut rc @ 0)) => {
|
||||
Some(&mut (ref mut old_value, ref mut rc @ -0x80000000i32 ... 0)) => {
|
||||
*old_value = From::from(value.bytes());
|
||||
*rc = 1;
|
||||
*rc += 1;
|
||||
false
|
||||
},
|
||||
Some(&mut (_, ref mut x)) => { *x += 1; false } ,
|
||||
@ -143,6 +149,20 @@ impl HashDB for MemoryDB {
|
||||
key
|
||||
}
|
||||
|
||||
fn emplace(&mut self, key: H256, value: Bytes) {
|
||||
match self.data.get_mut(&key) {
|
||||
Some(&mut (ref mut old_value, ref mut rc @ -0x80000000i32 ... 0)) => {
|
||||
*old_value = value;
|
||||
*rc += 1;
|
||||
return;
|
||||
},
|
||||
Some(&mut (_, ref mut x)) => { *x += 1; return; } ,
|
||||
None => {},
|
||||
}
|
||||
// ... None falls through into...
|
||||
self.data.insert(key, (value, 1));
|
||||
}
|
||||
|
||||
fn kill(&mut self, key: &H256) {
|
||||
if match self.data.get_mut(key) {
|
||||
Some(&mut (_, ref mut x)) => { *x -= 1; false }
|
||||
|
@ -1,5 +1,6 @@
|
||||
//! Nibble-orientated view onto byte-slice, allowing nibble-precision offsets.
|
||||
use std::cmp::*;
|
||||
use bytes::*;
|
||||
|
||||
/// Nibble-orientated view onto byte-slice, allowing nibble-precision offsets.
|
||||
///
|
||||
@ -69,6 +70,18 @@ impl<'a> NibbleSlice<'a> {
|
||||
}
|
||||
i
|
||||
}
|
||||
|
||||
pub fn encoded(&self, is_leaf: bool) -> Bytes {
|
||||
let l = self.len();
|
||||
let mut r = Bytes::with_capacity(l / 2 + 1);
|
||||
let mut i = l % 2;
|
||||
r.push(if i == 1 {0x10 + self.at(0)} else {0} + if is_leaf {0x20} else {0});
|
||||
while i < l {
|
||||
r.push(self.at(i) * 16 + self.at(i + 1));
|
||||
i += 2;
|
||||
}
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PartialEq for NibbleSlice<'a> {
|
||||
@ -126,6 +139,15 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoded() {
|
||||
let n = NibbleSlice::new(D);
|
||||
assert_eq!(n.encoded(false), &[0x00, 0x01, 0x23, 0x45]);
|
||||
assert_eq!(n.encoded(true), &[0x20, 0x01, 0x23, 0x45]);
|
||||
assert_eq!(n.mid(1).encoded(false), &[0x11, 0x23, 0x45]);
|
||||
assert_eq!(n.mid(1).encoded(true), &[0x31, 0x23, 0x45]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared() {
|
||||
let n = NibbleSlice::new(D);
|
||||
|
@ -186,6 +186,7 @@ impl HashDB for OverlayDB {
|
||||
}
|
||||
}
|
||||
fn insert(&mut self, value: &[u8]) -> H256 { self.overlay.insert(value) }
|
||||
fn emplace(&mut self, key: H256, value: Bytes) { self.overlay.emplace(key, value); }
|
||||
fn kill(&mut self, key: &H256) { self.overlay.kill(key); }
|
||||
}
|
||||
|
||||
|
23
src/rlp.rs
23
src/rlp.rs
@ -126,6 +126,12 @@ impl<'a> From<Rlp<'a>> for UntrustedRlp<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Prototype {
|
||||
Null,
|
||||
Data(usize),
|
||||
List(usize),
|
||||
}
|
||||
|
||||
impl<'a> Rlp<'a> {
|
||||
/// Create a new instance of `Rlp`
|
||||
pub fn new(bytes: &'a [u8]) -> Rlp<'a> {
|
||||
@ -134,6 +140,19 @@ impl<'a> Rlp<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the prototype of the RLP.
|
||||
pub fn prototype(&self) -> Prototype {
|
||||
if self.is_data() {
|
||||
Prototype::Data(self.size())
|
||||
}
|
||||
else if self.is_list() {
|
||||
Prototype::List(self.item_count())
|
||||
}
|
||||
else {
|
||||
Prototype::Null
|
||||
}
|
||||
}
|
||||
|
||||
/// The bare data of the rlp.
|
||||
///
|
||||
/// ```rust
|
||||
@ -208,6 +227,10 @@ impl<'a> Rlp<'a> {
|
||||
From::from(self.rlp.at(index).unwrap())
|
||||
}
|
||||
|
||||
pub fn data_at(&self, _index: usize) -> &[u8] {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
/// No value
|
||||
///
|
||||
/// ```rust
|
||||
|
101
src/trie.rs
101
src/trie.rs
@ -1,8 +1,9 @@
|
||||
use memorydb::*;
|
||||
use hashdb::*;
|
||||
use hash::*;
|
||||
//use rlp::*;
|
||||
//use bytes::*;
|
||||
use nibbleslice::*;
|
||||
use bytes::*;
|
||||
use rlp::*;
|
||||
|
||||
pub const NULL_RLP: [u8; 1] = [0x80; 1];
|
||||
pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] );
|
||||
@ -17,10 +18,10 @@ pub trait Trie {
|
||||
fn is_empty(&self) -> bool { *self.root() == SHA3_NULL_RLP }
|
||||
|
||||
// TODO: consider returning &[u8]...
|
||||
fn contains(key: &[u8]) -> bool;
|
||||
fn at(key: &[u8]) -> Option<&[u8]>;
|
||||
fn insert(key: &[u8], value: &[u8]);
|
||||
fn remove(key: &[u8]);
|
||||
fn contains(&self, key: &[u8]) -> bool;
|
||||
fn at(&self, key: &[u8]) -> Option<&[u8]>;
|
||||
fn insert(&mut self, key: &[u8], value: &[u8]);
|
||||
fn remove(&mut self, key: &[u8]);
|
||||
}
|
||||
|
||||
pub struct TrieDB {
|
||||
@ -28,6 +29,15 @@ pub struct TrieDB {
|
||||
root: H256,
|
||||
}
|
||||
|
||||
struct Diff {
|
||||
new: Vec<(H256, Bytes)>,
|
||||
old: Vec<H256>,
|
||||
}
|
||||
|
||||
impl Diff {
|
||||
pub fn new() -> Diff { Diff { new: vec![], old: vec![] }}
|
||||
}
|
||||
|
||||
impl TrieDB {
|
||||
pub fn new<T>(db: T) -> Self where T: HashDB + 'static { TrieDB{ db: Box::new(db), root: H256::new() } }
|
||||
|
||||
@ -35,37 +45,95 @@ impl TrieDB {
|
||||
|
||||
pub fn new_memory() -> Self { TrieDB{ db: Box::new(MemoryDB::new()), root: H256::new() } }
|
||||
|
||||
pub fn init(&mut self) { self.insert_root(&NULL_RLP); }
|
||||
pub fn init(&mut self) { self.set_root_rlp(&NULL_RLP); }
|
||||
|
||||
pub fn db(&self) -> &HashDB { self.db.as_ref() }
|
||||
|
||||
fn insert_root(&mut self, root_data: &[u8]) { self.root = self.db.insert(root_data); }
|
||||
fn set_root_rlp(&mut self, root_data: &[u8]) {
|
||||
self.db.kill(&self.root);
|
||||
self.root = self.db.insert(root_data);
|
||||
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 = {
|
||||
let root_rlp = self.db.lookup(&self.root).expect("Trie root not found!");
|
||||
self.merge(root_rlp, key, value)
|
||||
};
|
||||
self.apply(todo.1);
|
||||
self.set_root_rlp(&todo.0);
|
||||
}
|
||||
|
||||
fn apply(&mut self, diff: Diff) {
|
||||
for d in diff.old.iter() {
|
||||
self.db.kill(&d);
|
||||
}
|
||||
for d in diff.new.into_iter() {
|
||||
self.db.emplace(d.0, d.1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine the RLP of the node, assuming we're inserting `partial_key` into the
|
||||
/// node at `old`. This will *not* delete the old mode; 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.
|
||||
fn merge(&self, old: &[u8], partial_key: &NibbleSlice, value: &[u8]) -> (Bytes, Diff) {
|
||||
let o = Rlp::new(old);
|
||||
match o.prototype() {
|
||||
Prototype::List(17) => {
|
||||
// already have a branch. route and merge.
|
||||
unimplemented!();
|
||||
},
|
||||
Prototype::List(2) => {
|
||||
// already have an extension. either fast_forward, cleve or transmute_to_branch.
|
||||
unimplemented!();
|
||||
},
|
||||
Prototype::Data(0) => {
|
||||
(Self::compose_extension(partial_key, value, true), Diff::new())
|
||||
},
|
||||
_ => panic!("Invalid RLP for node."),
|
||||
}
|
||||
}
|
||||
|
||||
fn compose_extension(partial_key: &NibbleSlice, value: &[u8], is_leaf: bool) -> Bytes {
|
||||
println!("compose_extension {:?} {:?} {:?} ({:?})", partial_key, value, is_leaf, partial_key.encoded(is_leaf));
|
||||
let mut s = RlpStream::new_list(2);
|
||||
s.append(&partial_key.encoded(is_leaf));
|
||||
s.append(&value.to_vec()); // WTF?!?!
|
||||
//s.append(value); // <-- should be.
|
||||
let r = s.out();
|
||||
println!("output: -> {:?}", &r);
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
impl Trie for TrieDB {
|
||||
fn root(&self) -> &H256 { &self.root }
|
||||
|
||||
fn contains(_key: &[u8]) -> bool {
|
||||
fn contains(&self, _key: &[u8]) -> bool {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn at(_key: &[u8]) -> Option<&[u8]> {
|
||||
fn at(&self, _key: &[u8]) -> Option<&[u8]> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn insert(_key: &[u8], _value: &[u8]) {
|
||||
unimplemented!();
|
||||
fn insert(&mut self, key: &[u8], value: &[u8]) {
|
||||
(self as &mut TrieDB).add(&NibbleSlice::new(key), value);
|
||||
}
|
||||
|
||||
fn remove(_key: &[u8]) {
|
||||
fn remove(&mut self, _key: &[u8]) {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
fn playpen() {
|
||||
use overlaydb::*;
|
||||
use triehash::*;
|
||||
|
||||
(&[1, 2, 3]).starts_with(&[1, 2]);
|
||||
|
||||
@ -73,6 +141,7 @@ fn it_works() {
|
||||
t.init();
|
||||
assert_eq!(*t.root(), SHA3_NULL_RLP);
|
||||
assert!(t.is_empty());
|
||||
// TODO: make work:
|
||||
//assert_eq!(t.root(), SHA3_NULL_RLP);
|
||||
|
||||
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
|
||||
assert_eq!(*t.root(), hash256(&[ NibblePair::new_raw(vec![0x01u8, 0x23], vec![0x01u8, 0x23])]));
|
||||
}
|
Loading…
Reference in New Issue
Block a user