Skip to content

Commit 441ab41

Browse files
lexnvdmitry-markin
andauthored
kad: Set upper limits for kademlia messages (#357)
This PR sets the default kademlia message to 16 KiB The limit can be configurable using the config builder API. cc @paritytech/networking --------- Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> Co-authored-by: Dmitry Markin <dmitry@markin.tech>
1 parent 22b19c8 commit 441ab41

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/protocol/libp2p/kademlia/config.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ const PROTOCOL_NAME: &str = "/ipfs/kad/1.0.0";
5252
/// Kademlia replication factor.
5353
const REPLICATION_FACTOR: usize = 20usize;
5454

55+
/// Kademlia maximum message size. Should fit 64 KiB value + 4 KiB key.
56+
const DEFAULT_MAX_MESSAGE_SIZE: usize = 70 * 1024;
57+
5558
/// Kademlia configuration.
5659
#[derive(Debug)]
5760
pub struct Config {
@@ -104,6 +107,7 @@ impl Config {
104107
record_ttl: Duration,
105108
provider_ttl: Duration,
106109
provider_refresh_interval: Duration,
110+
max_message_size: usize,
107111
) -> (Self, KademliaHandle) {
108112
let (cmd_tx, cmd_rx) = channel(DEFAULT_CHANNEL_SIZE);
109113
let (event_tx, event_rx) = channel(DEFAULT_CHANNEL_SIZE);
@@ -122,7 +126,7 @@ impl Config {
122126
record_ttl,
123127
provider_ttl,
124128
provider_refresh_interval,
125-
codec: ProtocolCodec::UnsignedVarint(None),
129+
codec: ProtocolCodec::UnsignedVarint(Some(max_message_size)),
126130
replication_factor,
127131
known_peers,
128132
cmd_rx,
@@ -144,6 +148,7 @@ impl Config {
144148
DEFAULT_TTL,
145149
DEFAULT_PROVIDER_TTL,
146150
DEFAULT_PROVIDER_REFRESH_INTERVAL,
151+
DEFAULT_MAX_MESSAGE_SIZE,
147152
)
148153
}
149154
}
@@ -174,6 +179,9 @@ pub struct ConfigBuilder {
174179

175180
/// Republish interval for the provider records.
176181
pub(super) provider_refresh_interval: Duration,
182+
183+
/// Maximum message size.
184+
pub(crate) max_message_size: usize,
177185
}
178186

179187
impl Default for ConfigBuilder {
@@ -194,6 +202,7 @@ impl ConfigBuilder {
194202
record_ttl: DEFAULT_TTL,
195203
provider_ttl: DEFAULT_PROVIDER_TTL,
196204
provider_refresh_interval: DEFAULT_PROVIDER_REFRESH_INTERVAL,
205+
max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
197206
}
198207
}
199208

@@ -262,6 +271,15 @@ impl ConfigBuilder {
262271
self
263272
}
264273

274+
/// Set the maximum Kademlia message size.
275+
///
276+
/// Should fit `MemoryStore` max record size. If unspecified, the default maximum message size
277+
/// is 70 KiB.
278+
pub fn with_max_message_size(mut self, max_message_size: usize) -> Self {
279+
self.max_message_size = max_message_size;
280+
self
281+
}
282+
265283
/// Build Kademlia [`Config`].
266284
pub fn build(self) -> (Config, KademliaHandle) {
267285
Config::new(
@@ -273,6 +291,7 @@ impl ConfigBuilder {
273291
self.record_ttl,
274292
self.provider_ttl,
275293
self.provider_refresh_interval,
294+
self.max_message_size,
276295
)
277296
}
278297
}

src/protocol/libp2p/kademlia/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ mod tests {
12701270
let config = Config {
12711271
protocol_names: vec![ProtocolName::from("/kad/1")],
12721272
known_peers: HashMap::new(),
1273-
codec: ProtocolCodec::UnsignedVarint(None),
1273+
codec: ProtocolCodec::UnsignedVarint(Some(70 * 1024)),
12741274
replication_factor: 20usize,
12751275
update_mode: RoutingTableUpdateMode::Automatic,
12761276
validation_mode: IncomingRecordValidationMode::Automatic,

0 commit comments

Comments
 (0)