mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 10:31:03 +00:00
Add block data size check (#230)
* Add block data size check * Pass max_block_data_size everywhere * Fix build after merge * Fix ParachainWork initialization * Fix tests compilation
This commit is contained in:
committed by
Gavin Wood
parent
7a123fe8e9
commit
10ae8d48f5
@@ -413,5 +413,6 @@ fn make_table(data: &ApiData, local_key: &AuthorityKeyring, parent_hash: Hash) -
|
||||
Arc::new(local_key.pair()),
|
||||
parent_hash,
|
||||
store,
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -85,6 +85,9 @@ pub struct CustomConfiguration {
|
||||
grandpa::LinkHalfForService<Factory>
|
||||
)>,
|
||||
|
||||
/// Maximal `block_data` size.
|
||||
pub max_block_data_size: Option<u64>,
|
||||
|
||||
inherent_data_providers: InherentDataProviders,
|
||||
}
|
||||
|
||||
@@ -94,6 +97,7 @@ impl Default for CustomConfiguration {
|
||||
collating_for: None,
|
||||
grandpa_import_setup: None,
|
||||
inherent_data_providers: InherentDataProviders::new(),
|
||||
max_block_data_size: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -311,6 +315,7 @@ construct_service_factory! {
|
||||
key.clone(),
|
||||
extrinsic_store,
|
||||
SlotDuration::get_or_compute(&*client)?,
|
||||
service.config.custom.max_block_data_size,
|
||||
);
|
||||
|
||||
info!("Using authority key {}", key.public());
|
||||
|
||||
@@ -110,6 +110,7 @@ pub(crate) fn start<C, N, P, SC>(
|
||||
thread_pool: TaskExecutor,
|
||||
key: Arc<ed25519::Pair>,
|
||||
extrinsic_store: ExtrinsicStore,
|
||||
max_block_data_size: Option<u64>,
|
||||
) -> ServiceHandle
|
||||
where
|
||||
C: Collators + Send + Sync + 'static,
|
||||
@@ -147,6 +148,7 @@ pub(crate) fn start<C, N, P, SC>(
|
||||
notification.header.parent_hash().clone(),
|
||||
&authorities,
|
||||
key.clone(),
|
||||
max_block_data_size,
|
||||
)
|
||||
});
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ pub struct CollationFetch<C: Collators, P> {
|
||||
collators: C,
|
||||
live_fetch: Option<<C::Collation as IntoFuture>::Future>,
|
||||
client: Arc<P>,
|
||||
max_block_data_size: Option<u64>,
|
||||
}
|
||||
|
||||
impl<C: Collators, P> CollationFetch<C, P> {
|
||||
@@ -73,6 +74,7 @@ impl<C: Collators, P> CollationFetch<C, P> {
|
||||
relay_parent_hash: Hash,
|
||||
collators: C,
|
||||
client: Arc<P>,
|
||||
max_block_data_size: Option<u64>,
|
||||
) -> Self {
|
||||
CollationFetch {
|
||||
relay_parent: BlockId::hash(relay_parent_hash),
|
||||
@@ -81,6 +83,7 @@ impl<C: Collators, P> CollationFetch<C, P> {
|
||||
client,
|
||||
parachain,
|
||||
live_fetch: None,
|
||||
max_block_data_size,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +116,7 @@ impl<C: Collators, P: ProvideRuntimeApi> Future for CollationFetch<C, P>
|
||||
try_ready!(poll)
|
||||
};
|
||||
|
||||
let res = validate_collation(&*self.client, &self.relay_parent, &collation);
|
||||
let res = validate_collation(&*self.client, &self.relay_parent, &collation, self.max_block_data_size);
|
||||
|
||||
match res {
|
||||
Ok(e) => {
|
||||
@@ -179,7 +182,11 @@ error_chain! {
|
||||
}
|
||||
WrongHeadData(expected: Vec<u8>, got: Vec<u8>) {
|
||||
description("Parachain validation produced wrong head data."),
|
||||
display("Parachain validation produced wrong head data (expected: {:?}, got {:?}", expected, got),
|
||||
display("Parachain validation produced wrong head data (expected: {:?}, got {:?})", expected, got),
|
||||
}
|
||||
BlockDataTooBig(size: u64, max_size: u64) {
|
||||
description("Block data is too big."),
|
||||
display("Block data is too big (maximum allowed size: {}, actual size: {})", max_size, size),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -331,12 +338,20 @@ pub fn validate_collation<P>(
|
||||
client: &P,
|
||||
relay_parent: &BlockId,
|
||||
collation: &Collation,
|
||||
max_block_data_size: Option<u64>,
|
||||
) -> Result<Extrinsic, Error> where
|
||||
P: ProvideRuntimeApi,
|
||||
P::Api: ParachainHost<Block>,
|
||||
{
|
||||
use parachain::{IncomingMessage, ValidationParams};
|
||||
|
||||
if let Some(max_size) = max_block_data_size {
|
||||
let block_data_size = collation.pov.block_data.0.len() as u64;
|
||||
if block_data_size > max_size {
|
||||
return Err(ErrorKind::BlockDataTooBig(block_data_size, max_size).into());
|
||||
}
|
||||
}
|
||||
|
||||
let api = client.runtime_api();
|
||||
let para_id = collation.receipt.parachain_index;
|
||||
let validation_code = api.parachain_code(relay_parent, para_id)?
|
||||
|
||||
@@ -300,6 +300,7 @@ impl<C, N, P> ParachainValidation<C, N, P> where
|
||||
grandparent_hash: Hash,
|
||||
authorities: &[AuthorityId],
|
||||
sign_with: Arc<ed25519::Pair>,
|
||||
max_block_data_size: Option<u64>,
|
||||
)
|
||||
-> Result<Arc<AttestationTracker>, Error>
|
||||
{
|
||||
@@ -351,7 +352,14 @@ impl<C, N, P> ParachainValidation<C, N, P> where
|
||||
|
||||
debug!(target: "validation", "Active parachains: {:?}", active_parachains);
|
||||
|
||||
let table = Arc::new(SharedTable::new(authorities, group_info, sign_with.clone(), parent_hash, self.extrinsic_store.clone()));
|
||||
let table = Arc::new(SharedTable::new(
|
||||
authorities,
|
||||
group_info,
|
||||
sign_with.clone(),
|
||||
parent_hash,
|
||||
self.extrinsic_store.clone(),
|
||||
max_block_data_size,
|
||||
));
|
||||
let router = self.network.communication_for(
|
||||
table.clone(),
|
||||
authorities,
|
||||
@@ -362,6 +370,7 @@ impl<C, N, P> ParachainValidation<C, N, P> where
|
||||
parent_hash,
|
||||
id,
|
||||
router,
|
||||
max_block_data_size,
|
||||
)),
|
||||
Chain::Relay => None,
|
||||
};
|
||||
@@ -387,7 +396,8 @@ impl<C, N, P> ParachainValidation<C, N, P> where
|
||||
&self,
|
||||
relay_parent: Hash,
|
||||
validation_para: ParaId,
|
||||
build_router: N::BuildTableRouter
|
||||
build_router: N::BuildTableRouter,
|
||||
max_block_data_size: Option<u64>,
|
||||
) -> exit_future::Signal {
|
||||
use extrinsic_store::Data;
|
||||
|
||||
@@ -402,6 +412,7 @@ impl<C, N, P> ParachainValidation<C, N, P> where
|
||||
relay_parent,
|
||||
collators,
|
||||
client,
|
||||
max_block_data_size,
|
||||
);
|
||||
|
||||
collation_work.then(move |result| match result {
|
||||
@@ -466,6 +477,7 @@ pub struct ProposerFactory<C, N, P, SC, TxApi: PoolChainApi> {
|
||||
_service_handle: ServiceHandle,
|
||||
aura_slot_duration: SlotDuration,
|
||||
select_chain: SC,
|
||||
max_block_data_size: Option<u64>,
|
||||
}
|
||||
|
||||
impl<C, N, P, SC, TxApi> ProposerFactory<C, N, P, SC, TxApi> where
|
||||
@@ -491,6 +503,7 @@ impl<C, N, P, SC, TxApi> ProposerFactory<C, N, P, SC, TxApi> where
|
||||
key: Arc<ed25519::Pair>,
|
||||
extrinsic_store: ExtrinsicStore,
|
||||
aura_slot_duration: SlotDuration,
|
||||
max_block_data_size: Option<u64>,
|
||||
) -> Self {
|
||||
let parachain_validation = Arc::new(ParachainValidation {
|
||||
client: client.clone(),
|
||||
@@ -508,6 +521,7 @@ impl<C, N, P, SC, TxApi> ProposerFactory<C, N, P, SC, TxApi> where
|
||||
thread_pool,
|
||||
key.clone(),
|
||||
extrinsic_store,
|
||||
max_block_data_size,
|
||||
);
|
||||
|
||||
ProposerFactory {
|
||||
@@ -516,7 +530,8 @@ impl<C, N, P, SC, TxApi> ProposerFactory<C, N, P, SC, TxApi> where
|
||||
key,
|
||||
_service_handle: service_handle,
|
||||
aura_slot_duration,
|
||||
select_chain
|
||||
select_chain,
|
||||
max_block_data_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -548,6 +563,7 @@ impl<C, N, P, SC, TxApi> consensus::Environment<Block> for ProposerFactory<C, N,
|
||||
parent_header.parent_hash().clone(),
|
||||
authorities,
|
||||
sign_with,
|
||||
self.max_block_data_size,
|
||||
)?;
|
||||
|
||||
Ok(Proposer {
|
||||
|
||||
@@ -136,6 +136,7 @@ impl SharedTableInner {
|
||||
context: &TableContext,
|
||||
router: &R,
|
||||
statement: table::SignedStatement,
|
||||
max_block_data_size: Option<u64>,
|
||||
) -> Option<ParachainWork<
|
||||
<R::FetchValidationProof as IntoFuture>::Future,
|
||||
>> {
|
||||
@@ -191,7 +192,8 @@ impl SharedTableInner {
|
||||
work.map(|work| ParachainWork {
|
||||
extrinsic_store: self.extrinsic_store.clone(),
|
||||
relay_parent: context.parent_hash.clone(),
|
||||
work
|
||||
work,
|
||||
max_block_data_size,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -265,6 +267,7 @@ pub struct ParachainWork<Fetch> {
|
||||
work: Work<Fetch>,
|
||||
relay_parent: Hash,
|
||||
extrinsic_store: ExtrinsicStore,
|
||||
max_block_data_size: Option<u64>,
|
||||
}
|
||||
|
||||
impl<Fetch: Future> ParachainWork<Fetch> {
|
||||
@@ -279,11 +282,13 @@ impl<Fetch: Future> ParachainWork<Fetch> {
|
||||
P: Send + Sync + 'static,
|
||||
P::Api: ParachainHost<Block>,
|
||||
{
|
||||
let max_block_data_size = self.max_block_data_size;
|
||||
let validate = move |id: &_, collation: &_| {
|
||||
let res = ::collation::validate_collation(
|
||||
&*api,
|
||||
id,
|
||||
collation,
|
||||
max_block_data_size,
|
||||
);
|
||||
|
||||
match res {
|
||||
@@ -373,13 +378,15 @@ impl<Fetch, F, Err> Future for PrimedParachainWork<Fetch, F>
|
||||
pub struct SharedTable {
|
||||
context: Arc<TableContext>,
|
||||
inner: Arc<Mutex<SharedTableInner>>,
|
||||
max_block_data_size: Option<u64>,
|
||||
}
|
||||
|
||||
impl Clone for SharedTable {
|
||||
fn clone(&self) -> Self {
|
||||
SharedTable {
|
||||
Self {
|
||||
context: self.context.clone(),
|
||||
inner: self.inner.clone(),
|
||||
max_block_data_size: self.max_block_data_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -395,10 +402,12 @@ impl SharedTable {
|
||||
key: Arc<ed25519::Pair>,
|
||||
parent_hash: Hash,
|
||||
extrinsic_store: ExtrinsicStore,
|
||||
max_block_data_size: Option<u64>,
|
||||
) -> Self {
|
||||
let index_mapping = authorities.iter().enumerate().map(|(i, k)| (i as ValidatorIndex, k.clone())).collect();
|
||||
Self {
|
||||
context: Arc::new(TableContext { groups, key, parent_hash, index_mapping, }),
|
||||
max_block_data_size,
|
||||
inner: Arc::new(Mutex::new(SharedTableInner {
|
||||
table: Table::default(),
|
||||
validated: HashMap::new(),
|
||||
@@ -447,7 +456,7 @@ impl SharedTable {
|
||||
) -> Option<ParachainWork<
|
||||
<R::FetchValidationProof as IntoFuture>::Future,
|
||||
>> {
|
||||
self.inner.lock().import_remote_statement(&*self.context, router, statement)
|
||||
self.inner.lock().import_remote_statement(&*self.context, router, statement, self.max_block_data_size)
|
||||
}
|
||||
|
||||
/// Import many statements at once.
|
||||
@@ -467,7 +476,7 @@ impl SharedTable {
|
||||
let mut inner = self.inner.lock();
|
||||
|
||||
iterable.into_iter().map(move |statement| {
|
||||
inner.import_remote_statement(&*self.context, router, statement)
|
||||
inner.import_remote_statement(&*self.context, router, statement, self.max_block_data_size)
|
||||
}).collect()
|
||||
}
|
||||
|
||||
@@ -612,6 +621,7 @@ mod tests {
|
||||
local_key.clone(),
|
||||
parent_hash,
|
||||
ExtrinsicStore::new_in_memory(),
|
||||
None,
|
||||
);
|
||||
|
||||
let candidate = CandidateReceipt {
|
||||
@@ -665,6 +675,7 @@ mod tests {
|
||||
local_key.clone(),
|
||||
parent_hash,
|
||||
ExtrinsicStore::new_in_memory(),
|
||||
None,
|
||||
);
|
||||
|
||||
let candidate = CandidateReceipt {
|
||||
@@ -720,6 +731,7 @@ mod tests {
|
||||
},
|
||||
relay_parent,
|
||||
extrinsic_store: store.clone(),
|
||||
max_block_data_size: None,
|
||||
};
|
||||
|
||||
let validated = producer.prime_with(|_, _| Ok(Extrinsic { outgoing_messages: Vec::new() }))
|
||||
@@ -760,6 +772,7 @@ mod tests {
|
||||
},
|
||||
relay_parent,
|
||||
extrinsic_store: store.clone(),
|
||||
max_block_data_size: None,
|
||||
};
|
||||
|
||||
let validated = producer.prime_with(|_, _| Ok(Extrinsic { outgoing_messages: Vec::new() }))
|
||||
@@ -797,6 +810,7 @@ mod tests {
|
||||
local_key.clone(),
|
||||
parent_hash,
|
||||
ExtrinsicStore::new_in_memory(),
|
||||
None,
|
||||
);
|
||||
|
||||
let candidate = CandidateReceipt {
|
||||
@@ -861,6 +875,7 @@ mod tests {
|
||||
local_key.clone(),
|
||||
parent_hash,
|
||||
ExtrinsicStore::new_in_memory(),
|
||||
None,
|
||||
);
|
||||
|
||||
let candidate = CandidateReceipt {
|
||||
@@ -909,6 +924,7 @@ mod tests {
|
||||
Arc::new(AuthorityKeyring::Alice.pair()),
|
||||
Default::default(),
|
||||
ExtrinsicStore::new_in_memory(),
|
||||
None,
|
||||
);
|
||||
let expected_mapping = authorities.iter().enumerate().map(|(i, k)| (i as ValidatorIndex, k.clone())).collect();
|
||||
assert_eq!(shared_table.context.index_mapping, expected_mapping);
|
||||
|
||||
Reference in New Issue
Block a user