mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-01 03:33:47 +00:00
Adds beacon chain events, websocket event handler
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::checkpoint::CheckPoint;
|
||||
use crate::errors::{BeaconChainError as Error, BlockProductionError};
|
||||
use crate::eth1_chain::{Eth1Chain, Eth1ChainBackend};
|
||||
use crate::events::{EventHandler, EventKind};
|
||||
use crate::fork_choice::{Error as ForkChoiceError, ForkChoice};
|
||||
use crate::iter::{ReverseBlockRootIterator, ReverseStateRootIterator};
|
||||
use crate::metrics;
|
||||
@@ -95,6 +96,7 @@ pub trait BeaconChainTypes: Send + Sync + 'static {
|
||||
type LmdGhost: LmdGhost<Self::Store, Self::EthSpec>;
|
||||
type Eth1Chain: Eth1ChainBackend<Self::EthSpec>;
|
||||
type EthSpec: types::EthSpec;
|
||||
type EventHandler: EventHandler<Self::EthSpec>;
|
||||
}
|
||||
|
||||
/// Represents the "Beacon Chain" component of Ethereum 2.0. Allows import of blocks and block
|
||||
@@ -117,6 +119,8 @@ pub struct BeaconChain<T: BeaconChainTypes> {
|
||||
/// A state-machine that is updated with information from the network and chooses a canonical
|
||||
/// head block.
|
||||
pub fork_choice: ForkChoice<T>,
|
||||
/// A handler for events generated by the beacon chain.
|
||||
pub event_handler: T::EventHandler,
|
||||
/// Logging to CLI, etc.
|
||||
log: Logger,
|
||||
}
|
||||
@@ -126,6 +130,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
pub fn from_genesis(
|
||||
store: Arc<T::Store>,
|
||||
eth1_backend: T::Eth1Chain,
|
||||
event_handler: T::EventHandler,
|
||||
mut genesis_state: BeaconState<T::EthSpec>,
|
||||
mut genesis_block: BeaconBlock<T::EthSpec>,
|
||||
spec: ChainSpec,
|
||||
@@ -174,6 +179,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
canonical_head,
|
||||
genesis_block_root,
|
||||
fork_choice: ForkChoice::new(store.clone(), &genesis_block, genesis_block_root),
|
||||
event_handler,
|
||||
store,
|
||||
log,
|
||||
})
|
||||
@@ -183,6 +189,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
pub fn from_store(
|
||||
store: Arc<T::Store>,
|
||||
eth1_backend: T::Eth1Chain,
|
||||
event_handler: T::EventHandler,
|
||||
spec: ChainSpec,
|
||||
log: Logger,
|
||||
) -> Result<Option<BeaconChain<T>>, Error> {
|
||||
@@ -219,6 +226,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
slot_clock,
|
||||
fork_choice: ForkChoice::new(store.clone(), last_finalized_block, last_finalized_root),
|
||||
op_pool,
|
||||
event_handler,
|
||||
eth1_chain: Eth1Chain::new(eth1_backend),
|
||||
canonical_head: RwLock::new(p.canonical_head),
|
||||
genesis_block_root: p.genesis_block_root,
|
||||
@@ -629,6 +637,59 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
pub fn process_attestation(
|
||||
&self,
|
||||
attestation: Attestation<T::EthSpec>,
|
||||
) -> Result<AttestationProcessingOutcome, Error> {
|
||||
let outcome = self.process_attestation_internal(attestation.clone());
|
||||
|
||||
match &outcome {
|
||||
Ok(outcome) => match outcome {
|
||||
AttestationProcessingOutcome::Processed => {
|
||||
trace!(
|
||||
self.log,
|
||||
"Beacon attestation imported";
|
||||
"shard" => attestation.data.crosslink.shard,
|
||||
"target_epoch" => attestation.data.target.epoch,
|
||||
);
|
||||
let _ = self
|
||||
.event_handler
|
||||
.register(EventKind::BeaconAttestationImported {
|
||||
attestation: Box::new(attestation),
|
||||
});
|
||||
}
|
||||
other => {
|
||||
warn!(
|
||||
self.log,
|
||||
"Beacon attestation rejected";
|
||||
"reason" => format!("{:?}", other),
|
||||
);
|
||||
let _ = self
|
||||
.event_handler
|
||||
.register(EventKind::BeaconAttestationRejected {
|
||||
reason: format!("Invalid attestation: {:?}", other),
|
||||
attestation: Box::new(attestation),
|
||||
});
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
error!(
|
||||
self.log,
|
||||
"Beacon attestation processing error";
|
||||
"error" => format!("{:?}", e),
|
||||
);
|
||||
let _ = self
|
||||
.event_handler
|
||||
.register(EventKind::BeaconAttestationRejected {
|
||||
reason: format!("Internal error: {:?}", e),
|
||||
attestation: Box::new(attestation),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
outcome
|
||||
}
|
||||
|
||||
pub fn process_attestation_internal(
|
||||
&self,
|
||||
attestation: Attestation<T::EthSpec>,
|
||||
) -> Result<AttestationProcessingOutcome, Error> {
|
||||
metrics::inc_counter(&metrics::ATTESTATION_PROCESSING_REQUESTS);
|
||||
let timer = metrics::start_timer(&metrics::ATTESTATION_PROCESSING_TIMES);
|
||||
@@ -932,6 +993,57 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
pub fn process_block(
|
||||
&self,
|
||||
block: BeaconBlock<T::EthSpec>,
|
||||
) -> Result<BlockProcessingOutcome, Error> {
|
||||
let outcome = self.process_block_internal(block.clone());
|
||||
|
||||
match &outcome {
|
||||
Ok(outcome) => match outcome {
|
||||
BlockProcessingOutcome::Processed { block_root } => {
|
||||
trace!(
|
||||
self.log,
|
||||
"Beacon block imported";
|
||||
"block_root" => format!("{:?}", block_root),
|
||||
"block_slot" => format!("{:?}", block_root),
|
||||
);
|
||||
let _ = self.event_handler.register(EventKind::BeaconBlockImported {
|
||||
block_root: *block_root,
|
||||
block: Box::new(block),
|
||||
});
|
||||
}
|
||||
other => {
|
||||
warn!(
|
||||
self.log,
|
||||
"Beacon block rejected";
|
||||
"reason" => format!("{:?}", other),
|
||||
);
|
||||
let _ = self.event_handler.register(EventKind::BeaconBlockRejected {
|
||||
reason: format!("Invalid block: {:?}", other),
|
||||
block: Box::new(block),
|
||||
});
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
error!(
|
||||
self.log,
|
||||
"Beacon block processing error";
|
||||
"error" => format!("{:?}", e),
|
||||
);
|
||||
let _ = self.event_handler.register(EventKind::BeaconBlockRejected {
|
||||
reason: format!("Internal error: {:?}", e),
|
||||
block: Box::new(block),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
outcome
|
||||
}
|
||||
|
||||
/// Accept some block and attempt to add it to block DAG.
|
||||
///
|
||||
/// Will accept blocks from prior slots, however it will reject any block from a future slot.
|
||||
fn process_block_internal(
|
||||
&self,
|
||||
block: BeaconBlock<T::EthSpec>,
|
||||
) -> Result<BlockProcessingOutcome, Error> {
|
||||
metrics::inc_counter(&metrics::BLOCK_PROCESSING_REQUESTS);
|
||||
let full_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_TIMES);
|
||||
|
||||
Reference in New Issue
Block a user