Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

journaldb changes #10929

Merged
merged 7 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
journaldb trait moved to the lib.rs file
  • Loading branch information
debris committed Jul 30, 2019
commit 9c7b5f0d3bda7fd3bb178e6ffdea1563e4ce68aa
76 changes: 68 additions & 8 deletions util/journaldb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,84 @@ extern crate parity_util_mem as malloc_size_of;

extern crate parity_bytes as bytes;

use std::{fmt, str, io};
use std::sync::Arc;
#[cfg(test)]
use std::{
fmt, str, io,
sync::Arc,
collections::HashMap,
};

use bytes::Bytes;
use ethereum_types::H256;
use hash_db::HashDB;
use keccak_hasher::KeccakHasher;
use kvdb::{self, DBTransaction, DBValue};

/// Export the journaldb module.
mod traits;
mod archivedb;
mod earlymergedb;
mod overlayrecentdb;
mod refcounteddb;
mod util;
mod as_hash_db_impls;
mod overlaydb;

/// A `HashDB` which can manage a short-term journal potentially containing many forks of mutually
/// exclusive actions.
pub trait JournalDB: HashDB<KeccakHasher, DBValue> {
/// Return a copy of ourself, in a box.
fn boxed_clone(&self) -> Box<dyn JournalDB>;

/// Returns heap memory size used
fn mem_used(&self) -> usize;

/// Returns the size of journalled state in memory.
/// This function has a considerable speed requirement --
/// it must be fast enough to call several times per block imported.
fn journal_size(&self) -> usize { 0 }

/// Check if this database has any commits
fn is_empty(&self) -> bool;

/// Get the earliest era in the DB. None if there isn't yet any data in there.
fn earliest_era(&self) -> Option<u64> { None }

/// Get the latest era in the DB. None if there isn't yet any data in there.
fn latest_era(&self) -> Option<u64>;

/// Journal recent database operations as being associated with a given era and id.
// TODO: give the overlay to this function so journaldbs don't manage the overlays themselves.
fn journal_under(&mut self, batch: &mut DBTransaction, now: u64, id: &H256) -> io::Result<u32>;

pub mod overlaydb;
/// Mark a given block as canonical, indicating that competing blocks' states may be pruned out.
fn mark_canonical(&mut self, batch: &mut DBTransaction, era: u64, id: &H256) -> io::Result<u32>;

/// Export the `JournalDB` trait.
pub use self::traits::JournalDB;
/// 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>;

/// State data query
fn state(&self, _id: &H256) -> Option<Bytes>;

/// Whether this database is pruned.
fn is_prunable(&self) -> bool { true }

/// Get backing database.
fn backing(&self) -> &Arc<dyn kvdb::KeyValueDB>;

/// Clear internal strucutres. This should called after changes have been written
/// to the backing strage
fn flush(&self) {}

/// Consolidate all the insertions and deletions in the given memory overlay.
fn consolidate(&mut self, overlay: MemoryDB);

/// Primarily use for tests, highly inefficient.
fn keys(&self) -> HashMap<H256, i32>;
}

/// Alias to ethereum MemoryDB
type MemoryDB = memory_db::MemoryDB<
Expand Down
7 changes: 3 additions & 4 deletions util/journaldb/src/overlaydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ impl OverlayDB {

/// Revert all operations on this object (i.e. `insert()`s and `remove()`s) since the
/// last `commit()`.
pub fn revert(&mut self) { self.overlay.clear(); }

/// Get the number of references that would be committed.
pub fn commit_refs(&self, key: &H256) -> i32 { self.overlay.raw(key, EMPTY_PREFIX).map_or(0, |(_, refs)| refs) }
pub fn revert(&mut self) {
self.overlay.clear();
}

/// Get the refs and value of the given key.
fn payload(&self, key: &H256) -> Option<Payload> {
Expand Down
86 changes: 0 additions & 86 deletions util/journaldb/src/traits.rs

This file was deleted.