Decouple rocksdb dependency from ethcore (#8320)
* Move client DB opening logic to CLI * Move restoration db open logic to CLI This adds KeyValueDBHandler which handles opening a new database, thus allow us to move the restoration db open logic out of ethcore. * Move rocksdb's compactionprofile conversion to CLI * Move kvdb_rocksdb as test dependency for ethcore * Fix tests due to interface change * Fix service tests * Remove unused migration dep for ethcore
This commit is contained in:
@@ -15,13 +15,11 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::path::Path;
|
||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
||||
|
||||
use mode::Mode as IpcMode;
|
||||
use verification::{VerifierType, QueueConfig};
|
||||
use journaldb;
|
||||
use kvdb_rocksdb::CompactionProfile;
|
||||
|
||||
pub use std::time::Duration;
|
||||
pub use blockchain::Config as BlockChainConfig;
|
||||
@@ -45,17 +43,6 @@ impl Default for DatabaseCompactionProfile {
|
||||
}
|
||||
}
|
||||
|
||||
impl DatabaseCompactionProfile {
|
||||
/// Returns corresponding compaction profile.
|
||||
pub fn compaction_profile(&self, db_path: &Path) -> CompactionProfile {
|
||||
match *self {
|
||||
DatabaseCompactionProfile::Auto => CompactionProfile::auto(db_path),
|
||||
DatabaseCompactionProfile::SSD => CompactionProfile::ssd(),
|
||||
DatabaseCompactionProfile::HDD => CompactionProfile::hdd(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for DatabaseCompactionProfile {
|
||||
type Err = String;
|
||||
|
||||
|
||||
@@ -93,11 +93,9 @@ extern crate triehash;
|
||||
extern crate ansi_term;
|
||||
extern crate unexpected;
|
||||
extern crate kvdb;
|
||||
extern crate kvdb_rocksdb;
|
||||
extern crate kvdb_memorydb;
|
||||
extern crate util_error;
|
||||
extern crate snappy;
|
||||
extern crate migration;
|
||||
|
||||
extern crate ethabi;
|
||||
#[macro_use]
|
||||
@@ -130,6 +128,9 @@ extern crate trace_time;
|
||||
#[cfg_attr(test, macro_use)]
|
||||
extern crate evm;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate kvdb_rocksdb;
|
||||
|
||||
pub extern crate ethstore;
|
||||
|
||||
pub mod account_provider;
|
||||
|
||||
@@ -39,7 +39,7 @@ use parking_lot::{Mutex, RwLock, RwLockReadGuard};
|
||||
use util_error::UtilError;
|
||||
use bytes::Bytes;
|
||||
use journaldb::Algorithm;
|
||||
use kvdb_rocksdb::{Database, DatabaseConfig};
|
||||
use kvdb::{KeyValueDB, KeyValueDBHandler};
|
||||
use snappy;
|
||||
|
||||
/// Helper for removing directories in case of error.
|
||||
@@ -79,14 +79,13 @@ struct Restoration {
|
||||
snappy_buffer: Bytes,
|
||||
final_state_root: H256,
|
||||
guard: Guard,
|
||||
db: Arc<Database>,
|
||||
db: Arc<KeyValueDB>,
|
||||
}
|
||||
|
||||
struct RestorationParams<'a> {
|
||||
manifest: ManifestData, // manifest to base restoration on.
|
||||
pruning: Algorithm, // pruning algorithm for the database.
|
||||
db_path: PathBuf, // database path
|
||||
db_config: &'a DatabaseConfig, // configuration for the database.
|
||||
db: Arc<KeyValueDB>, // database
|
||||
writer: Option<LooseWriter>, // writer for recovered snapshot.
|
||||
genesis: &'a [u8], // genesis block of the chain.
|
||||
guard: Guard, // guard for the restoration directory.
|
||||
@@ -101,8 +100,7 @@ impl Restoration {
|
||||
let state_chunks = manifest.state_hashes.iter().cloned().collect();
|
||||
let block_chunks = manifest.block_hashes.iter().cloned().collect();
|
||||
|
||||
let raw_db = Arc::new(Database::open(params.db_config, &*params.db_path.to_string_lossy())
|
||||
.map_err(UtilError::from)?);
|
||||
let raw_db = params.db;
|
||||
|
||||
let chain = BlockChain::new(Default::default(), params.genesis, raw_db.clone());
|
||||
let components = params.engine.snapshot_components()
|
||||
@@ -211,10 +209,10 @@ pub struct ServiceParams {
|
||||
pub engine: Arc<EthEngine>,
|
||||
/// The chain's genesis block.
|
||||
pub genesis_block: Bytes,
|
||||
/// Database configuration options.
|
||||
pub db_config: DatabaseConfig,
|
||||
/// State pruning algorithm.
|
||||
pub pruning: Algorithm,
|
||||
/// Handler for opening a restoration DB.
|
||||
pub restoration_db_handler: Box<KeyValueDBHandler>,
|
||||
/// Async IO channel for sending messages.
|
||||
pub channel: Channel,
|
||||
/// The directory to put snapshots in.
|
||||
@@ -228,8 +226,8 @@ pub struct ServiceParams {
|
||||
/// This controls taking snapshots and restoring from them.
|
||||
pub struct Service {
|
||||
restoration: Mutex<Option<Restoration>>,
|
||||
restoration_db_handler: Box<KeyValueDBHandler>,
|
||||
snapshot_root: PathBuf,
|
||||
db_config: DatabaseConfig,
|
||||
io_channel: Mutex<Channel>,
|
||||
pruning: Algorithm,
|
||||
status: Mutex<RestorationStatus>,
|
||||
@@ -249,8 +247,8 @@ impl Service {
|
||||
pub fn new(params: ServiceParams) -> Result<Self, Error> {
|
||||
let mut service = Service {
|
||||
restoration: Mutex::new(None),
|
||||
restoration_db_handler: params.restoration_db_handler,
|
||||
snapshot_root: params.snapshot_root,
|
||||
db_config: params.db_config,
|
||||
io_channel: Mutex::new(params.channel),
|
||||
pruning: params.pruning,
|
||||
status: Mutex::new(RestorationStatus::Inactive),
|
||||
@@ -437,8 +435,7 @@ impl Service {
|
||||
let params = RestorationParams {
|
||||
manifest: manifest,
|
||||
pruning: self.pruning,
|
||||
db_path: self.restoration_db(),
|
||||
db_config: &self.db_config,
|
||||
db: self.restoration_db_handler.open(&self.restoration_db())?,
|
||||
writer: writer,
|
||||
genesis: &self.genesis_block,
|
||||
guard: Guard::new(rest_dir),
|
||||
@@ -638,6 +635,7 @@ mod tests {
|
||||
use snapshot::{ManifestData, RestorationStatus, SnapshotService};
|
||||
use super::*;
|
||||
use tempdir::TempDir;
|
||||
use tests::helpers::restoration_db_handler;
|
||||
|
||||
struct NoopDBRestore;
|
||||
impl DatabaseRestore for NoopDBRestore {
|
||||
@@ -657,7 +655,7 @@ mod tests {
|
||||
let snapshot_params = ServiceParams {
|
||||
engine: spec.engine.clone(),
|
||||
genesis_block: spec.genesis_block(),
|
||||
db_config: Default::default(),
|
||||
restoration_db_handler: restoration_db_handler(Default::default()),
|
||||
pruning: Algorithm::Archive,
|
||||
channel: service.channel(),
|
||||
snapshot_root: dir,
|
||||
@@ -709,8 +707,7 @@ mod tests {
|
||||
block_hash: H256::default(),
|
||||
},
|
||||
pruning: Algorithm::Archive,
|
||||
db_path: tempdir.path().to_owned(),
|
||||
db_config: &db_config,
|
||||
db: restoration_db_handler(db_config).open(&tempdir.path().to_owned()).unwrap(),
|
||||
writer: None,
|
||||
genesis: &gb,
|
||||
guard: Guard::benign(),
|
||||
|
||||
@@ -24,7 +24,7 @@ use ids::BlockId;
|
||||
use snapshot::service::{Service, ServiceParams};
|
||||
use snapshot::{self, ManifestData, SnapshotService};
|
||||
use spec::Spec;
|
||||
use tests::helpers::generate_dummy_client_with_spec_and_data;
|
||||
use tests::helpers::{generate_dummy_client_with_spec_and_data, restoration_db_handler};
|
||||
|
||||
use io::IoChannel;
|
||||
use kvdb_rocksdb::{Database, DatabaseConfig};
|
||||
@@ -65,7 +65,7 @@ fn restored_is_equivalent() {
|
||||
let service_params = ServiceParams {
|
||||
engine: spec.engine.clone(),
|
||||
genesis_block: spec.genesis_block(),
|
||||
db_config: db_config,
|
||||
restoration_db_handler: restoration_db_handler(db_config),
|
||||
pruning: ::journaldb::Algorithm::Archive,
|
||||
channel: IoChannel::disconnected(),
|
||||
snapshot_root: path,
|
||||
@@ -107,7 +107,7 @@ fn guards_delete_folders() {
|
||||
let service_params = ServiceParams {
|
||||
engine: spec.engine.clone(),
|
||||
genesis_block: spec.genesis_block(),
|
||||
db_config: DatabaseConfig::with_columns(::db::NUM_COLUMNS),
|
||||
restoration_db_handler: restoration_db_handler(DatabaseConfig::with_columns(::db::NUM_COLUMNS)),
|
||||
pruning: ::journaldb::Algorithm::Archive,
|
||||
channel: IoChannel::disconnected(),
|
||||
snapshot_root: tempdir.path().to_owned(),
|
||||
|
||||
@@ -33,8 +33,11 @@ use spec::Spec;
|
||||
use state_db::StateDB;
|
||||
use state::*;
|
||||
use std::sync::Arc;
|
||||
use std::path::Path;
|
||||
use transaction::{Action, Transaction, SignedTransaction};
|
||||
use views::BlockView;
|
||||
use kvdb::{KeyValueDB, KeyValueDBHandler};
|
||||
use kvdb_rocksdb::{Database, DatabaseConfig};
|
||||
|
||||
pub fn create_test_block(header: &Header) -> Bytes {
|
||||
let mut rlp = RlpStream::new_list(3);
|
||||
@@ -349,3 +352,19 @@ impl ChainNotify for TestNotify {
|
||||
self.messages.write().push(data);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn restoration_db_handler(config: DatabaseConfig) -> Box<KeyValueDBHandler> {
|
||||
use kvdb::Error;
|
||||
|
||||
struct RestorationDBHandler {
|
||||
config: DatabaseConfig,
|
||||
}
|
||||
|
||||
impl KeyValueDBHandler for RestorationDBHandler {
|
||||
fn open(&self, db_path: &Path) -> Result<Arc<KeyValueDB>, Error> {
|
||||
Ok(Arc::new(Database::open(&self.config, &db_path.to_string_lossy())?))
|
||||
}
|
||||
}
|
||||
|
||||
Box::new(RestorationDBHandler { config })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user