should_forward_block cleanup

This commit is contained in:
Grant Wuerker
2019-11-07 23:29:52 +09:00
parent 410c8aa2bd
commit 912dbf60b4

View File

@@ -13,8 +13,8 @@ use store::Store;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use tree_hash::SignedRoot; use tree_hash::SignedRoot;
use types::{Attestation, BeaconBlock, Epoch, EthSpec, Hash256, Slot, BeaconState, RelativeEpoch, Domain}; use types::{Attestation, BeaconBlock, Epoch, EthSpec, Hash256, Slot, BeaconState, RelativeEpoch, Domain};
use state_processing::per_block_processing::verify_block_signature;
use bls::SignatureSet; use bls::SignatureSet;
use state_processing::per_slot_processing;
//TODO: Put a maximum limit on the number of block that can be requested. //TODO: Put a maximum limit on the number of block that can be requested.
//TODO: Rate limit requests //TODO: Rate limit requests
@@ -458,20 +458,28 @@ impl<T: BeaconChainTypes> MessageProcessor<T> {
} }
fn should_forward_block(&mut self, block: BeaconBlock<T::EthSpec>) -> bool { fn should_forward_block(&mut self, block: BeaconBlock<T::EthSpec>) -> bool {
let parent_block: BeaconBlock<T::EthSpec> = self.chain.store.get(&block.parent_root).unwrap().unwrap(); if let Ok(Some(parent_block)) = self
let parent_state_root = parent_block.state_root;
let parent_state: BeaconState<T::EthSpec> = self
.chain .chain
.store .store
.get(&parent_state_root) .get::<BeaconBlock<T::EthSpec>>(&block.parent_root)
.unwrap().unwrap(); {
let mut parent_state = self
.chain
.store
.get::<BeaconState<T::EthSpec>>(&parent_block.state_root)
.unwrap().unwrap(); // should handle better
let relative_epoch = RelativeEpoch::from_slot( let relative_epoch = RelativeEpoch::from_slot(
parent_block.slot, parent_block.slot,
block.slot, block.slot,
T::EthSpec::slots_per_epoch() T::EthSpec::slots_per_epoch()
).unwrap(); ).map_err(|_| -> Result<RelativeEpoch, String> {
for _ in parent_state.slot.as_u64()..block.slot.as_u64() {
per_slot_processing(&mut parent_state, &self.chain.spec);
}
parent_state.build_committee_cache(RelativeEpoch::Current, &self.chain.spec);
Ok(RelativeEpoch::Current)
}).unwrap();
let proposer_index = parent_state let proposer_index = parent_state
.get_beacon_proposer_index( .get_beacon_proposer_index(
@@ -484,7 +492,7 @@ impl<T: BeaconChainTypes> MessageProcessor<T> {
let domain = self.chain.spec.get_domain( let domain = self.chain.spec.get_domain(
block.slot.epoch(T::EthSpec::slots_per_epoch()), block.slot.epoch(T::EthSpec::slots_per_epoch()),
Domain::BeaconProposer, Domain::BeaconProposer,
&parent_state.fork, &parent_state.fork
); );
let set = SignatureSet::single( let set = SignatureSet::single(
@@ -494,7 +502,10 @@ impl<T: BeaconChainTypes> MessageProcessor<T> {
domain domain
); );
set.is_valid() return set.is_valid();
}
false
} }
/// Process a gossip message declaring a new attestation. /// Process a gossip message declaring a new attestation.