refactor: extract on pipeline finished (#3590)

This commit is contained in:
Matthias Seitz
2023-07-05 11:50:51 +02:00
committed by GitHub
parent 7da36e0421
commit 1e3f0c0e86

View File

@ -27,7 +27,7 @@ use reth_rpc_types::engine::{
ExecutionPayload, ForkchoiceUpdated, PayloadAttributes, PayloadStatus, PayloadStatusEnum,
PayloadValidationError,
};
use reth_stages::{ControlFlow, Pipeline};
use reth_stages::{ControlFlow, Pipeline, PipelineError};
use reth_tasks::TaskSpawner;
use std::{
pin::Pin,
@ -1207,6 +1207,27 @@ where
return Some(Err(BeaconConsensusEngineError::PipelineChannelClosed))
}
EngineSyncEvent::PipelineFinished { result, reached_max_block } => {
return self.on_pipeline_finished(result, reached_max_block)
}
};
None
}
/// Invoked when the pipeline has finished.
///
/// Returns an Option to indicate whether the engine future should resolve:
///
/// Returns a result if:
/// - Ok(()) if the pipeline finished successfully
/// - Err(..) if the pipeline failed fatally
///
/// Returns None if the pipeline finished successfully and engine should continue.
fn on_pipeline_finished(
&mut self,
result: Result<ControlFlow, PipelineError>,
reached_max_block: bool,
) -> Option<Result<(), BeaconConsensusEngineError>> {
trace!(target: "consensus::engine", ?result, ?reached_max_block, "Pipeline finished");
match result {
Ok(ctrl) => {
@ -1231,9 +1252,9 @@ where
Ok(header) => match header {
Some(header) => header,
None => {
return Some(Err(Error::Provider(
ProviderError::HeaderNotFound(max_block.into()),
)
return Some(Err(Error::Provider(ProviderError::HeaderNotFound(
max_block.into(),
))
.into()))
}
},
@ -1245,10 +1266,7 @@ where
self.blockchain.set_canonical_head(max_header);
}
let sync_target_state = match self
.forkchoice_state_tracker
.sync_target_state()
{
let sync_target_state = match self.forkchoice_state_tracker.sync_target_state() {
Some(current_state) => current_state,
None => {
// This is only possible if the node was run with `debug.tip`
@ -1263,8 +1281,8 @@ where
// This can arise if we buffer the forkchoice head, and if the head is an
// ancestor of an invalid block.
//
// * The forkchoice head could be buffered if it were first sent as a
// `newPayload` request.
// * The forkchoice head could be buffered if it were first sent as a `newPayload`
// request.
//
// In this case, we won't have the head hash in the database, so we would
// set the pipeline sync target to a known-invalid head.
@ -1282,9 +1300,9 @@ where
.is_none()
{
// Update the state and hashes of the blockchain tree if possible.
match self.update_tree_on_finished_pipeline(
sync_target_state.finalized_block_hash,
) {
match self
.update_tree_on_finished_pipeline(sync_target_state.finalized_block_hash)
{
Ok(synced) => {
if synced {
// we're consider this synced and transition to live sync
@ -1307,8 +1325,6 @@ where
// Any pipeline error at this point is fatal.
Err(error) => return Some(Err(error.into())),
};
}
};
None
}