Skip to content

Commit

Permalink
fix(rpc): RPC Crate Cleanup (#126)
Browse files Browse the repository at this point in the history
### Description

Cleans up the `maili-rpc` crate.
  • Loading branch information
refcell authored Jan 23, 2025
1 parent b3bd14c commit 8240517
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 307 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ maili-registry = { version = "0.1.7", path = "crates/registry", default-features
maili-superchain = { version = "0.1.7", path = "crates/registry/superchain", default-features = false }

# OP Alloy
op-alloy-consensus = { version = "0.9.6", default-features = false }
op-alloy-flz = { version = "0.9.6", default-features = false }
op-alloy-consensus = { version = "0.9.6", default-features = false }
op-alloy-rpc-jsonrpsee = { version = "0.9.6", default-features = false }
op-alloy-rpc-types-engine = { version = "0.9.6", default-features = false }

# Alloy
alloy-eips = { version = "0.9.2", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/genesis/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use crate::{
RollupConfig, GRANITE_CHANNEL_TIMEOUT,
};

#[cfg(feature = "serde")]
const fn default_governed_by_optimism() -> bool {
false
}

#[cfg(feature = "serde")]
const fn default_batch_inbox_addr() -> Address {
Address::ZERO
}
Expand Down
45 changes: 33 additions & 12 deletions crates/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "maili-rpc"
description = "Optimism RPC API"
description = "Optimism RPC Types and API"

version.workspace = true
edition.workspace = true
Expand All @@ -16,24 +16,31 @@ workspace = true

[dependencies]
# Workspace
maili-protocol = { workspace = true, features = ["serde"] }
maili-protocol.workspace = true

# OP Alloy
op-alloy-rpc-types-engine.workspace = true

# Alloy
alloy-eips = { workspace = true, features = ["serde"] }
alloy-primitives = { workspace = true, features = ["map", "rlp", "serde"] }
alloy-eips.workspace = true
alloy-primitives = { workspace = true, features = ["map", "rlp"] }

# `interop` feature
async-trait = { workspace = true, optional = true }
maili-interop = { workspace = true, features = ["serde"], optional = true }
alloy-rpc-client = { workspace = true, features = ["reqwest"], optional = true }
# Misc
derive_more = { workspace = true, default-features = false, features = ["display", "from"] }

# `serde`
serde = { workspace = true, optional = true }

# RPC
# `jsonrpsee`
jsonrpsee = { workspace = true, optional = true }
getrandom = { workspace = true, optional = true } # req for wasm32-unknown-unknown
serde.workspace = true
maili-genesis = { workspace = true, optional = true }
op-alloy-rpc-jsonrpsee = { workspace = true, optional = true }

# Misc
derive_more = { workspace = true, default-features = false, features = ["display", "from"] }
# `interop` feature
async-trait = { workspace = true, optional = true }
maili-interop = { workspace = true, features = ["serde"], optional = true }
alloy-rpc-client = { workspace = true, features = ["reqwest"], optional = true }

[dev-dependencies]
serde_json.workspace = true
Expand All @@ -42,20 +49,34 @@ serde_json.workspace = true
default = ["std", "jsonrpsee"]
std = [
"maili-protocol/std",
"maili-genesis?/std",
"alloy-eips/std",
"alloy-primitives/std",
"op-alloy-rpc-types-engine/std",
]
serde = [
"dep:serde",
"maili-protocol/serde",
"maili-genesis?/serde",
"alloy-eips/serde",
"alloy-primitives/serde",
"op-alloy-rpc-types-engine/serde",
]
interop = [
"dep:maili-interop",
"dep:alloy-rpc-client",
"dep:async-trait",
]
jsonrpsee = [
"serde",
"dep:maili-genesis",
"dep:jsonrpsee",
"dep:getrandom",
"dep:op-alloy-rpc-jsonrpsee",
]
client = [
"jsonrpsee",
"jsonrpsee/client",
"jsonrpsee/async-client",
"op-alloy-rpc-jsonrpsee/client",
]
69 changes: 30 additions & 39 deletions crates/rpc/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
#![cfg(feature = "jsonrpsee")]
#![allow(missing_docs)]

//! Rollup Node
//! The Optimism RPC API.
use alloc::{boxed::Box, string::String, vec::Vec};
use core::net::IpAddr;

use alloy_eips::BlockNumberOrTag;
use alloy_primitives::{B256, U64};
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), allow(unused_imports))]
use getrandom as _; // required for compiling wasm32-unknown-unknown
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use maili_genesis::RollupConfig;
use maili_protocol::{ExecutingMessage, SafetyLevel};

#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), allow(unused_imports))]
use getrandom as _; // required for compiling wasm32-unknown-unknown

use crate::{
OutputResponse, PeerDump, PeerInfo, PeerStats, ProtocolVersion, RollupConfig, SafeHeadResponse,
OutputResponse, PeerDump, PeerInfo, PeerStats, ProtocolVersion, SafeHeadResponse,
SuperchainSignal, SyncStatus,
};

// Re-export apis defined in upstream `op-alloy-rpc-jsonrpsee`
pub use op_alloy_rpc_jsonrpsee::traits::{MinerApiExtServer, OpAdminApiServer};

#[cfg(feature = "client")]
pub use op_alloy_rpc_jsonrpsee::traits::{MinerApiExtClient, OpAdminApiClient};

