Skip to content

Commit 5d6c84f

Browse files
committed
Use DecodeWithMemLimit for Block
1 parent 5911b82 commit 5d6c84f

File tree

1 file changed

+21
-1
lines changed
  • substrate/primitives/runtime/src/generic

1 file changed

+21
-1
lines changed

substrate/primitives/runtime/src/generic/block.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ use crate::{
3232
Justifications,
3333
};
3434
use alloc::vec::Vec;
35+
use codec::{DecodeWithMemLimit, Error, Input};
3536
use sp_core::RuntimeDebug;
3637

38+
/// Maximum heap size for a block (in bytes).
39+
const MAX_BLOCK_HEAP_SIZE: usize = 50 * 1024 * 1024; // 50 MiB
40+
3741
/// Something to identify a block.
3842
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
3943
pub enum BlockId<Block: BlockT> {
@@ -79,18 +83,34 @@ impl<Block: BlockT> fmt::Display for BlockId<Block> {
7983

8084
/// Abstraction over a substrate block.
8185
#[derive(
82-
PartialEq, Eq, Clone, Encode, Decode, DecodeWithMemTracking, RuntimeDebug, scale_info::TypeInfo,
86+
PartialEq, Eq, Clone, Encode, DecodeWithMemTracking, RuntimeDebug, scale_info::TypeInfo,
8387
)]
8488
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8589
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
8690
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
91+
#[codec(decode_with_mem_tracking_bound(
92+
Header: DecodeWithMemTracking,
93+
Extrinsic: DecodeWithMemTracking)
94+
)]
8795
pub struct Block<Header, Extrinsic> {
8896
/// The block header.
8997
pub header: Header,
9098
/// The accompanying extrinsics.
9199
pub extrinsics: Vec<Extrinsic>,
92100
}
93101

102+
impl<Header, Extrinsic> Decode for Block<Header, Extrinsic>
103+
where
104+
Header: DecodeWithMemTracking,
105+
Extrinsic: DecodeWithMemTracking,
106+
{
107+
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
108+
let (header, extrinsics) =
109+
DecodeWithMemLimit::decode_with_mem_limit(input, MAX_BLOCK_HEAP_SIZE)?;
110+
Ok(Self { header, extrinsics })
111+
}
112+
}
113+
94114
impl<Header, Extrinsic> traits::HeaderProvider for Block<Header, Extrinsic>
95115
where
96116
Header: HeaderT,

0 commit comments

Comments
 (0)