From 33b1e6ddf446674f9ccd8d5d0e6346845013ff2b Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 17 Sep 2018 17:52:32 +1000 Subject: [PATCH] Partially implemented db wrapper Addresses issue #12 --- lighthouse/client/mod.rs | 8 +++++--- lighthouse/db/mod.rs | 27 +++++++++++++++++---------- lighthouse/sync/block.rs | 5 +++-- lighthouse/sync/network.rs | 8 ++++---- lighthouse/sync/sync_future.rs | 4 ++-- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lighthouse/client/mod.rs b/lighthouse/client/mod.rs index 89b1729ba..f76f87aef 100644 --- a/lighthouse/client/mod.rs +++ b/lighthouse/client/mod.rs @@ -1,6 +1,6 @@ use std::sync::Arc; use std::thread; -use super::db::{ DB, open_db }; +use super::db::{ DiskDB }; use super::config::LighthouseConfig; use super::futures::sync::mpsc::{ unbounded, @@ -10,10 +10,12 @@ use super::network_libp2p::state::NetworkState; use super::slog::Logger; use super::sync::run_sync_future; +use super::db::ClientDB; + /// Represents the co-ordination of the /// networking, syncing and RPC (not-yet-implemented) threads. pub struct Client { - pub db: Arc, + pub db: Arc, pub network_thread: thread::JoinHandle<()>, pub sync_thread: thread::JoinHandle<()>, } @@ -29,7 +31,7 @@ impl Client { { // Open the local db let db = { - let db = open_db(&config.data_dir); + let db = DiskDB::open(&config.data_dir); Arc::new(db) }; diff --git a/lighthouse/db/mod.rs b/lighthouse/db/mod.rs index 92323b3ac..23ddb9a37 100644 --- a/lighthouse/db/mod.rs +++ b/lighthouse/db/mod.rs @@ -1,14 +1,21 @@ extern crate rocksdb; -use std::fs; -use std::path::Path; -pub use self::rocksdb::DB; +mod disk_db; -pub fn open_db(path: &Path) -> DB { - let db_path = path.join("rocksdb"); - fs::create_dir_all(&db_path) - .expect(&format!("Unable to create {:?}", &db_path)); - let db = DB::open_default(db_path.join("lighthouse.rdb")) - .expect("Unable to open local database."); - db +pub use self::disk_db::DiskDB; + +#[derive(Debug)] +pub struct DBError { + message: String +} + +impl DBError { + fn new(message: String) -> Self { + Self { message } + } +} + +pub trait ClientDB: Sync + Send { + fn get(&self, col: &str, key: &[u8]) + -> Result, DBError>; } diff --git a/lighthouse/sync/block.rs b/lighthouse/sync/block.rs index 2612ca8b1..72d827285 100644 --- a/lighthouse/sync/block.rs +++ b/lighthouse/sync/block.rs @@ -1,4 +1,5 @@ -use super::db::DB; +use std::sync::Arc; +use super::db::ClientDB; use slog::Logger; pub enum BlockStatus { @@ -14,7 +15,7 @@ pub enum BlockStatus { pub fn process_unverified_blocks( _serialized_block: &[u8], - _db: &DB, + _db: Arc, _log: Logger) { // diff --git a/lighthouse/sync/network.rs b/lighthouse/sync/network.rs index 99d178ff0..1383b5040 100644 --- a/lighthouse/sync/network.rs +++ b/lighthouse/sync/network.rs @@ -1,5 +1,5 @@ use std::sync::Arc; -use super::db::DB; +use super::db::ClientDB; use slog::Logger; use super::network_libp2p::message::{ @@ -25,7 +25,7 @@ use super::futures::sync::mpsc::{ /// (e.g., libp2p) has an event to push up to the sync process. pub fn handle_network_event( event: NetworkEvent, - db: Arc, + db: Arc, network_tx: UnboundedSender, log: Logger) -> Result<(), ()> @@ -39,7 +39,7 @@ pub fn handle_network_event( if let Some(data) = event.data { handle_network_message( data, - &db, + db, network_tx, log) } else { @@ -56,7 +56,7 @@ pub fn handle_network_event( /// (e.g., libp2p) has sent a message to us. fn handle_network_message( message: Vec, - db: &DB, + db: Arc, _network_tx: UnboundedSender, log: Logger) -> Result<(), ()> diff --git a/lighthouse/sync/sync_future.rs b/lighthouse/sync/sync_future.rs index 06836fb30..0e46e9e50 100644 --- a/lighthouse/sync/sync_future.rs +++ b/lighthouse/sync/sync_future.rs @@ -10,7 +10,7 @@ use super::network_libp2p::message::{ }; use super::network::handle_network_event; use std::sync::Arc; -use super::db::DB; +use super::db::ClientDB; use slog::Logger; type NetworkSender = UnboundedSender; @@ -25,7 +25,7 @@ type SyncReceiver = UnboundedReceiver>; /// from the network and the RPC and update /// the state. pub fn run_sync_future( - db: Arc, + db: Arc, network_tx: NetworkSender, network_rx: NetworkReceiver, _sync_tx: SyncSender,