diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 7b1d7a696e4..85af7ef4fc8 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -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, @@ -3004,6 +3005,19 @@ impl BeaconChain { 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, diff --git a/beacon_node/beacon_chain/src/chain_config.rs b/beacon_node/beacon_chain/src/chain_config.rs index 9fe09c9822a..4aee06d468c 100644 --- a/beacon_node/beacon_chain/src/chain_config.rs +++ b/beacon_node/beacon_chain/src/chain_config.rs @@ -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 { @@ -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 } } } diff --git a/beacon_node/beacon_chain/src/errors.rs b/beacon_node/beacon_chain/src/errors.rs index 3d5aad3aa9c..cec72a5818a 100644 --- a/beacon_node/beacon_chain/src/errors.rs +++ b/beacon_node/beacon_chain/src/errors.rs @@ -185,6 +185,7 @@ pub enum BlockProductionError { GetPayloadFailed(execution_layer::Error), FailedToReadFinalizedBlock(store::Error), MissingFinalizedBlock(Hash256), + BlockTooLarge(usize), } easy_from_to!(BlockProcessingError, BlockProductionError); diff --git a/beacon_node/beacon_chain/src/metrics.rs b/beacon_node/beacon_chain/src/metrics.rs index 2967d40a18b..44b267647c5 100644 --- a/beacon_node/beacon_chain/src/metrics.rs +++ b/beacon_node/beacon_chain/src/metrics.rs @@ -107,6 +107,11 @@ lazy_static! { "Number of attestations in a block" ); + pub static ref BLOCK_SIZE: Result = try_create_histogram( + "beacon_block_total_size", + "Size of a signed beacon block" + ); + /* * Unaggregated Attestation Verification */ diff --git a/beacon_node/lighthouse_network/src/lib.rs b/beacon_node/lighthouse_network/src/lib.rs index 733dc72ab5b..b37b69dcfa6 100644 --- a/beacon_node/lighthouse_network/src/lib.rs +++ b/beacon_node/lighthouse_network/src/lib.rs @@ -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; diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index fc3ca2cc0b6..67fc910df75 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -471,6 +471,8 @@ pub fn get_config( }; } + client_config.chain.max_network_size = lighthouse_network::GOSSIP_MAX_SIZE; + if cli_args.is_present("slasher") { let slasher_dir = if let Some(slasher_dir) = cli_args.value_of("slasher-dir") { PathBuf::from(slasher_dir)