Skip to content

Commit 1f99788

Browse files
committed
Initial vector tests passing
1 parent 00a9479 commit 1f99788

File tree

6 files changed

+366
-162
lines changed

6 files changed

+366
-162
lines changed

ethportal-peertest/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
8888

8989
// Test history and state network Ping request on target node
9090
info!("Pinging {} on history network", target_node);
91-
let ping_result = history_overlay.send_ping(target_node.clone(), None).await;
91+
let ping_result = history_overlay.send_ping(target_node.clone()).await;
9292
match ping_result {
9393
Ok(val) => info!("Successful ping to History network: {:?}", val),
9494
Err(msg) => panic!("Invalid ping to History network: {:?}", msg),
9595
}
9696

9797
info!("Pinging {} on state network", target_node);
98-
let ping_result = state_overlay.send_ping(target_node.clone(), None).await;
98+
let ping_result = state_overlay.send_ping(target_node.clone()).await;
9999
match ping_result {
100100
Ok(val) => info!("Successful ping to State network: {:?}", val),
101101
Err(msg) => panic!("Invalid ping to State network: {:?}", msg),

trin-core/src/portalnet/overlay.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,14 @@ impl OverlayProtocol {
156156
}
157157

158158
/// Sends a `Ping` request to `enr`.
159-
pub async fn send_ping(
160-
&self,
161-
enr: Enr,
162-
payload: Option<Vec<u8>>,
163-
) -> Result<Pong, OverlayRequestError> {
159+
pub async fn send_ping(&self, enr: Enr) -> Result<Pong, OverlayRequestError> {
164160
// Construct the request.
165161
let enr_seq = self.discovery.read_with_warn().await.local_enr().seq();
166162
let data_radius = self.data_radius().await;
167-
let payload = CustomPayload::new(data_radius, payload);
163+
let custom_payload = CustomPayload::new(u64::to_be_bytes(data_radius.as_u64()).to_vec());
168164
let request = Ping {
169165
enr_seq,
170-
payload: Some(payload),
166+
custom_payload,
171167
};
172168

173169
let node_id = enr.node_id();

trin-core/src/portalnet/overlay_service.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::{
66
portalnet::{
77
discovery::Discovery,
88
types::{
9-
CustomPayload, FindContent, FindNodes, FoundContent, Message, Nodes, Ping, Pong,
10-
ProtocolId, Request, Response, SszEnr,
9+
ByteList, CustomPayload, FindContent, FindNodes, FoundContent, Message, Nodes, Ping,
10+
Pong, ProtocolId, Request, Response, SszEnr,
1111
},
1212
Enr, U256,
1313
},
@@ -18,6 +18,8 @@ use discv5::{enr::NodeId, kbucket::KBucketsTable};
1818
use futures::channel::oneshot;
1919
use log::{debug, info};
2020
use rocksdb::DB;
21+
use ssz::Encode;
22+
use ssz_types::VariableList;
2123
use thiserror::Error;
2224
use tokio::sync::{
2325
mpsc::{self, UnboundedReceiver, UnboundedSender},
@@ -257,8 +259,11 @@ impl OverlayService {
257259
);
258260
let enr_seq = self.local_enr().await.seq();
259261
let data_radius = self.data_radius().await;
260-
let payload = Some(CustomPayload::new(data_radius, None));
261-
Pong { enr_seq, payload }
262+
let custom_payload = CustomPayload::new(data_radius.as_ssz_bytes());
263+
Pong {
264+
enr_seq,
265+
custom_payload,
266+
}
262267
}
263268

264269
/// Builds a `Nodes` response for a `FindNodes` request.
@@ -276,16 +281,13 @@ impl OverlayService {
276281
request: FindContent,
277282
) -> Result<FoundContent, OverlayRequestError> {
278283
match self.db.get(&request.content_key) {
279-
Ok(Some(value)) => Ok(FoundContent {
280-
enrs: vec![],
281-
payload: value,
282-
}),
284+
Ok(Some(value)) => {
285+
let content = Some(ByteList::from(VariableList::from(value)));
286+
Ok(FoundContent::new(None, None, content))
287+
}
283288
Ok(None) => {
284-
let enrs = self.find_nodes_close_to_content(request.content_key).await;
285-
Ok(FoundContent {
286-
enrs,
287-
payload: vec![],
288-
})
289+
let enrs = Some(self.find_nodes_close_to_content(request.content_key).await);
290+
Ok(FoundContent::new(None, enrs, None))
289291
}
290292
Err(error) => panic!("Unable to respond to FindContent: {}", error),
291293
}
@@ -328,15 +330,15 @@ impl OverlayService {
328330
}
329331

330332
/// Returns a vector of the ENRs of the closest nodes by the given log2 distances.
331-
async fn nodes_by_distance(&self, mut log2_distances: Vec<u64>) -> Vec<Enr> {
333+
async fn nodes_by_distance(&self, mut log2_distances: Vec<u64>) -> Vec<SszEnr> {
332334
let mut nodes_to_send = Vec::new();
333335
log2_distances.sort_unstable();
334336
log2_distances.dedup();
335337

336338
let mut log2_distances = log2_distances.as_slice();
337339
if let Some(0) = log2_distances.first() {
338340
// If the distance is 0 send our local ENR.
339-
nodes_to_send.push(self.local_enr().await);
341+
nodes_to_send.push(SszEnr::new(self.local_enr().await));
340342
log2_distances = &log2_distances[1..];
341343
}
342344

@@ -347,7 +349,7 @@ impl OverlayService {
347349
.into_iter()
348350
.map(|entry| entry.node.value.clone())
349351
{
350-
nodes_to_send.push(node.enr());
352+
nodes_to_send.push(SszEnr::new(node.enr()));
351353
}
352354
}
353355
nodes_to_send

0 commit comments

Comments
 (0)