Increase limit on light client response size (#5461)

* Increase limit on light client response size

* Address review
This commit is contained in:
Pierre Krieger
2020-03-31 20:16:56 +02:00
committed by GitHub
parent 9789054fb8
commit f04486e5bc
@@ -77,7 +77,8 @@ use wasm_timer::Instant;
/// Configuration options for `LightClientHandler` behaviour. /// Configuration options for `LightClientHandler` behaviour.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Config { pub struct Config {
max_data_size: usize, max_request_size: usize,
max_response_size: usize,
max_pending_requests: usize, max_pending_requests: usize,
inactivity_timeout: Duration, inactivity_timeout: Duration,
request_timeout: Duration, request_timeout: Duration,
@@ -87,13 +88,15 @@ pub struct Config {
impl Config { impl Config {
/// Create a fresh configuration with the following options: /// 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 /// - max. pending requests = 128
/// - inactivity timeout = 15s /// - inactivity timeout = 15s
/// - request timeout = 15s /// - request timeout = 15s
pub fn new(id: &ProtocolId) -> Self { pub fn new(id: &ProtocolId) -> Self {
let mut c = Config { 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, max_pending_requests: 128,
inactivity_timeout: Duration::from_secs(15), inactivity_timeout: Duration::from_secs(15),
request_timeout: Duration::from_secs(15), request_timeout: Duration::from_secs(15),
@@ -103,9 +106,15 @@ impl Config {
c c
} }
/// Limit the max. length of incoming request bytes. /// Limit the max. length in bytes of a request.
pub fn set_max_data_size(&mut self, v: usize) -> &mut Self { pub fn set_max_request_size(&mut self, v: usize) -> &mut Self {
self.max_data_size = v; 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 self
} }
@@ -654,7 +663,7 @@ where
fn new_handler(&mut self) -> Self::ProtocolsHandler { fn new_handler(&mut self) -> Self::ProtocolsHandler {
let p = InboundProtocol { let p = InboundProtocol {
max_data_size: self.config.max_data_size, max_request_size: self.config.max_request_size,
protocol: self.config.protocol.clone(), protocol: self.config.protocol.clone(),
}; };
OneShotHandler::new(SubstreamProtocol::new(p), self.config.inactivity_timeout) OneShotHandler::new(SubstreamProtocol::new(p), self.config.inactivity_timeout)
@@ -841,7 +850,7 @@ where
let protocol = OutboundProtocol { let protocol = OutboundProtocol {
request: buf, request: buf,
request_id: id, request_id: id,
max_data_size: self.config.max_data_size, max_response_size: self.config.max_response_size,
protocol: self.config.protocol.clone(), protocol: self.config.protocol.clone(),
}; };
self.peers.get_mut(&peer).map(|info| info.status = PeerStatus::BusyWith(id)); self.peers.get_mut(&peer).map(|info| info.status = PeerStatus::BusyWith(id));
@@ -1008,7 +1017,7 @@ pub enum Event<T> {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct InboundProtocol { pub struct InboundProtocol {
/// The max. request length in bytes. /// The max. request length in bytes.
max_data_size: usize, max_request_size: usize,
/// The protocol to use for upgrade negotiation. /// The protocol to use for upgrade negotiation.
protocol: Bytes, protocol: Bytes,
} }
@@ -1032,7 +1041,7 @@ where
fn upgrade_inbound(self, mut s: T, _: Self::Info) -> Self::Future { fn upgrade_inbound(self, mut s: T, _: Self::Info) -> Self::Future {
let future = async move { 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[..]) { match api::v1::light::Request::decode(&vec[..]) {
Ok(r) => Ok(Event::Request(r, s)), Ok(r) => Ok(Event::Request(r, s)),
Err(e) => Err(ReadOneError::Io(io::Error::new(io::ErrorKind::Other, e))) Err(e) => Err(ReadOneError::Io(io::Error::new(io::ErrorKind::Other, e)))
@@ -1051,8 +1060,8 @@ pub struct OutboundProtocol {
request: Vec<u8>, request: Vec<u8>,
/// Local identifier for the request. Used to associate it with a response. /// Local identifier for the request. Used to associate it with a response.
request_id: u64, request_id: u64,
/// The max. request length in bytes. /// The max. response length in bytes.
max_data_size: usize, max_response_size: usize,
/// The protocol to use for upgrade negotiation. /// The protocol to use for upgrade negotiation.
protocol: Bytes, protocol: Bytes,
} }
@@ -1077,7 +1086,7 @@ where
fn upgrade_outbound(self, mut s: T, _: Self::Info) -> Self::Future { fn upgrade_outbound(self, mut s: T, _: Self::Info) -> Self::Future {
let future = async move { let future = async move {
write_one(&mut s, &self.request).await?; 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[..]) api::v1::light::Response::decode(&vec[..])
.map(|r| Event::Response(self.request_id, r)) .map(|r| Event::Response(self.request_id, r))
.map_err(|e| { .map_err(|e| {