diff --git a/src/hashdb.rs b/src/hashdb.rs index 48f5182bc..561c7843d 100644 --- a/src/hashdb.rs +++ b/src/hashdb.rs @@ -1,5 +1,4 @@ use hash::*; -use bytes::Bytes; pub trait HashDB { /// Look up a given hash into the bytes that hash to it, returning None if the @@ -17,7 +16,7 @@ pub trait HashDB { /// assert_eq!(m.lookup(&hash).unwrap(), hello_bytes); /// } /// ``` - fn lookup(&self, key: &H256) -> Option; + fn lookup(&self, key: &H256) -> Option<&[u8]>; /// Check for the existance of a hash-key. /// diff --git a/src/lib.rs b/src/lib.rs index 098e42928..983dc322c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,6 +34,7 @@ pub mod math; pub mod chainfilter; pub mod crypto; pub mod triehash; +pub mod trie; //pub mod network; diff --git a/src/memorydb.rs b/src/memorydb.rs index cbb9e426b..4ec02bd5e 100644 --- a/src/memorydb.rs +++ b/src/memorydb.rs @@ -100,12 +100,22 @@ impl MemoryDB { mem::swap(&mut self.data, &mut data); data } + + pub fn denote(&self, key: &H256, value: Bytes) -> &(Bytes, i32) { + if self.data.get(&key) == None { + unsafe { + let p = &self.data as *const HashMap as *mut HashMap; + (*p).insert(key.clone(), (value, 0)); + } + } + self.data.get(key).unwrap() + } } impl HashDB for MemoryDB { - fn lookup(&self, key: &H256) -> Option { + fn lookup(&self, key: &H256) -> Option<&[u8]> { match self.data.get(key) { - Some(&(ref d, rc)) if rc > 0 => Some(d.clone()), + Some(&(ref d, rc)) if rc > 0 => Some(d), _ => None } } @@ -143,3 +153,20 @@ impl HashDB for MemoryDB { } } +#[test] +fn memorydb_denote() { + let mut m = MemoryDB::new(); + let hello_bytes = b"Hello world!"; + let hash = m.insert(hello_bytes); + assert_eq!(m.lookup(&hash).unwrap(), b"Hello world!"); + + for _ in 0..1000 { + let r = H256::random(); + let k = r.sha3(); + let &(ref v, ref rc) = m.denote(&k, r.bytes().to_vec()); + assert_eq!(v, &r.bytes()); + assert_eq!(*rc, 0); + } + + assert_eq!(m.lookup(&hash).unwrap(), b"Hello world!"); +} \ No newline at end of file diff --git a/src/overlaydb.rs b/src/overlaydb.rs index 0f6ae3d77..b6f2b3eb2 100644 --- a/src/overlaydb.rs +++ b/src/overlaydb.rs @@ -135,19 +135,19 @@ impl OverlayDB { } impl HashDB for OverlayDB { - fn lookup(&self, key: &H256) -> Option { + fn lookup(&self, key: &H256) -> Option<&[u8]> { // return ok if positive; if negative, check backing - might be enough references there to make // it positive again. let k = self.overlay.raw(key); match k { - Some(&(ref d, rc)) if rc > 0 => Some(d.clone()), + Some(&(ref d, rc)) if rc > 0 => Some(d), _ => { let memrc = k.map(|&(_, rc)| rc).unwrap_or(0); match self.payload(key) { Some(x) => { let (d, rc) = x; if rc as i32 + memrc > 0 { - Some(d) + Some(&self.overlay.denote(key, d).0) } else { None @@ -193,7 +193,7 @@ impl HashDB for OverlayDB { fn overlaydb_overlay_insert_and_kill() { let mut trie = OverlayDB::new_temp(); let h = trie.insert(b"hello world"); - assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec())); + assert_eq!(trie.lookup(&h).unwrap(), b"hello world"); trie.kill(&h); assert_eq!(trie.lookup(&h), None); } @@ -202,11 +202,11 @@ fn overlaydb_overlay_insert_and_kill() { fn overlaydb_backing_insert_revert() { let mut trie = OverlayDB::new_temp(); let h = trie.insert(b"hello world"); - assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec())); + assert_eq!(trie.lookup(&h).unwrap(), b"hello world"); trie.commit().unwrap(); - assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec())); + assert_eq!(trie.lookup(&h).unwrap(), b"hello world"); trie.revert(); - assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec())); + assert_eq!(trie.lookup(&h).unwrap(), b"hello world"); } #[test] @@ -230,7 +230,7 @@ fn overlaydb_backing_kill_revert() { trie.kill(&h); assert_eq!(trie.lookup(&h), None); trie.revert(); - assert_eq!(trie.lookup(&h), Some(b"hello world".to_vec())); + assert_eq!(trie.lookup(&h).unwrap(), b"hello world"); } #[test] @@ -248,29 +248,29 @@ fn overlaydb_negative() { fn overlaydb_complex() { let mut trie = OverlayDB::new_temp(); let hfoo = trie.insert(b"foo"); - assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec())); + assert_eq!(trie.lookup(&hfoo).unwrap(), b"foo"); let hbar = trie.insert(b"bar"); - assert_eq!(trie.lookup(&hbar), Some(b"bar".to_vec())); + assert_eq!(trie.lookup(&hbar).unwrap(), b"bar"); trie.commit().unwrap(); - assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec())); - assert_eq!(trie.lookup(&hbar), Some(b"bar".to_vec())); + assert_eq!(trie.lookup(&hfoo).unwrap(), b"foo"); + assert_eq!(trie.lookup(&hbar).unwrap(), b"bar"); trie.insert(b"foo"); // two refs - assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec())); + assert_eq!(trie.lookup(&hfoo).unwrap(), b"foo"); trie.commit().unwrap(); - assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec())); - assert_eq!(trie.lookup(&hbar), Some(b"bar".to_vec())); + assert_eq!(trie.lookup(&hfoo).unwrap(), b"foo"); + assert_eq!(trie.lookup(&hbar).unwrap(), b"bar"); trie.kill(&hbar); // zero refs - delete assert_eq!(trie.lookup(&hbar), None); trie.kill(&hfoo); // one ref - keep - assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec())); + assert_eq!(trie.lookup(&hfoo).unwrap(), b"foo"); trie.commit().unwrap(); - assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec())); + assert_eq!(trie.lookup(&hfoo).unwrap(), b"foo"); trie.kill(&hfoo); // zero ref - would delete, but... assert_eq!(trie.lookup(&hfoo), None); trie.insert(b"foo"); // one ref - keep after all. - assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec())); + assert_eq!(trie.lookup(&hfoo).unwrap(), b"foo"); trie.commit().unwrap(); - assert_eq!(trie.lookup(&hfoo), Some(b"foo".to_vec())); + assert_eq!(trie.lookup(&hfoo).unwrap(), b"foo"); trie.kill(&hfoo); // zero ref - delete assert_eq!(trie.lookup(&hfoo), None); trie.commit().unwrap(); // diff --git a/src/trie.rs b/src/trie.rs new file mode 100644 index 000000000..889c9b5dc --- /dev/null +++ b/src/trie.rs @@ -0,0 +1,78 @@ +use memorydb::*; +use hashdb::*; +use hash::*; +//use rlp::*; +//use bytes::*; + +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] ); + +/*lazy_static! { + pub static ref NULL_RLP: Bytes = { let mut r = RlpStream::new(); r.append(&""); r.out().unwrap() }; + pub static ref SHA3_NULL_RLP: H256 = { use sha3::Hashable; NULL_RLP.sha3() }; +}*/ + +pub trait Trie { + fn root(&self) -> &H256; + 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]); +} + +pub struct TrieDB { + db: Box, + root: H256, +} + +impl TrieDB { + pub fn new(db: T) -> Self where T: HashDB + 'static { TrieDB{ db: Box::new(db), root: H256::new() } } + + pub fn new_boxed(db_box: Box) -> Self { TrieDB{ db: db_box, root: H256::new() } } + + 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 db(&self) -> &HashDB { self.db.as_ref() } + + fn insert_root(&mut self, root_data: &[u8]) { self.root = self.db.insert(root_data); } + +} + +impl Trie for TrieDB { + fn root(&self) -> &H256 { &self.root } + + fn contains(_key: &[u8]) -> bool { + unimplemented!(); + } + + fn at(_key: &[u8]) -> Option<&[u8]> { + unimplemented!(); + } + + fn insert(_key: &[u8], _value: &[u8]) { + unimplemented!(); + } + + fn remove(_key: &[u8]) { + unimplemented!(); + } +} + +#[test] +fn it_works() { + use overlaydb::*; + + (&[1, 2, 3]).starts_with(&[1, 2]); + + let mut t = TrieDB::new(OverlayDB::new_temp()); + t.init(); + assert_eq!(*t.root(), SHA3_NULL_RLP); + assert!(t.is_empty()); + // TODO: make work: + //assert_eq!(t.root(), SHA3_NULL_RLP); +} \ No newline at end of file