Skip to content

Commit

Permalink
Increase network limits (#2796)
Browse files Browse the repository at this point in the history
Fix max packet sizes

Fix max_payload_size function

Add merge block test

Fix max size calculation; fix up test

Clear comments

Add a payload_size_function

Use safe arith for payload calculation

Return an error if block too big in block production

Separate test to check if block is over limit
  • Loading branch information
pawanjay176 authored and paulhauner committed Nov 28, 2021
1 parent 5df6cc6 commit 83378ec
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 46 deletions.
14 changes: 14 additions & 0 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use safe_arith::SafeArith;
use slasher::Slasher;
use slog::{crit, debug, error, info, trace, warn, Logger};
use slot_clock::SlotClock;
use ssz::Encode;
use state_processing::{
common::get_indexed_attestation,
per_block_processing,
Expand Down Expand Up @@ -3006,6 +3007,19 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
Signature::empty(),
);

let block_size = block.ssz_bytes_len();
debug!(
self.log,
"Produced block on state";
"block_size" => block_size,
);

metrics::observe(&metrics::BLOCK_SIZE, block_size as f64);

if block_size > self.config.max_network_size {
return Err(BlockProductionError::BlockTooLarge(block_size));
}

let process_timer = metrics::start_timer(&metrics::BLOCK_PRODUCTION_PROCESS_TIMES);
per_block_processing(
&mut state,
Expand Down
3 changes: 3 additions & 0 deletions beacon_node/beacon_chain/src/chain_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct ChainConfig {
pub reconstruct_historic_states: bool,
/// Whether timeouts on `TimeoutRwLock`s are enabled or not.
pub enable_lock_timeouts: bool,
/// The max size of a message that can be sent over the network.
pub max_network_size: usize,
}

impl Default for ChainConfig {
Expand All @@ -25,6 +27,7 @@ impl Default for ChainConfig {
weak_subjectivity_checkpoint: None,
reconstruct_historic_states: false,
enable_lock_timeouts: true,
max_network_size: 10 * 1_048_576, // 10M
}
}
}
1 change: 1 addition & 0 deletions beacon_node/beacon_chain/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub enum BlockProductionError {
GetPayloadFailed(execution_layer::Error),
FailedToReadFinalizedBlock(store::Error),
MissingFinalizedBlock(Hash256),
BlockTooLarge(usize),
}

easy_from_to!(BlockProcessingError, BlockProductionError);
Expand Down
5 changes: 5 additions & 0 deletions beacon_node/beacon_chain/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ lazy_static! {
"Number of attestations in a block"
);

pub static ref BLOCK_SIZE: Result<Histogram> = try_create_histogram(
"beacon_block_total_size",
"Size of a signed beacon block"
);

/*
* Unaggregated Attestation Verification
*/
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::time::Duration;
use types::{ForkContext, ForkName};

/// The maximum transmit size of gossip messages in bytes.
pub const GOSSIP_MAX_SIZE: usize = 1_048_576;
pub const GOSSIP_MAX_SIZE: usize = 10 * 1_048_576; // 10M
/// This is a constant to be used in discovery. The lower bound of the gossipsub mesh.
pub const MESH_N_LOW: usize = 6;

Expand Down
2 changes: 2 additions & 0 deletions beacon_node/lighthouse_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub mod rpc;
mod service;
pub mod types;

pub use config::GOSSIP_MAX_SIZE;

use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::str::FromStr;

Expand Down
4 changes: 2 additions & 2 deletions beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
// Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of
// packet size for ssz container corresponding to `self.protocol`.
let ssz_limits = self.protocol.rpc_request_limits();
if length > self.max_packet_size || ssz_limits.is_out_of_bounds(length) {
if ssz_limits.is_out_of_bounds(length, self.max_packet_size) {
return Err(RPCError::InvalidData);
}
// Calculate worst case compression length for given uncompressed length
Expand Down Expand Up @@ -280,7 +280,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyOutboundCodec<TSpec> {
// Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of
// packet size for ssz container corresponding to `self.protocol`.
let ssz_limits = self.protocol.rpc_response_limits::<TSpec>();
if length > self.max_packet_size || ssz_limits.is_out_of_bounds(length) {
if ssz_limits.is_out_of_bounds(length, self.max_packet_size) {
return Err(RPCError::InvalidData);
}
// Calculate worst case compression length for given uncompressed length
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use methods::{
RPCResponseErrorCode, RequestId, ResponseTermination, StatusMessage, MAX_REQUEST_BLOCKS,
};
pub(crate) use outbound::OutboundRequest;
pub use protocol::{Protocol, RPCError};
pub use protocol::{Protocol, RPCError, MAX_RPC_SIZE};

pub(crate) mod codec;
mod handler;
Expand Down
19 changes: 9 additions & 10 deletions beacon_node/lighthouse_network/src/rpc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ lazy_static! {
)
.as_ssz_bytes()
.len();
pub static ref SIGNED_BEACON_BLOCK_MERGE_MAX: usize = SignedBeaconBlock::<MainnetEthSpec>::from_block(
BeaconBlock::Merge(BeaconBlockMerge::full(&MainnetEthSpec::default_spec())),
Signature::empty(),
)
.as_ssz_bytes()
.len();

/// The `BeaconBlockMerge` block has an `ExecutionPayload` field which has a max size ~16 GiB for future proofing.
/// We calculate the value from its fields instead of constructing the block and checking the length.
pub static ref SIGNED_BEACON_BLOCK_MERGE_MAX: usize = types::ExecutionPayload::<MainnetEthSpec>::max_execution_payload_size();

pub static ref BLOCKS_BY_ROOT_REQUEST_MIN: usize =
VariableList::<Hash256, MaxRequestBlocks>::from(Vec::<Hash256>::new())
Expand Down Expand Up @@ -95,7 +93,7 @@ lazy_static! {
}

/// The maximum bytes that can be sent across the RPC.
pub const MAX_RPC_SIZE: usize = 1_048_576; // 1M
pub const MAX_RPC_SIZE: usize = 10 * 1_048_576; // 10M
/// The protocol prefix the RPC protocol id.
const PROTOCOL_PREFIX: &str = "/eth2/beacon_chain/req";
/// Time allowed for the first byte of a request to arrive before we time out (Time To First Byte).
Expand Down Expand Up @@ -208,9 +206,10 @@ impl RpcLimits {
Self { min, max }
}

/// Returns true if the given length is out of bounds, false otherwise.
pub fn is_out_of_bounds(&self, length: usize) -> bool {
length > self.max || length < self.min
/// Returns true if the given length is is greater than `MAX_RPC_SIZE` or out of
/// bounds for the given ssz type, returns false otherwise.
pub fn is_out_of_bounds(&self, length: usize, max_rpc_size: usize) -> bool {
length > std::cmp::min(self.max, max_rpc_size) || length < self.min
}
}

Expand Down
1 change: 1 addition & 0 deletions beacon_node/lighthouse_network/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn fork_context() -> ForkContext {
// Set fork_epoch to `Some` to ensure that the `ForkContext` object
// includes altair in the list of forks
chain_spec.altair_fork_epoch = Some(types::Epoch::new(42));
chain_spec.merge_fork_epoch = Some(types::Epoch::new(84));
ForkContext::new::<E>(types::Slot::new(0), Hash256::zero(), &chain_spec)
}

Expand Down
Loading

0 comments on commit 83378ec

Please sign in to comment.