mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-09 03:17:55 +00:00
Add "Client" concept and RocksDB
This commit is contained in:
74
lighthouse/client/mod.rs
Normal file
74
lighthouse/client/mod.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
use std::sync::{ Arc, RwLock };
|
||||
use std::thread;
|
||||
use super::db::{ DB, open_db };
|
||||
use super::config::LighthouseConfig;
|
||||
use super::futures::sync::mpsc::{
|
||||
unbounded,
|
||||
};
|
||||
use super::network_libp2p::service::listen as network_listen;
|
||||
use super::network_libp2p::state::NetworkState;
|
||||
use super::slog::Logger;
|
||||
use super::sync::start_sync;
|
||||
|
||||
pub struct Client {
|
||||
pub db: Arc<RwLock<DB>>,
|
||||
pub threads: Vec<thread::JoinHandle<()>>
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(config: LighthouseConfig,
|
||||
log: Logger)
|
||||
-> Self
|
||||
{
|
||||
// Open the local db
|
||||
let db = {
|
||||
let db = open_db(&config.data_dir);
|
||||
Arc::new(RwLock::new(db))
|
||||
};
|
||||
|
||||
// Start the network thread
|
||||
let network_state = NetworkState::new(
|
||||
&config.data_dir,
|
||||
&config.p2p_listen_port,
|
||||
&log).expect("Network setup failed");
|
||||
let (network_thread, network_tx, network_rx) = {
|
||||
let (message_sender, message_receiver) = unbounded();
|
||||
let (event_sender, event_receiver) = unbounded();
|
||||
let network_log = log.new(o!());
|
||||
let thread = thread::spawn(move || {
|
||||
network_listen(
|
||||
network_state,
|
||||
event_sender,
|
||||
message_receiver,
|
||||
network_log,
|
||||
);
|
||||
});
|
||||
(thread, message_sender, event_receiver)
|
||||
};
|
||||
|
||||
// Start the sync thread
|
||||
let (sync_thread, _sync_tx, _sync_rx) = {
|
||||
let (sync_out_sender, sync_out_receiver) = unbounded();
|
||||
let (sync_in_sender, sync_in_receiver) = unbounded();
|
||||
let sync_log = log.new(o!());
|
||||
let sync_db = Arc::clone(&db);
|
||||
let thread = thread::spawn(move || {
|
||||
start_sync(
|
||||
sync_db,
|
||||
network_tx.clone(),
|
||||
network_rx,
|
||||
sync_out_sender,
|
||||
sync_in_receiver,
|
||||
sync_log,
|
||||
);
|
||||
});
|
||||
(thread, sync_in_sender, sync_out_receiver)
|
||||
};
|
||||
|
||||
// Return the client struct
|
||||
Self {
|
||||
db: db,
|
||||
threads: vec![sync_thread, network_thread]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::env;
|
||||
use std::{ env, fs };
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -17,6 +17,7 @@ impl LighthouseConfig {
|
||||
.expect("Unable to determine home dir.");
|
||||
home.join(DEFAULT_LIGHTHOUSE_DIR)
|
||||
};
|
||||
fs::create_dir_all(&data_dir).expect(&format!("Unable to create {:?}", &data_dir));
|
||||
let p2p_listen_port = 0;
|
||||
Self {
|
||||
data_dir,
|
||||
|
||||
14
lighthouse/db/mod.rs
Normal file
14
lighthouse/db/mod.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
extern crate rocksdb;
|
||||
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
pub use self::rocksdb::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
|
||||
}
|
||||
@@ -4,8 +4,10 @@ extern crate slog_term;
|
||||
extern crate slog_async;
|
||||
extern crate clap;
|
||||
extern crate network_libp2p;
|
||||
extern crate futures;
|
||||
|
||||
pub mod pubkeystore;
|
||||
pub mod db;
|
||||
pub mod client;
|
||||
pub mod shuffling;
|
||||
pub mod state;
|
||||
pub mod sync;
|
||||
@@ -17,9 +19,7 @@ use std::path::PathBuf;
|
||||
use slog::Drain;
|
||||
use clap::{ Arg, App };
|
||||
use config::LighthouseConfig;
|
||||
use network_libp2p::service::NetworkService;
|
||||
use network_libp2p::state::NetworkState;
|
||||
use sync::sync_start;
|
||||
use client::Client;
|
||||
|
||||
fn main() {
|
||||
let decorator = slog_term::TermDecorator::new().build();
|
||||
@@ -64,14 +64,11 @@ fn main() {
|
||||
info!(log, "";
|
||||
"data_dir" => &config.data_dir.to_str(),
|
||||
"port" => &config.p2p_listen_port);
|
||||
|
||||
let state = NetworkState::new(
|
||||
&config.data_dir,
|
||||
&config.p2p_listen_port,
|
||||
&log)
|
||||
.expect("setup failed");
|
||||
let (service, net_rx) = NetworkService::new(state, log.new(o!()));
|
||||
sync_start(service, net_rx, log.new(o!()));
|
||||
|
||||
let client = Client::new(config, log.new(o!()));
|
||||
for thread in client.threads {
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
info!(log, "Exiting.");
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
use super::state::block::Block;
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn verify_block(block: &Block, proposer: &usize) -> bool {
|
||||
// TODO: fetch proposer pubkey and verify it.
|
||||
return true;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
pub struct ShardAndCommittee {
|
||||
shard_id: u16,
|
||||
committee: Vec<u32>
|
||||
pub shard_id: u16,
|
||||
pub committee: Vec<u32>
|
||||
}
|
||||
|
||||
impl ShardAndCommittee {
|
||||
|
||||
@@ -2,36 +2,57 @@ extern crate futures;
|
||||
extern crate slog;
|
||||
extern crate tokio;
|
||||
|
||||
use super::network_libp2p::service::NetworkService;
|
||||
use self::futures::sync::mpsc::UnboundedReceiver;
|
||||
use self::futures::sync::mpsc::{
|
||||
UnboundedReceiver,
|
||||
UnboundedSender,
|
||||
};
|
||||
use self::futures::Stream;
|
||||
use slog::Logger;
|
||||
use self::tokio::timer::Interval;
|
||||
use self::tokio::prelude::*;
|
||||
use std::sync::{ RwLock, Arc };
|
||||
use super::network_libp2p::message::{
|
||||
NetworkEvent,
|
||||
OutgoingMessage,
|
||||
};
|
||||
use super::db::DB;
|
||||
use slog::Logger;
|
||||
|
||||
use std::time::{ Duration, Instant };
|
||||
|
||||
pub fn sync_start(service: NetworkService,
|
||||
net_stream: UnboundedReceiver<Vec<u8>>,
|
||||
log: Logger)
|
||||
{
|
||||
let net_rx = net_stream
|
||||
.for_each(move |msg| {
|
||||
debug!(&log, "Sync receive"; "msg" => format!("{:?}", msg));
|
||||
// service.send("hello".to_bytes());
|
||||
type NetworkSender = UnboundedSender<OutgoingMessage>;
|
||||
type NetworkReceiver = UnboundedReceiver<NetworkEvent>;
|
||||
|
||||
type SyncSender = UnboundedSender<Vec<u8>>;
|
||||
type SyncReceiver = UnboundedReceiver<Vec<u8>>;
|
||||
|
||||
pub fn start_sync(
|
||||
_db: Arc<RwLock<DB>>,
|
||||
network_tx: NetworkSender,
|
||||
network_rx: NetworkReceiver,
|
||||
_sync_tx: SyncSender,
|
||||
_sync_rx: SyncReceiver,
|
||||
log: Logger) {
|
||||
let rx_future = network_rx
|
||||
.for_each(move |event| {
|
||||
debug!(&log, "Sync receive";
|
||||
"msg" => format!("{:?}", event));
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|_| panic!("rx failed"));
|
||||
|
||||
let poll = Interval::new(Instant::now(), Duration::from_secs(2))
|
||||
let poll_future = Interval::new(Instant::now(), Duration::from_secs(2))
|
||||
.for_each(move |_| {
|
||||
service.send(vec![42, 42, 42]);
|
||||
let msg = OutgoingMessage {
|
||||
peer: None,
|
||||
data: vec![42, 42, 42]
|
||||
};
|
||||
network_tx.unbounded_send(msg);
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|_| panic!("send failed"));
|
||||
|
||||
let sync_future = poll
|
||||
.select(net_rx).map_err(|(err, _)| err)
|
||||
let sync_future = poll_future
|
||||
.select(rx_future).map_err(|(err, _)| err)
|
||||
.and_then(|((), n)| n);
|
||||
|
||||
tokio::run(sync_future);
|
||||
|
||||
Reference in New Issue
Block a user