mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: move beacon handle type (#13714)
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
use crate::engine::hooks::EngineHookError;
|
||||
use alloy_rpc_types_engine::ForkchoiceUpdateError;
|
||||
pub use reth_engine_primitives::BeaconForkChoiceUpdateError;
|
||||
use reth_errors::{DatabaseError, RethError};
|
||||
use reth_stages_api::PipelineError;
|
||||
|
||||
@ -42,38 +42,3 @@ impl From<DatabaseError> for BeaconConsensusEngineError {
|
||||
Self::Common(e.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents error cases for an applied forkchoice update.
|
||||
///
|
||||
/// This represents all possible error cases, that must be returned as JSON RPC errors back to the
|
||||
/// beacon node.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum BeaconForkChoiceUpdateError {
|
||||
/// Thrown when a forkchoice update resulted in an error.
|
||||
#[error("forkchoice update error: {0}")]
|
||||
ForkchoiceUpdateError(#[from] ForkchoiceUpdateError),
|
||||
/// Thrown when the engine task is unavailable/stopped.
|
||||
#[error("beacon consensus engine task stopped")]
|
||||
EngineUnavailable,
|
||||
/// An internal error occurred, not necessarily related to the update.
|
||||
#[error(transparent)]
|
||||
Internal(Box<dyn core::error::Error + Send + Sync>),
|
||||
}
|
||||
|
||||
impl BeaconForkChoiceUpdateError {
|
||||
/// Create a new internal error.
|
||||
pub fn internal<E: core::error::Error + Send + Sync + 'static>(e: E) -> Self {
|
||||
Self::Internal(Box::new(e))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RethError> for BeaconForkChoiceUpdateError {
|
||||
fn from(e: RethError) -> Self {
|
||||
Self::internal(e)
|
||||
}
|
||||
}
|
||||
impl From<DatabaseError> for BeaconForkChoiceUpdateError {
|
||||
fn from(e: DatabaseError) -> Self {
|
||||
Self::internal(e)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,94 +1,3 @@
|
||||
//! `BeaconConsensusEngine` external API
|
||||
|
||||
use crate::BeaconForkChoiceUpdateError;
|
||||
use alloy_rpc_types_engine::{
|
||||
ExecutionPayload, ExecutionPayloadSidecar, ForkchoiceState, ForkchoiceUpdated, PayloadStatus,
|
||||
};
|
||||
use futures::TryFutureExt;
|
||||
use reth_engine_primitives::{
|
||||
BeaconEngineMessage, BeaconOnNewPayloadError, EngineApiMessageVersion, EngineTypes,
|
||||
OnForkChoiceUpdated,
|
||||
};
|
||||
use reth_errors::RethResult;
|
||||
use tokio::sync::{mpsc::UnboundedSender, oneshot};
|
||||
|
||||
/// A _shareable_ beacon consensus frontend type. Used to interact with the spawned beacon consensus
|
||||
/// engine task.
|
||||
///
|
||||
/// See also `BeaconConsensusEngine`
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BeaconConsensusEngineHandle<Engine>
|
||||
where
|
||||
Engine: EngineTypes,
|
||||
{
|
||||
pub(crate) to_engine: UnboundedSender<BeaconEngineMessage<Engine>>,
|
||||
}
|
||||
|
||||
// === impl BeaconConsensusEngineHandle ===
|
||||
|
||||
impl<Engine> BeaconConsensusEngineHandle<Engine>
|
||||
where
|
||||
Engine: EngineTypes,
|
||||
{
|
||||
/// Creates a new beacon consensus engine handle.
|
||||
pub const fn new(to_engine: UnboundedSender<BeaconEngineMessage<Engine>>) -> Self {
|
||||
Self { to_engine }
|
||||
}
|
||||
|
||||
/// Sends a new payload message to the beacon consensus engine and waits for a response.
|
||||
///
|
||||
/// See also <https://github.com/ethereum/execution-apis/blob/3d627c95a4d3510a8187dd02e0250ecb4331d27e/src/engine/shanghai.md#engine_newpayloadv2>
|
||||
pub async fn new_payload(
|
||||
&self,
|
||||
payload: ExecutionPayload,
|
||||
sidecar: ExecutionPayloadSidecar,
|
||||
) -> Result<PayloadStatus, BeaconOnNewPayloadError> {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.to_engine.send(BeaconEngineMessage::NewPayload { payload, sidecar, tx });
|
||||
rx.await.map_err(|_| BeaconOnNewPayloadError::EngineUnavailable)?
|
||||
}
|
||||
|
||||
/// Sends a forkchoice update message to the beacon consensus engine and waits for a response.
|
||||
///
|
||||
/// See also <https://github.com/ethereum/execution-apis/blob/3d627c95a4d3510a8187dd02e0250ecb4331d27e/src/engine/shanghai.md#engine_forkchoiceupdatedv2>
|
||||
pub async fn fork_choice_updated(
|
||||
&self,
|
||||
state: ForkchoiceState,
|
||||
payload_attrs: Option<Engine::PayloadAttributes>,
|
||||
version: EngineApiMessageVersion,
|
||||
) -> Result<ForkchoiceUpdated, BeaconForkChoiceUpdateError> {
|
||||
Ok(self
|
||||
.send_fork_choice_updated(state, payload_attrs, version)
|
||||
.map_err(|_| BeaconForkChoiceUpdateError::EngineUnavailable)
|
||||
.await??
|
||||
.await?)
|
||||
}
|
||||
|
||||
/// Sends a forkchoice update message to the beacon consensus engine and returns the receiver to
|
||||
/// wait for a response.
|
||||
fn send_fork_choice_updated(
|
||||
&self,
|
||||
state: ForkchoiceState,
|
||||
payload_attrs: Option<Engine::PayloadAttributes>,
|
||||
version: EngineApiMessageVersion,
|
||||
) -> oneshot::Receiver<RethResult<OnForkChoiceUpdated>> {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.to_engine.send(BeaconEngineMessage::ForkchoiceUpdated {
|
||||
state,
|
||||
payload_attrs,
|
||||
tx,
|
||||
version,
|
||||
});
|
||||
rx
|
||||
}
|
||||
|
||||
/// Sends a transition configuration exchange message to the beacon consensus engine.
|
||||
///
|
||||
/// See also <https://github.com/ethereum/execution-apis/blob/3d627c95a4d3510a8187dd02e0250ecb4331d27e/src/engine/paris.md#engine_exchangetransitionconfigurationv1>
|
||||
///
|
||||
/// This only notifies about the exchange. The actual exchange is done by the engine API impl
|
||||
/// itself.
|
||||
pub fn transition_configuration_exchanged(&self) {
|
||||
let _ = self.to_engine.send(BeaconEngineMessage::TransitionConfigurationExchanged);
|
||||
}
|
||||
}
|
||||
pub use reth_engine_primitives::BeaconConsensusEngineHandle;
|
||||
|
||||
Reference in New Issue
Block a user