2020-01-17 14:27:28 +01:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
2019-01-07 11:33:07 +01:00
|
|
|
// This file is part of Parity Ethereum.
|
2018-04-13 21:14:53 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-04-13 21:14:53 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-04-13 21:14:53 +02:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-13 21:14:53 +02:00
|
|
|
|
|
|
|
extern crate kvdb_rocksdb;
|
|
|
|
extern crate migration_rocksdb;
|
2019-01-04 14:05:46 +01:00
|
|
|
extern crate ethcore_blockchain;
|
2018-04-13 21:14:53 +02:00
|
|
|
|
2019-12-12 13:21:51 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate tempdir;
|
|
|
|
|
2018-07-02 11:04:48 +02:00
|
|
|
use std::{io, fs};
|
2018-04-13 21:14:53 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::path::Path;
|
2018-06-20 15:13:07 +02:00
|
|
|
use blooms_db;
|
2019-01-04 14:05:46 +01:00
|
|
|
use ethcore_db::NUM_COLUMNS;
|
2018-04-13 21:14:53 +02:00
|
|
|
use ethcore::client::{ClientConfig, DatabaseCompactionProfile};
|
2018-06-20 15:13:07 +02:00
|
|
|
use kvdb::KeyValueDB;
|
2019-01-04 14:05:46 +01:00
|
|
|
use self::ethcore_blockchain::{BlockChainDBHandler, BlockChainDB};
|
2018-04-13 21:14:53 +02:00
|
|
|
use self::kvdb_rocksdb::{Database, DatabaseConfig};
|
|
|
|
|
|
|
|
use cache::CacheConfig;
|
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
mod blooms;
|
2018-04-13 21:14:53 +02:00
|
|
|
mod migration;
|
|
|
|
mod helpers;
|
|
|
|
|
|
|
|
pub use self::migration::migrate;
|
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
struct AppDB {
|
2019-08-27 17:29:33 +02:00
|
|
|
key_value: Arc<dyn KeyValueDB>,
|
2018-06-20 15:13:07 +02:00
|
|
|
blooms: blooms_db::Database,
|
|
|
|
trace_blooms: blooms_db::Database,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockChainDB for AppDB {
|
2019-08-27 17:29:33 +02:00
|
|
|
fn key_value(&self) -> &Arc<dyn KeyValueDB> {
|
2018-06-20 15:13:07 +02:00
|
|
|
&self.key_value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn blooms(&self) -> &blooms_db::Database {
|
|
|
|
&self.blooms
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trace_blooms(&self) -> &blooms_db::Database {
|
|
|
|
&self.trace_blooms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:14:53 +02:00
|
|
|
/// Create a restoration db handler using the config generated by `client_path` and `client_config`.
|
2019-08-27 17:29:33 +02:00
|
|
|
pub fn restoration_db_handler(client_path: &Path, client_config: &ClientConfig) -> Box<dyn BlockChainDBHandler> {
|
2018-04-13 21:14:53 +02:00
|
|
|
let client_db_config = helpers::client_db_config(client_path, client_config);
|
|
|
|
|
|
|
|
struct RestorationDBHandler {
|
|
|
|
config: DatabaseConfig,
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
impl BlockChainDBHandler for RestorationDBHandler {
|
2019-08-27 17:29:33 +02:00
|
|
|
fn open(&self, db_path: &Path) -> io::Result<Arc<dyn BlockChainDB>> {
|
2018-06-20 15:13:07 +02:00
|
|
|
open_database(&db_path.to_string_lossy(), &self.config)
|
2018-04-13 21:14:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::new(RestorationDBHandler {
|
|
|
|
config: client_db_config,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-03 16:59:11 +01:00
|
|
|
/// Open a new light client DB.
|
|
|
|
pub fn open_db_light(
|
2019-08-27 17:29:33 +02:00
|
|
|
client_path: &str,
|
|
|
|
cache_config: &CacheConfig,
|
|
|
|
compaction: &DatabaseCompactionProfile
|
|
|
|
) -> io::Result<Arc<dyn BlockChainDB>> {
|
2018-06-20 15:13:07 +02:00
|
|
|
let path = Path::new(client_path);
|
|
|
|
|
2018-04-13 21:14:53 +02:00
|
|
|
let db_config = DatabaseConfig {
|
2019-12-03 16:59:11 +01:00
|
|
|
memory_budget: helpers::memory_per_column_light(cache_config.blockchain() as usize),
|
2018-06-20 15:13:07 +02:00
|
|
|
compaction: helpers::compaction_profile(&compaction, path),
|
2018-04-13 21:14:53 +02:00
|
|
|
.. DatabaseConfig::with_columns(NUM_COLUMNS)
|
|
|
|
};
|
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
open_database(client_path, &db_config)
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:29:33 +02:00
|
|
|
pub fn open_database(client_path: &str, config: &DatabaseConfig) -> io::Result<Arc<dyn BlockChainDB>> {
|
2018-06-20 15:13:07 +02:00
|
|
|
let path = Path::new(client_path);
|
|
|
|
|
|
|
|
let blooms_path = path.join("blooms");
|
|
|
|
let trace_blooms_path = path.join("trace_blooms");
|
2018-06-22 10:52:01 +02:00
|
|
|
fs::create_dir_all(&blooms_path)?;
|
|
|
|
fs::create_dir_all(&trace_blooms_path)?;
|
2018-06-20 15:13:07 +02:00
|
|
|
|
|
|
|
let db = AppDB {
|
|
|
|
key_value: Arc::new(Database::open(&config, client_path)?),
|
|
|
|
blooms: blooms_db::Database::open(blooms_path)?,
|
|
|
|
trace_blooms: blooms_db::Database::open(trace_blooms_path)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Arc::new(db))
|
2018-04-13 21:14:53 +02:00
|
|
|
}
|