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
@@ -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,