Skip to content

Commit

Permalink
Add ProtocolKind enum for overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenev committed Sep 21, 2021
1 parent 111bddf commit b58bedc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
14 changes: 7 additions & 7 deletions trin-core/src/portalnet/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::utils::xor_two_values;

use super::{
discovery::Discovery,
types::{FindContent, FindNodes, Message, Ping, Request, SszEnr},
types::{FindContent, FindNodes, Message, Ping, ProtocolKind, Request, SszEnr},
Enr, U256,
};
use crate::portalnet::types::HexData;
Expand Down Expand Up @@ -186,7 +186,7 @@ impl OverlayProtocol {
&self,
data_radius: U256,
enr: Enr,
protocol: String,
protocol: ProtocolKind,
) -> Result<Vec<u8>, String> {
let enr_seq = self.discovery.read().await.local_enr().seq();
let msg = Ping {
Expand All @@ -198,7 +198,7 @@ impl OverlayProtocol {
.await
.send_talkreq(
enr,
protocol,
protocol.to_string(),
Message::Request(Request::Ping(msg)).to_bytes(),
)
.await
Expand All @@ -208,15 +208,15 @@ impl OverlayProtocol {
&self,
distances: Vec<u16>,
enr: Enr,
protocol: String,
protocol: ProtocolKind,
) -> Result<Vec<u8>, String> {
let msg = FindNodes { distances };
self.discovery
.write()
.await
.send_talkreq(
enr,
protocol,
protocol.to_string(),
Message::Request(Request::FindNodes(msg)).to_bytes(),
)
.await
Expand All @@ -226,15 +226,15 @@ impl OverlayProtocol {
&self,
content_key: Vec<u8>,
enr: Enr,
protocol: String,
protocol: ProtocolKind,
) -> Result<Vec<u8>, String> {
let msg = FindContent { content_key };
self.discovery
.write()
.await
.send_talkreq(
enr,
protocol,
protocol.to_string(),
Message::Request(Request::FindContent(msg)).to_bytes(),
)
.await
Expand Down
15 changes: 15 additions & 0 deletions trin-core/src/portalnet/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ use ssz_derive::{Decode, Encode};

use super::{Enr, U256};

#[derive(Debug, PartialEq, Clone)]
pub enum ProtocolKind {
History,
State,
}

impl ToString for ProtocolKind {
fn to_string(&self) -> String {
match self {
ProtocolKind::History => "history".to_string(),
ProtocolKind::State => "state".to_string(),
}
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct ProtocolMessage {
message_id: u8,
Expand Down
4 changes: 2 additions & 2 deletions trin-history/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use discv5::kbucket::KBucketsTable;
use log::debug;
use std::sync::Arc;
use tokio::sync::RwLock;
use trin_core::cli::STATE_NETWORK;
use trin_core::portalnet::{
discovery::Discovery,
overlay::{OverlayConfig, OverlayProtocol, PortalnetConfig},
types::ProtocolKind,
U256,
};

Expand Down Expand Up @@ -61,7 +61,7 @@ impl HistoryNetwork {
debug!("Pinging {} on portal history network", enr);
let ping_result = self
.overlay
.send_ping(U256::from(u64::MAX), enr, STATE_NETWORK.to_string())
.send_ping(U256::from(u64::MAX), enr, ProtocolKind::History)
.await?;
debug!("Portal history network Ping result: {:?}", ping_result);
}
Expand Down
4 changes: 2 additions & 2 deletions trin-state/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use discv5::kbucket::KBucketsTable;
use log::debug;
use std::sync::Arc;
use tokio::sync::RwLock;
use trin_core::cli::HISTORY_NETWORK;
use trin_core::portalnet::{
discovery::Discovery,
overlay::{OverlayConfig, OverlayProtocol, PortalnetConfig},
types::ProtocolKind,
U256,
};

Expand Down Expand Up @@ -61,7 +61,7 @@ impl StateNetwork {
debug!("Pinging {} on portal state network", enr);
let ping_result = self
.overlay
.send_ping(U256::from(u64::MAX), enr, HISTORY_NETWORK.to_string())
.send_ping(U256::from(u64::MAX), enr, ProtocolKind::State)
.await?;
debug!("Portal state network Ping result: {:?}", ping_result);
}
Expand Down

0 comments on commit b58bedc

Please # to comment.