Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 0 additions & 3 deletions parity/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,3 @@
mod impls;

pub use self::impls::{open_db_light, restoration_db_handler, migrate};

#[cfg(feature = "secretstore")]
pub use self::impls::open_secretstore_db;
6 changes: 3 additions & 3 deletions parity/db/rocksdb/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ pub fn migrate(path: &Path, compaction_profile: &DatabaseCompactionProfile) -> R

// Further migrations
if version < CURRENT_VERSION && exists(&db_path) {
println!("Migrating database from version {} to {}", version, CURRENT_VERSION);
info!(target: "migration", "Migrating database from version {} to {}", version, CURRENT_VERSION);
migrate_database(version, &db_path, consolidated_database_migrations(&compaction_profile)?)?;

if version < BLOOMS_DB_VERSION {
println!("Migrating blooms to blooms-db...");
info!(target: "migration", "Migrating blooms to blooms-db...");
let db_config = DatabaseConfig {
max_open_files: 64,
compaction: compaction_profile,
Expand All @@ -232,7 +232,7 @@ pub fn migrate(path: &Path, compaction_profile: &DatabaseCompactionProfile) -> R
migrate_blooms(&db_path, &db_config).map_err(Error::BloomsDB)?;
}

println!("Migration finished");
info!(target: "migration", "Migration finished");
}

// update version file.
Expand Down
14 changes: 3 additions & 11 deletions parity/db/rocksdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ extern crate kvdb_rocksdb;
extern crate migration_rocksdb;
extern crate ethcore_blockchain;

#[cfg(test)]
extern crate tempdir;

use std::{io, fs};
use std::sync::Arc;
use std::path::Path;
Expand Down Expand Up @@ -56,17 +59,6 @@ impl BlockChainDB for AppDB {
}
}

/// Open a secret store DB using the given secret store data path. The DB path is one level beneath the data path.
#[cfg(feature = "secretstore")]
pub fn open_secretstore_db(data_path: &str) -> Result<Arc<dyn KeyValueDB>, String> {
use std::path::PathBuf;

let mut db_path = PathBuf::from(data_path);
db_path.push("db");
let db_path = db_path.to_str().ok_or_else(|| "Invalid secretstore path".to_string())?;
Ok(Arc::new(Database::open_default(&db_path).map_err(|e| format!("Error opening database: {:?}", e))?))
}

