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

Bsc block import #14784

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft

Bsc block import #14784

wants to merge 12 commits into from

Conversation

loocapro
Copy link
Contributor

@loocapro loocapro commented Mar 1, 2025

Block Import Process for BSC

This PR enables block import for BSC with the following steps:

  1. Receive a NewBlockMessage from the network.
  2. Validate the block to ensure it adheres to consensus rules.
  3. Convert the block into execution data.
  4. Send the execution data to the engine service.
  5. Handle Parlia Fork Choice Update (FCU) based on block height and hash.
  6. Send the FCU message to the engine service.
  • BscBlockImport trait: wrapper on 2 channels to pass and receive messages back from the service
  • BscBlockImportService: standalone future that knows how to communicate with the EngineService and make a new block the new head
  • FCU handling: follow highest height, same height select lower block hash
  • Tests

@emhane emhane added A-networking Related to networking in general C-example Examples labels Mar 10, 2025
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.

some suggestions, but overall this is pretty good already

Comment on lines 55 to 60
/// Engine error
#[error(transparent)]
Engine(#[from] BeaconOnNewPayloadError),
/// Invalid payload
#[error("invalid payload: {0}")]
InvalidPayload(String),
Copy link
Collaborator

Choose a reason for hiding this comment

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

do you want to open another pr that introduces a Other<Box<dyn Error> variant for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here you go:

#14938

@jenpaff jenpaff linked an issue Mar 11, 2025 that may be closed by this pull request
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.

some additional suggestions, but over all this looks pretty good.

let's try to get the example working and then we can include this and make more incremental progress

Comment on lines +14 to +16
pub struct BscBlockImport<EngineT: EngineTypes> {
handle: ImportHandle<EngineT>,
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can keep this wrapper type for now but maybe we can also impl the trait directly on the handle

/// Future that processes a block import and returns its outcome
type ImportFuture<T> = Pin<Box<dyn Future<Output = Outcome<T>> + Send + Sync>>;

pub struct ImportHandle<T: EngineTypes> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's add some docs that describe how this is wired

Comment on lines +59 to +60
) -> Result<(), Box<dyn std::error::Error>> {
self.to_import.send((block, peer_id))?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should use the same error type here and mention that this returns an error if the channel is closed

pending_imports: FuturesUnordered<ImportFuture<T>>,
}

impl<Provider: BlockNumReader + Clone + 'static, T: EngineTypes> ImportService<Provider, T> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's put this in a where clause instead

Comment on lines +161 to +163
// Send forkchoice update if the payload is valid
if let Err(err) =
Self::send_fork_choice_update(&engine, &provider, hash, number).await
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's do this in two steps and avoid including to much in a single operation

new payload -> 1 future
on outcome -> next future

Comment on lines +225 to +228
/// Determines the head block hash according to Parlia consensus rules:
/// 1. Follow the highest block number
/// 2. For same height blocks, pick the one with lower hash
pub(crate) fn canonical_head(
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can likely start with a simple struct ParliaConsesnsus struct and include all of the logic in there, this should make upcoming refactoring work easier


/// Sends a new payload to the engine and waits for the outcome.
/// If it is valid, proceeds with Forkchoice Update (FCU).
fn import(&self, block: BlockMsg<T>, peer_id: PeerId) -> ImportFuture<T> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's rename this to something fn on_block

}
}

impl<Provider: BlockNumReader + Clone + 'static + Unpin, T: EngineTypes> Future
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer a where clause here as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-networking Related to networking in general C-example Examples
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

SDK example for Reth-BSC
3 participants