Skip to content

Commit

Permalink
fix: use Subnetwork type instead of parsing protocol string
Browse files Browse the repository at this point in the history
  • Loading branch information
Omar Carey authored and Omar Carey committed Feb 24, 2025
1 parent d4743c6 commit d6ab117
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
3 changes: 2 additions & 1 deletion crates/ethportal-api/src/discv5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<u8>) -> RpcResult<Bytes>;
async fn talk_req(&self, enr: Enr, protocol: Subnetwork, request: Vec<u8>) -> RpcResult<Bytes>;

/// Send a PING message to the designated node and wait for a PONG response.
#[method(name = "ping")]
Expand Down
4 changes: 3 additions & 1 deletion crates/ethportal-api/src/types/network.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions crates/rpc/src/discv5_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>) -> RpcResult<Bytes> {
let subnetwork = protocol
.parse::<Subnetwork>()
.map_err(|err| RpcServeError::Message(format!("Unable to parse Subnetwork: {err}")))?;
async fn talk_req(&self, enr: Enr, protocol: Subnetwork, request: Vec<u8>) -> RpcResult<Bytes> {
self.discv5
.send_talk_req(enr, subnetwork, request)
.send_talk_req(enr, protocol, request)
.await
.map_err(|err| RpcServeError::Message(err.to_string()).into())
}
Expand Down
2 changes: 1 addition & 1 deletion testing/ethportal-peertest/src/scenarios/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d6ab117

Please sign in to comment.