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
|
|
|
//! Disk-backed `HashDB` implementation.
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2016-03-11 13:50:39 +01:00
|
|
|
use hashdb::*;
|
2017-02-20 17:21:55 +01:00
|
|
|
use kvdb::{self, DBTransaction};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H256;
|
2017-10-10 20:01:27 +02:00
|
|
|
use error::UtilError;
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::Bytes;
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// A `HashDB` which can manage a short-term journal potentially containing many forks of mutually
|
2016-03-11 13:50:39 +01:00
|
|
|
/// exclusive actions.
|
2016-08-03 18:35:48 +02:00
|
|
|
pub trait JournalDB: HashDB {
|
2016-03-11 13:50:39 +01:00
|
|
|
/// Return a copy of ourself, in a box.
|
2016-03-28 09:42:50 +02:00
|
|
|
fn boxed_clone(&self) -> Box<JournalDB>;
|
2016-03-11 13:50:39 +01:00
|
|
|
|
|
|
|
/// Returns heap memory size used
|
|
|
|
fn mem_used(&self) -> usize;
|
|
|
|
|
2017-01-20 13:25:53 +01:00
|
|
|
/// 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 }
|
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
/// Check if this database has any commits
|
|
|
|
fn is_empty(&self) -> bool;
|
|
|
|
|
2016-10-14 14:44:56 +02:00
|
|
|
/// Get the earliest era in the DB. None if there isn't yet any data in there.
|
|
|
|
fn earliest_era(&self) -> Option<u64> { None }
|
|
|
|
|
2016-04-12 00:51:14 +02:00
|
|
|
/// Get the latest era in the DB. None if there isn't yet any data in there.
|
2016-04-12 03:42:50 +02:00
|
|
|
fn latest_era(&self) -> Option<u64>;
|
2016-04-12 00:51:14 +02:00
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
/// 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 themeselves.
|
|
|
|
fn journal_under(&mut self, batch: &mut DBTransaction, now: u64, id: &H256) -> Result<u32, UtilError>;
|
|
|
|
|
|
|
|
/// 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) -> Result<u32, UtilError>;
|
2016-03-11 19:15:56 +01:00
|
|
|
|
2016-08-03 16:34:32 +02:00
|
|
|
/// 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.
|
2016-08-25 16:43:56 +02:00
|
|
|
fn inject(&mut self, batch: &mut DBTransaction) -> Result<u32, UtilError>;
|
2016-08-03 16:34:32 +02:00
|
|
|
|
2016-03-11 19:15:56 +01:00
|
|
|
/// State data query
|
2016-07-11 12:34:29 +02:00
|
|
|
fn state(&self, _id: &H256) -> Option<Bytes>;
|
2016-06-02 20:34:38 +02:00
|
|
|
|
2016-06-03 12:10:10 +02:00
|
|
|
/// Whether this database is pruned.
|
|
|
|
fn is_pruned(&self) -> bool { true }
|
2016-07-28 23:46:24 +02:00
|
|
|
|
|
|
|
/// Get backing database.
|
2017-02-20 17:21:55 +01:00
|
|
|
fn backing(&self) -> &Arc<kvdb::KeyValueDB>;
|
2016-07-28 23:46:24 +02:00
|
|
|
|
2016-08-11 18:58:11 +02:00
|
|
|
/// Clear internal strucutres. This should called after changes have been written
|
|
|
|
/// to the backing strage
|
|
|
|
fn flush(&self) {}
|
|
|
|
|
2016-08-25 14:28:45 +02:00
|
|
|
/// Consolidate all the insertions and deletions in the given memory overlay.
|
|
|
|
fn consolidate(&mut self, overlay: ::memorydb::MemoryDB);
|
|
|
|
|
2016-07-28 23:46:24 +02:00
|
|
|
/// Commit all changes in a single batch
|
2016-08-03 16:34:32 +02:00
|
|
|
#[cfg(test)]
|
2016-07-28 23:46:24 +02:00
|
|
|
fn commit_batch(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result<u32, UtilError> {
|
2016-08-25 16:43:56 +02:00
|
|
|
let mut batch = self.backing().transaction();
|
2016-12-27 12:53:56 +01:00
|
|
|
let mut ops = self.journal_under(&mut batch, now, id)?;
|
2016-09-26 17:14:44 +02:00
|
|
|
|
|
|
|
if let Some((end_era, canon_id)) = end {
|
2016-12-27 12:53:56 +01:00
|
|
|
ops += self.mark_canonical(&mut batch, end_era, &canon_id)?;
|
2016-09-26 17:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = self.backing().write(batch).map(|_| ops).map_err(Into::into);
|
2016-08-11 18:58:11 +02:00
|
|
|
self.flush();
|
|
|
|
result
|
2016-07-28 23:46:24 +02:00
|
|
|
}
|
2016-08-03 16:34:32 +02:00
|
|
|
|
|
|
|
/// Inject all changes in a single batch.
|
|
|
|
#[cfg(test)]
|
|
|
|
fn inject_batch(&mut self) -> Result<u32, UtilError> {
|
2016-08-25 16:43:56 +02:00
|
|
|
let mut batch = self.backing().transaction();
|
2016-12-27 12:53:56 +01:00
|
|
|
let res = self.inject(&mut batch)?;
|
2016-08-03 16:34:32 +02:00
|
|
|
self.backing().write(batch).map(|_| res).map_err(Into::into)
|
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|