-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add explicit limits to notifications sizes and adjust yamux buffer size #7925
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,7 +83,9 @@ use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnbound | |
| use std::{ | ||
| borrow::Cow, | ||
| collections::{HashMap, HashSet}, | ||
| convert::TryFrom as _, | ||
| fs, | ||
| iter, | ||
| marker::PhantomData, | ||
| num:: NonZeroUsize, | ||
| pin::Pin, | ||
|
|
@@ -283,6 +285,46 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> { | |
| config | ||
| }; | ||
|
|
||
| let (transport, bandwidth) = { | ||
| let (config_mem, config_wasm) = match params.network_config.transport { | ||
| TransportConfig::MemoryOnly => (true, None), | ||
| TransportConfig::Normal { wasm_external_transport, .. } => | ||
| (false, wasm_external_transport) | ||
| }; | ||
|
|
||
| // The yamux buffer size limit is configured to be equal to the maximum frame size | ||
| // of all protocols. 10 bytes are added to each limit for the length prefix that | ||
| // is not included in the upper layer protocols limit but is still present in the | ||
| // yamux buffer. | ||
| let yamux_maximum_buffer_size = { | ||
| let requests_max = params.network_config | ||
| .request_response_protocols.iter() | ||
| .map(|cfg| usize::try_from(cfg.max_request_size).unwrap_or(usize::max_value())); | ||
| let responses_max = params.network_config | ||
| .request_response_protocols.iter() | ||
| .map(|cfg| usize::try_from(cfg.max_response_size).unwrap_or(usize::max_value())); | ||
| let notifs_max = params.network_config | ||
| .extra_sets.iter() | ||
| .map(|cfg| usize::try_from(cfg.max_notification_size).unwrap_or(usize::max_value())); | ||
|
|
||
| // A "default" max is added to cover all the other protocols: ping, identify, | ||
| // kademlia. | ||
| let default_max = 1024 * 1024; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am seeing this number (1024 * 1024) a lot, maybe we can define it somewhere as default value? Either just a constant or maybe even make a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has a different meaning every time, and each |
||
| iter::once(default_max) | ||
| .chain(requests_max).chain(responses_max).chain(notifs_max) | ||
| .max().expect("iterator known to always yield at least one element; qed") | ||
| .saturating_add(10) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I understand correctly, that the addition here accounts for the additional bytes needed for the length delimiter added via
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Would you mind documenting that?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe even |
||
| }; | ||
|
|
||
| transport::build_transport( | ||
| local_identity, | ||
| config_mem, | ||
| config_wasm, | ||
| params.network_config.yamux_window_size, | ||
| yamux_maximum_buffer_size | ||
| ) | ||
| }; | ||
|
|
||
| let behaviour = { | ||
| let result = Behaviour::new( | ||
| protocol, | ||
|
|
@@ -305,20 +347,6 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> { | |
| } | ||
| }; | ||
|
|
||
| let (transport, bandwidth) = { | ||
| let (config_mem, config_wasm) = match params.network_config.transport { | ||
| TransportConfig::MemoryOnly => (true, None), | ||
| TransportConfig::Normal { wasm_external_transport, .. } => | ||
| (false, wasm_external_transport) | ||
| }; | ||
|
|
||
| transport::build_transport( | ||
| local_identity, | ||
| config_mem, | ||
| config_wasm, | ||
| params.network_config.yamux_window_size | ||
| ) | ||
| }; | ||
| let mut builder = SwarmBuilder::new(transport, behaviour, local_peer_id.clone()) | ||
| .connection_limits(ConnectionLimits::default() | ||
| .with_max_established_per_peer(Some(crate::MAX_CONNECTIONS_PER_PEER as u32)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need that
try_from? Why not simply makemax_notification_sizean usize to begin with?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The size of something that is transmitted on the network should never be a
usize, as we want this size limit to be the same for all participants.However a buffer length, however, is correctly a
usize.