2021-09-29 22:14:15 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
use eth1::http::RpcError;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
pub const LATEST_TAG: &str = "latest";
|
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
use crate::engines::ForkChoiceState;
|
2022-02-28 22:07:48 +00:00
|
|
|
pub use types::{Address, EthSpec, ExecutionBlockHash, ExecutionPayload, Hash256, Uint256};
|
2021-09-29 22:14:15 +00:00
|
|
|
|
|
|
|
pub mod http;
|
2021-11-15 06:13:38 +00:00
|
|
|
pub mod json_structures;
|
2021-09-29 22:14:15 +00:00
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
pub type PayloadId = [u8; 8];
|
2021-09-29 22:14:15 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Reqwest(reqwest::Error),
|
|
|
|
BadResponse(String),
|
|
|
|
RequestFailed(String),
|
2022-02-28 22:07:48 +00:00
|
|
|
InvalidExecutePayloadResponse(&'static str),
|
2021-09-29 22:14:15 +00:00
|
|
|
JsonRpc(RpcError),
|
|
|
|
Json(serde_json::Error),
|
|
|
|
ServerMessage { code: i64, message: String },
|
|
|
|
Eip155Failure,
|
|
|
|
IsSyncing,
|
2022-02-28 22:07:48 +00:00
|
|
|
ExecutionBlockNotFound(ExecutionBlockHash),
|
2021-09-29 22:14:15 +00:00
|
|
|
ExecutionHeadBlockNotFound,
|
2022-02-28 22:07:48 +00:00
|
|
|
ParentHashEqualsBlockHash(ExecutionBlockHash),
|
2021-11-15 06:13:38 +00:00
|
|
|
PayloadIdUnavailable,
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::Error> for Error {
|
|
|
|
fn from(e: reqwest::Error) -> Self {
|
|
|
|
Error::Reqwest(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<serde_json::Error> for Error {
|
|
|
|
fn from(e: serde_json::Error) -> Self {
|
|
|
|
Error::Json(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A generic interface for an execution engine API.
|
|
|
|
#[async_trait]
|
|
|
|
pub trait EngineApi {
|
|
|
|
async fn upcheck(&self) -> Result<(), Error>;
|
|
|
|
|
|
|
|
async fn get_block_by_number<'a>(
|
|
|
|
&self,
|
|
|
|
block_by_number: BlockByNumberQuery<'a>,
|
|
|
|
) -> Result<Option<ExecutionBlock>, Error>;
|
|
|
|
|
|
|
|
async fn get_block_by_hash<'a>(
|
|
|
|
&self,
|
2022-02-28 22:07:48 +00:00
|
|
|
block_hash: ExecutionBlockHash,
|
2021-09-29 22:14:15 +00:00
|
|
|
) -> Result<Option<ExecutionBlock>, Error>;
|
|
|
|
|
2022-02-17 21:47:06 +00:00
|
|
|
async fn new_payload_v1<T: EthSpec>(
|
2021-09-29 22:14:15 +00:00
|
|
|
&self,
|
|
|
|
execution_payload: ExecutionPayload<T>,
|
2022-02-17 21:47:06 +00:00
|
|
|
) -> Result<PayloadStatusV1, Error>;
|
2021-09-29 22:14:15 +00:00
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
async fn get_payload_v1<T: EthSpec>(
|
2021-09-29 22:14:15 +00:00
|
|
|
&self,
|
|
|
|
payload_id: PayloadId,
|
|
|
|
) -> Result<ExecutionPayload<T>, Error>;
|
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
async fn forkchoice_updated_v1(
|
2021-09-29 22:14:15 +00:00
|
|
|
&self,
|
2021-11-15 06:13:38 +00:00
|
|
|
forkchoice_state: ForkChoiceState,
|
|
|
|
payload_attributes: Option<PayloadAttributes>,
|
|
|
|
) -> Result<ForkchoiceUpdatedResponse, Error>;
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2022-02-17 21:47:06 +00:00
|
|
|
pub enum PayloadStatusV1Status {
|
2021-09-29 22:14:15 +00:00
|
|
|
Valid,
|
|
|
|
Invalid,
|
|
|
|
Syncing,
|
2022-02-17 21:47:06 +00:00
|
|
|
Accepted,
|
|
|
|
InvalidBlockHash,
|
|
|
|
InvalidTerminalBlock,
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 06:13:38 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2022-02-17 21:47:06 +00:00
|
|
|
pub struct PayloadStatusV1 {
|
|
|
|
pub status: PayloadStatusV1Status,
|
2022-02-28 22:07:48 +00:00
|
|
|
pub latest_valid_hash: Option<ExecutionBlockHash>,
|
2021-12-12 09:04:21 +00:00
|
|
|
pub validation_error: Option<String>,
|
2021-09-29 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum BlockByNumberQuery<'a> {
|
|
|
|
Tag(&'a str),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ExecutionBlock {
|
2021-10-02 01:39:11 +00:00
|
|
|
#[serde(rename = "hash")]
|
2022-02-28 22:07:48 +00:00
|
|
|
pub block_hash: ExecutionBlockHash,
|
2021-10-02 01:39:11 +00:00
|
|
|
#[serde(rename = "number", with = "eth2_serde_utils::u64_hex_be")]
|
2021-09-29 22:14:15 +00:00
|
|
|
pub block_number: u64,
|
2022-02-28 22:07:48 +00:00
|
|
|
pub parent_hash: ExecutionBlockHash,
|
2021-09-29 22:14:15 +00:00
|
|
|
pub total_difficulty: Uint256,
|
|
|
|
}
|
2021-11-15 06:13:38 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct PayloadAttributes {
|
|
|
|
pub timestamp: u64,
|
2022-03-03 02:10:57 +00:00
|
|
|
pub prev_randao: Hash256,
|
2021-12-12 09:04:21 +00:00
|
|
|
pub suggested_fee_recipient: Address,
|
2021-11-15 06:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub struct ForkchoiceUpdatedResponse {
|
2022-02-17 21:47:06 +00:00
|
|
|
pub payload_status: PayloadStatusV1,
|
2021-11-15 06:13:38 +00:00
|
|
|
pub payload_id: Option<PayloadId>,
|
|
|
|
}
|