Merge pull request #144 from ralexstokes/add-rustfmt-to-travis

Add rustfmt to travis
This commit is contained in:
Paul Hauner 2019-01-11 09:14:07 +11:00 committed by GitHub
commit 7df6481349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 75 additions and 44 deletions

View File

@ -1,5 +1,6 @@
language: rust language: rust
script: script:
- cargo fmt --all -- --check
- cargo build --verbose --all - cargo build --verbose --all
- cargo test --verbose --all - cargo test --verbose --all
rust: rust:
@ -10,3 +11,5 @@ matrix:
allow_failures: allow_failures:
- rust: nightly - rust: nightly
fast_finish: true fast_finish: true
install:
- rustup component add rustfmt

View File

@ -14,9 +14,9 @@ mod justified_slot;
mod shard_block; mod shard_block;
mod signature; mod signature;
pub use crate::enums::{Invalid, Outcome, Error};
pub use crate::block_inclusion::validate_attestation_for_block; pub use crate::block_inclusion::validate_attestation_for_block;
pub use crate::justified_slot::validate_attestation_justified_slot; pub use crate::enums::{Error, Invalid, Outcome};
pub use crate::justified_block::validate_attestation_justified_block_hash; pub use crate::justified_block::validate_attestation_justified_block_hash;
pub use crate::signature::validate_attestation_signature; pub use crate::justified_slot::validate_attestation_justified_slot;
pub use crate::shard_block::validate_attestation_data_shard_block_hash; pub use crate::shard_block::validate_attestation_data_shard_block_hash;
pub use crate::signature::validate_attestation_signature;

View File

@ -3,8 +3,8 @@ extern crate types;
extern crate validator_induction; extern crate validator_induction;
extern crate validator_shuffling; extern crate validator_shuffling;
mod beacon_state;
mod beacon_block; mod beacon_block;
mod beacon_state;
pub use crate::beacon_block::genesis_beacon_block; pub use crate::beacon_block::genesis_beacon_block;
pub use crate::beacon_state::{genesis_beacon_state, Error as GenesisError}; pub use crate::beacon_state::{genesis_beacon_state, Error as GenesisError};

View File

