Skip to content

Commit aebbaa2

Browse files
committed
revert kademlia cfg, use Permissive and enable msg validation
1 parent fca2302 commit aebbaa2

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/p2p/behaviour.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,26 @@ impl DriaBehaviour {
2424
let peer_id = public_key.to_peer_id();
2525
Self {
2626
relay: relay_behavior,
27-
gossipsub: create_gossipsub_behavior(key.clone()),
27+
gossipsub: create_gossipsub_behavior(peer_id),
2828
kademlia: create_kademlia_behavior(peer_id),
2929
autonat: create_autonat_behavior(peer_id),
30-
identify: create_identify_behavior(public_key),
3130
dcutr: create_dcutr_behavior(peer_id),
31+
identify: create_identify_behavior(public_key),
3232
}
3333
}
3434
}
3535

3636
/// Configures the Kademlia DHT behavior for the node.
3737
#[inline]
3838
fn create_kademlia_behavior(local_peer_id: PeerId) -> kad::Behaviour<MemoryStore> {
39-
use kad::{Behaviour, Caching, Config};
39+
use kad::{Behaviour, Config};
4040

4141
const QUERY_TIMEOUT_SECS: u64 = 5 * 60;
4242
const RECORD_TTL_SECS: u64 = 30;
4343

4444
let mut cfg = Config::new(DRIA_PROTO_NAME);
4545
cfg.set_query_timeout(Duration::from_secs(QUERY_TIMEOUT_SECS))
46-
.set_record_ttl(Some(Duration::from_secs(RECORD_TTL_SECS)))
47-
.set_replication_interval(None)
48-
.set_caching(Caching::Disabled)
49-
.set_publication_interval(None);
46+
.set_record_ttl(Some(Duration::from_secs(RECORD_TTL_SECS)));
5047

5148
Behaviour::with_config(local_peer_id, MemoryStore::new(local_peer_id), cfg)
5249
}
@@ -85,12 +82,18 @@ fn create_autonat_behavior(local_peer_id: PeerId) -> autonat::Behaviour {
8582

8683
/// Configures the Gossipsub behavior for pub/sub messaging across peers.
8784
#[inline]
88-
fn create_gossipsub_behavior(id_keys: Keypair) -> gossipsub::Behaviour {
89-
use gossipsub::{Behaviour, ConfigBuilder, Message, MessageAuthenticity, MessageId};
85+
fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
86+
use gossipsub::{
87+
Behaviour, ConfigBuilder, Message, MessageAuthenticity, MessageId, ValidationMode,
88+
};
9089

9190
/// Message TTL in seconds
9291
const MESSAGE_TTL_SECS: u64 = 100;
9392

93+
/// We accept permissive validation mode, meaning that we accept all messages
94+
/// and check their fields based on whether they exist or not.
95+
const VALIDATION_MODE: ValidationMode = ValidationMode::Permissive;
96+
9497
/// Gossip cache TTL in seconds
9598
const GOSSIP_TTL_SECS: u64 = 100;
9699

@@ -112,14 +115,16 @@ fn create_gossipsub_behavior(id_keys: Keypair) -> gossipsub::Behaviour {
112115
};
113116

114117
Behaviour::new(
115-
MessageAuthenticity::Signed(id_keys),
118+
MessageAuthenticity::Author(author),
116119
ConfigBuilder::default()
117120
.heartbeat_interval(Duration::from_secs(10))
118121
.max_transmit_size(MAX_TRANSMIT_SIZE) // 256 KB
119122
.message_id_fn(message_id_fn)
120123
.message_ttl(Duration::from_secs(MESSAGE_TTL_SECS))
121124
.gossip_ttl(Duration::from_secs(GOSSIP_TTL_SECS))
122125
.message_capacity(MESSAGE_CAPACITY)
126+
.validation_mode(VALIDATION_MODE)
127+
.validate_messages()
123128
.max_ihave_length(MAX_IHAVE_LENGTH)
124129
.build()
125130
.expect("Valid config"), // TODO: better error handling

0 commit comments

Comments
 (0)