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(l1): modularize forkchoice updated code #1406

Closed
wants to merge 3 commits into from

Conversation

mpaulucci
Copy link
Collaborator

Motivation
The motivation is to be able to reutilize code across the different versions of forkchoice updates, but without creating unnecesary abstractions and leaking them to the rest of the codebase.

This is a POC

Description

  • Removed payload_attributes being a result. I think this was introduced to avoid erroing when we can parse payload_attributes succesfully (spec says we should move on with forkchoice updated)
  • Refactored the code to simplify the flow and extract some functions.

@@ -19,24 +20,18 @@ use crate::{
#[derive(Debug)]
pub struct ForkChoiceUpdatedV3 {
pub fork_choice_state: ForkChoiceState,
#[allow(unused)]
pub payload_attributes: Result<Option<PayloadAttributesV3>, String>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed this, since the object type should reflect the json schema that we receive via RPC. Also this was leaking to the outside (see rpc/engine/forkchoice)

@@ -50,10 +45,20 @@ impl RpcHandler for ForkChoiceUpdatedV3 {
if params.len() != 2 {
return Err(RpcErr::BadParams("Expected 2 params".to_owned()));
}

let forkchoice_state: ForkChoiceState = serde_json::from_value(params[0].clone())?;
// if there is an error when parsing, set to None
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To avoid having the Result, if there is a parsing error I set the payload attribute to None. This is not spec compliant, but we should find another way of dealing with this case without changing the type.

}
};
let (head_block_opt, mut response) =
handle_forkchoice(&self.fork_choice_state, context.clone())?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Most of the code has been extracted to handle_forkchoice, which we can reuse across the different FCU versions

}

fn build_payload_v3(
attributes: &PayloadAttributesV3,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here is where we could technically use a PayloadAttributes enum that contains all posibilities

pub enum PayloadAttributes {
    PayloadAttributesV1,
    PayloadAttributesV2,
    PayloadAttributesV3,
}

pub payload_attributes: Option<PayloadAttributesV3>,
}

impl RpcHandler for ForkChoiceUpdatedV2 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A POC of a handler for ForkChoiceUpdatedV2 , theres a bit more of repeated code, but it is way more decoupled and has less abstractions.

@mpaulucci mpaulucci changed the title refactor(l1): Modularize forkchoice updated code refactor(l1): modularize forkchoice updated code Dec 4, 2024
@mpaulucci mpaulucci marked this pull request as ready for review December 4, 2024 19:26
@mpaulucci mpaulucci requested a review from a team as a code owner December 4, 2024 19:26
github-merge-queue bot pushed a commit that referenced this pull request Dec 5, 2024
**Motivation**

We need to support post merge V1, V2 and V3 for `GetPayload`,
`NewPayload` and `ForkChoiceUpdated` functions

**Description**

Moved @mpaulucci ideas from his
[PR](#1406) into [my own
PR](#1395) to get V2 code for
`ForkChoiceUpdated`
It includes the removal of payload_attributes being a result

Closes #892,
#1184,
#1186
@mpaulucci
Copy link
Collaborator Author

Included in #1411

@mpaulucci mpaulucci closed this Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant