Implemented, in principle.

This commit is contained in:
Gav Wood 2015-11-28 19:28:59 +01:00
parent 5c57523dd9
commit 358962d2ea
4 changed files with 28 additions and 27 deletions

View File

@ -1,5 +1,3 @@
#![feature(concat_idents)]
use rustc_serialize::hex::*; use rustc_serialize::hex::*;
#[derive(Debug)] #[derive(Debug)]
@ -27,7 +25,8 @@ impl From<BaseDataError> for EthcoreError {
} }
// TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted. // TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted.
/*macro_rules! assimilate { /*#![feature(concat_idents)]
macro_rules! assimilate {
($name:ident) => ( ($name:ident) => (
impl From<concat_idents!($name, Error)> for EthcoreError { impl From<concat_idents!($name, Error)> for EthcoreError {
fn from(err: concat_idents!($name, Error)) -> EthcoreError { fn from(err: concat_idents!($name, Error)) -> EthcoreError {

View File

@ -21,7 +21,6 @@ pub mod sha3;
pub mod hashdb; pub mod hashdb;
pub mod memorydb; pub mod memorydb;
pub mod overlaydb; pub mod overlaydb;
pub mod bloom;
pub mod math; pub mod math;
//pub mod network; //pub mod network;

View File

@ -3,7 +3,6 @@
use error::*; use error::*;
use hash::*; use hash::*;
use bytes::*; use bytes::*;
use sha3::*;
use rlp::*; use rlp::*;
use hashdb::*; use hashdb::*;
use memorydb::*; use memorydb::*;
@ -19,26 +18,30 @@ pub struct OverlayDB {
impl OverlayDB { impl OverlayDB {
/// Create a new instance of OverlayDB given a `backing` database. /// 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) } OverlayDB{ overlay: MemoryDB::new(), backing: Arc::new(backing) }
} }
/// Commit all memory operations to the backing database. /// Commit all memory operations to the backing database.
fn commit(&mut self) -> Result<u32, EthcoreError> { pub fn commit(&mut self) -> Result<u32, EthcoreError> {
let mut ret = 0u32; let mut ret = 0u32;
for i in self.overlay.drain().into_iter() { for i in self.overlay.drain().into_iter() {
let (key, (value, rc)) = i; let (key, (value, rc)) = i;
if rc != 0 { if rc != 0 {
let new_entry = match self.payload(&key) { match self.payload(&key) {
Some(x) => { Some(x) => {
let (back_value, back_rc) = 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)); 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 => { 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; ret += 1;
@ -48,21 +51,21 @@ impl OverlayDB {
} }
/// Get the refs and value of the given key. /// Get the refs and value of the given key.
fn payload(&self, key: &H256) -> Option<(Bytes, i32)> { fn payload(&self, key: &H256) -> Option<(Bytes, u32)> {
db.get(&key.bytes()) self.backing.get(&key.bytes())
.expect("Low-level database error. Some issue with your hard disk?") .expect("Low-level database error. Some issue with your hard disk?")
.map(|d| { .map(|d| {
Rlp r(d.deref()); let r = Rlp::new(d.deref());
r(Bytes, i32) (Bytes::decode(&r.at(1).unwrap()).unwrap(), u32::decode(&r.at(0).unwrap()).unwrap())
}) })
} }
/// Get the refs and value of the given key. /// 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); let mut s = RlpStream::new_list(2);
s.append(payload.1); s.append(&payload.1);
s.append(payload.0); s.append(&payload.0);
backing.put(&key.bytes(), &s.out().unwrap()); 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) { match self.payload(key) {
Some(x) => { Some(x) => {
let (d, rc) = x; let (d, rc) = x;
if rc + memrc > 0 { if rc as i32 + memrc > 0 {
Some(d) Some(d)
} }
else { else {
@ -97,13 +100,13 @@ impl HashDB for OverlayDB {
// it positive again. // it positive again.
let k = self.overlay.raw(key); let k = self.overlay.raw(key);
match k { 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); let memrc = k.map(|&(_, rc)| rc).unwrap_or(0);
match self.payload(key) { match self.payload(key) {
Some(x) => { Some(x) => {
let (d, rc) = x; let (_, rc) = x;
if rc + memrc > 0 { if rc as i32 + memrc > 0 {
true true
} }
else { else {