Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions client/network/src/protocol/light_client_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ use wasm_timer::Instant;
/// Configuration options for `LightClientHandler` behaviour.
#[derive(Debug, Clone)]
pub struct Config {
max_data_size: usize,
max_request_size: usize,
max_response_size: usize,
max_pending_requests: usize,
inactivity_timeout: Duration,
request_timeout: Duration,
Expand All @@ -87,13 +88,15 @@ pub struct Config {
impl Config {
/// Create a fresh configuration with the following options:
///
/// - max. data size = 1 MiB
/// - max. request size = 1 MiB
/// - max. response size = 16 MiB
/// - max. pending requests = 128
/// - inactivity timeout = 15s
/// - request timeout = 15s
pub fn new(id: &ProtocolId) -> Self {
let mut c = Config {
max_data_size: 1024 * 1024,
max_request_size: 1 * 1024 * 1024,
max_response_size: 16 * 1024 * 1024,
max_pending_requests: 128,
inactivity_timeout: Duration::from_secs(15),
request_timeout: Duration::from_secs(15),
Expand All @@ -103,9 +106,15 @@ impl Config {
c
}

/// Limit the max. length of incoming request bytes.
pub fn set_max_data_size(&mut self, v: usize) -> &mut Self {
self.max_data_size = v;
/// Limit the max. length in bytes of a request.
pub fn set_max_request_size(&mut self, v: usize) -> &mut Self {
self.max_request_size = v;
self
}

/// Limit the max. length in bytes of a response.
pub fn set_max_response_size(&mut self, v: usize) -> &mut Self {
self.max_response_size = v;
self
}

Expand Down Expand Up @@ -654,7 +663,7 @@ where

fn new_handler(&mut self) -> Self::ProtocolsHandler {
let p = InboundProtocol {
max_data_size: self.config.max_data_size,
max_request_size: self.config.max_request_size,
protocol: self.config.protocol.clone(),
};
OneShotHandler::new(SubstreamProtocol::new(p), self.config.inactivity_timeout)
Expand Down Expand Up @@ -841,7 +850,7 @@ where
let protocol = OutboundProtocol {
request: buf,
request_id: id,
max_data_size: self.config.max_data_size,
max_response_size: self.config.max_response_size,
protocol: self.config.protocol.clone(),
};
self.peers.get_mut(&peer).map(|info| info.status = PeerStatus::BusyWith(id));
Expand Down Expand Up @@ -1008,7 +1017,7 @@ pub enum Event<T> {
#[derive(Debug, Clone)]
pub struct InboundProtocol {
/// The max. request length in bytes.
max_data_size: usize,
max_request_size: usize,
/// The protocol to use for upgrade negotiation.
protocol: Bytes,
}
Expand All @@ -1032,7 +1041,7 @@ where

fn upgrade_inbound(self, mut s: T, _: Self::Info) -> Self::Future {
let future = async move {
let vec = read_one(&mut s, self.max_data_size).await?;
let vec = read_one(&mut s, self.max_request_size).await?;
match api::v1::light::Request::decode(&vec[..]) {
Ok(r) => Ok(Event::Request(r, s)),
Err(e) => Err(ReadOneError::Io(io::Error::new(io::ErrorKind::Other, e)))
Expand All @@ -1051,8 +1060,8 @@ pub struct OutboundProtocol {
request: Vec<u8>,
/// Local identifier for the request. Used to associate it with a response.
request_id: u64,
/// The max. request length in bytes.
max_data_size: usize,
/// The max. response length in bytes.
max_response_size: usize,
/// The protocol to use for upgrade negotiation.
protocol: Bytes,
}
Expand All @@ -1077,7 +1086,7 @@ where
fn upgrade_outbound(self, mut s: T, _: Self::Info) -> Self::Future {
let future = async move {
write_one(&mut s, &self.request).await?;
let vec = read_one(&mut s, self.max_data_size).await?;
let vec = read_one(&mut s, self.max_response_size).await?;
api::v1::light::Response::decode(&vec[..])
.map(|r| Event::Response(self.request_id, r))
.map_err(|e| {
Expand Down