diff --git a/node/network/src/behaviour/gossip_cache.rs b/node/network/src/behaviour/gossip_cache.rs index acaa3c7..78b38d3 100644 --- a/node/network/src/behaviour/gossip_cache.rs +++ b/node/network/src/behaviour/gossip_cache.rs @@ -20,6 +20,8 @@ pub struct GossipCache { topic_msgs: HashMap, Key>>, /// Timeout for Example messages. example: Option, + /// Timeout for NewFile messages. + new_file: Option, /// Timeout for FindFile messages. find_file: Option, /// Timeout for FindChunks messages. @@ -37,6 +39,8 @@ pub struct GossipCacheBuilder { default_timeout: Option, /// Timeout for Example messages. example: Option, + /// Timeout for NewFile messges. + new_file: Option, /// Timeout for blocks FindFile messages. find_file: Option, /// Timeout for blocks FindChunks messages. @@ -64,6 +68,12 @@ impl GossipCacheBuilder { self } + /// Timeout for NewFile messages. + pub fn new_file_timeout(mut self, timeout: Duration) -> Self { + self.new_file = Some(timeout); + self + } + /// Timeout for FindFile messages. pub fn find_file_timeout(mut self, timeout: Duration) -> Self { self.find_file = Some(timeout); @@ -98,6 +108,7 @@ impl GossipCacheBuilder { let GossipCacheBuilder { default_timeout, example, + new_file, find_file, find_chunks, announce_file, @@ -109,6 +120,7 @@ impl GossipCacheBuilder { expirations: DelayQueue::default(), topic_msgs: HashMap::default(), example: example.or(default_timeout), + new_file: new_file.or(default_timeout), find_file: find_file.or(default_timeout), find_chunks: find_chunks.or(default_timeout), announce_file: announce_file.or(default_timeout), @@ -129,6 +141,7 @@ impl GossipCache { pub fn insert(&mut self, topic: GossipTopic, data: Vec) { let expire_timeout = match topic.kind() { GossipKind::Example => self.example, + GossipKind::NewFile => self.new_file, GossipKind::FindFile => self.find_file, GossipKind::FindChunks => self.find_chunks, GossipKind::AnnounceFile => self.announce_file, diff --git a/node/network/src/behaviour/mod.rs b/node/network/src/behaviour/mod.rs index d06c692..f3c6c26 100644 --- a/node/network/src/behaviour/mod.rs +++ b/node/network/src/behaviour/mod.rs @@ -232,6 +232,9 @@ impl Behaviour { let topic: Topic = GossipTopic::new(kind, GossipEncoding::default()).into(); topic.hash() }; + params + .topics + .insert(get_hash(GossipKind::NewFile), TopicScoreParams::default()); params .topics .insert(get_hash(GossipKind::FindFile), TopicScoreParams::default()); diff --git a/node/network/src/types/mod.rs b/node/network/src/types/mod.rs index bb0a661..c8f93a7 100644 --- a/node/network/src/types/mod.rs +++ b/node/network/src/types/mod.rs @@ -7,7 +7,7 @@ pub type Enr = discv5::enr::Enr; pub use globals::NetworkGlobals; pub use pubsub::{ - AnnounceChunks, AnnounceFile, AnnounceShardConfig, FindChunks, FindFile, HasSignature, + AnnounceChunks, AnnounceFile, AnnounceShardConfig, FindChunks, FindFile, HasSignature, NewFile, PubsubMessage, SignedAnnounceChunks, SignedAnnounceFile, SignedAnnounceShardConfig, SignedMessage, SnappyTransform, }; diff --git a/node/network/src/types/pubsub.rs b/node/network/src/types/pubsub.rs index 13f6a60..700878b 100644 --- a/node/network/src/types/pubsub.rs +++ b/node/network/src/types/pubsub.rs @@ -114,6 +114,14 @@ impl ssz::Decode for WrappedPeerId { } } +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] +pub struct NewFile { + pub tx_id: TxID, + pub num_shard: usize, + pub shard_id: usize, + pub timestamp: u32, +} + #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct FindFile { pub tx_id: TxID, @@ -205,6 +213,7 @@ type SignedAnnounceFiles = Vec; #[derive(Debug, Clone, PartialEq, Eq)] pub enum PubsubMessage { ExampleMessage(u64), + NewFile(NewFile), FindFile(FindFile), FindChunks(FindChunks), AnnounceFile(Vec), @@ -283,6 +292,7 @@ impl PubsubMessage { pub fn kind(&self) -> GossipKind { match self { PubsubMessage::ExampleMessage(_) => GossipKind::Example, + PubsubMessage::NewFile(_) => GossipKind::NewFile, PubsubMessage::FindFile(_) => GossipKind::FindFile, PubsubMessage::FindChunks(_) => GossipKind::FindChunks, PubsubMessage::AnnounceFile(_) => GossipKind::AnnounceFile, @@ -309,6 +319,9 @@ impl PubsubMessage { GossipKind::Example => Ok(PubsubMessage::ExampleMessage( u64::from_ssz_bytes(data).map_err(|e| format!("{:?}", e))?, )), + GossipKind::NewFile => Ok(PubsubMessage::NewFile( + NewFile::from_ssz_bytes(data).map_err(|e| format!("{:?}", e))?, + )), GossipKind::FindFile => Ok(PubsubMessage::FindFile( FindFile::from_ssz_bytes(data).map_err(|e| format!("{:?}", e))?, )), @@ -341,6 +354,7 @@ impl PubsubMessage { // messages for us. match &self { PubsubMessage::ExampleMessage(data) => data.as_ssz_bytes(), + PubsubMessage::NewFile(data) => data.as_ssz_bytes(), PubsubMessage::FindFile(data) => data.as_ssz_bytes(), PubsubMessage::FindChunks(data) => data.as_ssz_bytes(), PubsubMessage::AnnounceFile(data) => data.as_ssz_bytes(), @@ -356,6 +370,9 @@ impl std::fmt::Display for PubsubMessage { PubsubMessage::ExampleMessage(msg) => { write!(f, "Example message: {}", msg) } + PubsubMessage::NewFile(msg) => { + write!(f, "NewFile message: {:?}", msg) + } PubsubMessage::FindFile(msg) => { write!(f, "FindFile message: {:?}", msg) } diff --git a/node/network/src/types/topics.rs b/node/network/src/types/topics.rs index e9dfba8..a4fc80b 100644 --- a/node/network/src/types/topics.rs +++ b/node/network/src/types/topics.rs @@ -8,13 +8,15 @@ use strum::AsRefStr; pub const TOPIC_PREFIX: &str = "eth2"; pub const SSZ_SNAPPY_ENCODING_POSTFIX: &str = "ssz_snappy"; pub const EXAMPLE_TOPIC: &str = "example"; +pub const NEW_FILE_TOPIC: &str = "new_file"; pub const FIND_FILE_TOPIC: &str = "find_file"; pub const FIND_CHUNKS_TOPIC: &str = "find_chunks"; pub const ANNOUNCE_FILE_TOPIC: &str = "announce_file"; pub const ANNOUNCE_CHUNKS_TOPIC: &str = "announce_chunks"; pub const ANNOUNCE_SHARD_CONFIG_TOPIC: &str = "announce_shard_config"; -pub const CORE_TOPICS: [GossipKind; 4] = [ +pub const CORE_TOPICS: [GossipKind; 5] = [ + GossipKind::NewFile, GossipKind::FindFile, GossipKind::FindChunks, GossipKind::AnnounceFile, @@ -37,6 +39,7 @@ pub struct GossipTopic { #[strum(serialize_all = "snake_case")] pub enum GossipKind { Example, + NewFile, FindFile, FindChunks, AnnounceFile, @@ -77,6 +80,7 @@ impl GossipTopic { let kind = match topic_parts[2] { EXAMPLE_TOPIC => GossipKind::Example, + NEW_FILE_TOPIC => GossipKind::NewFile, FIND_FILE_TOPIC => GossipKind::FindFile, FIND_CHUNKS_TOPIC => GossipKind::FindChunks, ANNOUNCE_FILE_TOPIC => GossipKind::AnnounceFile, @@ -106,6 +110,7 @@ impl From for String { let kind = match topic.kind { GossipKind::Example => EXAMPLE_TOPIC, + GossipKind::NewFile => NEW_FILE_TOPIC, GossipKind::FindFile => FIND_FILE_TOPIC, GossipKind::FindChunks => FIND_CHUNKS_TOPIC, GossipKind::AnnounceFile => ANNOUNCE_FILE_TOPIC, @@ -125,6 +130,7 @@ impl std::fmt::Display for GossipTopic { let kind = match self.kind { GossipKind::Example => EXAMPLE_TOPIC, + GossipKind::NewFile => NEW_FILE_TOPIC, GossipKind::FindFile => FIND_FILE_TOPIC, GossipKind::FindChunks => FIND_CHUNKS_TOPIC, GossipKind::AnnounceFile => ANNOUNCE_FILE_TOPIC, diff --git a/node/router/src/libp2p_event_handler.rs b/node/router/src/libp2p_event_handler.rs index d1b09cc..c42dc07 100644 --- a/node/router/src/libp2p_event_handler.rs +++ b/node/router/src/libp2p_event_handler.rs @@ -316,6 +316,8 @@ impl Libp2pEventHandler { match message { PubsubMessage::ExampleMessage(_) => MessageAcceptance::Ignore, + // TODO qbit: handle the NewFile pubsub message + PubsubMessage::NewFile(_) => todo!(), PubsubMessage::FindFile(msg) => { metrics::LIBP2P_HANDLE_PUBSUB_FIND_FILE.mark(1); self.on_find_file(msg).await