Run rustfmt globally.

Using `$ cargo fmt` in the root. Closes #68.
This commit is contained in:
Paul Hauner
2018-11-04 15:35:00 +01:00
parent 7cc2800916
commit 900ffac5e0
43 changed files with 754 additions and 975 deletions

View File

@@ -16,10 +16,9 @@ const DEFAULT_LIGHTHOUSE_DIR: &str = ".lighthouse";
impl LighthouseConfig {
/// Build a new lighthouse configuration from defaults.
pub fn default() -> Self{
pub fn default() -> Self {
let data_dir = {
let home = dirs::home_dir()
.expect("Unable to determine home dir.");
let home = dirs::home_dir().expect("Unable to determine home dir.");
home.join(DEFAULT_LIGHTHOUSE_DIR)
};
fs::create_dir_all(&data_dir)

View File

@@ -4,15 +4,11 @@ extern crate rocksdb;
mod disk_db;
mod memory_db;
mod traits;
pub mod stores;
mod traits;
use self::stores::COLUMNS;
pub use self::disk_db::DiskDB;
pub use self::memory_db::MemoryDB;
pub use self::traits::{
DBError,
DBValue,
ClientDB,
};
pub use self::traits::{ClientDB, DBError, DBValue};

View File

@@ -1,21 +1,12 @@
use super::{
ClientDB,
DBError,
};
use super::{ClientDB, DBError};
mod beacon_block_store;
mod pow_chain_store;
mod validator_store;
pub use self::beacon_block_store::{
BeaconBlockStore,
BeaconBlockAtSlotError,
};
pub use self::beacon_block_store::{BeaconBlockAtSlotError, BeaconBlockStore};
pub use self::pow_chain_store::PoWChainStore;
pub use self::validator_store::{
ValidatorStore,
ValidatorStoreError,
};
pub use self::validator_store::{ValidatorStore, ValidatorStoreError};
use super::bls;
@@ -23,8 +14,4 @@ pub const BLOCKS_DB_COLUMN: &str = "blocks";
pub const POW_CHAIN_DB_COLUMN: &str = "powchain";
pub const VALIDATOR_DB_COLUMN: &str = "validator";
pub const COLUMNS: [&str; 3] = [
BLOCKS_DB_COLUMN,
POW_CHAIN_DB_COLUMN,
VALIDATOR_DB_COLUMN,
];
pub const COLUMNS: [&str; 3] = [BLOCKS_DB_COLUMN, POW_CHAIN_DB_COLUMN, VALIDATOR_DB_COLUMN];

View File

@@ -1,32 +1,24 @@
use std::sync::Arc;
use super::{
ClientDB,
DBError,
};
use super::POW_CHAIN_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError};
use std::sync::Arc;
pub struct PoWChainStore<T>
where T: ClientDB
where
T: ClientDB,
{
db: Arc<T>,
}
impl<T: ClientDB> PoWChainStore<T> {
pub fn new(db: Arc<T>) -> Self {
Self {
db,
}
Self { db }
}
pub fn put_block_hash(&self, hash: &[u8])
-> Result<(), DBError>
{
pub fn put_block_hash(&self, hash: &[u8]) -> Result<(), DBError> {
self.db.put(DB_COLUMN, hash, &[0])
}
pub fn block_hash_exists(&self, hash: &[u8])
-> Result<bool, DBError>
{
pub fn block_hash_exists(&self, hash: &[u8]) -> Result<bool, DBError> {
self.db.exists(DB_COLUMN, hash)
}
}

View File

@@ -1,16 +1,10 @@
extern crate bytes;
use self::bytes::{
BufMut,
BytesMut,
};
use std::sync::Arc;
use super::{
ClientDB,
DBError,
};
use super::VALIDATOR_DB_COLUMN as DB_COLUMN;
use self::bytes::{BufMut, BytesMut};
use super::bls::PublicKey;
use super::VALIDATOR_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError};
use std::sync::Arc;
#[derive(Debug, PartialEq)]
pub enum ValidatorStoreError {
@@ -30,66 +24,63 @@ enum KeyPrefixes {
}
pub struct ValidatorStore<T>
where T: ClientDB
where
T: ClientDB,
{
db: Arc<T>,
}
impl<T: ClientDB> ValidatorStore<T> {
pub fn new(db: Arc<T>) -> Self {
Self {
db,
}
Self { db }
}
fn prefix_bytes(&self, key_prefix: &KeyPrefixes)
-> Vec<u8>
{
fn prefix_bytes(&self, key_prefix: &KeyPrefixes) -> Vec<u8> {
match key_prefix {
KeyPrefixes::PublicKey => b"pubkey".to_vec(),
}
}
fn get_db_key_for_index(&self, key_prefix: &KeyPrefixes, index: usize)
-> Vec<u8>
{
fn get_db_key_for_index(&self, key_prefix: &KeyPrefixes, index: usize) -> Vec<u8> {
let mut buf = BytesMut::with_capacity(6 + 8);
buf.put(self.prefix_bytes(key_prefix));
buf.put_u64_be(index as u64);
buf.take().to_vec()
}
pub fn put_public_key_by_index(&self, index: usize, public_key: &PublicKey)
-> Result<(), ValidatorStoreError>
{
pub fn put_public_key_by_index(
&self,
index: usize,
public_key: &PublicKey,
) -> Result<(), ValidatorStoreError> {
let key = self.get_db_key_for_index(&KeyPrefixes::PublicKey, index);
let val = public_key.as_bytes();
self.db.put(DB_COLUMN, &key[..], &val[..])
.map_err(ValidatorStoreError::from)
self.db
.put(DB_COLUMN, &key[..], &val[..])
.map_err(ValidatorStoreError::from)
}
pub fn get_public_key_by_index(&self, index: usize)
-> Result<Option<PublicKey>, ValidatorStoreError>
{
pub fn get_public_key_by_index(
&self,
index: usize,
) -> Result<Option<PublicKey>, ValidatorStoreError> {
let key = self.get_db_key_for_index(&KeyPrefixes::PublicKey, index);
let val = self.db.get(DB_COLUMN, &key[..])?;
match val {
None => Ok(None),
Some(val) => {
match PublicKey::from_bytes(&val) {
Ok(key) => Ok(Some(key)),
Err(_) => Err(ValidatorStoreError::DecodeError),
}
}
Some(val) => match PublicKey::from_bytes(&val) {
Ok(key) => Ok(Some(key)),
Err(_) => Err(ValidatorStoreError::DecodeError),
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::super::MemoryDB;
use super::super::bls::Keypair;
use super::*;
#[test]
fn test_validator_store_put_get() {
@@ -112,16 +103,19 @@ mod tests {
* Check all keys are retrieved correctly.
*/
for i in 0..keys.len() {
let retrieved = store.get_public_key_by_index(i)
.unwrap().unwrap();
let retrieved = store.get_public_key_by_index(i).unwrap().unwrap();
assert_eq!(retrieved, keys[i].pk);
}
/*
* Check that an index that wasn't stored returns None.
*/
assert!(store.get_public_key_by_index(keys.len() + 1)
.unwrap().is_none());
assert!(
store
.get_public_key_by_index(keys.len() + 1)
.unwrap()
.is_none()
);
}
#[test]
@@ -132,7 +126,9 @@ mod tests {
let key = store.get_db_key_for_index(&KeyPrefixes::PublicKey, 42);
db.put(DB_COLUMN, &key[..], "cats".as_bytes()).unwrap();
assert_eq!(store.get_public_key_by_index(42),
Err(ValidatorStoreError::DecodeError));
assert_eq!(
store.get_public_key_by_index(42),
Err(ValidatorStoreError::DecodeError)
);
}
}

View File

@@ -1,7 +1,7 @@
#[macro_use]
extern crate slog;
extern crate slog_term;
extern crate slog_async;
extern crate slog_term;
// extern crate ssz;
extern crate clap;
extern crate futures;
@@ -12,9 +12,9 @@ mod config;
use std::path::PathBuf;
use slog::Drain;
use clap::{ Arg, App };
use clap::{App, Arg};
use config::LighthouseConfig;
use slog::Drain;
fn main() {
let decorator = slog_term::TermDecorator::new().build();
@@ -26,17 +26,19 @@ fn main() {
.version("0.0.1")
.author("Sigma Prime <paul@sigmaprime.io>")
.about("Eth 2.0 Client")
.arg(Arg::with_name("datadir")
.long("datadir")
.value_name("DIR")
.help("Data directory for keys and databases.")
.takes_value(true))
.arg(Arg::with_name("port")
.long("port")
.value_name("PORT")
.help("Network listen port for p2p connections.")
.takes_value(true))
.get_matches();
.arg(
Arg::with_name("datadir")
.long("datadir")
.value_name("DIR")
.help("Data directory for keys and databases.")
.takes_value(true),
).arg(
Arg::with_name("port")
.long("port")
.value_name("PORT")
.help("Network listen port for p2p connections.")
.takes_value(true),
).get_matches();
let mut config = LighthouseConfig::default();
@@ -60,8 +62,10 @@ fn main() {
"data_dir" => &config.data_dir.to_str(),
"port" => &config.p2p_listen_port);
error!(log,
"Lighthouse under development and does not provide a user demo.");
error!(
log,
"Lighthouse under development and does not provide a user demo."
);
info!(log, "Exiting.");
}