mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 01:05:47 +00:00
some cleanup and notes
This commit is contained in:
@@ -216,9 +216,11 @@ impl<T: BeaconChainTypes + 'static> MessageHandler<T> {
|
|||||||
PubsubMessage::Attestation(message) => match self.decode_gossip_attestation(message) {
|
PubsubMessage::Attestation(message) => match self.decode_gossip_attestation(message) {
|
||||||
Ok(attestation) => {
|
Ok(attestation) => {
|
||||||
// TODO: Apply more sophisticated validation and decoding logic - 524 verify attestation
|
// TODO: Apply more sophisticated validation and decoding logic - 524 verify attestation
|
||||||
self.propagate_message(id, peer_id.clone());
|
let should_forward_on = self.message_processor
|
||||||
self.message_processor
|
.on_attestation_gossip(peer_id.clone(), attestation);
|
||||||
.on_attestation_gossip(peer_id, attestation);
|
if should_forward_on {
|
||||||
|
self.propagate_message(id, peer_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
debug!(self.log, "Invalid gossiped attestation"; "peer_id" => format!("{}", peer_id), "Error" => format!("{:?}", e));
|
debug!(self.log, "Invalid gossiped attestation"; "peer_id" => format!("{}", peer_id), "Error" => format!("{:?}", e));
|
||||||
|
|||||||
@@ -418,8 +418,7 @@ impl<T: BeaconChainTypes> MessageProcessor<T> {
|
|||||||
present_slot,
|
present_slot,
|
||||||
block_slot,
|
block_slot,
|
||||||
} if present_slot + FUTURE_SLOT_TOLERANCE >= block_slot => {
|
} if present_slot + FUTURE_SLOT_TOLERANCE >= block_slot => {
|
||||||
//TODO: Decide the logic here
|
self.should_forward_block(block)
|
||||||
SHOULD_NOT_FORWARD_GOSSIP_BLOCK
|
|
||||||
}
|
}
|
||||||
BlockProcessingOutcome::BlockIsAlreadyKnown => {
|
BlockProcessingOutcome::BlockIsAlreadyKnown => {
|
||||||
self.should_forward_block(block)
|
self.should_forward_block(block)
|
||||||
@@ -458,41 +457,57 @@ 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 {
|
||||||
|
// Retrieve the parent block, if it exists.
|
||||||
if let Ok(Some(parent_block)) = self
|
if let Ok(Some(parent_block)) = self
|
||||||
.chain
|
.chain
|
||||||
.store
|
.store
|
||||||
.get::<BeaconBlock<T::EthSpec>>(&block.parent_root)
|
.get::<BeaconBlock<T::EthSpec>>(&block.parent_root)
|
||||||
{
|
{
|
||||||
let mut parent_state = self
|
// Check if the parent block's state is equal to the current state, if it is, then
|
||||||
|
// we can validate the block using the current chain.
|
||||||
|
let mut state =
|
||||||
|
match self.chain.head().beacon_state_root == parent_block.state_root {
|
||||||
|
true => self.chain.head().beacon_state.clone(),
|
||||||
|
false => self
|
||||||
.chain
|
.chain
|
||||||
.store
|
.store
|
||||||
.get::<BeaconState<T::EthSpec>>(&parent_block.state_root)
|
.get::<BeaconState<T::EthSpec>>(&parent_block.state_root)
|
||||||
.unwrap().unwrap(); // should handle better
|
.unwrap().unwrap() // should handle better
|
||||||
|
};
|
||||||
|
|
||||||
|
// Determine the epochal relationship between the state used to validate the block and
|
||||||
|
// the block itself.
|
||||||
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()
|
||||||
).map_err(|_| -> Result<RelativeEpoch, String> {
|
).map_err(|_| -> Result<RelativeEpoch, String> {
|
||||||
for _ in parent_state.slot.as_u64()..block.slot.as_u64() {
|
// If the block is more than one epoch in the future, we must fast-forward to
|
||||||
per_slot_processing(&mut parent_state, &self.chain.spec);
|
// the state and compute the committee.
|
||||||
|
for _ in state.slot.as_u64()..block.slot.as_u64() {
|
||||||
|
per_slot_processing(&mut state, &self.chain.spec);
|
||||||
}
|
}
|
||||||
parent_state.build_committee_cache(RelativeEpoch::Current, &self.chain.spec);
|
|
||||||
|
// Note: this is expensive.
|
||||||
|
state.build_committee_cache(RelativeEpoch::Current, &self.chain.spec);
|
||||||
|
|
||||||
Ok(RelativeEpoch::Current)
|
Ok(RelativeEpoch::Current)
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
|
|
||||||
let proposer_index = parent_state
|
// Retrieve the block's designated proposer.
|
||||||
|
let proposer_index = state
|
||||||
.get_beacon_proposer_index(
|
.get_beacon_proposer_index(
|
||||||
block.slot,
|
block.slot,
|
||||||
relative_epoch,
|
relative_epoch,
|
||||||
&self.chain.spec
|
&self.chain.spec
|
||||||
).unwrap();
|
).unwrap();
|
||||||
let proposer = &parent_state.validators.get(proposer_index).unwrap();
|
let proposer = state.validators.get(proposer_index).unwrap();
|
||||||
|
|
||||||
|
// Create a SignatureSet and validate it.
|
||||||
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
|
&state.fork
|
||||||
);
|
);
|
||||||
|
|
||||||
let set = SignatureSet::single(
|
let set = SignatureSet::single(
|
||||||
@@ -505,13 +520,15 @@ impl<T: BeaconChainTypes> MessageProcessor<T> {
|
|||||||
return set.is_valid();
|
return set.is_valid();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The signature can not be verified without the parent's state, so in this case, we simply
|
||||||
|
// do not forward.
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Process a gossip message declaring a new attestation.
|
/// Process a gossip message declaring a new attestation.
|
||||||
///
|
///
|
||||||
/// Not currently implemented.
|
/// Not currently implemented.
|
||||||
pub fn on_attestation_gossip(&mut self, _peer_id: PeerId, msg: Attestation<T::EthSpec>) {
|
pub fn on_attestation_gossip(&mut self, _peer_id: PeerId, msg: Attestation<T::EthSpec>) -> bool {
|
||||||
match self.chain.process_attestation(msg.clone()) {
|
match self.chain.process_attestation(msg.clone()) {
|
||||||
Ok(outcome) => {
|
Ok(outcome) => {
|
||||||
info!(
|
info!(
|
||||||
@@ -537,7 +554,13 @@ impl<T: BeaconChainTypes> MessageProcessor<T> {
|
|||||||
);
|
);
|
||||||
error!(self.log, "Invalid gossip attestation"; "error" => format!("{:?}", e));
|
error!(self.log, "Invalid gossip attestation"; "error" => format!("{:?}", e));
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn should_forward_attestation(&self, attestation: Attestation<T::EthSpec>) -> bool {
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user