Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions ethcore/snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,8 @@ impl StateRebuilder {
}
}

let backing = self.db.backing().clone();
let mut batch = backing.transaction();
// Drain the transaction overlay and put the data into the batch.
self.db.inject(&mut batch)?;
backing.write_buffered(batch);

let batch = self.db.drain_transaction_overlay()?;
self.db.backing().write(batch)?;
Ok(())
}

Expand Down
15 changes: 6 additions & 9 deletions util/journaldb/src/archivedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ impl JournalDB for ArchiveDB {
Ok(0)
}

fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
let mut inserts = 0usize;
let mut deletes = 0usize;
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction> {
let mut batch = DBTransaction::new();

for i in self.overlay.drain() {
let (key, (value, rc)) = i;
Expand All @@ -153,19 +152,17 @@ impl JournalDB for ArchiveDB {
return Err(error_key_already_exists(&key));
}
batch.put(self.column, key.as_bytes(), &value);
inserts += 1;
}
if rc < 0 {
assert!(rc == -1);
if self.backing.get(self.column, key.as_bytes())?.is_none() {
return Err(error_negatively_reference_hash(&key));
}
batch.delete(self.column, key.as_bytes());
deletes += 1;
}
}

Ok((inserts + deletes) as u32)
Ok(batch)
}

