11use std:: collections:: hash_map:: DefaultHasher ;
22use std:: collections:: HashSet ;
33use std:: hash:: { Hash , Hasher } ;
4- use std:: {
5- collections:: { HashMap , VecDeque } ,
6- env,
7- task:: Poll ,
8- time:: Duration ,
9- } ;
4+ use std:: { collections:: HashMap , task:: Poll } ;
105
6+ use libp2p:: gossipsub:: MessageId ;
117use libp2p:: swarm:: { ConnectionClosed , FromSwarm } ;
128use libp2p:: PeerId ;
139use libp2p:: {
1410 gossipsub:: { self , IdentTopic , Message , MessageAuthenticity } ,
1511 identity:: Keypair ,
1612 swarm:: { NetworkBehaviour , THandlerInEvent , ToSwarm } ,
1713} ;
18- use prost:: Message as ProstMessage ;
19- use topos_core:: api:: grpc:: tce:: v1:: Batch ;
20- use topos_metrics:: P2P_GOSSIP_BATCH_SIZE ;
21- use tracing:: { debug, error, warn} ;
14+ use tracing:: { debug, trace, warn} ;
2215
2316use crate :: error:: P2PError ;
2417use crate :: { constants, event:: ComposedEvent , TOPOS_ECHO , TOPOS_GOSSIP , TOPOS_READY } ;
2518
2619use super :: HealthStatus ;
2720
28- const MAX_BATCH_SIZE : usize = 10 ;
29-
3021pub struct Behaviour {
31- batch_size : usize ,
3222 gossipsub : gossipsub:: Behaviour ,
33- pending : HashMap < & ' static str , VecDeque < Vec < u8 > > > ,
34- tick : tokio:: time:: Interval ,
3523 /// List of connected peers per topic.
3624 connected_peer : HashMap < & ' static str , HashSet < PeerId > > ,
3725 /// The health status of the gossip behaviour
@@ -43,18 +31,16 @@ impl Behaviour {
4331 & mut self ,
4432 topic : & ' static str ,
4533 message : Vec < u8 > ,
46- ) -> Result < usize , & ' static str > {
34+ ) -> Result < MessageId , P2PError > {
4735 match topic {
48- TOPOS_GOSSIP => {
49- if let Ok ( msg_id) = self . gossipsub . publish ( IdentTopic :: new ( topic) , message) {
50- debug ! ( "Published on topos_gossip: {:?}" , msg_id) ;
51- }
36+ TOPOS_GOSSIP | TOPOS_ECHO | TOPOS_READY => {
37+ let msg_id = self . gossipsub . publish ( IdentTopic :: new ( topic) , message) ?;
38+ trace ! ( "Published on topos_gossip: {:?}" , msg_id) ;
39+
40+ Ok ( msg_id)
5241 }
53- TOPOS_ECHO | TOPOS_READY => self . pending . entry ( topic) . or_default ( ) . push_back ( message) ,
54- _ => return Err ( "Invalid topic" ) ,
42+ _ => Err ( P2PError :: InvalidGossipTopic ( topic) ) ,
5543 }
56-
57- Ok ( 0 )
5844 }
5945
6046 pub fn subscribe ( & mut self ) -> Result < ( ) , P2PError > {
@@ -71,10 +57,6 @@ impl Behaviour {
7157 }
7258
7359 pub async fn new ( peer_key : Keypair ) -> Self {
74- let batch_size = env:: var ( "TOPOS_GOSSIP_BATCH_SIZE" )
75- . map ( |v| v. parse :: < usize > ( ) )
76- . unwrap_or ( Ok ( MAX_BATCH_SIZE ) )
77- . unwrap ( ) ;
7860 let gossipsub = gossipsub:: ConfigBuilder :: default ( )
7961 . max_transmit_size ( 2 * 1024 * 1024 )
8062 . validation_mode ( gossipsub:: ValidationMode :: Strict )
@@ -99,21 +81,7 @@ impl Behaviour {
9981 . unwrap ( ) ;
10082
10183 Self {
102- batch_size,
10384 gossipsub,
104- pending : [
105- ( TOPOS_ECHO , VecDeque :: new ( ) ) ,
106- ( TOPOS_READY , VecDeque :: new ( ) ) ,
107- ]
108- . into_iter ( )
109- . collect ( ) ,
110- tick : tokio:: time:: interval ( Duration :: from_millis (
111- env:: var ( "TOPOS_GOSSIP_INTERVAL" )
112- . map ( |v| v. parse :: < u64 > ( ) )
113- . unwrap_or ( Ok ( 100 ) )
114- . unwrap ( ) ,
115- ) ) ,
116-
11785 connected_peer : Default :: default ( ) ,
11886 health_status : Default :: default ( ) ,
11987 }
@@ -191,26 +159,6 @@ impl NetworkBehaviour for Behaviour {
191159 & mut self ,
192160 cx : & mut std:: task:: Context < ' _ > ,
193161 ) -> Poll < ToSwarm < Self :: ToSwarm , THandlerInEvent < Self > > > {
194- if self . tick . poll_tick ( cx) . is_ready ( ) {
195- // Publish batch
196- for ( topic, queue) in self . pending . iter_mut ( ) {
197- if !queue. is_empty ( ) {
198- let num_of_message = queue. len ( ) . min ( self . batch_size ) ;
199- let batch = Batch {
200- messages : queue. drain ( 0 ..num_of_message) . collect ( ) ,
201- } ;
202-
203- debug ! ( "Publishing {} {}" , batch. messages. len( ) , topic) ;
204- let msg = batch. encode_to_vec ( ) ;
205- P2P_GOSSIP_BATCH_SIZE . observe ( batch. messages . len ( ) as f64 ) ;
206- match self . gossipsub . publish ( IdentTopic :: new ( * topic) , msg) {
207- Ok ( message_id) => debug ! ( "Published {} {}" , topic, message_id) ,
208- Err ( error) => error ! ( "Failed to publish {}: {}" , topic, error) ,
209- }
210- }
211- }
212- }
213-
214162 match self . gossipsub . poll ( cx) {
215163 Poll :: Pending => return Poll :: Pending ,
216164 Poll :: Ready ( ToSwarm :: GenerateEvent ( event) ) => match event {
@@ -231,6 +179,7 @@ impl NetworkBehaviour for Behaviour {
231179 topic : TOPOS_GOSSIP ,
232180 message : data,
233181 source,
182+ id : message_id,
234183 } ,
235184 ) ) )
236185 }
@@ -240,6 +189,7 @@ impl NetworkBehaviour for Behaviour {
240189 topic : TOPOS_ECHO ,
241190 message : data,
242191 source,
192+ id : message_id,
243193 } ,
244194 ) ) )
245195 }
@@ -249,6 +199,7 @@ impl NetworkBehaviour for Behaviour {
249199 topic : TOPOS_READY ,
250200 message : data,
251201 source,
202+ id : message_id,
252203 } ,
253204 ) ) )
254205 }
0 commit comments