Skip to content

Commit 9575229

Browse files
authored
Remove without_storage_info from parachains pallet (#1596)
* remove without_storage_info from pallet-bridge-parachains * fix benchmarks
1 parent b7918c8 commit 9575229

File tree

10 files changed

+258
-69
lines changed

10 files changed

+258
-69
lines changed

bin/millau/runtime/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ impl pallet_bridge_messages::Config<WithRialtoParachainMessagesInstance> for Run
550550
parameter_types! {
551551
pub const RialtoParasPalletName: &'static str = bp_rialto::PARAS_PALLET_NAME;
552552
pub const WestendParasPalletName: &'static str = bp_westend::PARAS_PALLET_NAME;
553+
pub const MaxRialtoParaHeadSize: u32 = bp_rialto::MAX_NESTED_PARACHAIN_HEAD_SIZE;
554+
pub const MaxWestendParaHeadSize: u32 = bp_westend::MAX_NESTED_PARACHAIN_HEAD_SIZE;
553555
}
554556

555557
/// Instance of the with-Rialto parachains pallet.
@@ -562,6 +564,7 @@ impl pallet_bridge_parachains::Config<WithRialtoParachainsInstance> for Runtime
562564
type ParasPalletName = RialtoParasPalletName;
563565
type TrackedParachains = frame_support::traits::Everything;
564566
type HeadsToKeep = HeadersToKeep;
567+
type MaxParaHeadSize = MaxRialtoParaHeadSize;
565568
}
566569

567570
/// Instance of the with-Westend parachains pallet.
@@ -574,6 +577,7 @@ impl pallet_bridge_parachains::Config<WithWestendParachainsInstance> for Runtime
574577
type ParasPalletName = WestendParasPalletName;
575578
type TrackedParachains = frame_support::traits::Everything;
576579
type HeadsToKeep = HeadersToKeep;
580+
type MaxParaHeadSize = MaxWestendParaHeadSize;
577581
}
578582

579583
construct_runtime!(

modules/grandpa/src/lib.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@
3636
// Runtime-generated enums
3737
#![allow(clippy::large_enum_variant)]
3838

39-
use storage_types::{StoredAuthoritySet, StoredBridgedHeader};
39+
use storage_types::StoredAuthoritySet;
4040

4141
use bp_header_chain::{justification::GrandpaJustification, InitializationData};
42-
use bp_runtime::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf, OwnedBridgeModule};
42+
use bp_runtime::{
43+
BlockNumberOf, BoundedStorageValue, Chain, HashOf, HasherOf, HeaderOf, OwnedBridgeModule,
44+
};
4345
use finality_grandpa::voter_set::VoterSet;
4446
use frame_support::{ensure, fail};
4547
use frame_system::ensure_signed;
@@ -73,6 +75,9 @@ pub type BridgedBlockHash<T, I> = HashOf<<T as Config<I>>::BridgedChain>;
7375
pub type BridgedBlockHasher<T, I> = HasherOf<<T as Config<I>>::BridgedChain>;
7476
/// Header of the bridged chain.
7577
pub type BridgedHeader<T, I> = HeaderOf<<T as Config<I>>::BridgedChain>;
78+
/// Stored header of the bridged chain.
79+
pub type StoredBridgedHeader<T, I> =
80+
BoundedStorageValue<<T as Config<I>>::MaxBridgedHeaderSize, BridgedHeader<T, I>>;
7681

