Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(engine): update sync state for r/w hooks #4687

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions crates/consensus/beacon/src/engine/hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use reth_interfaces::sync::SyncState;
use reth_primitives::BlockNumber;
use std::{
fmt::Debug,
Expand Down Expand Up @@ -86,10 +85,7 @@ impl EngineHookEvent {

/// An action that the caller of [hook][`EngineHook`] should act upon.
#[derive(Debug, Copy, Clone)]
pub enum EngineHookAction {
/// Notify about a [SyncState] update.
UpdateSyncState(SyncState),
}
pub enum EngineHookAction {}

/// An error returned by [hook][`EngineHook`].
#[derive(Debug, thiserror::Error)]
Expand Down
11 changes: 2 additions & 9 deletions crates/consensus/beacon/src/engine/hooks/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use futures::FutureExt;
use metrics::Counter;
use reth_db::database::Database;
use reth_interfaces::{sync::SyncState, RethError};
use reth_interfaces::RethError;
use reth_primitives::BlockNumber;
use reth_prune::{Pruner, PrunerError, PrunerWithResult};
use reth_tasks::TaskSpawner;
Expand Down Expand Up @@ -104,14 +104,7 @@ impl<DB: Database + 'static> PruneHook<DB> {
self.metrics.runs.increment(1);
self.pruner_state = PrunerState::Running(rx);

Some((
EngineHookEvent::Started,
// Engine can't process any FCU/payload messages from CL while we're
// pruning, as pruner needs an exclusive write access to the database. To
// prevent CL from sending us unneeded updates, we need to respond `true`
// on `eth_syncing` request.
Some(EngineHookAction::UpdateSyncState(SyncState::Syncing)),
))
Some((EngineHookEvent::Started, None))
} else {
self.pruner_state = PrunerState::Idle(Some(pruner));
Some((EngineHookEvent::NotReady, None))
Expand Down
35 changes: 22 additions & 13 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
message::OnForkChoiceUpdated,
metrics::EngineMetrics,
},
hooks::{EngineContext, EngineHookAction, EngineHooksController},
hooks::{EngineContext, EngineHooksController},
sync::{EngineSyncController, EngineSyncEvent},
};
use futures::{Future, StreamExt};
Expand Down Expand Up @@ -69,7 +69,7 @@ mod handle;
pub use handle::BeaconConsensusEngineHandle;

mod forkchoice;
use crate::hooks::{EngineHooks, PolledHook};
use crate::hooks::{EngineHookEvent, EngineHooks, PolledHook};
pub use forkchoice::ForkchoiceStatus;

mod metrics;
Expand Down Expand Up @@ -1678,19 +1678,28 @@ where

fn on_hook_result(&self, result: PolledHook) -> Result<(), BeaconConsensusEngineError> {
if let Some(action) = result.action {
match action {
EngineHookAction::UpdateSyncState(state) => {
self.sync_state_updater.update_sync_state(state)
}
}
match action {}
}

if result.event.is_finished() && result.db_access_level.is_read_write() {
// If the hook had read-write access to the database,
// it means that the engine may have accumulated some buffered blocks.
if let Err(error) = self.blockchain.connect_buffered_blocks_to_canonical_hashes() {
error!(target: "consensus::engine", ?error, "Error connecting buffered blocks to canonical hashes on hook result");
return Err(error.into())
if result.db_access_level.is_read_write() {
match result.event {
EngineHookEvent::NotReady => {}
EngineHookEvent::Started => {
// If the hook has read-write access to the database, it means that the engine
// can't process any FCU/payload messages from CL. To prevent CL from sending us
// unneeded updates, we need to respond `true` on `eth_syncing` request.
self.sync_state_updater.update_sync_state(SyncState::Syncing)
}
EngineHookEvent::Finished(_) => {
// If the hook had read-write access to the database, it means that the engine
// may have accumulated some buffered blocks.
if let Err(error) =
self.blockchain.connect_buffered_blocks_to_canonical_hashes()
{
error!(target: "consensus::engine", ?error, "Error connecting buffered blocks to canonical hashes on hook result");
return Err(error.into())
}
}
}
}

Expand Down