@ -6,12 +6,13 @@ pub mod address;
pub mod aggregate_signature; pub mod aggregate_signature;
pub mod bitfield; pub mod bitfield;
pub mod hash256; pub mod hash256;
pub mod signature;
pub mod secret_key;
pub mod public_key; pub mod public_key;
pub mod secret_key;
pub mod signature;
pub trait TestRandom<T> pub trait TestRandom<T>
where T: RngCore where
T: RngCore,
{ {
fn random_for_test(rng: &mut T) -> Self; fn random_for_test(rng: &mut T) -> Self;
} }
@ -35,7 +36,8 @@ impl<T: RngCore> TestRandom<T> for usize {
} }
impl<T: RngCore, U> TestRandom<T> for Vec<U> impl<T: RngCore, U> TestRandom<T> for Vec<U>
where U: TestRandom<T> where
U: TestRandom<T>,
{ {
fn random_for_test(rng: &mut T) -> Self { fn random_for_test(rng: &mut T) -> Self {
vec![ vec![

View File

@ -1,5 +1,5 @@
use super::bls::PublicKey; use super::bls::PublicKey;
use super::{Hash256}; use super::Hash256;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use ssz::{Decodable, DecodeError, Encodable, SszStream}; use ssz::{Decodable, DecodeError, Encodable, SszStream};
@ -40,7 +40,7 @@ pub struct ValidatorRecord {
pub exit_count: u64, pub exit_count: u64,
pub custody_commitment: Hash256, pub custody_commitment: Hash256,
pub latest_custody_reseed_slot: u64, pub latest_custody_reseed_slot: u64,
pub penultimate_custody_reseed_slot: u64 pub penultimate_custody_reseed_slot: u64,
} }
impl ValidatorRecord { impl ValidatorRecord {
@ -132,7 +132,7 @@ impl Decodable for ValidatorRecord {
exit_count, exit_count,
custody_commitment, custody_commitment,
latest_custody_reseed_slot, latest_custody_reseed_slot,
penultimate_custody_reseed_slot penultimate_custody_reseed_slot,
}, },
i, i,
)) ))

View File

@ -19,8 +19,8 @@ pub use self::bls_aggregates::AggregatePublicKey;
pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97; pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97;
use hashing::canonical_hash; use hashing::canonical_hash;
use std::default::Default;
use ssz::ssz_encode; use ssz::ssz_encode;
use std::default::Default;
fn extend_if_needed(hash: &mut Vec<u8>) { fn extend_if_needed(hash: &mut Vec<u8>) {
// NOTE: bls_aggregates crate demands 48 bytes, this may be removed as we get closer to production // NOTE: bls_aggregates crate demands 48 bytes, this may be removed as we get closer to production

View File

@ -1,19 +1,19 @@
use bls::{verify_proof_of_possession}; use bls::verify_proof_of_possession;
use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus};
use spec::ChainSpec; use spec::ChainSpec;
use types::{BeaconState, Deposit, ValidatorRecord, ValidatorStatus};
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum ValidatorInductionError { pub enum ValidatorInductionError {
InvalidShard, InvalidShard,
InvaidProofOfPossession, InvaidProofOfPossession,
InvalidWithdrawalCredentials InvalidWithdrawalCredentials,
} }
pub fn process_deposit( pub fn process_deposit(
state: &mut BeaconState, state: &mut BeaconState,
deposit: &Deposit, deposit: &Deposit,
spec: &ChainSpec) spec: &ChainSpec,
-> Result<usize, ValidatorInductionError> { ) -> Result<usize, ValidatorInductionError> {
let deposit_input = &deposit.deposit_data.deposit_input; let deposit_input = &deposit.deposit_data.deposit_input;
let deposit_data = &deposit.deposit_data; let deposit_data = &deposit.deposit_data;
@ -22,18 +22,22 @@ pub fn process_deposit(
return Err(ValidatorInductionError::InvaidProofOfPossession); return Err(ValidatorInductionError::InvaidProofOfPossession);
} }
let validator_index = state.validator_registry.iter() let validator_index = state
.validator_registry
.iter()
.position(|validator| validator.pubkey == deposit_input.pubkey); .position(|validator| validator.pubkey == deposit_input.pubkey);
match validator_index { match validator_index {
Some(i) => { Some(i) => {
if state.validator_registry[i].withdrawal_credentials == deposit_input.withdrawal_credentials { if state.validator_registry[i].withdrawal_credentials
== deposit_input.withdrawal_credentials
{
state.validator_balances[i] += deposit_data.value; state.validator_balances[i] += deposit_data.value;
return Ok(i); return Ok(i);
} }
Err(ValidatorInductionError::InvalidWithdrawalCredentials) Err(ValidatorInductionError::InvalidWithdrawalCredentials)
}, }
None => { None => {
let validator = ValidatorRecord { let validator = ValidatorRecord {
pubkey: deposit_input.pubkey.clone(), pubkey: deposit_input.pubkey.clone(),
@ -45,7 +49,7 @@ pub fn process_deposit(
exit_count: 0, exit_count: 0,
custody_commitment: deposit_input.custody_commitment, custody_commitment: deposit_input.custody_commitment,
latest_custody_reseed_slot: 0, latest_custody_reseed_slot: 0,
penultimate_custody_reseed_slot: 0 penultimate_custody_reseed_slot: 0,
}; };
match min_empty_validator_index(state, spec) { match min_empty_validator_index(state, spec) {
@ -53,7 +57,7 @@ pub fn process_deposit(
state.validator_registry[i] = validator; state.validator_registry[i] = validator;
state.validator_balances[i] = deposit_data.value; state.validator_balances[i] = deposit_data.value;
Ok(i) Ok(i)
}, }
None => { None => {
state.validator_registry.push(validator); state.validator_registry.push(validator);
state.validator_balances.push(deposit_data.value); state.validator_balances.push(deposit_data.value);
@ -64,14 +68,13 @@ pub fn process_deposit(
} }
} }
fn min_empty_validator_index( fn min_empty_validator_index(state: &BeaconState, spec: &ChainSpec) -> Option<usize> {
state: &BeaconState,
spec: &ChainSpec
) -> Option<usize> {
for i in 0..state.validator_registry.len() { for i in 0..state.validator_registry.len() {
if state.validator_balances[i] == 0 if state.validator_balances[i] == 0
&& state.validator_registry[i].latest_status_change_slot && state.validator_registry[i].latest_status_change_slot
+ spec.zero_balance_validator_ttl <= state.slot { + spec.zero_balance_validator_ttl
<= state.slot
{
return Some(i); return Some(i);
} }
} }
@ -107,7 +110,10 @@ mod tests {
(dep.deposit_data.deposit_input.pubkey == val.pubkey) (dep.deposit_data.deposit_input.pubkey == val.pubkey)
& (dep.deposit_data.deposit_input.withdrawal_credentials == val.withdrawal_credentials) & (dep.deposit_data.deposit_input.withdrawal_credentials == val.withdrawal_credentials)
& (dep.deposit_data.deposit_input.randao_commitment == val.randao_commitment) & (dep.deposit_data.deposit_input.randao_commitment == val.randao_commitment)
& (verify_proof_of_possession(&dep.deposit_data.deposit_input.proof_of_possession, &val.pubkey)) & (verify_proof_of_possession(
&dep.deposit_data.deposit_input.proof_of_possession,
&val.pubkey,
))
} }
#[test] #[test]
@ -120,7 +126,10 @@ mod tests {
let result = process_deposit(&mut state, &deposit, &spec); let result = process_deposit(&mut state, &deposit, &spec);
assert_eq!(result.unwrap(), 0); assert_eq!(result.unwrap(), 0);
assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); assert!(deposit_equals_record(
&deposit,
&state.validator_registry[0]
));
assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_registry.len(), 1);
assert_eq!(state.validator_balances.len(), 1); assert_eq!(state.validator_balances.len(), 1);
} }
@ -135,7 +144,10 @@ mod tests {
let result = process_deposit(&mut state, &deposit, &spec); let result = process_deposit(&mut state, &deposit, &spec);
deposit.deposit_data.value = DEPOSIT_GWEI; deposit.deposit_data.value = DEPOSIT_GWEI;
assert_eq!(result.unwrap(), i); assert_eq!(result.unwrap(), i);
assert!(deposit_equals_record(&deposit, &state.validator_registry[i])); assert!(deposit_equals_record(
&deposit,
&state.validator_registry[i]
));
assert_eq!(state.validator_registry.len(), i + 1); assert_eq!(state.validator_registry.len(), i + 1);
assert_eq!(state.validator_balances.len(), i + 1); assert_eq!(state.validator_balances.len(), i + 1);
} }
@ -151,7 +163,8 @@ mod tests {
deposit.deposit_data.value = DEPOSIT_GWEI; deposit.deposit_data.value = DEPOSIT_GWEI;
validator.pubkey = deposit.deposit_data.deposit_input.pubkey.clone(); validator.pubkey = deposit.deposit_data.deposit_input.pubkey.clone();
validator.withdrawal_credentials = deposit.deposit_data.deposit_input.withdrawal_credentials; validator.withdrawal_credentials =
deposit.deposit_data.deposit_input.withdrawal_credentials;
validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment; validator.randao_commitment = deposit.deposit_data.deposit_input.randao_commitment;
state.validator_registry.push(validator); state.validator_registry.push(validator);
@ -160,7 +173,10 @@ mod tests {
let result = process_deposit(&mut state, &deposit, &spec); let result = process_deposit(&mut state, &deposit, &spec);
assert_eq!(result.unwrap(), 0); assert_eq!(result.unwrap(), 0);
assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); assert!(deposit_equals_record(
&deposit,
&state.validator_registry[0]
));
assert_eq!(state.validator_balances[0], DEPOSIT_GWEI * 2); assert_eq!(state.validator_balances[0], DEPOSIT_GWEI * 2);
assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_registry.len(), 1);
assert_eq!(state.validator_balances.len(), 1); assert_eq!(state.validator_balances.len(), 1);
@ -183,7 +199,10 @@ mod tests {
let result = process_deposit(&mut state, &deposit, &spec); let result = process_deposit(&mut state, &deposit, &spec);
assert_eq!(result.unwrap(), 0); assert_eq!(result.unwrap(), 0);
assert!(deposit_equals_record(&deposit, &state.validator_registry[0])); assert!(deposit_equals_record(
&deposit,
&state.validator_registry[0]
));
assert_eq!(state.validator_balances[0], DEPOSIT_GWEI); assert_eq!(state.validator_balances[0], DEPOSIT_GWEI);
assert_eq!(state.validator_registry.len(), 1); assert_eq!(state.validator_registry.len(), 1);
assert_eq!(state.validator_balances.len(), 1); assert_eq!(state.validator_balances.len(), 1);
@ -195,11 +214,15 @@ mod tests {
let mut deposit = get_deposit(); let mut deposit = get_deposit();
let spec = ChainSpec::foundation(); let spec = ChainSpec::foundation();
deposit.deposit_data.value = DEPOSIT_GWEI; deposit.deposit_data.value = DEPOSIT_GWEI;
deposit.deposit_data.deposit_input.proof_of_possession = create_proof_of_possession(&Keypair::random()); deposit.deposit_data.deposit_input.proof_of_possession =
create_proof_of_possession(&Keypair::random());
let result = process_deposit(&mut state, &deposit, &spec); let result = process_deposit(&mut state, &deposit, &spec);
assert_eq!(result, Err(ValidatorInductionError::InvaidProofOfPossession)); assert_eq!(
result,
Err(ValidatorInductionError::InvaidProofOfPossession)
);
assert_eq!(state.validator_registry.len(), 0); assert_eq!(state.validator_registry.len(), 0);
assert_eq!(state.validator_balances.len(), 0); assert_eq!(state.validator_balances.len(), 0);
} }

View File

@ -1,8 +1,8 @@
extern crate bls; extern crate bls;
extern crate hashing; extern crate hashing;
extern crate types;
extern crate spec; extern crate spec;
extern crate types;
mod inductor; mod inductor;
pub use crate::inductor::{ValidatorInductionError, process_deposit}; pub use crate::inductor::{process_deposit, ValidatorInductionError};

View File

@ -1,4 +1,4 @@
use chain::{BlockProcessingOutcome, BeaconChain}; use chain::{BeaconChain, BlockProcessingOutcome};
use db::{ use db::{
stores::{BeaconBlockStore, BeaconStateStore}, stores::{BeaconBlockStore, BeaconStateStore},
MemoryDB, MemoryDB,

View File

@ -44,7 +44,8 @@ impl DiskDB {
let db = match columns { let db = match columns {
None => DB::open(&options, db_path), None => DB::open(&options, db_path),
Some(columns) => DB::open_cf(&options, db_path, columns), Some(columns) => DB::open_cf(&options, db_path, columns),
}.expect("Unable to open local database");; }
.expect("Unable to open local database");;
Self { db } Self { db }
} }

View File

@ -42,10 +42,10 @@ mod tests {
use super::super::super::MemoryDB; use super::super::super::MemoryDB;
use super::*; use super::*;
use std::sync::Arc;
use ssz::ssz_encode; use ssz::ssz_encode;
use types::Hash256; use std::sync::Arc;
use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use types::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use types::Hash256;
test_crud_for_store!(BeaconStateStore, DB_COLUMN); test_crud_for_store!(BeaconStateStore, DB_COLUMN);

View File

@ -12,8 +12,8 @@ mod config;
use std::path::PathBuf; use std::path::PathBuf;
use clap::{App, Arg};
use crate::config::LighthouseConfig; use crate::config::LighthouseConfig;
use clap::{App, Arg};
use slog::Drain; use slog::Drain;
fn main() { fn main() {
@ -32,13 +32,15 @@ fn main() {
.value_name("DIR") .value_name("DIR")
.help("Data directory for keys and databases.") .help("Data directory for keys and databases.")
.takes_value(true), .takes_value(true),
).arg( )
.arg(
Arg::with_name("port") Arg::with_name("port")
.long("port") .long("port")
.value_name("PORT") .value_name("PORT")
.help("Network listen port for p2p connections.") .help("Network listen port for p2p connections.")
.takes_value(true), .takes_value(true),
).get_matches(); )
.get_matches();
let mut config = LighthouseConfig::default(); let mut config = LighthouseConfig::default();