From d6ab1174e7d2ebd7a866c1afbaf22b77b41f7e1f Mon Sep 17 00:00:00 2001 From: Omar Carey Date: Mon, 24 Feb 2025 09:10:47 -0800 Subject: [PATCH] fix: use Subnetwork type instead of parsing protocol string --- crates/ethportal-api/src/discv5.rs | 3 ++- crates/ethportal-api/src/types/network.rs | 4 +++- crates/rpc/src/discv5_rpc.rs | 7 ++----- testing/ethportal-peertest/src/scenarios/basic.rs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/ethportal-api/src/discv5.rs b/crates/ethportal-api/src/discv5.rs index bc0710271..3c09b09c4 100644 --- a/crates/ethportal-api/src/discv5.rs +++ b/crates/ethportal-api/src/discv5.rs @@ -5,6 +5,7 @@ use jsonrpsee::{core::RpcResult, proc_macros::rpc}; use crate::types::{ discv5::{NodeInfo, Pong, RoutingTableInfo}, enr::Enr, + network::Subnetwork, }; /// Discv5 JSON-RPC endpoints @@ -44,7 +45,7 @@ pub trait Discv5Api { /// Send a TALKREQ request with a payload to a given peer and wait for response. #[method(name = "talkReq")] - async fn talk_req(&self, enr: Enr, protocol: String, request: Vec) -> RpcResult; + async fn talk_req(&self, enr: Enr, protocol: Subnetwork, request: Vec) -> RpcResult; /// Send a PING message to the designated node and wait for a PONG response. #[method(name = "ping")] diff --git a/crates/ethportal-api/src/types/network.rs b/crates/ethportal-api/src/types/network.rs index 9a448a8aa..fcfa1ce5b 100644 --- a/crates/ethportal-api/src/types/network.rs +++ b/crates/ethportal-api/src/types/network.rs @@ -1,5 +1,7 @@ use std::fmt; +use serde::{Deserialize, Serialize}; + /// Enum for different "core" networks. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Network { @@ -29,7 +31,7 @@ impl std::str::FromStr for Network { } /// Enum for various different portal subnetworks in a "core" network. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)] pub enum Subnetwork { Beacon, History, diff --git a/crates/rpc/src/discv5_rpc.rs b/crates/rpc/src/discv5_rpc.rs index 3e2ecf6df..62dbc9eb9 100644 --- a/crates/rpc/src/discv5_rpc.rs +++ b/crates/rpc/src/discv5_rpc.rs @@ -98,12 +98,9 @@ impl Discv5ApiServer for Discv5Api { } /// Send a TALKREQ request with a payload to a given peer and wait for response. - async fn talk_req(&self, enr: Enr, protocol: String, request: Vec) -> RpcResult { - let subnetwork = protocol - .parse::() - .map_err(|err| RpcServeError::Message(format!("Unable to parse Subnetwork: {err}")))?; + async fn talk_req(&self, enr: Enr, protocol: Subnetwork, request: Vec) -> RpcResult { self.discv5 - .send_talk_req(enr, subnetwork, request) + .send_talk_req(enr, protocol, request) .await .map_err(|err| RpcServeError::Message(err.to_string()).into()) } diff --git a/testing/ethportal-peertest/src/scenarios/basic.rs b/testing/ethportal-peertest/src/scenarios/basic.rs index fe65c493e..758bc72a8 100644 --- a/testing/ethportal-peertest/src/scenarios/basic.rs +++ b/testing/ethportal-peertest/src/scenarios/basic.rs @@ -107,7 +107,7 @@ pub async fn test_discv5_update_node_info(target: &Client) { pub async fn test_discv5_talk_req(target: &Client, peertest: &Peertest) { let enr = peertest.bootnode.enr.clone(); - let protocol = String::from("beacon"); + let protocol = Subnetwork::Beacon; let data_radius = U256::MAX.as_ssz_bytes(); let request = Message::Ping(Ping { enr_seq: 1,