From 9bca97e2a4465fb52dc4398d9dede2bf78217d28 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 22 Jul 2024 10:08:49 +0200 Subject: [PATCH] chore: improve engine api event handling (#9682) --- crates/engine/tree/src/chain.rs | 6 +++--- crates/engine/tree/src/engine.rs | 26 +++++++++++--------------- crates/engine/tree/src/tree/mod.rs | 19 ++++++++++++++++--- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/crates/engine/tree/src/chain.rs b/crates/engine/tree/src/chain.rs index 4e7a3e3eb1..09197c090e 100644 --- a/crates/engine/tree/src/chain.rs +++ b/crates/engine/tree/src/chain.rs @@ -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 { /// /// 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 { /// Request an action to backfill sync - BackfillSync(BackfillAction), + BackfillAction(BackfillAction), /// Other event emitted by the handler Event(T), } diff --git a/crates/engine/tree/src/engine.rs b/crates/engine/tree/src/engine.rs index d83dd04fc3..a6cb75271e 100644 --- a/crates/engine/tree/src/engine.rs +++ b/crates/engine/tree/src/engine.rs @@ -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 for EngineApiEvent { diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index 69e14bf7b8..485f13693f 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -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) { let _ = self