Skip to content

Commit 40d81ff

Browse files
committed
Enforce consensus block size limit
1 parent 6e61946 commit 40d81ff

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

lib/state/block.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ pub fn validate(
2626
header: &Header,
2727
body: &Body,
2828
) -> Result<(bitcoin::Amount, MerkleRoot), Error> {
29+
let body_size =
30+
borsh::object_length(&body).map_err(Error::BorshSerialize)?;
31+
if body_size > Body::MAX_SIZE {
32+
return Err(Error::BodyTooLarge);
33+
}
34+
2935
let tip_hash = state.try_get_tip(rotxn)?;
3036
if header.prev_side_hash != tip_hash {
3137
let err = error::InvalidHeader::PrevSideHash {
@@ -123,6 +129,12 @@ pub fn prevalidate(
123129
header: &Header,
124130
body: &Body,
125131
) -> Result<PrevalidatedBlock, Error> {
132+
let body_size =
133+
borsh::object_length(&body).map_err(Error::BorshSerialize)?;
134+
if body_size > Body::MAX_SIZE {
135+
return Err(Error::BodyTooLarge);
136+
}
137+
126138
let tip_hash = state.try_get_tip(rotxn)?;
127139
if header.prev_side_hash != tip_hash {
128140
let err = error::InvalidHeader::PrevSideHash {
@@ -131,6 +143,7 @@ pub fn prevalidate(
131143
};
132144
return Err(Error::InvalidHeader(err));
133145
};
146+
134147
let mut coinbase_value = bitcoin::Amount::ZERO;
135148
for output in &body.coinbase {
136149
coinbase_value = coinbase_value

lib/state/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ pub enum Error {
180180
BitName(#[from] BitName),
181181
#[error("bitname {name_hash} already registered")]
182182
BitNameAlreadyRegistered { name_hash: BitNameId },
183+
#[error("body too large")]
184+
BodyTooLarge,
183185
#[error("bundle too heavy {weight} > {max_weight}")]
184186
BundleTooHeavy { weight: u64, max_weight: u64 },
185187
#[error(transparent)]

types/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,9 @@ impl Body {
572572
}
573573
}
574574

575+
/// Size limit in bytes
576+
pub const MAX_SIZE: usize = 8 * 1024 * 1024;
577+
575578
pub fn authorized_transactions(&self) -> Vec<AuthorizedTransaction> {
576579
let mut authorizations_iter = self.authorizations.iter();
577580
self.transactions

0 commit comments

Comments
 (0)