From 57d55b1d682a61af88ec5e848b3a5615f7a3c3a2 Mon Sep 17 00:00:00 2001 From: Rahul Guha <69rahul16@gmail.com> Date: Wed, 17 Apr 2024 17:55:11 +0530 Subject: [PATCH 1/3] added ClientVersionV1 --- crates/rpc-types-engine/src/identification.rs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 crates/rpc-types-engine/src/identification.rs diff --git a/crates/rpc-types-engine/src/identification.rs b/crates/rpc-types-engine/src/identification.rs new file mode 100644 index 00000000000..c52fabf2458 --- /dev/null +++ b/crates/rpc-types-engine/src/identification.rs @@ -0,0 +1,26 @@ +use alloy_primitives::B64; +use serde::{Deserialize, Serialize}; + +pub enum ClientCode { + BU, // besu + EJ, // ethereumJS + EG, // erigon + GE, // go-ethereum + GR, // grandine + LH, // lighthouse + LS, // lodestar + NM, // nethermind + NB, // nimbus + TK, // teku + PM, // prysm + RH, // reth +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ClientVersionV1 { + pub code :ClientCode, + pub name: String, + pub version: String, + pub commit : Vec +} \ No newline at end of file From 6861906360e23e163c787de784ffb22a0c3e0b20 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 17 Apr 2024 15:35:19 +0200 Subject: [PATCH 2/3] touchups --- crates/rpc-types-engine/src/identification.rs | 135 +++++++++++++++--- crates/rpc-types-engine/src/lib.rs | 5 +- 2 files changed, 123 insertions(+), 17 deletions(-) diff --git a/crates/rpc-types-engine/src/identification.rs b/crates/rpc-types-engine/src/identification.rs index c52fabf2458..0deeb3b4797 100644 --- a/crates/rpc-types-engine/src/identification.rs +++ b/crates/rpc-types-engine/src/identification.rs @@ -1,26 +1,129 @@ -use alloy_primitives::B64; +//! Client identification: + use serde::{Deserialize, Serialize}; +use std::str::FromStr; +/// This enum defines a standard for specifying a client with just two letters. Clients teams which +/// have a code reserved in this list MUST use this code when identifying themselves. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum ClientCode { - BU, // besu + /// Besu + BU, + /// EthereumJS EJ, // ethereumJS - EG, // erigon - GE, // go-ethereum - GR, // grandine - LH, // lighthouse - LS, // lodestar - NM, // nethermind - NB, // nimbus - TK, // teku - PM, // prysm - RH, // reth + /// Erigon + EG, + /// Geth, go-ethereum + GE, + /// Grandine + GR, + /// Lighthouse + LH, + /// Lodestar + LS, + /// Nethermind + NM, + /// Nimbus + NB, + /// Teku + TK, + /// Prysm + PM, + /// Reth + RH, } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +impl ClientCode { + /// Returns the client identifier as str. + pub const fn as_str(&self) -> &'static str { + match self { + ClientCode::BU => "BU", + ClientCode::EJ => "EJ", + ClientCode::EG => "EG", + ClientCode::GE => "GE", + ClientCode::GR => "GR", + ClientCode::LH => "LH", + ClientCode::LS => "LS", + ClientCode::NM => "NM", + ClientCode::NB => "NB", + ClientCode::TK => "TK", + ClientCode::PM => "PM", + ClientCode::RH => "RH", + } + } + + /// Returns the human readable client name for the given code. + pub const fn client_name(&self) -> &'static str { + match self { + ClientCode::BU => "Besu", + ClientCode::EJ => "EthereumJS", + ClientCode::EG => "Erigon", + ClientCode::GE => "Geth", + ClientCode::GR => "Grandine", + ClientCode::LH => "Lighthouse", + ClientCode::LS => "Lodestar", + ClientCode::NM => "Nethermind", + ClientCode::NB => "Nimbus", + ClientCode::TK => "Teku", + ClientCode::PM => "Prysm", + ClientCode::RH => "Reth", + } + } +} + +impl FromStr for ClientCode { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "BU" => Ok(ClientCode::BU), + "EJ" => Ok(ClientCode::EJ), + "EG" => Ok(ClientCode::EG), + "GE" => Ok(ClientCode::GE), + "GR" => Ok(ClientCode::GR), + "LH" => Ok(ClientCode::LH), + "LS" => Ok(ClientCode::LS), + "NM" => Ok(ClientCode::NM), + "NB" => Ok(ClientCode::NB), + "TK" => Ok(ClientCode::TK), + "PM" => Ok(ClientCode::PM), + "RH" => Ok(ClientCode::RH), + s => Err(s.to_string()), + } + } +} + +impl std::fmt::Display for ClientCode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_str()) + } +} + +/// Contains information which identifies a client implementation. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ClientVersionV1 { - pub code :ClientCode, + /// Client code, e.g. GE for Geth + pub code: ClientCode, + /// Human-readable name of the client, e.g. Lighthouse or go-ethereum pub name: String, + /// The version string of the current implementation e.g. v4.6.0 or 1.0.0-alpha.1 or + /// 1.0.0+20130313144700 pub version: String, - pub commit : Vec -} \ No newline at end of file + /// first four bytes of the latest commit hash of this build e.g: `fa4ff922` + pub commit: String, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn client_id_serde() { + let s = r#"{"code":"RH","name":"Reth","version":"v1.10.8","commit":"fa4ff922"}"#; + let v: ClientVersionV1 = serde_json::from_str(s).unwrap(); + assert_eq!(v.code, ClientCode::RH); + assert_eq!(v.name, "Reth"); + assert_eq!(serde_json::to_string(&v).unwrap(), s); + } +} diff --git a/crates/rpc-types-engine/src/lib.rs b/crates/rpc-types-engine/src/lib.rs index fef6851dfd2..732e5ece32d 100644 --- a/crates/rpc-types-engine/src/lib.rs +++ b/crates/rpc-types-engine/src/lib.rs @@ -23,11 +23,14 @@ mod cancun; mod exit; mod forkchoice; +mod identification; mod optimism; pub mod payload; mod transition; -pub use self::{cancun::*, exit::*, forkchoice::*, optimism::*, payload::*, transition::*}; +pub use self::{ + cancun::*, exit::*, forkchoice::*, identification::*, optimism::*, payload::*, transition::*, +}; #[doc(inline)] pub use alloy_eips::eip6110::Deposit; From 1e127438582c26a33e809f072fbc23fcf4e56273 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 17 Apr 2024 15:37:28 +0200 Subject: [PATCH 3/3] rustfmt --- crates/rpc-types-engine/src/identification.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rpc-types-engine/src/identification.rs b/crates/rpc-types-engine/src/identification.rs index 0deeb3b4797..08bf2b8c765 100644 --- a/crates/rpc-types-engine/src/identification.rs +++ b/crates/rpc-types-engine/src/identification.rs @@ -10,7 +10,7 @@ pub enum ClientCode { /// Besu BU, /// EthereumJS - EJ, // ethereumJS + EJ, /// Erigon EG, /// Geth, go-ethereum