diff --git a/crates/rpc-types/src/config.rs b/crates/rpc-types/src/config.rs deleted file mode 100644 index d1a825cb..00000000 --- a/crates/rpc-types/src/config.rs +++ /dev/null @@ -1,83 +0,0 @@ -#![allow(missing_docs)] -//! OP rollup config types. - -use alloy_eips::BlockNumHash; -use alloy_primitives::{Address, B256}; -use serde::{Deserialize, Serialize}; - -// https://github.com/ethereum-optimism/optimism/blob/c7ad0ebae5dca3bf8aa6f219367a95c15a15ae41/op-service/eth/types.go#L371 -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct SystemConfig { - pub batcher_addr: Address, - pub overhead: B256, - pub scalar: B256, - pub gas_limit: u64, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct Genesis { - pub l1: BlockNumHash, - pub l2: BlockNumHash, - pub l2_time: u64, - pub system_config: SystemConfig, -} - -// -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct RollupConfig { - pub genesis: Genesis, - pub block_time: u64, - pub max_sequencer_drift: u64, - pub seq_window_size: u64, - - #[serde(rename = "channel_timeout")] - pub channel_timeout_bedrock: u64, - pub channel_timeout_granite: u64, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub l1_chain_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub l2_chain_id: Option, - - #[serde(default, skip_serializing_if = "Option::is_none")] - pub regolith_time: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub canyon_time: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub delta_time: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub ecotone_time: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub fjord_time: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub granite_time: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub interop_time: Option, - pub batch_inbox_address: Address, - pub deposit_contract_address: Address, - pub l1_system_config_address: Address, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub protocol_versions_address: Option
, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub da_challenge_address: Option
, - pub da_challenge_window: u64, - pub da_resolve_window: u64, - pub use_plasma: bool, -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_rollup_config() { - let s = r#"{"genesis":{"l1":{"hash":"0x438335a20d98863a4c0c97999eb2481921ccd28553eac6f913af7c12aec04108", "number": 424242 },"l2":{"hash":"0xdbf6a80fef073de06add9b0d14026d6e5a86c85f6d102c36d3d8e9cf89c2afd3", "number": 1337 },"l2_time":1686068903,"system_config":{"batcherAddr":"0x6887246668a3b87f54deb3b94ba47a6f63f32985","overhead":"0x00000000000000000000000000000000000000000000000000000000000000bc","scalar":"0x00000000000000000000000000000000000000000000000000000000000a6fe0","gasLimit":30000000}},"block_time":2,"max_sequencer_drift":600,"seq_window_size":3600,"channel_timeout":300,"channel_timeout_granite":50,"l1_chain_id":1,"l2_chain_id":10,"regolith_time":0,"canyon_time":1704992401,"delta_time":1708560000,"ecotone_time":1710374401,"batch_inbox_address":"0xff00000000000000000000000000000000000010","deposit_contract_address":"0xbeb5fc579115071764c7423a4f12edde41f106ed","l1_system_config_address":"0x229047fed2591dbec1ef1118d64f7af3db9eb290","protocol_versions_address":"0x8062abc286f5e7d9428a0ccb9abd71e50d93b935","da_challenge_address":"0x0000000000000000000000000000000000000000","da_challenge_window":0,"da_resolve_window":0,"use_plasma":false}"#; - - let deserialize = serde_json::from_str::(s).unwrap(); - - assert_eq!( - serde_json::from_str::(s).unwrap(), - serde_json::to_value(&deserialize).unwrap() - ); - } -} diff --git a/crates/rpc-types/src/lib.rs b/crates/rpc-types/src/lib.rs index b07336ca..f45d0f6c 100644 --- a/crates/rpc-types/src/lib.rs +++ b/crates/rpc-types/src/lib.rs @@ -9,29 +9,11 @@ extern crate alloc; -mod config; -pub use config::{Genesis, RollupConfig, SystemConfig}; - mod genesis; pub use genesis::{OpBaseFeeInfo, OpChainInfo, OpGenesisInfo}; -mod net; -pub use net::{ - Connectedness, Direction, GossipScores, PeerDump, PeerInfo, PeerScores, PeerStats, - ReqRespScores, TopicScores, -}; - -mod output; -pub use output::OutputResponse; - mod receipt; pub use receipt::{L1BlockInfo, OpTransactionReceipt, OpTransactionReceiptFields}; -mod safe_head; -pub use safe_head::SafeHeadResponse; - -mod sync; -pub use sync::{L1BlockRef, L2BlockRef, SyncStatus}; - mod transaction; pub use transaction::{OpTransactionFields, OpTransactionRequest, Transaction}; diff --git a/crates/rpc-types/src/net.rs b/crates/rpc-types/src/net.rs deleted file mode 100644 index 8c569260..00000000 --- a/crates/rpc-types/src/net.rs +++ /dev/null @@ -1,305 +0,0 @@ -#![allow(missing_docs)] -//! Network RPC types - -use alloc::{string::String, vec::Vec}; -use alloy_primitives::{map::HashMap, ChainId}; -use core::net::IpAddr; -use serde::{ - de::{self, Unexpected}, - Deserialize, Deserializer, Serialize, Serializer, -}; - -// https://github.com/ethereum-optimism/optimism/blob/8dd17a7b114a7c25505cd2e15ce4e3d0f7e3f7c1/op-node/p2p/store/iface.go#L13 -#[derive(Clone, Debug, Copy, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct TopicScores { - pub time_in_mesh: f64, - pub first_message_deliveries: f64, - pub mesh_message_deliveries: f64, - pub invalid_message_deliveries: f64, -} - -// https://github.com/ethereum-optimism/optimism/blob/8dd17a7b114a7c25505cd2e15ce4e3d0f7e3f7c1/op-node/p2p/store/iface.go#L20C6-L20C18 -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GossipScores { - pub total: f64, - pub blocks: TopicScores, - #[serde(rename = "IPColocationFactor")] - pub ip_colocation_factor: f64, - pub behavioral_penalty: f64, -} - -// https://github.com/ethereum-optimism/optimism/blob/8dd17a7b114a7c25505cd2e15ce4e3d0f7e3f7c1/op-node/p2p/store/iface.go#L31C1-L35C2 -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ReqRespScores { - pub valid_responses: f64, - pub error_responses: f64, - pub rejected_payloads: f64, -} - -// https://github.com/ethereum-optimism/optimism/blob/8dd17a7b114a7c25505cd2e15ce4e3d0f7e3f7c1/op-node/p2p/store/iface.go#L81 -#[derive(Clone, Debug, Copy, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PeerScores { - pub gossip: GossipScores, - pub req_resp: ReqRespScores, -} - -// https://github.com/ethereum-optimism/optimism/blob/develop/op-node/p2p/rpc_api.go#L15 -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PeerInfo { - #[serde(rename = "peerID")] - pub peer_id: String, - #[serde(rename = "nodeID")] - pub node_id: String, - pub user_agent: String, - pub protocol_version: String, - #[serde(rename = "ENR")] - pub enr: String, - pub addresses: Vec, - pub protocols: Option>, - /// 0: "`NotConnected`", 1: "Connected", - /// 2: "`CanConnect`" (gracefully disconnected) - /// 3: "`CannotConnect`" (tried but failed) - pub connectedness: Connectedness, - /// 0: "Unknown", 1: "Inbound" (if the peer contacted us) - /// 2: "Outbound" (if we connected to them) - pub direction: Direction, - pub protected: bool, - #[serde(rename = "chainID")] - pub chain_id: ChainId, - /// nanosecond - pub latency: u64, - pub gossip_blocks: bool, - #[serde(rename = "scores")] - pub peer_scores: PeerScores, -} -// https://github.com/ethereum-optimism/optimism/blob/40750a58e7a4a6f06370d18dfe6c6eab309012d9/op-node/p2p/rpc_api.go#L36 -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PeerDump { - pub total_connected: u32, - pub peers: HashMap, - pub banned_peers: Vec, - #[serde(rename = "bannedIPS")] - pub banned_ips: Vec, - // todo: should be IPNet - pub banned_subnets: Vec, -} - -// https://github.com/ethereum-optimism/optimism/blob/develop/op-node/p2p/rpc_server.go#L203 -#[derive(Clone, Debug, Copy, Serialize, Deserialize)] -pub struct PeerStats { - pub connected: u32, - pub table: u32, - #[serde(rename = "blocksTopic")] - pub blocks_topic: u32, - #[serde(rename = "blocksTopicV2")] - pub blocks_topic_v2: u32, - #[serde(rename = "blocksTopicV3")] - pub blocks_topic_v3: u32, - pub banned: u32, - pub known: u32, -} - -/// Represents the connectivity state of a peer in a network, indicating the reachability and -/// interaction status of a node with its peers. -#[derive(Clone, Debug, PartialEq, Copy, Default, Serialize, Deserialize)] -#[repr(u8)] -pub enum Connectedness { - /// No current connection to the peer, and no recent history of a successful connection. - #[default] - NotConnected = 0, - - /// An active, open connection to the peer exists. - Connected = 1, - - /// Connection to the peer is possible but not currently established; usually implies a past - /// successful connection. - CanConnect = 2, - - /// Recent attempts to connect to the peer failed, indicating potential issues in reachability - /// or peer status. - CannotConnect = 3, - - /// Connection to the peer is limited; may not have full capabilities. - Limited = 4, -} - -impl core::fmt::Display for Connectedness { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - Self::NotConnected => write!(f, "Not Connected"), - Self::Connected => write!(f, "Connected"), - Self::CanConnect => write!(f, "Can Connect"), - Self::CannotConnect => write!(f, "Cannot Connect"), - Self::Limited => write!(f, "Limited"), - } - } -} - -impl From for Connectedness { - fn from(value: u8) -> Self { - match value { - 0 => Self::NotConnected, - 1 => Self::Connected, - 2 => Self::CanConnect, - 3 => Self::CannotConnect, - 4 => Self::Limited, - _ => Self::NotConnected, - } - } -} -/// Direction represents the direction of a connection. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub enum Direction { - /// Unknown is the default direction when the direction is not specified. - #[default] - Unknown = 0, - /// Inbound is for when the remote peer initiated the connection. - Inbound = 1, - /// Outbound is for when the local peer initiated the connection. - Outbound = 2, -} - -impl Serialize for Direction { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u8(*self as u8) - } -} - -impl<'de> Deserialize<'de> for Direction { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let value = u8::deserialize(deserializer)?; - match value { - 0 => Ok(Self::Unknown), - 1 => Ok(Self::Inbound), - 2 => Ok(Self::Outbound), - _ => Err(de::Error::invalid_value( - Unexpected::Unsigned(value as u64), - &"a value between 0 and 2", - )), - } - } -} -impl core::fmt::Display for Direction { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!( - f, - "{}", - match self { - Self::Unknown => "Unknown", - Self::Inbound => "Inbound", - Self::Outbound => "Outbound", - } - ) - } -} -#[cfg(test)] -mod tests { - use super::*; - use serde_json::{self}; - - #[test] - fn test_direction_serialization() { - assert_eq!( - serde_json::to_string(&Direction::Unknown).unwrap(), - "0", - "Serialization failed for Direction::Unknown" - ); - assert_eq!( - serde_json::to_string(&Direction::Inbound).unwrap(), - "1", - "Serialization failed for Direction::Inbound" - ); - assert_eq!( - serde_json::to_string(&Direction::Outbound).unwrap(), - "2", - "Serialization failed for Direction::Outbound" - ); - } - - #[test] - fn test_direction_deserialization() { - let unknown: Direction = serde_json::from_str("0").unwrap(); - let inbound: Direction = serde_json::from_str("1").unwrap(); - let outbound: Direction = serde_json::from_str("2").unwrap(); - - assert_eq!(unknown, Direction::Unknown, "Deserialization mismatch for Direction::Unknown"); - assert_eq!(inbound, Direction::Inbound, "Deserialization mismatch for Direction::Inbound"); - assert_eq!( - outbound, - Direction::Outbound, - "Deserialization mismatch for Direction::Outbound" - ); - } - #[test] - fn test_peer_info_connectedness_serialization() { - let peer_info = PeerInfo { - peer_id: String::from("peer123"), - node_id: String::from("node123"), - user_agent: String::from("MyUserAgent"), - protocol_version: String::from("v1"), - enr: String::from("enr123"), - addresses: vec![String::from("127.0.0.1")], - protocols: Some(vec![String::from("eth"), String::from("p2p")]), - connectedness: Connectedness::Connected, - direction: Direction::Outbound, - protected: true, - chain_id: 1, - latency: 100, - gossip_blocks: true, - peer_scores: PeerScores { - gossip: GossipScores { - total: 1.0, - blocks: TopicScores { - time_in_mesh: 10.0, - first_message_deliveries: 5.0, - mesh_message_deliveries: 2.0, - invalid_message_deliveries: 0.0, - }, - ip_colocation_factor: 0.5, - behavioral_penalty: 0.1, - }, - req_resp: ReqRespScores { - valid_responses: 10.0, - error_responses: 1.0, - rejected_payloads: 0.0, - }, - }, - }; - - let serialized = serde_json::to_string(&peer_info).expect("Serialization failed"); - - let deserialized: PeerInfo = - serde_json::from_str(&serialized).expect("Deserialization failed"); - - assert_eq!(peer_info.peer_id, deserialized.peer_id); - assert_eq!(peer_info.node_id, deserialized.node_id); - assert_eq!(peer_info.user_agent, deserialized.user_agent); - assert_eq!(peer_info.protocol_version, deserialized.protocol_version); - assert_eq!(peer_info.enr, deserialized.enr); - assert_eq!(peer_info.addresses, deserialized.addresses); - assert_eq!(peer_info.protocols, deserialized.protocols); - assert_eq!(peer_info.connectedness, deserialized.connectedness); - assert_eq!(peer_info.direction, deserialized.direction); - assert_eq!(peer_info.protected, deserialized.protected); - assert_eq!(peer_info.chain_id, deserialized.chain_id); - assert_eq!(peer_info.latency, deserialized.latency); - assert_eq!(peer_info.gossip_blocks, deserialized.gossip_blocks); - assert_eq!(peer_info.peer_scores.gossip.total, deserialized.peer_scores.gossip.total); - assert_eq!( - peer_info.peer_scores.req_resp.valid_responses, - deserialized.peer_scores.req_resp.valid_responses - ); - } -} diff --git a/crates/rpc-types/src/output.rs b/crates/rpc-types/src/output.rs deleted file mode 100644 index d4f8cebe..00000000 --- a/crates/rpc-types/src/output.rs +++ /dev/null @@ -1,25 +0,0 @@ -//! Output Types - -use crate::sync::{L2BlockRef, SyncStatus}; -use alloy_primitives::B256; -use serde::{Deserialize, Serialize}; - -/// An [output response][or] for Optimism Rollup. -/// -/// [or]: https://github.com/ethereum-optimism/optimism/blob/f20b92d3eb379355c876502c4f28e72a91ab902f/op-service/eth/output.go#L10-L17 -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct OutputResponse { - /// The output version. - pub version: B256, - /// The output root hash. - pub output_root: B256, - /// A reference to the L2 block. - pub block_ref: L2BlockRef, - /// The withdrawal storage root. - pub withdrawal_storage_root: B256, - /// The state root. - pub state_root: B256, - /// The status of the node sync. - pub sync_status: SyncStatus, -} diff --git a/crates/rpc-types/src/safe_head.rs b/crates/rpc-types/src/safe_head.rs deleted file mode 100644 index 9523af97..00000000 --- a/crates/rpc-types/src/safe_head.rs +++ /dev/null @@ -1,29 +0,0 @@ -//! Contains the response for a safe head request. - -use alloy_eips::BlockNumHash; -use serde::{Deserialize, Serialize}; - -/// The safe head response. -/// -/// -/// Note: the optimism "eth.BlockID" type is number,hash -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct SafeHeadResponse { - /// The L1 block. - pub l1_block: BlockNumHash, - /// The safe head. - pub safe_head: BlockNumHash, -} - -#[cfg(test)] -mod tests { - use super::*; - - // - #[test] - fn test_safe_head_response() { - let s = r#"{"l1Block":{"hash":"0x7de331305c2bb3e5642a2adcb9c003cc67cefc7b05a3da5a6a4b12cf3af15407","number":6834391},"safeHead":{"hash":"0xa5e5ec1ade7d6fef209f73861bf0080950cde74c4b0c07823983eb5225e282a8","number":18266679}}"#; - let _response: SafeHeadResponse = serde_json::from_str(s).unwrap(); - } -} diff --git a/crates/rpc-types/src/sync.rs b/crates/rpc-types/src/sync.rs deleted file mode 100644 index 28bb252e..00000000 --- a/crates/rpc-types/src/sync.rs +++ /dev/null @@ -1,67 +0,0 @@ -//! Op types related to sync. - -use alloy_eips::BlockNumHash; -use alloy_primitives::{BlockNumber, B256}; -use serde::{Deserialize, Serialize}; - -/// The block reference for an L2 block. -/// -/// See: -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct L2BlockRef { - /// The block hash. - pub hash: B256, - /// The block number. - pub number: BlockNumber, - /// The parent hash. - pub parent_hash: B256, - /// The timestamp. - pub timestamp: u64, - /// The L1 origin. - #[serde(rename = "l1origin")] - pub l1_origin: BlockNumHash, - /// The sequence number. - pub sequence_number: u64, -} - -/// The block reference for an L1 block. -/// -/// See: -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct L1BlockRef { - /// The block hash. - pub hash: B256, - /// The block number. - pub number: BlockNumber, - /// The parent hash. - pub parent_hash: B256, - /// The timestamp. - pub timestamp: u64, -} - -/// The [`SyncStatus`][ss] of an Optimism Rollup Node. -/// -/// [ss]: https://github.com/ethereum-optimism/optimism/blob/develop/op-service/eth/sync_status.go#L5 -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct SyncStatus { - /// The current L1 block. - pub current_l1: L1BlockRef, - /// The current L1 finalized block. - pub current_l1_finalized: L1BlockRef, - /// The L1 head block ref. - pub head_l1: L1BlockRef, - /// The L1 safe head block ref. - pub safe_l1: L1BlockRef, - /// The finalized L1 block ref. - pub finalized_l1: L1BlockRef, - /// The unsafe L2 block ref. - pub unsafe_l2: L2BlockRef, - /// The safe L2 block ref. - pub safe_l2: L2BlockRef, - /// The finalized L2 block ref. - pub finalized_l2: L2BlockRef, - /// The pending safe L2 block ref. - pub pending_safe_l2: L2BlockRef, -}