Skip to content

Commit

Permalink
Add RawContentKey type
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenev committed May 16, 2022
1 parent 8debf1f commit f7f95b1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions trin-core/src/jsonrpc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use validator::{Validate, ValidationError};
use crate::{
jsonrpc::endpoints::{HistoryEndpoint, StateEndpoint, TrinEndpoint},
portalnet::types::{
content_key::OverlayContentKey,
content_key::{OverlayContentKey, RawContentKey},
messages::{ByteList, CustomPayload, SszEnr},
},
};
Expand Down Expand Up @@ -274,7 +274,7 @@ impl TryFrom<[&Value; 2]> for OfferParams {
.collect();

if let Ok(content_keys) = content_keys {
let content_keys: Result<Vec<Vec<u8>>, _> =
let content_keys: Result<Vec<RawContentKey>, _> =
content_keys.iter().map(hex::decode).collect();

if let Ok(content_keys) = content_keys {
Expand Down
11 changes: 7 additions & 4 deletions trin-core/src/portalnet/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ use crate::portalnet::{
},
};

use crate::utp::{
stream::{UtpListenerRequest, UtpSocket, BUF_SIZE},
trin_helpers::{UtpAccept, UtpMessage},
use crate::{
portalnet::types::content_key::RawContentKey,
utp::{
stream::{UtpListenerRequest, UtpSocket, BUF_SIZE},
trin_helpers::{UtpAccept, UtpMessage},
},
};
use discv5::{
enr::NodeId,
Expand Down Expand Up @@ -331,7 +334,7 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
/// Offer is also sent to nodes after FindContent (POKE)
pub async fn send_offer(
&self,
content_keys: Vec<Vec<u8>>,
content_keys: Vec<RawContentKey>,
enr: Enr,
) -> Result<Accept, OverlayRequestError> {
// Construct the request.
Expand Down
7 changes: 5 additions & 2 deletions trin-core/src/portalnet/overlay_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use crate::{
utp::stream::UtpListenerRequest,
};

use crate::utp::{stream::UtpPayload, trin_helpers::UtpStreamId};
use crate::{
portalnet::types::content_key::RawContentKey,
utp::{stream::UtpPayload, trin_helpers::UtpStreamId},
};
use delay_map::HashSetDelay;
use discv5::{
enr::NodeId,
Expand Down Expand Up @@ -776,7 +779,7 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
}

/// Process accepted uTP payload of the OFFER?ACCEPT stream
fn process_accept_utp_payload(&self, content_keys: Vec<Vec<u8>>, payload: UtpPayload) {
fn process_accept_utp_payload(&self, content_keys: Vec<RawContentKey>, payload: UtpPayload) {
// TODO: Verify the payload, store the content and propagate gossip.
debug!("DEBUG: Processing content keys: {content_keys:?}, with payload: {payload:?}");
}
Expand Down
3 changes: 3 additions & 0 deletions trin-core/src/portalnet/types/content_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use ssz::{self, Decode, Encode};
use ssz_derive::{Decode, Encode};
use ssz_types::{typenum, FixedVector, VariableList};

/// Raw overlay content key
pub type RawContentKey = Vec<u8>;

/// Types whose values represent keys to lookup content items in an overlay network.
/// Keys are serializable.
pub trait OverlayContentKey: Into<Vec<u8>> + TryFrom<Vec<u8>> + Clone {
Expand Down
4 changes: 2 additions & 2 deletions trin-core/src/portalnet/types/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ssz_types::{typenum, BitList, VariableList};
use thiserror::Error;
use validator::ValidationError;

use crate::portalnet::Enr;
use crate::portalnet::{types::content_key::RawContentKey, Enr};

pub type ByteList = VariableList<u8, typenum::U2048>;

Expand Down Expand Up @@ -492,7 +492,7 @@ impl TryInto<Value> for Content {

#[derive(Debug, PartialEq, Clone, Encode, Decode)]
pub struct Offer {
pub content_keys: Vec<Vec<u8>>,
pub content_keys: Vec<RawContentKey>,
}

#[derive(Debug, PartialEq, Clone, Encode, Decode)]
Expand Down
7 changes: 5 additions & 2 deletions trin-core/src/utp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use tokio::{

use crate::{
locks::RwLoggingExt,
portalnet::types::messages::{ByteList, Content::Content, ProtocolId},
portalnet::types::{
content_key::RawContentKey,
messages::{ByteList, Content::Content, ProtocolId},
},
utp::{
packets::{ExtensionType, Packet, PacketType, HEADER_SIZE},
time::{now_microseconds, Delay, Timestamp},
Expand Down Expand Up @@ -107,7 +110,7 @@ struct DelayDifferenceSample {
/// and uTP listener
pub enum UtpListenerRequest {
/// Request to listen for Accept stream
AcceptStream(ConnId, Vec<Vec<u8>>),
AcceptStream(ConnId, Vec<RawContentKey>),
/// Request to initialize uTP streram with remote node
Connect(ConnId, NodeId, oneshot::Sender<anyhow::Result<UtpSocket>>),
/// Request to listen for FindCOntent stream and send content data
Expand Down
4 changes: 2 additions & 2 deletions trin-core/src/utp/trin_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]
// These are just some Trin helper functions

use crate::portalnet::types::messages::Content;
use crate::portalnet::types::{content_key::RawContentKey, messages::Content};
use ssz_derive::{Decode, Encode};

// These Utp impl are related to sending messages over uTP not the implementation itself or stream
Expand Down Expand Up @@ -57,7 +57,7 @@ pub enum UtpStreamId {
FindContentStream,
FindContentData(Content),
OfferStream,
AcceptStream(Vec<Vec<u8>>),
AcceptStream(Vec<RawContentKey>),
}

#[cfg(test)]
Expand Down

0 comments on commit f7f95b1

Please sign in to comment.