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

chore(engine): refactor pipeline outcome processing #7692

Merged
merged 2 commits into from Apr 17, 2024

Conversation

rkrasiuk
Copy link
Member

@rkrasiuk rkrasiuk commented Apr 17, 2024

Description

Refactor and simplify the processing of pipeline outcome.

Remove the anti-pattern of returning Option<Result<(), _>> where the only use case for returning Some(Ok(())) was to indicate that the pipeline has reached the max block.

Gets rid of match result statements and uses residual instead.

@rkrasiuk rkrasiuk added C-debt A section of code is hard to understand or change A-consensus Related to the consensus engine labels Apr 17, 2024
Comment on lines +1401 to +1409
if error.is_fatal() {
error!(target: "consensus::engine", %error, "Encountered fatal error while making sync target canonical: {:?}, {:?}", error, hash);
} else if !error.is_block_hash_not_found() {
debug!(
target: "consensus::engine",
"Unexpected error while making sync target canonical: {:?}, {:?}",
error,
hash
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlockchainTreeError::BlockHashNotFoundInChain is never fatal


let new_head = outcome.into_header();
debug!(target: "consensus::engine", hash=?new_head.hash(), number=new_head.number, "Canonicalized new head");
let Some(target) = self.forkchoice_state_tracker.sync_target_state() else { return Ok(()) };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only flattens if let Some(_) { ... } else { return Ok(()) } to let Some(_) = else { return Ok(()) }; ...

Comment on lines +1574 to +1575
// Terminate the sync early if it's reached the maximum user-configured block.
SyncEventOutcome::ReachedMaxBlock
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returns Ok(SyncEventOutcome::ReachedMaxBlock) instead of Some(Ok(()))

EngineSyncEvent::PipelineFinished { result, reached_max_block } => {
trace!(target: "consensus::engine", ?result, ?reached_max_block, "Pipeline finished");
// Any pipeline error at this point is fatal.
let ctrl = result?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any pipeline error is considered fatal, same as before

Comment on lines -1675 to -1680
let newest_finalized = self
.forkchoice_state_tracker
.sync_target_state()
.map(|s| s.finalized_block_hash)
.and_then(|h| self.blockchain.buffered_header_by_hash(h))
.map(|header| header.number);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this unnecessarily retrieved optional self.forkchoice_state_tracker.sync_target_state(), but we already retrieved and ensured that it's some up above

Comment on lines -1687 to -1695
let pipeline_target = if let (Some(progress), Some(finalized_number)) =
(ctrl.block_number(), newest_finalized)
{
// Determines whether or not we should run the pipeline again, in case the
// new gap is large enough to warrant running the pipeline.
self.can_pipeline_sync_to_finalized(progress, finalized_number, None)
} else {
None
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if let (Some(), Some()) { /* invoke fn that returns an option */ } else { None } is simply refactored to .zip().and_then(...)

@rkrasiuk
Copy link
Member Author

Preemptive hive run, because why not
https://github.com/paradigmxyz/reth/actions/runs/8718483460

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these look okay.

pending hive

@rkrasiuk
Copy link
Member Author

hive results are unchanged

@rkrasiuk rkrasiuk added this pull request to the merge queue Apr 17, 2024
Merged via the queue into main with commit 7cda594 Apr 17, 2024
49 of 57 checks passed
@rkrasiuk rkrasiuk deleted the rkrasiuk/engine-refactor-pipeline-outcome branch April 17, 2024 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-consensus Related to the consensus engine C-debt A section of code is hard to understand or change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants