Skip to content

Commit

Permalink
feat: use reth-rpc-types
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Dec 18, 2023
1 parent 0ba6b61 commit 34074fe
Show file tree
Hide file tree
Showing 52 changed files with 5,685 additions and 1,255 deletions.
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ tracing-subscriber = "0.3.18"

tempfile = "3.8"

assert_matches = "1.5"
auto_impl = "1.1"
base64 = "0.21"
bimap = "0.6"
Expand All @@ -68,9 +67,18 @@ pin-project = "1.1"
rand = "0.8.5"
reqwest = { version = "0.11.18", default-features = false }
semver = "1.0"
thiserror = "1.0"
url = "2.4"

## serde
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_with = "3.4"

## misc-testing
arbitrary = "1.3"
assert_matches = "1.5"
similar-asserts = "1.5"
proptest = "1.4"
proptest-derive = "0.4"
serial_test = "2.0"
thiserror = "1.0"
url = "2.4"
27 changes: 19 additions & 8 deletions crates/rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,43 @@ repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
# ethereum
alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
ethereum_ssz_derive = { version = "0.5", optional = true }
ethereum_ssz = { version = "0.5", optional = true }

itertools.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
serde_with.workspace = true
thiserror.workspace = true
url = "2.3"
secp256k1 = { version = "0.27.0", default-features = false, features = [
"global-context",
"rand-std",
"recovery",
] }

# arbitrary
arbitrary = { version = "1.1", features = ["derive"], optional = true }
proptest = { version = "1.0", optional = true }
arbitrary = { version = "1.3", features = ["derive"], optional = true }
proptest = { version = "1.4", optional = true }
proptest-derive = { version = "0.4", optional = true }

# jsonrpsee
jsonrpsee-types = { version = "0.20", optional = true }


[features]
default = ["jsonrpsee"]
arbitrary = ["dep:arbitrary", "dep:proptest-derive", "dep:proptest", "alloy-primitives/arbitrary"]
jsonrpsee = ["dep:jsonrpsee-types"]

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["rlp", "serde", "arbitrary"] }
alloy-primitives = { workspace = true, features = ["rand", "rlp", "serde", "arbitrary"] }

arbitrary = { version = "1.1", features = ["derive"] }
proptest = "1.0"
proptest-derive = "0.4"
similar-asserts = "1.4"
arbitrary = { workspace = true, features = ["derive"] }
proptest.workspace = true
proptest-derive.workspace = true
rand.workspace = true
similar-asserts.workspace = true
105 changes: 105 additions & 0 deletions crates/rpc-types/src/admin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use crate::{NodeRecord, PeerId};
use alloy_primitives::{B256, U256};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
net::{IpAddr, SocketAddr},
};

/// Represents the `admin_nodeInfo` response, which can be queried for all the information
/// known about the running node at the networking granularity.
///
/// Note: this format is not standardized. Reth follows Geth's format,
/// see: <https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-admin>
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NodeInfo {
/// Enode of the node in URL format.
pub enode: NodeRecord,
/// ID of the local node.
pub id: PeerId,
/// IP of the local node.
pub ip: IpAddr,
/// Address exposed for listening for the local node.
#[serde(rename = "listenAddr")]
pub listen_addr: SocketAddr,
/// Ports exposed by the node for discovery and listening.
pub ports: Ports,
/// Name of the network
pub name: String,
/// Networking protocols being run by the local node.
pub protocols: Protocols,
}

impl NodeInfo {
/// Creates a new instance of `NodeInfo`.
pub fn new(enr: NodeRecord, status: NetworkStatus) -> NodeInfo {
NodeInfo {
enode: enr,
id: enr.id,
ip: enr.address,
listen_addr: enr.tcp_addr(),
ports: Ports { discovery: enr.udp_port, listener: enr.tcp_port },
name: status.client_version,
protocols: Protocols { eth: status.eth_protocol_info, other: Default::default() },
}
}
}

/// All supported protocols
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Protocols {
/// Info about `eth` sub-protocol
pub eth: EthProtocolInfo,
/// Placeholder for any other protocols
#[serde(flatten, default)]
pub other: BTreeMap<String, serde_json::Value>,
}

/// Ports exposed by the node for discovery and listening.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Ports {
/// Port exposed for node discovery.
pub discovery: u16,
/// Port exposed for listening.
pub listener: u16,
}

