mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-28 08:37:59 -05:00
chore: improve engine api event handling (#9682)
This commit is contained in:
@@ -112,7 +112,7 @@ where
|
||||
match this.handler.poll(cx) {
|
||||
Poll::Ready(handler_event) => {
|
||||
match handler_event {
|
||||
HandlerEvent::BackfillSync(action) => {
|
||||
HandlerEvent::BackfillAction(action) => {
|
||||
// forward action to backfill_sync
|
||||
this.backfill_sync.on_action(action);
|
||||
continue 'outer
|
||||
@@ -169,7 +169,7 @@ pub enum ChainEvent<T> {
|
||||
///
|
||||
/// The [`ChainOrchestrator`] is responsible for advancing this handler through
|
||||
/// [`ChainHandler::poll`] and handling the emitted events, for example
|
||||
/// [`HandlerEvent::BackfillSync`] to start a backfill sync. Events from the [`ChainOrchestrator`]
|
||||
/// [`HandlerEvent::BackfillAction`] to start a backfill sync. Events from the [`ChainOrchestrator`]
|
||||
/// are passed to the handler via [`ChainHandler::on_event`], e.g.
|
||||
/// [`FromOrchestrator::BackfillSyncStarted`] once the backfill sync started or finished.
|
||||
pub trait ChainHandler: Send + Sync {
|
||||
@@ -187,7 +187,7 @@ pub trait ChainHandler: Send + Sync {
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum HandlerEvent<T> {
|
||||
/// Request an action to backfill sync
|
||||
BackfillSync(BackfillAction),
|
||||
BackfillAction(BackfillAction),
|
||||
/// Other event emitted by the handler
|
||||
Event(T),
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! An engine API handler for the chain.
|
||||
|
||||
use crate::{
|
||||
backfill::BackfillAction,
|
||||
chain::{ChainHandler, FromOrchestrator, HandlerEvent},
|
||||
download::{BlockDownloader, DownloadAction, DownloadOutcome},
|
||||
tree::TreeEvent,
|
||||
};
|
||||
use futures::{Stream, StreamExt};
|
||||
use reth_beacon_consensus::{BeaconConsensusEngineEvent, BeaconEngineMessage};
|
||||
@@ -75,10 +75,10 @@ where
|
||||
match ev {
|
||||
RequestHandlerEvent::HandlerEvent(ev) => {
|
||||
return match ev {
|
||||
HandlerEvent::BackfillSync(target) => {
|
||||
HandlerEvent::BackfillAction(target) => {
|
||||
// bubble up backfill sync request request
|
||||
self.downloader.on_action(DownloadAction::Clear);
|
||||
Poll::Ready(HandlerEvent::BackfillSync(target))
|
||||
Poll::Ready(HandlerEvent::BackfillAction(target))
|
||||
}
|
||||
HandlerEvent::Event(ev) => {
|
||||
// bubble up the event
|
||||
@@ -191,16 +191,10 @@ where
|
||||
EngineApiEvent::BeaconConsensus(ev) => {
|
||||
RequestHandlerEvent::HandlerEvent(HandlerEvent::Event(ev))
|
||||
}
|
||||
EngineApiEvent::FromTree(ev) => match ev {
|
||||
TreeEvent::BackfillAction(target) => {
|
||||
RequestHandlerEvent::HandlerEvent(HandlerEvent::BackfillSync(target))
|
||||
}
|
||||
TreeEvent::Download(download) => RequestHandlerEvent::Download(download),
|
||||
TreeEvent::TreeAction(ev) => {
|
||||
// TODO revise this
|
||||
return Poll::Pending
|
||||
}
|
||||
},
|
||||
EngineApiEvent::BackfillAction(action) => {
|
||||
RequestHandlerEvent::HandlerEvent(HandlerEvent::BackfillAction(action))
|
||||
}
|
||||
EngineApiEvent::Download(action) => RequestHandlerEvent::Download(action),
|
||||
};
|
||||
Poll::Ready(ev)
|
||||
}
|
||||
@@ -212,8 +206,10 @@ pub enum EngineApiEvent {
|
||||
/// Event from the consensus engine.
|
||||
// TODO(mattsse): find a more appropriate name for this variant, consider phasing it out.
|
||||
BeaconConsensus(BeaconConsensusEngineEvent),
|
||||
/// Bubbled from tree.
|
||||
FromTree(TreeEvent),
|
||||
/// Backfill action is needed.
|
||||
BackfillAction(BackfillAction),
|
||||
/// Block download is needed.
|
||||
Download(DownloadRequest),
|
||||
}
|
||||
|
||||
impl From<BeaconConsensusEngineEvent> for EngineApiEvent {
|
||||
|
||||
@@ -404,9 +404,7 @@ where
|
||||
},
|
||||
FromEngine::DownloadedBlocks(blocks) => {
|
||||
if let Some(event) = self.on_downloaded(blocks) {
|
||||
if let Err(err) = self.outgoing.send(EngineApiEvent::FromTree(event)) {
|
||||
error!("Failed to send event: {err:?}");
|
||||
}
|
||||
self.on_tree_event(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -439,6 +437,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles a tree event.
|
||||
fn on_tree_event(&self, event: TreeEvent) {
|
||||
match event {
|
||||
TreeEvent::TreeAction(action) => {
|
||||
// TODO: handle
|
||||
}
|
||||
TreeEvent::BackfillAction(action) => {
|
||||
self.emit_event(EngineApiEvent::BackfillAction(action));
|
||||
}
|
||||
TreeEvent::Download(action) => {
|
||||
self.emit_event(EngineApiEvent::Download(action));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Emits an outgoing event to the engine.
|
||||
fn emit_event(&self, event: impl Into<EngineApiEvent>) {
|
||||
let _ = self
|
||||
|
||||
Reference in New Issue
Block a user