Skip to content

Commit 390fcaf

Browse files
authored
add engine api types (#3)
* add some rpc types * fmt
1 parent 2d16ffb commit 390fcaf

File tree

15 files changed

+699
-20
lines changed

15 files changed

+699
-20
lines changed

based/Cargo.lock

Lines changed: 373 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

based/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ repository = "https://github.com/gattaca-com/based-op"
1010

1111
[workspace.dependencies]
1212
bop-common = { path = "crates/common" }
13+
bop-rpc = { path = "crates/rpc" }
1314

1415
alloy-consensus = "0.9.2"
1516
alloy-eips = "0.9.2"
@@ -20,13 +21,13 @@ op-alloy-rpc-types-engine = "0.9.6"
2021
axum = { version = "0.8.1", features = ["macros"] }
2122
tokio = { version = "1.43.0", features = ["full"] }
2223
reqwest = { version = "0.12.12", features = ["json"] }
23-
jsonrpsee-types = "0.24.8"
24+
jsonrpsee = { version = "0.24", features = ["http-client", "server", "macros"] }
2425

2526
tracing = "0.1.41"
2627
tracing-appender = "0.2.3"
2728
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
2829

29-
serde = "1.0.217"
30+
serde = { version = "1.0.217", features = ["derive"] }
3031
serde_json = "1.0.137"
3132
toml = "0.8.19"
3233

@@ -47,4 +48,4 @@ shared_memory = "^0.12"
4748

4849
# threading
4950
core_affinity = "0.8.1"
50-
crossbeam-channel = "0.5.13"
51+
crossbeam-channel = "0.5.14"

based/bin/gateway/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[package]
22
name = "bop-gateway"
3-
version = "0.1.0"
4-
edition = "2021"
3+
version.workspace = true
4+
edition.workspace = true
5+
rust-version.workspace = true
56

67
[dependencies]
78
bop-common.workspace = true

based/bin/mux/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[package]
22
name = "bop-mux"
3-
version = "0.1.0"
4-
edition = "2021"
3+
version.workspace = true
4+
edition.workspace = true
5+
rust-version.workspace = true
56

67
[dependencies]
78
bop-common.workspace = true
@@ -15,6 +16,6 @@ serde_json.workspace = true
1516
op-alloy-rpc-types-engine.workspace = true
1617
alloy-rpc-types.workspace = true
1718
uuid.workspace = true
18-
jsonrpsee-types.workspace = true
19+
jsonrpsee.workspace = true
1920
thiserror.workspace = true
2021
eyre.workspace = true

based/bin/mux/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use axum::{
22
response::{IntoResponse, Response},
33
Json,
44
};
5-
use jsonrpsee_types::{ErrorCode, ErrorObject};
5+
use jsonrpsee::types::{ErrorCode, ErrorObject};
66
use reqwest::StatusCode;
77

8-
pub type RpcResult<T> = std::result::Result<T, jsonrpsee_types::ErrorObjectOwned>;
8+
pub type RpcResult<T> = std::result::Result<T, jsonrpsee::types::ErrorObjectOwned>;
99

1010
#[derive(thiserror::Error, Debug)]
1111
pub enum MuxError {
@@ -22,7 +22,7 @@ pub enum MuxError {
2222
TokioJoin(#[from] tokio::task::JoinError),
2323

2424
#[error("jsonrpsee error: {0}")]
25-
Jsonrpsee(#[from] jsonrpsee_types::ErrorObject<'static>),
25+
Jsonrpsee(#[from] jsonrpsee::types::ErrorObject<'static>),
2626
}
2727

2828
impl IntoResponse for MuxError {

based/bin/mux/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use axum::{
1010
};
1111
use bop_common::utils::{init_tracing, wait_for_signal};
1212
use error::{MuxError, RpcResult};
13-
use jsonrpsee_types::{Id, Request};
13+
use jsonrpsee::types::{Id, Request};
1414
use op_alloy_rpc_types_engine::OpExecutionPayloadEnvelopeV3;
1515
use reqwest::{header, Client, StatusCode, Url};
1616
use serde_json::{self};
@@ -182,7 +182,7 @@ async fn mux_request(
182182

183183
// validate with fallback
184184
let request = Request {
185-
jsonrpc: jsonrpsee_types::TwoPointZero,
185+
jsonrpc: jsonrpsee::types::TwoPointZero,
186186
id: Id::Number(0),
187187
method: NEW_PAYLOAD_METHOD.into(),
188188
params: Some(params),

based/crates/common/Cargo.toml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
[package]
22
name = "bop-common"
3-
version = "0.1.0"
4-
edition = "2021"
3+
version.workspace = true
4+
edition.workspace = true
5+
rust-version.workspace = true
56

67
[dependencies]
8+
alloy-primitives.workspace = true
9+
op-alloy-rpc-types-engine.workspace = true
10+
alloy-rpc-types.workspace = true
711

12+
crossbeam-channel.workspace = true
813
tokio.workspace = true
914
tracing.workspace = true
1015
tracing-subscriber.workspace = true
1116
tracing-appender.workspace = true
1217

13-
serde = { workspace = true, features = ["derive"] }
18+
serde.workspace = true
1419
thiserror.workspace = true
1520
eyre.workspace = true
1621
once_cell.workspace = true
@@ -19,4 +24,8 @@ quanta.workspace = true
1924
directories.workspace = true
2025
shared_memory.workspace = true
2126

22-
crossbeam-channel.workspace = true
27+
28+
jsonrpsee.workspace = true
29+
30+
[dev-dependencies]
31+
serde_json.workspace = true

based/crates/common/src/api.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use alloy_primitives::{Address, Bytes, B256, U256};
2+
use alloy_rpc_types::{
3+
engine::{ExecutionPayloadV3, ForkchoiceState, ForkchoiceUpdated, PayloadId, PayloadStatus},
4+
Block, BlockId, BlockNumberOrTag, TransactionReceipt,
5+
};
6+
use jsonrpsee::proc_macros::rpc;
7+
use op_alloy_rpc_types_engine::{OpExecutionPayloadEnvelopeV3, OpPayloadAttributes};
8+
9+
use super::rpc::RpcResult;
10+
11+
pub const CAPABILITIES: &[&str] = &["engine_forkchoiceUpdatedV3", "engine_getPayloadV3", "engine_newPayloadV3"];
12+
13+
/// The Engine API is used by the consensus layer to interact with the execution layer. Here we
14+
/// implement a minimal subset of the API for the gateway to return blocks to the op-node
15+
///
16+
/// ref: https://github.com/ethereum/execution-apis/tree/main/src/engine
17+
/// ref: https://specs.optimism.io/protocol/exec-engine.html#engine-api
18+
///
19+
/// NOTE: currently only v3 endpoints are supported
20+
#[rpc(client, server, namespace = "engine")]
21+
pub trait EngineApi {
22+
/// Used by the op-node to set which blocks are considered canonical.
23+
///
24+
/// If payload attributes is set then block production for next block should start and a
25+
/// `PayloadId` is returned to be called in `get_payload`
26+
#[method(name = "forkchoiceUpdatedV3")]
27+
async fn fork_choice_updated_v3(
28+
&self,
29+
fork_choice_state: ForkchoiceState,
30+
payload_attributes: Option<OpPayloadAttributes>,
31+
) -> RpcResult<ForkchoiceUpdated>;
32+
33+
/// Used to validate an execution payload
34+
#[method(name = "newPayloadV3")]
35+
async fn new_payload_v3(
36+
&self,
37+
payload: ExecutionPayloadV3,
38+
versioned_hashes: Vec<B256>,
39+
parent_beacon_block_root: B256,
40+
) -> RpcResult<PayloadStatus>;
41+
42+
/// Used to fetch an execution payload from a previous `payload_id` set in `forkchoiceUpdatedV3`
43+
#[method(name = "getPayloadV3")]
44+
async fn get_payload_v3(&self, payload_id: PayloadId) -> RpcResult<OpExecutionPayloadEnvelopeV3>;
45+
}
46+
47+
/// The Eth API is used to interact with the EL directly.
48+
///
49+
/// This is a temporary API that the gateway implements to serve the latest preconf state, before a
50+
/// gossip protocol is implemented in op-node. Historical state will not be served from this API
51+
#[rpc(client, server, namespace = "eth")]
52+
pub trait EthApi {
53+
/// Sends signed transaction, returning its hash
54+
#[method(name = "sendRawTransaction")]
55+
async fn send_raw_transaction(&self, bytes: Bytes) -> RpcResult<B256>;
56+
57+
/// Returns the receipt of a transaction by transaction hash
58+
#[method(name = "getTransactionReceipt")]
59+
async fn transaction_receipt(&self, hash: B256) -> RpcResult<Option<TransactionReceipt>>;
60+
61+
/// Returns the number of most recent block
62+
#[method(name = "blockNumber")]
63+
async fn block_number(&self) -> RpcResult<U256>;
64+
65+
/// Returns a block with a given identifier
66+
#[method(name = "getBlockByNumber")]
67+
async fn block_by_number(&self, number: BlockNumberOrTag, full: bool) -> RpcResult<Option<Block>>;
68+
69+
/// Returns information about a block by hash.
70+
#[method(name = "getBlockByHash")]
71+
async fn block_by_hash(&self, hash: B256, full: bool) -> RpcResult<Option<Block>>;
72+
73+
/// Returns the nonce of a given address at a given block number.
74+
#[method(name = "getTransactionCount")]
75+
async fn transaction_count(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<U256>;
76+
77+
/// Returns the balance of the account of given address.
78+
#[method(name = "getBalance")]
79+
async fn balance(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<U256>;
80+
}

based/crates/common/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::{net::SocketAddr, time::Duration};
2+
3+
#[derive(Debug, Clone)]
4+
pub struct Config {
5+
pub engine_api_addr: SocketAddr,
6+
/// Internal RPC timeout to wait for engine API response
7+
pub engine_api_timeout: Duration,
8+
}

based/crates/common/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
pub mod api;
12
pub mod communication;
3+
pub mod config;
4+
pub mod rpc;
5+
pub mod runtime;
26
pub mod time;
37
pub mod utils;

0 commit comments

Comments
 (0)