fn latest_era(&self) -> Option<u64> { self.latest_era }
Expand Down Expand Up @@ -209,7 +206,7 @@ mod tests {
use hash_db::{HashDB, EMPTY_PREFIX};
use super::*;
use kvdb_memorydb;
use crate::{JournalDB, inject_batch, commit_batch};
use crate::{JournalDB, drain_overlay, commit_batch};

#[test]
fn insert_same_in_fork() {
Expand Down Expand Up @@ -463,11 +460,11 @@ mod tests {
fn inject() {
let mut jdb = ArchiveDB::new(Arc::new(kvdb_memorydb::create(1)), 0);
let key = jdb.insert(EMPTY_PREFIX, b"dog");
inject_batch(&mut jdb).unwrap();
drain_overlay(&mut jdb).unwrap();

assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), b"dog".to_vec());
jdb.remove(&key, EMPTY_PREFIX);
inject_batch(&mut jdb).unwrap();
drain_overlay(&mut jdb).unwrap();

assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
}
Expand Down
14 changes: 6 additions & 8 deletions util/journaldb/src/earlymergedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,9 @@ impl JournalDB for EarlyMergeDB {
Ok(0)
}

fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
let mut ops = 0;
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction> {
let mut batch = DBTransaction::new();
for (key, (value, rc)) in self.overlay.drain() {
if rc != 0 { ops += 1 }

match rc {
0 => {}
1 => {
Expand All @@ -497,7 +495,7 @@ impl JournalDB for EarlyMergeDB {
}
}

Ok(ops)
Ok(batch)
}

fn consolidate(&mut self, with: super::MemoryDB) {
Expand Down Expand Up @@ -529,7 +527,7 @@ mod tests {
use hash_db::{HashDB, EMPTY_PREFIX};
use super::*;
use kvdb_memorydb;
use crate::{inject_batch, commit_batch};
use crate::{drain_overlay, commit_batch};

#[test]
fn insert_same_in_fork() {
Expand Down Expand Up @@ -1050,11 +1048,11 @@ mod tests {
fn inject() {
let mut jdb = new_db();
let key = jdb.insert(EMPTY_PREFIX, b"dog");
inject_batch(&mut jdb).unwrap();
drain_overlay(&mut jdb).unwrap();

assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), b"dog".to_vec());
jdb.remove(&key, EMPTY_PREFIX);
inject_batch(&mut jdb).unwrap();
drain_overlay(&mut jdb).unwrap();

assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
}
Expand Down
16 changes: 6 additions & 10 deletions util/journaldb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ pub trait JournalDB: HashDB<KeccakHasher, DBValue> {
/// Commit all queued insert and delete operations without affecting any journalling -- this requires that all insertions
/// and deletions are indeed canonical and will likely lead to an invalid database if that assumption is violated.
///
/// Any keys or values inserted or deleted must be completely independent of those affected
/// by any previous `commit` operations. Essentially, this means that `inject` can be used
/// either to restore a state to a fresh database, or to insert data which may only be journalled
/// from this point onwards.
fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32>;
/// Returns a transaction to be committed.
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction>;

/// State data query
fn state(&self, _id: &H256) -> Option<Bytes>;
Expand Down Expand Up @@ -213,12 +210,11 @@ pub fn new_memory_db() -> MemoryDB {
MemoryDB::from_null_node(&rlp::NULL_RLP, rlp::NULL_RLP.as_ref().into())
}

#[cfg(test)]
/// Inject all changes in a single batch.
pub fn inject_batch(jdb: &mut dyn JournalDB) -> io::Result<u32> {
let mut batch = jdb.backing().transaction();
let res = jdb.inject(&mut batch)?;
jdb.backing().write(batch).map(|_| res).map_err(Into::into)
#[cfg(test)]
pub fn drain_overlay(jdb: &mut dyn JournalDB) -> io::Result<()> {
let batch = jdb.drain_transaction_overlay()?;
jdb.backing().write(batch).map_err(Into::into)
}

/// Commit all changes in a single batch
Expand Down
14 changes: 6 additions & 8 deletions util/journaldb/src/overlayrecentdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,9 @@ impl JournalDB for OverlayRecentDB {
Ok(ops as u32)
}

fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
let mut ops = 0;
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction> {
let mut batch = DBTransaction::new();
for (key, (value, rc)) in self.transaction_overlay.drain() {
if rc != 0 { ops += 1 }

match rc {
0 => {}
_ if rc > 0 => {
Expand All @@ -417,7 +415,7 @@ impl JournalDB for OverlayRecentDB {
}
}

Ok(ops)
Ok(batch)
}

fn state(&self, key: &H256) -> Option<Bytes> {
Expand Down Expand Up @@ -507,7 +505,7 @@ mod tests {
use super::*;
use hash_db::{HashDB, EMPTY_PREFIX};
use kvdb_memorydb;
use crate::{JournalDB, inject_batch, commit_batch};
use crate::{JournalDB, drain_overlay, commit_batch};

fn new_db() -> OverlayRecentDB {
let backing = Arc::new(kvdb_memorydb::create(1));
Expand Down Expand Up @@ -1026,11 +1024,11 @@ mod tests {
fn inject() {
let mut jdb = new_db();
let key = jdb.insert(EMPTY_PREFIX, b"dog");
inject_batch(&mut jdb).unwrap();
drain_overlay(&mut jdb).unwrap();

assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), b"dog".to_vec());
jdb.remove(&key, EMPTY_PREFIX);
inject_batch(&mut jdb).unwrap();
drain_overlay(&mut jdb).unwrap();

assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
}
Expand Down
11 changes: 6 additions & 5 deletions util/journaldb/src/refcounteddb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ impl JournalDB for RefCountedDB {
Ok(r)
}

fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
fn drain_transaction_overlay(&mut self) -> io::Result<DBTransaction> {
self.inserts.clear();
for remove in self.removes.drain(..) {
self.forward.remove(&remove, EMPTY_PREFIX);
}
self.forward.commit_to_batch(batch)
let mut batch = DBTransaction::new();
self.forward.commit_to_batch(&mut batch).map(|_| batch)
}

fn consolidate(&mut self, mut with: super::MemoryDB) {
Expand All @@ -224,7 +225,7 @@ mod tests {
use hash_db::{HashDB, EMPTY_PREFIX};
use super::*;
use kvdb_memorydb;
use crate::{JournalDB, inject_batch, commit_batch};
use crate::{JournalDB, drain_overlay, commit_batch};

fn new_db() -> RefCountedDB {
let backing = Arc::new(kvdb_memorydb::create(1));
Expand Down Expand Up @@ -338,11 +339,11 @@ mod tests {
fn inject() {
let mut jdb = new_db();
let key = jdb.insert(EMPTY_PREFIX, b"dog");
inject_batch(&mut jdb).unwrap();
drain_overlay(&mut jdb).unwrap();

assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), b"dog".to_vec());
jdb.remove(&key, EMPTY_PREFIX);
inject_batch(&mut jdb).unwrap();
drain_overlay(&mut jdb).unwrap();

assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
}
Expand Down