Skip to content

Commit

Permalink
feat: add missing admin_* methods (alloy-rs#991)
Browse files Browse the repository at this point in the history
* feat: add missing admin_* methods

* fix: add 0x prefix to subscription ids

* fix: u16 -> U128

* fix: remork to match other subscriptions

* lint(fmt): fix formatting

* fix: use alloy_pubsub when pubsub is enabled

* fix: add missing type path

* fix(docs): remove outdated comment

* doc: nit

---------

Co-authored-by: DaniPopes <[email protected]>
  • Loading branch information
alexfertel and DaniPopes authored Jul 3, 2024
1 parent 3cbefdd commit ec9e13e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/provider/src/ext/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pub trait AdminApi<N, T>: 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<NodeInfo>;

/// Subscribe to events received by peers over the network.
#[cfg(feature = "pubsub")]
async fn subscribe_peer_events(
&self,
) -> TransportResult<alloy_pubsub::Subscription<alloy_rpc_types_admin::PeerEvent>>;
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
Expand Down Expand Up @@ -64,6 +70,17 @@ where
async fn node_info(&self) -> TransportResult<NodeInfo> {
self.client().request("admin_nodeInfo", ()).await
}

#[cfg(feature = "pubsub")]
async fn subscribe_peer_events(
&self,
) -> TransportResult<alloy_pubsub::Subscription<alloy_rpc_types_admin::PeerEvent>> {
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
}
}

#[cfg(test)]
Expand Down
45 changes: 45 additions & 0 deletions crates/rpc-types-admin/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,51 @@ 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<String>,
/// The protocol of the peer.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub protocol: Option<String>,
/// The message code.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub msg_code: Option<u64>,
/// The message size.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub msg_size: Option<u32>,
/// The local endpoint of the TCP connection.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub local_address: Option<SocketAddr>,
/// The remote endpoint of the TCP connection.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remote_address: Option<SocketAddr>,
}

mod handshake {
use super::*;

Expand Down

0 comments on commit ec9e13e

Please sign in to comment.