From 3b76596c0577394bb1652f230604dd2a7877f1c6 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Sat, 29 Jun 2024 10:55:18 +0200 Subject: [PATCH 1/9] feat: add missing admin_* methods --- crates/provider/src/ext/admin.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/provider/src/ext/admin.rs b/crates/provider/src/ext/admin.rs index 2165481cc34..8b4a403cb84 100644 --- a/crates/provider/src/ext/admin.rs +++ b/crates/provider/src/ext/admin.rs @@ -31,6 +31,17 @@ pub trait AdminApi: Send + Sync { /// Returns general information about the node as well as information about the running p2p /// protocols (e.g. `eth`, `snap`). async fn node_info(&self) -> TransportResult; + + /// Subscribe to events received by peers over the network. + /// + /// Like other subscription methods, this returns the ID of the subscription, which is then used + /// in all events subsequently. + /// + /// To unsubscribe from peer events, call `unsubscribe_peer_events`. + async fn subscribe_peer_events(&self) -> TransportResult>; + + /// Cancel subscription with `id`. + async fn unsubscribe_peer_events(&self, id: u16) -> TransportResult; } #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] @@ -64,6 +75,16 @@ where async fn node_info(&self) -> TransportResult { self.client().request("admin_nodeInfo", ()).await } + + async fn subscribe_peer_events(&self) -> TransportResult> { + self.client().request("admin_peerEvents_subscribe", ()).await + } + + async fn unsubscribe_peer_events(&self, id: u16) -> TransportResult { + // Id's have no leading zeroes and are `0x` prefixed. + let id = format!("{id:x}"); + self.client().request("admin_peerEvents_unsubscribe", (id,)).await + } } #[cfg(test)] From e4ebf0e5ff03d962081d27a2f1e65f6aa81ccce1 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Sat, 29 Jun 2024 11:05:24 +0200 Subject: [PATCH 2/9] fix: add 0x prefix to subscription ids --- crates/provider/src/ext/admin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/provider/src/ext/admin.rs b/crates/provider/src/ext/admin.rs index 8b4a403cb84..840c9b4665d 100644 --- a/crates/provider/src/ext/admin.rs +++ b/crates/provider/src/ext/admin.rs @@ -82,7 +82,7 @@ where async fn unsubscribe_peer_events(&self, id: u16) -> TransportResult { // Id's have no leading zeroes and are `0x` prefixed. - let id = format!("{id:x}"); + let id = format!("{id:#x}"); self.client().request("admin_peerEvents_unsubscribe", (id,)).await } } From 59a001b9752117197445f3b698a76637b72168db Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Sat, 29 Jun 2024 11:09:59 +0200 Subject: [PATCH 3/9] fix: u16 -> U128 --- crates/provider/src/ext/admin.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/provider/src/ext/admin.rs b/crates/provider/src/ext/admin.rs index 840c9b4665d..07f682f54f0 100644 --- a/crates/provider/src/ext/admin.rs +++ b/crates/provider/src/ext/admin.rs @@ -1,6 +1,7 @@ //! This module extends the Ethereum JSON-RPC provider with the Admin namespace's RPC methods. use crate::Provider; use alloy_network::Network; +use alloy_primitives::U128; use alloy_rpc_types_admin::{NodeInfo, PeerInfo}; use alloy_transport::{Transport, TransportResult}; @@ -38,10 +39,10 @@ pub trait AdminApi: Send + Sync { /// in all events subsequently. /// /// To unsubscribe from peer events, call `unsubscribe_peer_events`. - async fn subscribe_peer_events(&self) -> TransportResult>; + async fn subscribe_peer_events(&self) -> TransportResult>; /// Cancel subscription with `id`. - async fn unsubscribe_peer_events(&self, id: u16) -> TransportResult; + async fn unsubscribe_peer_events(&self, id: U128) -> TransportResult; } #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] @@ -76,12 +77,11 @@ where self.client().request("admin_nodeInfo", ()).await } - async fn subscribe_peer_events(&self) -> TransportResult> { + async fn subscribe_peer_events(&self) -> TransportResult> { self.client().request("admin_peerEvents_subscribe", ()).await } - async fn unsubscribe_peer_events(&self, id: u16) -> TransportResult { - // Id's have no leading zeroes and are `0x` prefixed. + async fn unsubscribe_peer_events(&self, id: U128) -> TransportResult { let id = format!("{id:#x}"); self.client().request("admin_peerEvents_unsubscribe", (id,)).await } From 3e308e35bffbb0f1199ab6912eb021e9703b4519 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Sat, 29 Jun 2024 16:01:12 +0200 Subject: [PATCH 4/9] fix: remork to match other subscriptions --- crates/provider/src/ext/admin.rs | 26 +++++++++-------- crates/rpc-types-admin/src/admin.rs | 44 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/crates/provider/src/ext/admin.rs b/crates/provider/src/ext/admin.rs index 07f682f54f0..4d828215ff0 100644 --- a/crates/provider/src/ext/admin.rs +++ b/crates/provider/src/ext/admin.rs @@ -1,7 +1,7 @@ //! This module extends the Ethereum JSON-RPC provider with the Admin namespace's RPC methods. use crate::Provider; use alloy_network::Network; -use alloy_primitives::U128; +use alloy_pubsub::Subscription; use alloy_rpc_types_admin::{NodeInfo, PeerInfo}; use alloy_transport::{Transport, TransportResult}; @@ -39,10 +39,10 @@ pub trait AdminApi: Send + Sync { /// in all events subsequently. /// /// To unsubscribe from peer events, call `unsubscribe_peer_events`. - async fn subscribe_peer_events(&self) -> TransportResult>; - - /// Cancel subscription with `id`. - async fn unsubscribe_peer_events(&self, id: U128) -> TransportResult; + #[cfg(feature = "pubsub")] + async fn subscribe_peer_events( + &self, + ) -> TransportResult>; } #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] @@ -77,13 +77,15 @@ where self.client().request("admin_nodeInfo", ()).await } - async fn subscribe_peer_events(&self) -> TransportResult> { - self.client().request("admin_peerEvents_subscribe", ()).await - } - - async fn unsubscribe_peer_events(&self, id: U128) -> TransportResult { - let id = format!("{id:#x}"); - self.client().request("admin_peerEvents_unsubscribe", (id,)).await + #[cfg(feature = "pubsub")] + async fn subscribe_peer_events( + &self, + ) -> TransportResult> { + self.root().pubsub_frontend()?; + let mut call = self.client().request("admin_peerEvents_subscribe", ()); + call.set_is_subscription(); + let id = call.await?; + self.root().get_subscription(id).await } } diff --git a/crates/rpc-types-admin/src/admin.rs b/crates/rpc-types-admin/src/admin.rs index 4ef48287bc0..0793c1a3075 100644 --- a/crates/rpc-types-admin/src/admin.rs +++ b/crates/rpc-types-admin/src/admin.rs @@ -185,6 +185,50 @@ pub struct PeerNetworkInfo { pub static_node: bool, } +/// The type of a peer event. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum PeerEventType { + /// A peer was added to the server. + Add, + /// A peer was dropped from the server. + Drop, + /// A message was successfully sent to the peer. + MsgSend, + /// A message was successfully received by the peer. + MsgRecv, +} + +/// An event emitted when peers are either added or dropped from a p2p server or when a message is sent or received on a peer connection. +/// +/// See [geth's `PeerEvent` struct](https://github.com/ethereum/go-ethereum/blob/94579932b18931115f28aa7f87f02450bda084c9/p2p/peer.go#L94-L103) for the source of each field. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct PeerEvent { + /// The type of the event. + #[serde(rename = "type")] + pub kind: PeerEventType, + /// The peer's enode ID. + pub peer: String, + /// An error ocurred on the peer. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub error: Option, + /// The protocol of the peer. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub protocol: Option, + /// The message code. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub msg_code: Option, + /// The message size. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub msg_size: Option, + /// The local endpoint of the TCP connection. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub local_address: Option, + /// The remote endpoint of the TCP connection. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub remote_address: Option, +} + mod handshake { use super::*; From faf31902de571499de9d3220e162fb4a5a8fb76f Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Sat, 29 Jun 2024 16:01:44 +0200 Subject: [PATCH 5/9] lint(fmt): fix formatting --- crates/rpc-types-admin/src/admin.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/rpc-types-admin/src/admin.rs b/crates/rpc-types-admin/src/admin.rs index 0793c1a3075..2814f16160e 100644 --- a/crates/rpc-types-admin/src/admin.rs +++ b/crates/rpc-types-admin/src/admin.rs @@ -199,7 +199,8 @@ pub enum PeerEventType { MsgRecv, } -/// An event emitted when peers are either added or dropped from a p2p server or when a message is sent or received on a peer connection. +/// An event emitted when peers are either added or dropped from a p2p server or when a message is +/// sent or received on a peer connection. /// /// See [geth's `PeerEvent` struct](https://github.com/ethereum/go-ethereum/blob/94579932b18931115f28aa7f87f02450bda084c9/p2p/peer.go#L94-L103) for the source of each field. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] From 69e9481f5c9610d770fcdfa7cc22cf45bef4bfe7 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Sat, 29 Jun 2024 16:05:59 +0200 Subject: [PATCH 6/9] fix: use alloy_pubsub when pubsub is enabled --- crates/provider/src/ext/admin.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/provider/src/ext/admin.rs b/crates/provider/src/ext/admin.rs index 4d828215ff0..c0e3ff8e6cf 100644 --- a/crates/provider/src/ext/admin.rs +++ b/crates/provider/src/ext/admin.rs @@ -1,7 +1,6 @@ //! This module extends the Ethereum JSON-RPC provider with the Admin namespace's RPC methods. use crate::Provider; use alloy_network::Network; -use alloy_pubsub::Subscription; use alloy_rpc_types_admin::{NodeInfo, PeerInfo}; use alloy_transport::{Transport, TransportResult}; @@ -80,7 +79,7 @@ where #[cfg(feature = "pubsub")] async fn subscribe_peer_events( &self, - ) -> TransportResult> { + ) -> TransportResult> { self.root().pubsub_frontend()?; let mut call = self.client().request("admin_peerEvents_subscribe", ()); call.set_is_subscription(); From 4bc2865ecd4992b3fbe406984b6c0dd5be0fe647 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Sat, 29 Jun 2024 16:13:58 +0200 Subject: [PATCH 7/9] fix: add missing type path --- crates/provider/src/ext/admin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/provider/src/ext/admin.rs b/crates/provider/src/ext/admin.rs index c0e3ff8e6cf..bc8b1874869 100644 --- a/crates/provider/src/ext/admin.rs +++ b/crates/provider/src/ext/admin.rs @@ -41,7 +41,7 @@ pub trait AdminApi: Send + Sync { #[cfg(feature = "pubsub")] async fn subscribe_peer_events( &self, - ) -> TransportResult>; + ) -> TransportResult>; } #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] From 729b4ea7ef90eaeedb4573a5d382a22331af0d58 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Sat, 29 Jun 2024 18:20:53 +0200 Subject: [PATCH 8/9] fix(docs): remove outdated comment --- crates/provider/src/ext/admin.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/provider/src/ext/admin.rs b/crates/provider/src/ext/admin.rs index bc8b1874869..6283754f61e 100644 --- a/crates/provider/src/ext/admin.rs +++ b/crates/provider/src/ext/admin.rs @@ -36,8 +36,6 @@ pub trait AdminApi: Send + Sync { /// /// Like other subscription methods, this returns the ID of the subscription, which is then used /// in all events subsequently. - /// - /// To unsubscribe from peer events, call `unsubscribe_peer_events`. #[cfg(feature = "pubsub")] async fn subscribe_peer_events( &self, From d58fee9d3607e518275b559b19fa77a325851ae7 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 3 Jul 2024 19:40:57 +0200 Subject: [PATCH 9/9] doc: nit --- crates/provider/src/ext/admin.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/provider/src/ext/admin.rs b/crates/provider/src/ext/admin.rs index 6283754f61e..158f9598d37 100644 --- a/crates/provider/src/ext/admin.rs +++ b/crates/provider/src/ext/admin.rs @@ -33,9 +33,6 @@ pub trait AdminApi: Send + Sync { async fn node_info(&self) -> TransportResult; /// Subscribe to events received by peers over the network. - /// - /// Like other subscription methods, this returns the ID of the subscription, which is then used - /// in all events subsequently. #[cfg(feature = "pubsub")] async fn subscribe_peer_events( &self,