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: André Silva <123550+andresilva@users.noreply.github.com>

* Added comment

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2020-11-05 20:47:40 +01:00
committed by GitHub
parent 3c50838dc3
commit 6ded31477d
3 changed files with 34 additions and 15 deletions
@@ -42,6 +42,15 @@ use std::marker::PhantomData;
use prometheus_endpoint::Registry as PrometheusRegistry;
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.
pub struct ProposerFactory<A, B, C> {
spawn_handle: Box<dyn SpawnNamed>,
@@ -53,6 +62,7 @@ pub struct ProposerFactory<A, B, C> {
metrics: PrometheusMetrics,
/// phantom member to pin the `Backend` type.
_phantom: PhantomData<B>,
max_block_size: usize,
}
impl<A, B, C> ProposerFactory<A, B, C> {
@@ -68,8 +78,17 @@ impl<A, B, C> ProposerFactory<A, B, C> {
transaction_pool,
metrics: PrometheusMetrics::new(prometheus),
_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>
@@ -103,6 +122,7 @@ impl<B, Block, C, A> ProposerFactory<A, B, C>
now,
metrics: self.metrics.clone(),
_phantom: PhantomData,
max_block_size: self.max_block_size,
};
proposer
@@ -143,6 +163,7 @@ pub struct Proposer<B, Block: BlockT, C, A: TransactionPool> {
now: Box<dyn Fn() -> time::Instant + Send + Sync>,
metrics: PrometheusMetrics,
_phantom: PhantomData<B>,
max_block_size: usize,
}
impl<A, B, Block, C> sp_consensus::Proposer<Block> for
@@ -334,7 +355,12 @@ impl<A, B, Block, C> Proposer<B, Block, C, A>
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);
}
@@ -17,8 +17,6 @@
//! Block evaluation and evaluation errors.
use super::MAX_BLOCK_SIZE;
use codec::Encode;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, One, CheckedConversion};
@@ -42,11 +40,8 @@ pub enum Error {
#[error("Proposal had wrong number. Expected {expected:?}, got {got:?}")]
WrongNumber { expected: BlockNumber, got: BlockNumber },
/// Proposal exceeded the maximum size.
#[error(
"Proposal exceeded the maximum size of {} by {} bytes.",
MAX_BLOCK_SIZE, .0.saturating_sub(MAX_BLOCK_SIZE)
)]
ProposalTooLarge(usize),
#[error("Proposal size {block_size} exceeds maximum allowed size of {max_block_size}.")]
ProposalTooLarge { block_size: usize, max_block_size: usize },
}
/// Attempt to evaluate a substrate block as a node block, returning error
@@ -55,28 +50,29 @@ pub fn evaluate_initial<Block: BlockT>(
proposal: &Block,
parent_hash: &<Block as BlockT>::Hash,
parent_number: <<Block as BlockT>::Header as HeaderT>::Number,
max_block_size: usize,
) -> Result<()> {
let encoded = Encode::encode(proposal);
let proposal = Block::decode(&mut &encoded[..])
.map_err(|e| Error::BadProposalFormat(e))?;
if encoded.len() > MAX_BLOCK_SIZE {
return Err(Error::ProposalTooLarge(encoded.len()))
if encoded.len() > max_block_size {
return Err(Error::ProposalTooLarge { max_block_size, block_size: encoded.len() })
}
if *parent_hash != *proposal.header().parent_hash() {
return Err(Error::WrongParentHash {
expected: format!("{:?}", *parent_hash),
got: format!("{:?}", proposal.header().parent_hash())
});
})
}
if parent_number + One::one() != *proposal.header().number() {
return Err(Error::WrongNumber {
expected: parent_number.checked_into::<u128>().map(|x| x + 1),
got: (*proposal.header().number()).checked_into::<u128>(),
});
})
}
Ok(())
@@ -46,9 +46,6 @@ pub mod import_queue;
pub mod evaluation;
mod metrics;
// block size limit.
const MAX_BLOCK_SIZE: usize = 4 * 1024 * 1024 + 512;
pub use self::error::Error;
pub use block_import::{
BlockImport, BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImportParams, BlockCheckParams,