From 358962d2ea5f17a1e8f89adaad07501b760d6b1a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 28 Nov 2015 19:28:59 +0100 Subject: [PATCH] Implemented, in principle. --- src/error.rs | 5 ++--- src/lib.rs | 1 - src/overlaydb.rs | 41 ++++++++++++++++++++++------------------- src/rlp.rs | 8 ++++---- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/error.rs b/src/error.rs index bb09e2e1b..f6c64a54f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,3 @@ -#![feature(concat_idents)] - use rustc_serialize::hex::*; #[derive(Debug)] @@ -27,7 +25,8 @@ impl From for EthcoreError { } // TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted. -/*macro_rules! assimilate { +/*#![feature(concat_idents)] +macro_rules! assimilate { ($name:ident) => ( impl From for EthcoreError { fn from(err: concat_idents!($name, Error)) -> EthcoreError { diff --git a/src/lib.rs b/src/lib.rs index 1c8ad9ccc..e918c7ca9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,6 @@ pub mod sha3; pub mod hashdb; pub mod memorydb; pub mod overlaydb; -pub mod bloom; pub mod math; //pub mod network; diff --git a/src/overlaydb.rs b/src/overlaydb.rs index c59be085a..d2f838bb7 100644 --- a/src/overlaydb.rs +++ b/src/overlaydb.rs @@ -3,7 +3,6 @@ use error::*; use hash::*; use bytes::*; -use sha3::*; use rlp::*; use hashdb::*; use memorydb::*; @@ -19,26 +18,30 @@ pub struct OverlayDB { impl OverlayDB { /// Create a new instance of OverlayDB given a `backing` database. - fn new(backing: DB) -> OverlayDB { + pub fn new(backing: DB) -> OverlayDB { OverlayDB{ overlay: MemoryDB::new(), backing: Arc::new(backing) } } /// Commit all memory operations to the backing database. - fn commit(&mut self) -> Result { + pub fn commit(&mut self) -> Result { let mut ret = 0u32; for i in self.overlay.drain().into_iter() { let (key, (value, rc)) = i; if rc != 0 { - let new_entry = match self.payload(&key) { + match self.payload(&key) { Some(x) => { let (back_value, back_rc) = x; - if back_rc + rc < 0 { + let total_rc: i32 = back_rc as i32 + rc; + if total_rc < 0 { return Err(From::from(BaseDataError::NegativelyReferencedHash)); } - self.put_payload(&key, (back_value, rc + back_rc)); + self.put_payload(&key, (back_value, total_rc as u32)); } None => { - self.put_payload(&key, (value, rc)); + if rc < 0 { + return Err(From::from(BaseDataError::NegativelyReferencedHash)); + } + self.put_payload(&key, (value, rc as u32)); } }; ret += 1; @@ -48,21 +51,21 @@ impl OverlayDB { } /// Get the refs and value of the given key. - fn payload(&self, key: &H256) -> Option<(Bytes, i32)> { - db.get(&key.bytes()) + fn payload(&self, key: &H256) -> Option<(Bytes, u32)> { + self.backing.get(&key.bytes()) .expect("Low-level database error. Some issue with your hard disk?") .map(|d| { - Rlp r(d.deref()); - r(Bytes, i32) + let r = Rlp::new(d.deref()); + (Bytes::decode(&r.at(1).unwrap()).unwrap(), u32::decode(&r.at(0).unwrap()).unwrap()) }) } /// Get the refs and value of the given key. - fn put_payload(&self, key: &H256, payload: (Bytes, i32)) { + fn put_payload(&self, key: &H256, payload: (Bytes, u32)) { let mut s = RlpStream::new_list(2); - s.append(payload.1); - s.append(payload.0); - backing.put(&key.bytes(), &s.out().unwrap()); + s.append(&payload.1); + s.append(&payload.0); + self.backing.put(&key.bytes(), &s.out().unwrap()).expect("Low-level database error. Some issue with your hard disk?"); } } @@ -78,7 +81,7 @@ impl HashDB for OverlayDB { match self.payload(key) { Some(x) => { let (d, rc) = x; - if rc + memrc > 0 { + if rc as i32 + memrc > 0 { Some(d) } else { @@ -97,13 +100,13 @@ impl HashDB for OverlayDB { // it positive again. let k = self.overlay.raw(key); match k { - Some(&(ref d, rc)) if rc > 0 => true, + Some(&(_, rc)) if rc > 0 => true, _ => { let memrc = k.map(|&(_, rc)| rc).unwrap_or(0); match self.payload(key) { Some(x) => { - let (d, rc) = x; - if rc + memrc > 0 { + let (_, rc) = x; + if rc as i32 + memrc > 0 { true } else { diff --git a/src/rlp.rs b/src/rlp.rs index 4b6df2c6e..53860579b 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -50,10 +50,10 @@ //! } //! //! fn decode_list() { -//! // ["cat", "dog"] -//! let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; -//! let rlp = Rlp::new(&data); -//! let _ : Vec = Decodable::decode(&rlp).unwrap(); +//! // ["cat", "dog"] +//! let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; +//! let rlp = Rlp::new(&data); +//! let _ : Vec = Decodable::decode(&rlp).unwrap(); //! } //! //! fn decode_list2() {