2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-03-11 13:50:39 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
//! `JournalDB` interface and implementation.
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2017-10-17 10:40:45 +02:00
|
|
|
extern crate heapsize;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
2017-10-16 16:12:54 +02:00
|
|
|
extern crate ethcore_bigint as bigint;
|
|
|
|
extern crate ethcore_bytes as bytes;
|
|
|
|
extern crate hashdb;
|
|
|
|
extern crate kvdb;
|
2017-10-17 10:40:45 +02:00
|
|
|
extern crate memorydb;
|
|
|
|
extern crate parking_lot;
|
|
|
|
extern crate rlp;
|
2017-10-16 16:12:54 +02:00
|
|
|
extern crate util_error as error;
|
2017-10-17 10:40:45 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
extern crate ethcore_logger;
|
|
|
|
#[cfg(test)]
|
2017-11-10 19:04:55 +01:00
|
|
|
extern crate keccak_hash as keccak;
|
2017-10-17 10:40:45 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate kvdb_memorydb;
|
2017-10-16 16:12:54 +02:00
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::{fmt, str};
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
/// Export the journaldb module.
|
2017-10-10 20:01:27 +02:00
|
|
|
mod traits;
|
2016-03-11 13:50:39 +01:00
|
|
|
mod archivedb;
|
2016-03-12 11:19:42 +01:00
|
|
|
mod earlymergedb;
|
|
|
|
mod overlayrecentdb;
|
2016-03-13 18:09:44 +01:00
|
|
|
mod refcounteddb;
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2017-10-16 16:12:54 +02:00
|
|
|
pub mod overlaydb;
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Export the `JournalDB` trait.
|
2016-03-11 13:50:39 +01:00
|
|
|
pub use self::traits::JournalDB;
|
|
|
|
|
2016-03-11 14:45:19 +01:00
|
|
|
/// A journal database algorithm.
|
2016-07-25 16:09:47 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
2016-03-11 14:45:19 +01:00
|
|
|
pub enum Algorithm {
|
|
|
|
/// Keep all keys forever.
|
|
|
|
Archive,
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2016-03-11 14:45:19 +01:00
|
|
|
/// Ancient and recent history maintained separately; recent history lasts for particular
|
|
|
|
/// number of blocks.
|
|
|
|
///
|
|
|
|
/// Inserts go into backing database, journal retains knowledge of whether backing DB key is
|
|
|
|
/// ancient or recent. Non-canon inserts get explicitly reverted and removed from backing DB.
|
|
|
|
EarlyMerge,
|
|
|
|
|
|
|
|
/// Ancient and recent history maintained separately; recent history lasts for particular
|
|
|
|
/// number of blocks.
|
|
|
|
///
|
|
|
|
/// Inserts go into memory overlay, which is tried for key fetches. Memory overlay gets
|
|
|
|
/// flushed in backing only at end of recent history.
|
|
|
|
OverlayRecent,
|
|
|
|
|
|
|
|
/// Ancient and recent history maintained separately; recent history lasts for particular
|
|
|
|
/// number of blocks.
|
|
|
|
///
|
|
|
|
/// References are counted in disk-backed DB.
|
|
|
|
RefCounted,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Algorithm {
|
2016-07-11 15:22:08 +02:00
|
|
|
fn default() -> Algorithm { Algorithm::OverlayRecent }
|
2016-03-11 14:45:19 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
impl str::FromStr for Algorithm {
|
2016-07-25 16:09:47 +02:00
|
|
|
type Err = String;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"archive" => Ok(Algorithm::Archive),
|
|
|
|
"light" => Ok(Algorithm::EarlyMerge),
|
|
|
|
"fast" => Ok(Algorithm::OverlayRecent),
|
|
|
|
"basic" => Ok(Algorithm::RefCounted),
|
|
|
|
e => Err(format!("Invalid algorithm: {}", e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Algorithm {
|
|
|
|
/// Returns static str describing journal database algorithm.
|
|
|
|
pub fn as_str(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
Algorithm::Archive => "archive",
|
|
|
|
Algorithm::EarlyMerge => "light",
|
|
|
|
Algorithm::OverlayRecent => "fast",
|
|
|
|
Algorithm::RefCounted => "basic",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:45:55 +02:00
|
|
|
/// Returns static str describing journal database algorithm.
|
|
|
|
pub fn as_internal_name_str(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
Algorithm::Archive => "archive",
|
|
|
|
Algorithm::EarlyMerge => "earlymerge",
|
|
|
|
Algorithm::OverlayRecent => "overlayrecent",
|
|
|
|
Algorithm::RefCounted => "refcounted",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
/// Returns true if pruning strategy is stable
|
|
|
|
pub fn is_stable(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Algorithm::Archive | Algorithm::OverlayRecent => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns all algorithm types.
|
|
|
|
pub fn all_types() -> Vec<Algorithm> {
|
|
|
|
vec![Algorithm::Archive, Algorithm::EarlyMerge, Algorithm::OverlayRecent, Algorithm::RefCounted]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 14:45:19 +01:00
|
|
|
impl fmt::Display for Algorithm {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-07-25 16:09:47 +02:00
|
|
|
write!(f, "{}", self.as_str())
|
2016-03-11 14:45:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 17:21:55 +01:00
|
|
|
/// Create a new `JournalDB` trait object over a generic key-value database.
|
|
|
|
pub fn new(backing: Arc<::kvdb::KeyValueDB>, algorithm: Algorithm, col: Option<u32>) -> Box<JournalDB> {
|
2016-03-11 14:45:19 +01:00
|
|
|
match algorithm {
|
2016-07-28 23:46:24 +02:00
|
|
|
Algorithm::Archive => Box::new(archivedb::ArchiveDB::new(backing, col)),
|
|
|
|
Algorithm::EarlyMerge => Box::new(earlymergedb::EarlyMergeDB::new(backing, col)),
|
|
|
|
Algorithm::OverlayRecent => Box::new(overlayrecentdb::OverlayRecentDB::new(backing, col)),
|
|
|
|
Algorithm::RefCounted => Box::new(refcounteddb::RefCountedDB::new(backing, col)),
|
2016-03-11 14:45:19 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:58:28 +02:00
|
|
|
|
|
|
|
// all keys must be at least 12 bytes
|
2017-04-19 14:58:19 +02:00
|
|
|
const DB_PREFIX_LEN : usize = ::kvdb::PREFIX_LEN;
|
|
|
|
const LATEST_ERA_KEY : [u8; ::kvdb::PREFIX_LEN] = [ b'l', b'a', b's', b't', 0, 0, 0, 0, 0, 0, 0, 0 ];
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Algorithm;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_journal_algorithm_parsing() {
|
|
|
|
assert_eq!(Algorithm::Archive, "archive".parse().unwrap());
|
|
|
|
assert_eq!(Algorithm::EarlyMerge, "light".parse().unwrap());
|
|
|
|
assert_eq!(Algorithm::OverlayRecent, "fast".parse().unwrap());
|
|
|
|
assert_eq!(Algorithm::RefCounted, "basic".parse().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_journal_algorithm_printing() {
|
|
|
|
assert_eq!(Algorithm::Archive.to_string(), "archive".to_owned());
|
|
|
|
assert_eq!(Algorithm::EarlyMerge.to_string(), "light".to_owned());
|
|
|
|
assert_eq!(Algorithm::OverlayRecent.to_string(), "fast".to_owned());
|
|
|
|
assert_eq!(Algorithm::RefCounted.to_string(), "basic".to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_journal_algorithm_is_stable() {
|
|
|
|
assert!(Algorithm::Archive.is_stable());
|
|
|
|
assert!(Algorithm::OverlayRecent.is_stable());
|
|
|
|
assert!(!Algorithm::EarlyMerge.is_stable());
|
|
|
|
assert!(!Algorithm::RefCounted.is_stable());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_journal_algorithm_default() {
|
|
|
|
assert_eq!(Algorithm::default(), Algorithm::OverlayRecent);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_journal_algorithm_all_types() {
|
|
|
|
// compiling should fail if some cases are not covered
|
|
|
|
let mut archive = 0;
|
|
|
|
let mut earlymerge = 0;
|
|
|
|
let mut overlayrecent = 0;
|
|
|
|
let mut refcounted = 0;
|
|
|
|
|
|
|
|
for a in &Algorithm::all_types() {
|
|
|
|
match *a {
|
|
|
|
Algorithm::Archive => archive += 1,
|
|
|
|
Algorithm::EarlyMerge => earlymerge += 1,
|
|
|
|
Algorithm::OverlayRecent => overlayrecent += 1,
|
|
|
|
Algorithm::RefCounted => refcounted += 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(archive, 1);
|
|
|
|
assert_eq!(earlymerge, 1);
|
|
|
|
assert_eq!(overlayrecent, 1);
|
|
|
|
assert_eq!(refcounted, 1);
|
|
|
|
}
|
2017-02-20 17:21:55 +01:00
|
|
|
}
|