Skip to content
Snippets Groups Projects
Commit 6ded3147 authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

Make the maximum block size configurable (#7499)


* Make the maximum block size configurable

This pr makes the maximum block size configurable. The maximum block
size is used after proposing a new block to check if the new block is
not exceeding the maximum.

* Update primitives/consensus/common/src/evaluation.rs

Co-authored-by: default avatarAndré Silva <123550+andresilva@users.noreply.github.com>

* Added comment

Co-authored-by: default avatarAndré Silva <123550+andresilva@users.noreply.github.com>
parent 3c50838d
Branches
No related merge requests found
...@@ -42,6 +42,15 @@ use std::marker::PhantomData; ...@@ -42,6 +42,15 @@ use std::marker::PhantomData;
use prometheus_endpoint::Registry as PrometheusRegistry; use prometheus_endpoint::Registry as PrometheusRegistry;
use sc_proposer_metrics::MetricsLink as PrometheusMetrics; use sc_proposer_metrics::MetricsLink as PrometheusMetrics;
/// Default maximum block size in bytes used by [`Proposer`].
///
/// Can be overwritten by [`ProposerFactory::set_maxium_block_size`].
///
/// Be aware that there is also an upper packet size on what the networking code
/// will accept. If the block doesn't fit in such a package, it can not be
/// transferred to other nodes.
pub const DEFAULT_MAX_BLOCK_SIZE: usize = 4 * 1024 * 1024 + 512;
/// Proposer factory. /// Proposer factory.
pub struct ProposerFactory<A, B, C> { pub struct ProposerFactory<A, B, C> {
spawn_handle: Box<dyn SpawnNamed>, spawn_handle: Box<dyn SpawnNamed>,
...@@ -53,6 +62,7 @@ pub struct ProposerFactory<A, B, C> { ...@@ -53,6 +62,7 @@ pub struct ProposerFactory<A, B, C> {
metrics: PrometheusMetrics, metrics: PrometheusMetrics,
/// phantom member to pin the `Backend` type. /// phantom member to pin the `Backend` type.
_phantom: PhantomData<B>, _phantom: PhantomData<B>,
max_block_size: usize,
} }
impl<A, B, C> ProposerFactory<A, B, C> { impl<A, B, C> ProposerFactory<A, B, C> {
...@@ -68,8 +78,17 @@ impl<A, B, C> ProposerFactory<A, B, C> { ...@@ -68,8 +78,17 @@ impl<A, B, C> ProposerFactory<A, B, C> {
transaction_pool, transaction_pool,
metrics: PrometheusMetrics::new(prometheus), metrics: PrometheusMetrics::new(prometheus),
_phantom: PhantomData, _phantom: PhantomData,
max_block_size: DEFAULT_MAX_BLOCK_SIZE,
} }
} }
/// Set the maximum block size in bytes.
///
/// The default value for the maximum block size is:
/// [`DEFAULT_MAX_BLOCK_SIZE`].
pub fn set_maximum_block_size(&mut self, size: usize) {
self.max_block_size = size;
}
} }
impl<B, Block, C, A> ProposerFactory<A, B, C> impl<B, Block, C, A> ProposerFactory<A, B, C>
...@@ -103,6 +122,7 @@ impl<B, Block, C, A> ProposerFactory<A, B, C> ...@@ -103,6 +122,7 @@ impl<B, Block, C, A> ProposerFactory<A, B, C>
now, now,
metrics: self.metrics.clone(), metrics: self.metrics.clone(),
_phantom: PhantomData, _phantom: PhantomData,
max_block_size: self.max_block_size,
}; };
proposer proposer
...@@ -143,6 +163,7 @@ pub struct Proposer<B, Block: BlockT, C, A: TransactionPool> { ...@@ -143,6 +163,7 @@ pub struct Proposer<B, Block: BlockT, C, A: TransactionPool> {
now: Box<dyn Fn() -> time::Instant + Send + Sync>, now: Box<dyn Fn() -> time::Instant + Send + Sync>,
metrics: PrometheusMetrics, metrics: PrometheusMetrics,
_phantom: PhantomData<B>, _phantom: PhantomData<B>,
max_block_size: usize,
} }
impl<A, B, Block, C> sp_consensus::Proposer<Block> for impl<A, B, Block, C> sp_consensus::Proposer<Block> for
...@@ -334,7 +355,12 @@ impl<A, B, Block, C> Proposer<B, Block, C, A> ...@@ -334,7 +355,12 @@ impl<A, B, Block, C> Proposer<B, Block, C, A>
error!("Failed to verify block encoding/decoding"); error!("Failed to verify block encoding/decoding");
} }
if let Err(err) = evaluation::evaluate_initial(&block, &self.parent_hash, self.parent_number) { if let Err(err) = evaluation::evaluate_initial(
&block,
&self.parent_hash,
self.parent_number,
self.max_block_size,
) {
error!("Failed to evaluate authored block: {:?}", err); error!("Failed to evaluate authored block: {:?}", err);
} }
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
//! Block evaluation and evaluation errors. //! Block evaluation and evaluation errors.
use super::MAX_BLOCK_SIZE;
use codec::Encode; use codec::Encode;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, One, CheckedConversion}; use sp_runtime::traits::{Block as BlockT, Header as HeaderT, One, CheckedConversion};
...@@ -42,11 +40,8 @@ pub enum Error { ...@@ -42,11 +40,8 @@ pub enum Error {
#[error("Proposal had wrong number. Expected {expected:?}, got {got:?}")] #[error("Proposal had wrong number. Expected {expected:?}, got {got:?}")]
WrongNumber { expected: BlockNumber, got: BlockNumber }, WrongNumber { expected: BlockNumber, got: BlockNumber },
/// Proposal exceeded the maximum size. /// Proposal exceeded the maximum size.
#[error( #[error("Proposal size {block_size} exceeds maximum allowed size of {max_block_size}.")]
"Proposal exceeded the maximum size of {} by {} bytes.", ProposalTooLarge { block_size: usize, max_block_size: usize },
MAX_BLOCK_SIZE, .0.saturating_sub(MAX_BLOCK_SIZE)
)]
ProposalTooLarge(usize),
} }
/// Attempt to evaluate a substrate block as a node block, returning error /// Attempt to evaluate a substrate block as a node block, returning error
...@@ -55,28 +50,29 @@ pub fn evaluate_initial<Block: BlockT>( ...@@ -55,28 +50,29 @@ pub fn evaluate_initial<Block: BlockT>(
proposal: &Block, proposal: &Block,
parent_hash: &<Block as BlockT>::Hash, parent_hash: &<Block as BlockT>::Hash,
parent_number: <<Block as BlockT>::Header as HeaderT>::Number, parent_number: <<Block as BlockT>::Header as HeaderT>::Number,
max_block_size: usize,
) -> Result<()> { ) -> Result<()> {
let encoded = Encode::encode(proposal); let encoded = Encode::encode(proposal);
let proposal = Block::decode(&mut &encoded[..]) let proposal = Block::decode(&mut &encoded[..])
.map_err(|e| Error::BadProposalFormat(e))?; .map_err(|e| Error::BadProposalFormat(e))?;
if encoded.len() > MAX_BLOCK_SIZE { if encoded.len() > max_block_size {
return Err(Error::ProposalTooLarge(encoded.len())) return Err(Error::ProposalTooLarge { max_block_size, block_size: encoded.len() })
} }
if *parent_hash != *proposal.header().parent_hash() { if *parent_hash != *proposal.header().parent_hash() {
return Err(Error::WrongParentHash { return Err(Error::WrongParentHash {
expected: format!("{:?}", *parent_hash), expected: format!("{:?}", *parent_hash),
got: format!("{:?}", proposal.header().parent_hash()) got: format!("{:?}", proposal.header().parent_hash())
}); })
} }
if parent_number + One::one() != *proposal.header().number() { if parent_number + One::one() != *proposal.header().number() {
return Err(Error::WrongNumber { return Err(Error::WrongNumber {
expected: parent_number.checked_into::<u128>().map(|x| x + 1), expected: parent_number.checked_into::<u128>().map(|x| x + 1),
got: (*proposal.header().number()).checked_into::<u128>(), got: (*proposal.header().number()).checked_into::<u128>(),
}); })
} }
Ok(()) Ok(())
......
...@@ -46,9 +46,6 @@ pub mod import_queue; ...@@ -46,9 +46,6 @@ pub mod import_queue;
pub mod evaluation; pub mod evaluation;
mod metrics; mod metrics;
// block size limit.
const MAX_BLOCK_SIZE: usize = 4 * 1024 * 1024 + 512;
pub use self::error::Error; pub use self::error::Error;
pub use block_import::{ pub use block_import::{
BlockImport, BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImportParams, BlockCheckParams, BlockImport, BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImportParams, BlockCheckParams,
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment