|
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; |
| 7 | +use libp2p::gossipsub::ValidationMode; |
6 | 8 | use libp2p::identity::Keypair; |
7 | 9 | use libp2p::kad::store::MemoryStore; |
8 | 10 | use libp2p::swarm::behaviour::toggle::Toggle; |
@@ -81,8 +83,59 @@ impl MixedBehaviour { |
81 | 83 | StreamProtocol::try_from_owned(format!("/starknet/kad/{chain_id}/1.0.0")) |
82 | 84 | .expect("Failed to create StreamProtocol from a string that starts with /"); |
83 | 85 | let kademlia_config = kad::Config::new(protocol_name); |
84 | | - let connection_limits = ConnectionLimits::default().with_max_established_per_peer(Some(1)); |
| 86 | + let connection_limits = ConnectionLimits::default(); // .with_max_established_per_peer(Some(1)); |
85 | 87 |
|
| 88 | + let gossipsub_config = gossipsub::ConfigBuilder::default() |
| 89 | + .max_transmit_size(1 << 34) |
| 90 | + .flood_publish(false) |
| 91 | + .heartbeat_interval(Duration::from_millis(700)) |
| 92 | + .validation_mode(ValidationMode::None) |
| 93 | + .message_id_fn(|message| { |
| 94 | + let mut source_string = message.source.as_ref().map(|id| id.to_bytes()).unwrap_or_default(); |
| 95 | + source_string |
| 96 | + .extend_from_slice(&message.sequence_number.unwrap_or_default().to_be_bytes()); |
| 97 | + source_string.extend_from_slice(&message.data[0..16]); |
| 98 | + gossipsub::MessageId::from(source_string) |
| 99 | + }) |
| 100 | + |
| 101 | + |
| 102 | + // .max_messages_per_rpc(Some(1)) |
| 103 | + .history_length(5) |
| 104 | + |
| 105 | + // .connection_handler_queue_len(50_000) |
| 106 | + |
| 107 | + .mesh_n(99) // Target mesh peers |
| 108 | + .mesh_n_low(99) // Minimum before adding (default: 5) |
| 109 | + .mesh_n_high(99) // Maximum before pruning (default |
| 110 | + // .publish_queue_duration(Duration::from_millis(500)) // default: 5s |
| 111 | + // .forward_queue_duration(Duration::from_millis(100)) // default: 1s |
| 112 | + |
| 113 | + |
| 114 | + // .heartbeat_interval(Duration::from_millis(200)) |
| 115 | + // Set gossip_lazy to 0 - minimum number of peers to gossip to |
| 116 | + .gossip_lazy(0) |
| 117 | + |
| 118 | + // Set gossip_factor to 0.0 - factor for dynamic gossip peer selection |
| 119 | + .gossip_factor(0.0) |
| 120 | + |
| 121 | + // Set history_gossip to 0 - number of past heartbeats to gossip about |
| 122 | + .history_gossip(0) |
| 123 | + |
| 124 | + // Optional: Set max_ihave_length to 0 - maximum IHAVE message IDs per message |
| 125 | + .max_ihave_length(0) |
| 126 | + |
| 127 | + // Optional: Set max_ihave_messages to 0 - maximum IHAVE messages per heartbeat |
| 128 | + .max_ihave_messages(0) |
| 129 | + |
| 130 | + // Optional: Set gossip_retransmission to 0 - disable IWANT retries |
| 131 | + .gossip_retransimission(0) |
| 132 | + |
| 133 | + // Enable IDONTWANT optimization |
| 134 | + // .idontwant_message_size_threshold(1000) // Adjust based on your message sizes |
| 135 | + .idontwant_on_publish(true) // Prevent echo-back on publish |
| 136 | + |
| 137 | + .build() |
| 138 | + .expect("Failed to build gossipsub config"); |
86 | 139 | Self { |
87 | 140 | limits: connection_limits::Behaviour::new(connection_limits), |
88 | 141 | peer_manager: peer_manager::PeerManager::new(peer_manager_config), |
@@ -123,12 +176,8 @@ impl MixedBehaviour { |
123 | 176 | ), |
124 | 177 | sqmr: sqmr::Behaviour::new(streamed_bytes_config), |
125 | 178 | gossipsub: gossipsub::Behaviour::new( |
126 | | - gossipsub::MessageAuthenticity::Signed(keypair), |
127 | | - gossipsub::ConfigBuilder::default() |
128 | | - // TODO(shahak): try to reduce this bound. |
129 | | - .max_transmit_size(1 << 34) |
130 | | - .build() |
131 | | - .expect("Failed to build gossipsub config"), |
| 179 | + gossipsub::MessageAuthenticity::Author(local_peer_id), |
| 180 | + gossipsub_config, |
132 | 181 | ) |
133 | 182 | .unwrap_or_else(|err_string| { |
134 | 183 | panic!( |
|
0 commit comments