/// Create a restoration db handler using the config generated by `client_path` and `client_config`.
pub fn restoration_db_handler(client_path: &Path, client_config: &ClientConfig) -> Box<dyn BlockChainDBHandler> {
let client_db_config = helpers::client_db_config(client_path, client_config);
Expand Down
7 changes: 3 additions & 4 deletions parity/secretstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ mod server {
use ethcore_secretstore;
use parity_crypto::publickey::KeyPair;
use ansi_term::Colour::{Red, White};
use db;
use super::{Configuration, Dependencies, NodeSecretKey, ContractAddress, Executor};

fn into_service_contract_address(address: ContractAddress) -> ethcore_secretstore::ContractAddress {
Expand All @@ -136,13 +135,13 @@ mod server {

/// Key server
pub struct KeyServer {
_key_server: Box<ethcore_secretstore::KeyServer>,
_key_server: Box<dyn ethcore_secretstore::KeyServer>,
}

impl KeyServer {
/// Create new key server
pub fn new(mut conf: Configuration, deps: Dependencies, executor: Executor) -> Result<Self, String> {
let self_secret: Arc<ethcore_secretstore::NodeKeyPair> = match conf.self_secret.take() {
let self_secret: Arc<dyn ethcore_secretstore::NodeKeyPair> = match conf.self_secret.take() {
Some(NodeSecretKey::Plain(secret)) => Arc::new(ethcore_secretstore::PlainNodeKeyPair::new(
KeyPair::from_secret(secret).map_err(|e| format!("invalid secret: {}", e))?)),
#[cfg(feature = "accounts")]
Expand Down Expand Up @@ -203,7 +202,7 @@ mod server {

cconf.cluster_config.nodes.insert(self_secret.public().clone(), cconf.cluster_config.listener_address.clone());

let db = db::open_secretstore_db(&conf.data_path)?;
let db = ethcore_secretstore::open_secretstore_db(&conf.data_path)?;
let key_server = ethcore_secretstore::start(deps.client, deps.sync, deps.miner, self_secret, cconf, db, executor)
.map_err(|e| format!("Error starting KeyServer {}: {}", key_server_name, e))?;

Expand Down
4 changes: 3 additions & 1 deletion secret-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ ethcore-accounts = { path = "../accounts", optional = true}
ethcore-call-contract = { path = "../ethcore/call-contract" }
ethcore-sync = { path = "../ethcore/sync" }
ethereum-types = "0.8.0"
ethkey = { path = "../accounts/ethkey", optional = true }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this too, to get parity-ethereum to build. A bit concerning CI didn't catch this.

futures = "0.1"
hyper = { version = "0.12", default-features = false }
keccak-hash = "0.4.0"
kvdb = "0.1"
kvdb-rocksdb = "0.2.0"
lazy_static = "1.0"
log = "0.4"
parity-bytes = "0.1"
Expand Down Expand Up @@ -48,4 +50,4 @@ tempdir = "0.3"
kvdb-rocksdb = "0.2.0"

[features]
accounts = ["ethcore-accounts"]
accounts = ["ethcore-accounts", "ethkey"]
49 changes: 12 additions & 37 deletions secret-store/src/key_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ use kvdb::KeyValueDB;
use types::{Error, ServerKeyId, NodeId};
use serialization::{SerializablePublic, SerializableSecret, SerializableH256, SerializableAddress};

/// Key of version value.
const DB_META_KEY_VERSION: &'static [u8; 7] = b"version";
/// Current db version.
const CURRENT_VERSION: u8 = 3;

/// Encrypted key share, stored by key storage on the single key server.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct DocumentKeyShare {
Expand Down Expand Up @@ -116,26 +111,7 @@ struct SerializableDocumentKeyShareVersionV3 {
impl PersistentKeyStorage {
/// Create new persistent document encryption keys storage
pub fn new(db: Arc<dyn KeyValueDB>) -> Result<Self, Error> {
let db = upgrade_db(db)?;

Ok(PersistentKeyStorage {
db: db,
})
}
}

fn upgrade_db(db: Arc<dyn KeyValueDB>) -> Result<Arc<dyn KeyValueDB>, Error> {
let version = db.get(None, DB_META_KEY_VERSION)?;
let version = version.and_then(|v| v.get(0).cloned());
match version {
None => {
let mut batch = db.transaction();
batch.put(None, DB_META_KEY_VERSION, &[CURRENT_VERSION]);
db.write(batch)?;
Ok(db)
},
Some(CURRENT_VERSION) => Ok(db),
_ => Err(Error::Database(format!("unsupported SecretStore database version: {:?}", version))),
Ok(Self { db })
}
}

Expand All @@ -144,7 +120,7 @@ impl KeyStorage for PersistentKeyStorage {
let key: SerializableDocumentKeyShareV3 = key.into();
let key = serde_json::to_vec(&key).map_err(|e| Error::Database(e.to_string()))?;
let mut batch = self.db.transaction();
batch.put(None, document.as_bytes(), &key);
batch.put(Some(0), document.as_bytes(), &key);
self.db.write(batch).map_err(Into::into)
}

Expand All @@ -153,7 +129,7 @@ impl KeyStorage for PersistentKeyStorage {
}

fn get(&self, document: &ServerKeyId) -> Result<Option<DocumentKeyShare>, Error> {
self.db.get(None, document.as_bytes())
self.db.get(Some(0), document.as_bytes())
.map_err(|e| Error::Database(e.to_string()))
.and_then(|key| match key {
None => Ok(None),
Expand All @@ -166,28 +142,28 @@ impl KeyStorage for PersistentKeyStorage {

fn remove(&self, document: &ServerKeyId) -> Result<(), Error> {
let mut batch = self.db.transaction();
batch.delete(None, document.as_bytes());
batch.delete(Some(0), document.as_bytes());
self.db.write(batch).map_err(Into::into)
}

fn clear(&self) -> Result<(), Error> {
let mut batch = self.db.transaction();
for (key, _) in self.iter() {
batch.delete(None, key.as_bytes());
batch.delete(Some(0), key.as_bytes());
}
self.db.write(batch)
.map_err(|e| Error::Database(e.to_string()))
}

fn contains(&self, document: &ServerKeyId) -> bool {
self.db.get(None, document.as_bytes())
self.db.get(Some(0), document.as_bytes())
.map(|k| k.is_some())
.unwrap_or(false)
}

fn iter<'a>(&'a self) -> Box<dyn Iterator<Item=(ServerKeyId, DocumentKeyShare)> + 'a> {
Box::new(PersistentKeyStorageIterator {
iter: self.db.iter(None),
iter: self.db.iter(Some(0)),
})
}
}
Expand Down Expand Up @@ -290,14 +266,12 @@ impl From<SerializableDocumentKeyShareV3> for DocumentKeyShare {

#[cfg(test)]
pub mod tests {
extern crate tempdir;

use std::collections::HashMap;
use std::sync::Arc;
use parking_lot::RwLock;
use self::tempdir::TempDir;
use tempdir::TempDir;
use crypto::publickey::{Random, Generator, Public};
use kvdb_rocksdb::Database;
use kvdb_rocksdb::{Database, DatabaseConfig};
use types::{Error, ServerKeyId};
use super::{KeyStorage, PersistentKeyStorage, DocumentKeyShare, DocumentKeyShareVersion};

Expand Down Expand Up @@ -376,7 +350,8 @@ pub mod tests {
};
let key3 = ServerKeyId::from_low_u64_be(3);

let db = Database::open_default(&tempdir.path().display().to_string()).unwrap();
let db_config = DatabaseConfig::with_columns(Some(1));
let db = Database::open(&db_config, &tempdir.path().display().to_string()).unwrap();

let key_storage = PersistentKeyStorage::new(Arc::new(db)).unwrap();
key_storage.insert(key1.clone(), value1.clone()).unwrap();
Expand All @@ -386,7 +361,7 @@ pub mod tests {
assert_eq!(key_storage.get(&key3), Ok(None));
drop(key_storage);

let db = Database::open_default(&tempdir.path().display().to_string()).unwrap();
let db = Database::open(&db_config, &tempdir.path().display().to_string()).unwrap();

let key_storage = PersistentKeyStorage::new(Arc::new(db)).unwrap();
assert_eq!(key_storage.get(&key1), Ok(Some(value1)));
Expand Down
21 changes: 19 additions & 2 deletions secret-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern crate ethereum_types;
extern crate hyper;
extern crate keccak_hash as hash;
extern crate kvdb;
extern crate kvdb_rocksdb;
extern crate parity_bytes as bytes;
extern crate parity_crypto as crypto;
extern crate parity_runtime;
Expand Down Expand Up @@ -53,12 +54,12 @@ extern crate lazy_static;
#[macro_use]
extern crate log;

#[cfg(test)]
#[cfg(any(test, feature = "accounts"))]
extern crate ethkey;
#[cfg(test)]
extern crate env_logger;
#[cfg(test)]
extern crate kvdb_rocksdb;
extern crate tempdir;

#[cfg(feature = "accounts")]
extern crate ethcore_accounts as accounts;
Expand All @@ -76,9 +77,11 @@ mod key_server_set;
mod node_key_pair;
mod listener;
mod trusted_client;
mod migration;

use std::sync::Arc;
use kvdb::KeyValueDB;
use kvdb_rocksdb::{Database, DatabaseConfig};
use ethcore::client::Client;
use ethcore::miner::Miner;
use sync::SyncProvider;
Expand All @@ -91,6 +94,20 @@ pub use self::node_key_pair::PlainNodeKeyPair;
#[cfg(feature = "accounts")]
pub use self::node_key_pair::KeyStoreNodeKeyPair;

/// Open a secret store DB using the given secret store data path. The DB path is one level beneath the data path.
pub fn open_secretstore_db(data_path: &str) -> Result<Arc<dyn KeyValueDB>, String> {
use std::path::PathBuf;

migration::upgrade_db(data_path).map_err(|e| e.to_string())?;

let mut db_path = PathBuf::from(data_path);
db_path.push("db");
let db_path = db_path.to_str().ok_or_else(|| "Invalid secretstore path".to_string())?;

let config = DatabaseConfig::with_columns(Some(1));
Ok(Arc::new(Database::open(&config, &db_path).map_err(|e| format!("Error opening database: {:?}", e))?))
}

/// Start new key server instance
pub fn start(client: Arc<Client>, sync: Arc<dyn SyncProvider>, miner: Arc<Miner>, self_key_pair: Arc<dyn NodeKeyPair>, mut config: ServiceConfiguration,
db: Arc<dyn KeyValueDB>, executor: Executor) -> Result<Box<dyn KeyServer>, Error>
Expand Down
Loading