/// The status of the network being ran by the local node.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NetworkStatus {
/// The local node client version.
pub client_version: String,
/// The current ethereum protocol version
pub protocol_version: u64,
/// Information about the Ethereum Wire Protocol.
pub eth_protocol_info: EthProtocolInfo,
}

/// Information about the Ethereum Wire Protocol (ETH)
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EthProtocolInfo {
/// The current difficulty at the head of the chain.
#[serde(deserialize_with = "crate::serde_helpers::json_u256::deserialize_json_u256")]
pub difficulty: U256,
/// The block hash of the head of the chain.
pub head: B256,
/// Network ID in base 10.
pub network: u64,
/// Genesis block of the current chain.
pub genesis: B256,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_node_info_roundtrip() {
let sample = r#"{"enode":"enode://44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d@[::]:30303","id":"44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d","ip":"::","listenAddr":"[::]:30303","name":"reth","ports":{"discovery":30303,"listener":30303},"protocols":{"eth":{"difficulty":17334254859343145000,"genesis":"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3","head":"0xb83f73fbe6220c111136aefd27b160bf4a34085c65ba89f24246b3162257c36a","network":1}}}"#;

let info: NodeInfo = serde_json::from_str(sample).unwrap();
let serialized = serde_json::to_string_pretty(&info).unwrap();
let de_serialized: NodeInfo = serde_json::from_str(&serialized).unwrap();
assert_eq!(info, de_serialized)
}
}
4 changes: 4 additions & 0 deletions crates/rpc-types/src/beacon/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const BLS_DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
pub const BLS_PUBLIC_KEY_BYTES_LEN: usize = 48;
pub const BLS_SECRET_KEY_BYTES_LEN: usize = 32;
pub const BLS_SIGNATURE_BYTES_LEN: usize = 96;
30 changes: 30 additions & 0 deletions crates/rpc-types/src/beacon/events/attestation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use alloy_primitives::B256;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttestationData {
#[serde_as(as = "DisplayFromStr")]
pub slot: u64,
#[serde_as(as = "DisplayFromStr")]
pub index: u64,
pub beacon_block_root: B256,
pub source: Source,
pub target: Target,
}

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Source {
#[serde_as(as = "DisplayFromStr")]
pub epoch: u64,
pub root: B256,
}
#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Target {
#[serde_as(as = "DisplayFromStr")]
pub epoch: u64,
pub root: B256,
}
54 changes: 54 additions & 0 deletions crates/rpc-types/src/beacon/events/light_client_finality.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use alloy_primitives::{Bytes, B256};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LightClientFinalityData {
pub attested_header: AttestedHeader,
pub finalized_header: FinalizedHeader,
pub finality_branch: Vec<String>,
pub sync_aggregate: SyncAggregate,
#[serde_as(as = "DisplayFromStr")]
pub signature_slot: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttestedHeader {
pub beacon: Beacon,
}

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Beacon {
#[serde_as(as = "DisplayFromStr")]
pub slot: u64,
#[serde_as(as = "DisplayFromStr")]
pub proposer_index: u64,
pub parent_root: B256,
pub state_root: B256,
pub body_root: B256,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FinalizedHeader {
pub beacon: Beacon2,
}

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Beacon2 {
#[serde_as(as = "DisplayFromStr")]
pub slot: u64,
#[serde_as(as = "DisplayFromStr")]
pub proposer_index: u64,
pub parent_root: B256,
pub state_root: B256,
pub body_root: B256,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SyncAggregate {
pub sync_committee_bits: Bytes,
pub sync_committee_signature: Bytes,
}
35 changes: 35 additions & 0 deletions crates/rpc-types/src/beacon/events/light_client_optimistic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use alloy_primitives::{Bytes, B256};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LightClientOptimisticData {
pub attested_header: AttestedHeader,
pub sync_aggregate: SyncAggregate,
#[serde_as(as = "DisplayFromStr")]
pub signature_slot: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttestedHeader {
pub beacon: Beacon,
}

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Beacon {
#[serde_as(as = "DisplayFromStr")]
pub slot: u64,
#[serde_as(as = "DisplayFromStr")]
pub proposer_index: u64,
pub parent_root: B256,
pub state_root: B256,
pub body_root: B256,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SyncAggregate {
pub sync_committee_bits: Bytes,
pub sync_committee_signature: Bytes,
}
Loading

0 comments on commit 34074fe

Please sign in to comment.