/// Optimism specified rpc interface.
///
/// https://docs.optimism.io/builders/node-operators/json-rpc
Expand Down Expand Up @@ -58,73 +62,70 @@ pub trait OpP2PApi {
#[method(name = "self")]
async fn opp2p_self(&self) -> RpcResult<PeerInfo>;

/// Returns information of peers
#[method(name = "peers")]
async fn opp2p_peers(&self) -> RpcResult<PeerDump>;

/// Returns statistics of peers
#[method(name = "peerStats")]
async fn opp2p_peer_stats(&self) -> RpcResult<PeerStats>;

/// Returns the discovery table
#[method(name = "discoveryTable")]
async fn opp2p_discovery_table(&self) -> RpcResult<Vec<String>>;

/// Blocks the given peer
#[method(name = "blockPeer")]
async fn opp2p_block_peer(&self, peer: String) -> RpcResult<()>;

/// Lists blocked peers
#[method(name = "listBlockedPeers")]
async fn opp2p_list_blocked_peers(&self) -> RpcResult<Vec<String>>;

/// Blocks the given address
#[method(name = "blocAddr")]
async fn opp2p_block_addr(&self, ip: IpAddr) -> RpcResult<()>;

/// Unblocks the given address
#[method(name = "unblockAddr")]
async fn opp2p_unblock_addr(&self, ip: IpAddr) -> RpcResult<()>;

/// Lists blocked addresses
#[method(name = "listBlockedAddrs")]
async fn opp2p_list_blocked_addrs(&self) -> RpcResult<Vec<IpAddr>>;

/// todo: should be IPNet?
// TODO: should be IPNet?
/// Blocks the given subnet
#[method(name = "blockSubnet")]
async fn opp2p_block_subnet(&self, subnet: String) -> RpcResult<()>;

/// todo: should be IPNet?
// TODO: should be IPNet?
/// Unblocks the given subnet
#[method(name = "unblockSubnet")]
async fn opp2p_unblock_subnet(&self, subnet: String) -> RpcResult<()>;

/// todo: should be IPNet?
// TODO: should be IPNet?
/// Lists blocked subnets
#[method(name = "listBlockedSubnets")]
async fn opp2p_list_blocked_subnets(&self) -> RpcResult<Vec<String>>;

/// Protects the given peer
#[method(name = "protectPeer")]
async fn opp2p_protect_peer(&self, peer: String) -> RpcResult<()>;

/// Unprotects the given peer
#[method(name = "unprotectPeer")]
async fn opp2p_unprotect_peer(&self, peer: String) -> RpcResult<()>;

/// Connects to the given peer
#[method(name = "connectPeer")]
async fn opp2p_connect_peer(&self, peer: String) -> RpcResult<()>;

/// Disconnects from the given peer
#[method(name = "disconnectPeer")]
async fn opp2p_disconnect_peer(&self, peer: String) -> RpcResult<()>;
}

/// The admin namespace endpoints
/// https://github.com/ethereum-optimism/optimism/blob/c7ad0ebae5dca3bf8aa6f219367a95c15a15ae41/op-node/node/api.go#L28-L36
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "admin"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "admin"))]
pub trait OpAdminApi {
#[method(name = "resetDerivationPipeline")]
async fn admin_reset_derivation_pipeline(&self) -> RpcResult<()>;

#[method(name = "startSequencer")]
async fn admin_start_sequencer(&self, block_hash: B256) -> RpcResult<()>;

#[method(name = "stopSequencer")]
async fn admin_stop_sequencer(&self) -> RpcResult<B256>;

#[method(name = "sequencerActive")]
async fn admin_sequencer_active(&self) -> RpcResult<bool>;
}

/// Engine API extension for Optimism superchain signaling
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "engine"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "engine"))]
Expand All @@ -143,16 +144,6 @@ pub trait EngineApiExt {
async fn signal_superchain_v1(&self, signal: SuperchainSignal) -> RpcResult<ProtocolVersion>;
}

/// Op API extension for controlling the miner.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "miner"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "miner"))]
pub trait MinerApiExt {
/// Sets the maximum data availability size of any tx allowed in a block, and the total max l1
/// data size of the block. 0 means no maximum.
#[method(name = "setMaxDASize")]
async fn set_max_da_size(&self, max_tx_size: U64, max_block_size: U64) -> RpcResult<bool>;
}

/// Supervisor API for interop.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "supervisor"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "supervisor"))]
Expand Down
42 changes: 42 additions & 0 deletions crates/rpc/src/attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Optimism Payload attributes that reference the parent L2 block.
use maili_protocol::L2BlockInfo;
use op_alloy_rpc_types_engine::OpPayloadAttributes;

/// Optimism Payload Attributes with parent block reference.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OpAttributesWithParent {
/// The payload attributes.
pub attributes: OpPayloadAttributes,
/// The parent block reference.
pub parent: L2BlockInfo,
/// Whether the current batch is the last in its span.
pub is_last_in_span: bool,
}

impl OpAttributesWithParent {
/// Create a new [OpAttributesWithParent] instance.
pub const fn new(
attributes: OpPayloadAttributes,
parent: L2BlockInfo,
is_last_in_span: bool,
) -> Self {
Self { attributes, parent, is_last_in_span }
}

/// Returns the payload attributes.
pub const fn attributes(&self) -> &OpPayloadAttributes {
&self.attributes
}

/// Returns the parent block reference.
pub const fn parent(&self) -> &L2BlockInfo {
&self.parent
}

/// Returns whether the current batch is the last in its span.
pub const fn is_last_in_span(&self) -> bool {
self.is_last_in_span
}
}
83 changes: 0 additions & 83 deletions crates/rpc/src/js_types/config.rs

This file was deleted.

Loading

0 comments on commit 8240517

Please sign in to comment.