Fix clippy lints in beacon_chain

This commit is contained in:
Paul Hauner 2019-02-13 10:28:38 +11:00
parent 61bbbab33d
commit d2a1000520
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 26 additions and 45 deletions

View File

@ -17,6 +17,7 @@ const PHASE_0_CUSTODY_BIT: bool = false;
/// ///
/// Note: `Attestations` are stored in memory and never deleted. This is not scalable and must be /// Note: `Attestations` are stored in memory and never deleted. This is not scalable and must be
/// rectified in a future revision. /// rectified in a future revision.
#[derive(Default)]
pub struct AttestationAggregator { pub struct AttestationAggregator {
store: HashMap<Vec<u8>, Attestation>, store: HashMap<Vec<u8>, Attestation>,
} }

View File

@ -1,6 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use types::Hash256; use types::Hash256;
#[derive(Default)]
pub struct AttestationTargets { pub struct AttestationTargets {
map: HashMap<u64, Hash256>, map: HashMap<u64, Hash256>,
} }

View File

@ -67,7 +67,6 @@ pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock> {
pub attestation_aggregator: RwLock<AttestationAggregator>, pub attestation_aggregator: RwLock<AttestationAggregator>,
canonical_head: RwLock<CheckPoint>, canonical_head: RwLock<CheckPoint>,
finalized_head: RwLock<CheckPoint>, finalized_head: RwLock<CheckPoint>,
justified_head: RwLock<CheckPoint>,
pub state: RwLock<BeaconState>, pub state: RwLock<BeaconState>,
pub latest_attestation_targets: RwLock<AttestationTargets>, pub latest_attestation_targets: RwLock<AttestationTargets>,
pub spec: ChainSpec, pub spec: ChainSpec,
@ -106,25 +105,19 @@ where
block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?; block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?;
let block_graph = BlockGraph::new(); let block_graph = BlockGraph::new();
block_graph.add_leaf(&Hash256::zero(), block_root.clone()); block_graph.add_leaf(&Hash256::zero(), block_root);
let finalized_head = RwLock::new(CheckPoint::new( let finalized_head = RwLock::new(CheckPoint::new(
genesis_block.clone(), genesis_block.clone(),
block_root.clone(), block_root,
genesis_state.clone(), genesis_state.clone(),
state_root.clone(), state_root,
));
let justified_head = RwLock::new(CheckPoint::new(
genesis_block.clone(),
block_root.clone(),
genesis_state.clone(),
state_root.clone(),
)); ));
let canonical_head = RwLock::new(CheckPoint::new( let canonical_head = RwLock::new(CheckPoint::new(
genesis_block.clone(), genesis_block.clone(),
block_root.clone(), block_root,
genesis_state.clone(), genesis_state.clone(),
state_root.clone(), state_root,
)); ));
let attestation_aggregator = RwLock::new(AttestationAggregator::new()); let attestation_aggregator = RwLock::new(AttestationAggregator::new());
@ -137,11 +130,10 @@ where
block_graph, block_graph,
attestation_aggregator, attestation_aggregator,
state: RwLock::new(genesis_state.clone()), state: RwLock::new(genesis_state.clone()),
justified_head,
finalized_head, finalized_head,
canonical_head, canonical_head,
latest_attestation_targets, latest_attestation_targets,
spec: spec, spec,
}) })
} }
@ -211,7 +203,7 @@ where
for _ in state_slot.as_u64()..slot.as_u64() { for _ in state_slot.as_u64()..slot.as_u64() {
self.state self.state
.write() .write()
.per_slot_processing(head_block_root.clone(), &self.spec)?; .per_slot_processing(head_block_root, &self.spec)?;
} }
Ok(()) Ok(())
} }
@ -299,30 +291,28 @@ where
/// Produce an `AttestationData` that is valid for the present `slot` and given `shard`. /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> { pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> {
let justified_epoch = self.justified_epoch(); let justified_epoch = self.justified_epoch();
let justified_block_root = self let justified_block_root = *self
.state .state
.read() .read()
.get_block_root( .get_block_root(
justified_epoch.start_slot(self.spec.epoch_length), justified_epoch.start_slot(self.spec.epoch_length),
&self.spec, &self.spec,
) )
.ok_or_else(|| Error::BadRecentBlockRoots)? .ok_or_else(|| Error::BadRecentBlockRoots)?;
.clone();
let epoch_boundary_root = self let epoch_boundary_root = *self
.state .state
.read() .read()
.get_block_root( .get_block_root(
self.state.read().current_epoch_start_slot(&self.spec), self.state.read().current_epoch_start_slot(&self.spec),
&self.spec, &self.spec,
) )
.ok_or_else(|| Error::BadRecentBlockRoots)? .ok_or_else(|| Error::BadRecentBlockRoots)?;
.clone();
Ok(AttestationData { Ok(AttestationData {
slot: self.state.read().slot, slot: self.state.read().slot,
shard, shard,
beacon_block_root: self.head().beacon_block_root.clone(), beacon_block_root: self.head().beacon_block_root,
epoch_boundary_root, epoch_boundary_root,
shard_block_root: Hash256::zero(), shard_block_root: Hash256::zero(),
latest_crosslink: Crosslink { latest_crosslink: Crosslink {
@ -359,7 +349,7 @@ where
let targets = self.latest_attestation_targets.read(); let targets = self.latest_attestation_targets.read();
match targets.get(validator_index) { match targets.get(validator_index) {
Some(hash) => Some(hash.clone()), Some(hash) => Some(*hash),
None => None, None => None,
} }
} }
@ -449,15 +439,11 @@ where
let parent_state = self let parent_state = self
.state_store .state_store
.get_reader(&parent_state_root)? .get_reader(&parent_state_root)?
.ok_or(Error::DBInconsistent(format!( .ok_or_else(|| Error::DBInconsistent(format!("Missing state {}", parent_state_root)))?
"Missing state {}",
parent_state_root
)))?
.into_beacon_state() .into_beacon_state()
.ok_or(Error::DBInconsistent(format!( .ok_or_else(|| {
"State SSZ invalid {}", Error::DBInconsistent(format!("State SSZ invalid {}", parent_state_root))
parent_state_root })?;
)))?;
// TODO: check the block proposer signature BEFORE doing a state transition. This will // TODO: check the block proposer signature BEFORE doing a state transition. This will
// significantly lower exposure surface to DoS attacks. // significantly lower exposure surface to DoS attacks.
@ -465,7 +451,7 @@ where
// Transition the parent state to the present slot. // Transition the parent state to the present slot.
let mut state = parent_state; let mut state = parent_state;
for _ in state.slot.as_u64()..present_slot.as_u64() { for _ in state.slot.as_u64()..present_slot.as_u64() {
if let Err(e) = state.per_slot_processing(parent_block_root.clone(), &self.spec) { if let Err(e) = state.per_slot_processing(parent_block_root, &self.spec) {
return Ok(BlockProcessingOutcome::InvalidBlock( return Ok(BlockProcessingOutcome::InvalidBlock(
InvalidBlock::SlotProcessingError(e), InvalidBlock::SlotProcessingError(e),
)); ));
@ -493,20 +479,14 @@ where
self.state_store.put(&state_root, &ssz_encode(&state)[..])?; self.state_store.put(&state_root, &ssz_encode(&state)[..])?;
// Update the block DAG. // Update the block DAG.
self.block_graph self.block_graph.add_leaf(&parent_block_root, block_root);
.add_leaf(&parent_block_root, block_root.clone());
// If the parent block was the parent_block, automatically update the canonical head. // If the parent block was the parent_block, automatically update the canonical head.
// //
// TODO: this is a first-in-best-dressed scenario that is not ideal; fork_choice should be // TODO: this is a first-in-best-dressed scenario that is not ideal; fork_choice should be
// run instead. // run instead.
if self.head().beacon_block_root == parent_block_root { if self.head().beacon_block_root == parent_block_root {
self.update_canonical_head( self.update_canonical_head(block.clone(), block_root, state.clone(), state_root);
block.clone(),
block_root.clone(),
state.clone(),
state_root.clone(),
);
// Update the local state variable. // Update the local state variable.
*self.state.write() = state.clone(); *self.state.write() = state.clone();
} }
@ -535,15 +515,13 @@ where
attestations.len() attestations.len()
); );
let parent_root = state let parent_root = *state.get_block_root(state.slot.saturating_sub(1_u64), &self.spec)?;
.get_block_root(state.slot.saturating_sub(1_u64), &self.spec)?
.clone();
let mut block = BeaconBlock { let mut block = BeaconBlock {
slot: state.slot, slot: state.slot,
parent_root, parent_root,
state_root: Hash256::zero(), // Updated after the state is calculated. state_root: Hash256::zero(), // Updated after the state is calculated.
randao_reveal: randao_reveal, randao_reveal,
eth1_data: Eth1Data { eth1_data: Eth1Data {
// TODO: replace with real data // TODO: replace with real data
deposit_root: Hash256::zero(), deposit_root: Hash256::zero(),
@ -553,7 +531,7 @@ where
body: BeaconBlockBody { body: BeaconBlockBody {
proposer_slashings: vec![], proposer_slashings: vec![],
attester_slashings: vec![], attester_slashings: vec![],
attestations: attestations, attestations,
deposits: vec![], deposits: vec![],
exits: vec![], exits: vec![],
}, },

View File

@ -11,6 +11,7 @@ use types::Hash256;
/// Presently, the DAG root (genesis block) is not tracked. /// Presently, the DAG root (genesis block) is not tracked.
/// ///
/// The BlogGraph is thread-safe due to internal RwLocks. /// The BlogGraph is thread-safe due to internal RwLocks.
#[derive(Default)]
pub struct BlockGraph { pub struct BlockGraph {
pub leaves: RwLock<HashSet<Hash256>>, pub leaves: RwLock<HashSet<Hash256>>,
} }