|
1 | 1 | // TODO(shahak): Erase main_behaviour and make this a separate module. |
2 | 2 |
|
3 | 3 | use std::convert::Infallible; |
| 4 | +use std::time::Duration; |
4 | 5 |
|
5 | 6 | use libp2p::connection_limits::ConnectionLimits; |
6 | 7 | use libp2p::identity::Keypair; |
@@ -94,6 +95,57 @@ impl MixedBehaviour { |
94 | 95 | let kademlia_config = kad::Config::new(protocol_name); |
95 | 96 | let connection_limits = ConnectionLimits::default(); // .with_max_established_per_peer(Some(1)); |
96 | 97 |
|
| 98 | + let gossipsub_config = gossipsub::ConfigBuilder::default() |
| 99 | + .max_transmit_size(1 << 34) |
| 100 | + .flood_publish(false) |
| 101 | + .heartbeat_interval(Duration::from_millis(700)) |
| 102 | + // .validation_mode(ValidationMode::None) |
| 103 | + .message_id_fn(|message| { |
| 104 | + let mut source_string = message.source.as_ref().map(|id| id.to_bytes()).unwrap_or_default(); |
| 105 | + source_string |
| 106 | + .extend_from_slice(&message.sequence_number.unwrap_or_default().to_be_bytes()); |
| 107 | + source_string.extend_from_slice(&message.data[0..16]); |
| 108 | + gossipsub::MessageId::from(source_string) |
| 109 | + }) |
| 110 | + |
| 111 | + |
| 112 | + // .max_messages_per_rpc(Some(1)) |
| 113 | + .history_length(5) |
| 114 | + |
| 115 | + // .connection_handler_queue_len(50_000) |
| 116 | + |
| 117 | + .mesh_n(10) // Target mesh peers |
| 118 | + .mesh_n_low(10) // Minimum before adding (default: 5) |
| 119 | + .mesh_n_high(10) // Maximum before pruning (default |
| 120 | + // .publish_queue_duration(Duration::from_millis(500)) // default: 5s |
| 121 | + // .forward_queue_duration(Duration::from_millis(100)) // default: 1s |
| 122 | + |
| 123 | + |
| 124 | + // .heartbeat_interval(Duration::from_millis(200)) |
| 125 | + // Set gossip_lazy to 0 - minimum number of peers to gossip to |
| 126 | + .gossip_lazy(0) |
| 127 | + |
| 128 | + // Set gossip_factor to 0.0 - factor for dynamic gossip peer selection |
| 129 | + .gossip_factor(0.0) |
| 130 | + |
| 131 | + // Set history_gossip to 0 - number of past heartbeats to gossip about |
| 132 | + .history_gossip(0) |
| 133 | + |
| 134 | + // Optional: Set max_ihave_length to 0 - maximum IHAVE message IDs per message |
| 135 | + .max_ihave_length(0) |
| 136 | + |
| 137 | + // Optional: Set max_ihave_messages to 0 - maximum IHAVE messages per heartbeat |
| 138 | + .max_ihave_messages(0) |
| 139 | + |
| 140 | + // Optional: Set gossip_retransmission to 0 - disable IWANT retries |
| 141 | + .gossip_retransimission(0) |
| 142 | + |
| 143 | + // Enable IDONTWANT optimization |
| 144 | + // .idontwant_message_size_threshold(1000) // Adjust based on your message sizes |
| 145 | + .idontwant_on_publish(true) // Prevent echo-back on publish |
| 146 | + |
| 147 | + .build() |
| 148 | + .expect("Failed to build gossipsub config"); |
97 | 149 | Self { |
98 | 150 | limits: connection_limits::Behaviour::new(connection_limits), |
99 | 151 | peer_manager: peer_manager::PeerManager::new(peer_manager_config), |
@@ -134,12 +186,8 @@ impl MixedBehaviour { |
134 | 186 | ), |
135 | 187 | sqmr: sqmr::Behaviour::new(streamed_bytes_config), |
136 | 188 | gossipsub: gossipsub::Behaviour::new( |
137 | | - gossipsub::MessageAuthenticity::Signed(keypair.clone()), |
138 | | - gossipsub::ConfigBuilder::default() |
139 | | - // TODO(shahak): try to reduce this bound. |
140 | | - .max_transmit_size(1 << 34) |
141 | | - .build() |
142 | | - .expect("Failed to build gossipsub config"), |
| 189 | + gossipsub::MessageAuthenticity::Signed(keypair.clone()), |
| 190 | + gossipsub_config, |
143 | 191 | ) |
144 | 192 | .unwrap_or_else(|err_string| { |
145 | 193 | panic!( |
|
0 commit comments