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

@@ -1,15 +1,6 @@
use super::{ Hash256, Bitfield };
use super::bls::{
AggregateSignature,
BLS_AGG_SIG_BYTE_SIZE,
};
use super::ssz::{
Encodable,
Decodable,
DecodeError,
decode_ssz_list,
SszStream,
};
use super::bls::{AggregateSignature, BLS_AGG_SIG_BYTE_SIZE};
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
use super::{Bitfield, Hash256};
pub const MIN_SSZ_ATTESTION_RECORD_LENGTH: usize = {
8 + // slot
@@ -19,7 +10,7 @@ pub const MIN_SSZ_ATTESTION_RECORD_LENGTH: usize = {
5 + // attester_bitfield (assuming 1 byte of bitfield)
8 + // justified_slot
32 + // justified_block_hash
4 + BLS_AGG_SIG_BYTE_SIZE // aggregate sig (two 256 bit points)
4 + BLS_AGG_SIG_BYTE_SIZE // aggregate sig (two 256 bit points)
};
#[derive(Debug, Clone, PartialEq)]
@@ -48,9 +39,7 @@ impl Encodable for AttestationRecord {
}
impl Decodable for AttestationRecord {
fn ssz_decode(bytes: &[u8], i: usize)
-> Result<(Self, usize), DecodeError>
{
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slot, i) = u64::ssz_decode(bytes, i)?;
let (shard_id, i) = u16::ssz_decode(bytes, i)?;
let (oblique_parent_hashes, i) = decode_ssz_list(bytes, i)?;
@@ -60,8 +49,8 @@ impl Decodable for AttestationRecord {
let (justified_block_hash, i) = Hash256::ssz_decode(bytes, i)?;
// Do aggregate sig decoding properly.
let (agg_sig_bytes, i) = decode_ssz_list(bytes, i)?;
let aggregate_sig = AggregateSignature::from_bytes(&agg_sig_bytes)
.map_err(|_| DecodeError::TooShort)?; // also could be TooLong
let aggregate_sig =
AggregateSignature::from_bytes(&agg_sig_bytes).map_err(|_| DecodeError::TooShort)?; // also could be TooLong
let attestation_record = Self {
slot,
@@ -92,11 +81,10 @@ impl AttestationRecord {
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::ssz::SszStream;
use super::*;
#[test]
pub fn test_attestation_record_min_ssz_length() {
@@ -124,11 +112,13 @@ mod tests {
let mut ssz_stream = SszStream::new();
ssz_stream.append(&original);
let (decoded, _) = AttestationRecord::
ssz_decode(&ssz_stream.drain(), 0).unwrap();
let (decoded, _) = AttestationRecord::ssz_decode(&ssz_stream.drain(), 0).unwrap();
assert_eq!(original.slot, decoded.slot);
assert_eq!(original.shard_id, decoded.shard_id);
assert_eq!(original.oblique_parent_hashes, decoded.oblique_parent_hashes);
assert_eq!(
original.oblique_parent_hashes,
decoded.oblique_parent_hashes
);
assert_eq!(original.shard_block_hash, decoded.shard_block_hash);
assert_eq!(original.attester_bitfield, decoded.attester_bitfield);
assert_eq!(original.justified_slot, decoded.justified_slot);

View File

@@ -1,12 +1,7 @@
use super::Hash256;
use super::attestation_record::AttestationRecord;
use super::special_record::SpecialRecord;
use super::ssz::{
Encodable,
Decodable,
DecodeError,
SszStream,
};
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::Hash256;
pub const MIN_SSZ_BLOCK_LENGTH: usize = {
8 + // slot
@@ -16,7 +11,7 @@ pub const MIN_SSZ_BLOCK_LENGTH: usize = {
32 + // active_state_root
32 + // crystallized_state_root
4 + // attestations (assuming empty)
4 // specials (assuming empty)
4 // specials (assuming empty)
};
pub const MAX_SSZ_BLOCK_LENGTH: usize = MIN_SSZ_BLOCK_LENGTH + (1 << 24);
@@ -68,9 +63,7 @@ impl Encodable for BeaconBlock {
}
impl Decodable for BeaconBlock {
fn ssz_decode(bytes: &[u8], i: usize)
-> Result<(Self, usize), DecodeError>
{
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slot, i) = u64::ssz_decode(bytes, i)?;
let (randao_reveal, i) = Hash256::ssz_decode(bytes, i)?;
let (pow_chain_reference, i) = Hash256::ssz_decode(bytes, i)?;
@@ -87,13 +80,12 @@ impl Decodable for BeaconBlock {
active_state_root,
crystallized_state_root,
attestations,
specials
specials,
};
Ok((block, i))
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -21,7 +21,7 @@ impl ChainConfig {
pub fn standard() -> Self {
Self {
cycle_length: 64,
deposit_size_gwei: 32 * (10^9),
deposit_size_gwei: 32 * (10 ^ 9),
shard_count: 1024,
min_committee_size: 128,
max_validator_churn_quotient: 32,
@@ -32,28 +32,26 @@ impl ChainConfig {
}
pub fn validate(&self) -> bool {
// criteria that ensure the config is valid
// criteria that ensure the config is valid
// shard_count / cycle_length > 0 otherwise validator delegation
// will fail.
if self.shard_count / u16::from(self.cycle_length) == 0 {
return false;
}
// shard_count / cycle_length > 0 otherwise validator delegation
// will fail.
if self.shard_count / u16::from(self.cycle_length) == 0 {
return false;
}
true
true
}
#[cfg(test)]
pub fn super_fast_tests() -> Self {
Self {
cycle_length: 2,
deposit_size_gwei: 32 * (10^9),
deposit_size_gwei: 32 * (10 ^ 9),
shard_count: 2,
min_committee_size: 2,
max_validator_churn_quotient: 32,
genesis_time: TEST_GENESIS_TIME, // arbitrary
genesis_time: TEST_GENESIS_TIME, // arbitrary
slot_duration_millis: 16 * 1000,
initial_validators: vec![],
}

View File

@@ -1,37 +1,33 @@
extern crate ethereum_types;
extern crate bls;
extern crate boolean_bitfield;
extern crate ethereum_types;
extern crate ssz;
pub mod active_state;
pub mod attestation_record;
pub mod crystallized_state;
pub mod chain_config;
pub mod beacon_block;
pub mod chain_config;
pub mod crosslink_record;
pub mod crystallized_state;
pub mod shard_and_committee;
pub mod special_record;
pub mod validator_record;
pub mod validator_registration;
use self::ethereum_types::{
H256,
H160,
U256,
};
use self::boolean_bitfield::BooleanBitfield;
use self::ethereum_types::{H160, H256, U256};
use std::collections::HashMap;
pub use active_state::ActiveState;
pub use attestation_record::AttestationRecord;
pub use crystallized_state::CrystallizedState;
pub use chain_config::ChainConfig;
pub use beacon_block::BeaconBlock;
pub use chain_config::ChainConfig;
pub use crosslink_record::CrosslinkRecord;
pub use crystallized_state::CrystallizedState;
pub use shard_and_committee::ShardAndCommittee;
pub use special_record::{ SpecialRecord, SpecialRecordKind };
pub use validator_record::{ ValidatorRecord, ValidatorStatus };
pub use validator_registration::{ ValidatorRegistration };
pub use special_record::{SpecialRecord, SpecialRecordKind};
pub use validator_record::{ValidatorRecord, ValidatorStatus};
pub use validator_registration::ValidatorRegistration;
pub type Hash256 = H256;
pub type Address = H160;

View File

@@ -1,7 +1,7 @@
#[derive(Clone, Debug, PartialEq)]
pub struct ShardAndCommittee {
pub shard: u16,
pub committee: Vec<usize>
pub committee: Vec<usize>,
}
impl ShardAndCommittee {

View File

@@ -1,10 +1,4 @@
use super::ssz::{
Encodable,
Decodable,
DecodeError,
SszStream,
};
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
/// The value of the "type" field of SpecialRecord.
///
@@ -16,7 +10,6 @@ pub enum SpecialRecordKind {
RandaoChange = 2,
}
/// The structure used in the `BeaconBlock.specials` field.
#[derive(Debug, PartialEq, Clone)]
pub struct SpecialRecord {
@@ -51,13 +44,14 @@ impl SpecialRecord {
/// Returns `None` if `self.kind` is an unknown value.
pub fn resolve_kind(&self) -> Option<SpecialRecordKind> {
match self.kind {
x if x == SpecialRecordKind::Logout as u8
=> Some(SpecialRecordKind::Logout),
x if x == SpecialRecordKind::CasperSlashing as u8
=> Some(SpecialRecordKind::CasperSlashing),
x if x == SpecialRecordKind::RandaoChange as u8
=> Some(SpecialRecordKind::RandaoChange),
_ => None
x if x == SpecialRecordKind::Logout as u8 => Some(SpecialRecordKind::Logout),
x if x == SpecialRecordKind::CasperSlashing as u8 => {
Some(SpecialRecordKind::CasperSlashing)
}
x if x == SpecialRecordKind::RandaoChange as u8 => {
Some(SpecialRecordKind::RandaoChange)
}
_ => None,
}
}
}
@@ -70,16 +64,13 @@ impl Encodable for SpecialRecord {
}
impl Decodable for SpecialRecord {
fn ssz_decode(bytes: &[u8], i: usize)
-> Result<(Self, usize), DecodeError>
{
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (kind, i) = u8::ssz_decode(bytes, i)?;
let (data, i) = Decodable::ssz_decode(bytes, i)?;
Ok((SpecialRecord{kind, data}, i))
Ok((SpecialRecord { kind, data }, i))
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -132,7 +123,10 @@ mod tests {
let s = SpecialRecord::randao_change(&vec![]);
assert_eq!(s.resolve_kind(), Some(SpecialRecordKind::RandaoChange));
let s = SpecialRecord { kind: 88, data: vec![] };
let s = SpecialRecord {
kind: 88,
data: vec![],
};
assert_eq!(s.resolve_kind(), None);
}
}

View File

@@ -1,20 +1,14 @@
use super::{
Hash256,
Address,
};
use super::bls::{
PublicKey,
Keypair
};
use super::bls::{Keypair, PublicKey};
use super::{Address, Hash256};
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ValidatorStatus {
PendingActivation = 0,
Active = 1,
PendingExit = 2,
PendingWithdraw = 3,
Withdrawn = 5,
Penalized = 127,
PendingActivation = 0,
Active = 1,
PendingExit = 2,
PendingWithdraw = 3,
Withdrawn = 5,
Penalized = 127,
}
#[derive(Debug, Clone, PartialEq)]
@@ -50,7 +44,6 @@ impl ValidatorRecord {
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -1,14 +1,5 @@
use bls::{
create_proof_of_possession,
Keypair,
PublicKey,
Signature,
};
use super::{
Address,
Hash256,
};
use super::{Address, Hash256};
use bls::{create_proof_of_possession, Keypair, PublicKey, Signature};
/// The information gathered from the PoW chain validator registration function.
#[derive(Debug, Clone, PartialEq)]