7782
#[frame_support::pallet]
7883
pub mod pallet {
@@ -199,8 +204,18 @@ pub mod pallet {
199204

200205
let is_authorities_change_enacted =
201206
try_enact_authority_change::<T, I>(&finality_target, set_id)?;
202-
let finality_target =
203-
StoredBridgedHeader::<T, I>::try_from_bridged_header(*finality_target)?;
207+
let finality_target = StoredBridgedHeader::<T, I>::try_from_inner(*finality_target)
208+
.map_err(|e| {
209+
log::error!(
210+
target: LOG_TARGET,
211+
"Size of header {:?} ({}) is larger that the configured value {}",
212+
hash,
213+
e.value_size,
214+
e.maximal_size,
215+
);
216+
217+
Error::<T, I>::TooLargeHeader
218+
})?;
204219
<RequestCount<T, I>>::mutate(|count| *count += 1);
205220
insert_header::<T, I>(finality_target, hash);
206221
log::info!(
@@ -504,7 +519,8 @@ pub mod pallet {
504519
init_params;
505520
let authority_set = StoredAuthoritySet::<T, I>::try_new(authority_list, set_id)
506521
.map_err(|_| Error::TooManyAuthoritiesInSet)?;
507-
let header = StoredBridgedHeader::<T, I>::try_from_bridged_header(*header)?;
522+
let header = StoredBridgedHeader::<T, I>::try_from_inner(*header)
523+
.map_err(|_| Error::<T, I>::TooLargeHeader)?;
508524

509525
let initial_hash = header.hash();
510526
<InitialHash<T, I>>::put(initial_hash);
@@ -538,7 +554,7 @@ pub mod pallet {
538554
);
539555
let hash = header.hash();
540556
insert_header::<T, I>(
541-
StoredBridgedHeader::try_from_bridged_header(header)
557+
StoredBridgedHeader::<T, I>::try_from_inner(header)
542558
.expect("only used from benchmarks; benchmarks are correct; qed"),
543559
hash,
544560
);
@@ -553,7 +569,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
553569
/// if the pallet has not been initialized yet.
554570
pub fn best_finalized() -> Option<BridgedHeader<T, I>> {
555571
let (_, hash) = <BestFinalized<T, I>>::get()?;
556-
<ImportedHeaders<T, I>>::get(hash).map(|h| h.0)
572+
<ImportedHeaders<T, I>>::get(hash).map(|h| h.into_inner())
557573
}
558574

559575
/// Check if a particular header is known to the bridge pallet.
@@ -1103,7 +1119,7 @@ mod tests {
11031119
<BestFinalized<TestRuntime>>::put((2, hash));
11041120
<ImportedHeaders<TestRuntime>>::insert(
11051121
hash,
1106-
StoredBridgedHeader::try_from_bridged_header(header).unwrap(),
1122+
StoredBridgedHeader::<TestRuntime, ()>::try_from_inner(header).unwrap(),
11071123
);
11081124

11091125
assert_ok!(

modules/grandpa/src/storage_types.rs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
//! Wrappers for public types that are implementing `MaxEncodedLen`
1818
19-
use crate::{BridgedHeader, Config, Error};
19+
use crate::Config;
2020

2121
use bp_header_chain::AuthoritySet;
2222
use codec::{Decode, Encode, MaxEncodedLen};
23-
use frame_support::{traits::Get, BoundedVec, RuntimeDebugNoBound};
24-
use scale_info::{Type, TypeInfo};
23+
use frame_support::{BoundedVec, RuntimeDebugNoBound};
24+
use scale_info::TypeInfo;
2525
use sp_finality_grandpa::{AuthorityId, AuthorityList, AuthorityWeight, SetId};
2626

2727
/// A bounded list of Grandpa authorities with associated weights.
@@ -64,43 +64,3 @@ impl<T: Config<I>, I: 'static> From<StoredAuthoritySet<T, I>> for AuthoritySet {
6464
AuthoritySet { authorities: t.authorities.into(), set_id: t.set_id }
6565
}
6666
}
67-
68-
/// A bounded chain header.
69-
#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebugNoBound)]
70-
pub struct StoredBridgedHeader<T: Config<I>, I: 'static>(pub BridgedHeader<T, I>);
71-
72-
impl<T: Config<I>, I: 'static> StoredBridgedHeader<T, I> {
73-
/// Construct `StoredBridgedHeader` from the `BridgedHeader` with all required checks.
74-
pub fn try_from_bridged_header(header: BridgedHeader<T, I>) -> Result<Self, Error<T, I>> {
75-
// this conversion is heavy (since we do encoding here), so we may want to optimize it later
76-
// (e.g. by introducing custom Encode implementation, and turning `StoredBridgedHeader` into
77-
// `enum StoredBridgedHeader { Decoded(BridgedHeader), Encoded(Vec<u8>) }`)
78-
if header.encoded_size() > T::MaxBridgedHeaderSize::get() as usize {
79-
Err(Error::TooLargeHeader)
80-
} else {
81-
Ok(StoredBridgedHeader(header))
82-
}
83-
}
84-
}
85-
86-
impl<T: Config<I>, I: 'static> sp_std::ops::Deref for StoredBridgedHeader<T, I> {
87-
type Target = BridgedHeader<T, I>;
88-
89-
fn deref(&self) -> &Self::Target {
90-
&self.0
91-
}
92-
}
93-
94-
impl<T: Config<I>, I: 'static> TypeInfo for StoredBridgedHeader<T, I> {
95-
type Identity = Self;
96-
97-
fn type_info() -> Type {
98-
BridgedHeader::<T, I>::type_info()
99-
}
100-
}
101-
102-
impl<T: Config<I>, I: 'static> MaxEncodedLen for StoredBridgedHeader<T, I> {
103-
fn max_encoded_len() -> usize {
104-
T::MaxBridgedHeaderSize::get() as usize
105-
}
106-
}

0 commit comments

Comments
 (0)