Skip to content

Commit

Permalink
Remove without_storage_info from parachains pallet (paritytech#1596)
Browse files Browse the repository at this point in the history
* remove without_storage_info from pallet-bridge-parachains

* fix benchmarks
  • Loading branch information
svyatonik authored Oct 10, 2022
1 parent b7918c8 commit 9575229
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 69 deletions.
4 changes: 4 additions & 0 deletions bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ impl pallet_bridge_messages::Config<WithRialtoParachainMessagesInstance> for Run
parameter_types! {
pub const RialtoParasPalletName: &'static str = bp_rialto::PARAS_PALLET_NAME;
pub const WestendParasPalletName: &'static str = bp_westend::PARAS_PALLET_NAME;
pub const MaxRialtoParaHeadSize: u32 = bp_rialto::MAX_NESTED_PARACHAIN_HEAD_SIZE;
pub const MaxWestendParaHeadSize: u32 = bp_westend::MAX_NESTED_PARACHAIN_HEAD_SIZE;
}

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

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

construct_runtime!(
Expand Down
32 changes: 24 additions & 8 deletions modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
// Runtime-generated enums
#![allow(clippy::large_enum_variant)]

use storage_types::{StoredAuthoritySet, StoredBridgedHeader};
use storage_types::StoredAuthoritySet;

use bp_header_chain::{justification::GrandpaJustification, InitializationData};
use bp_runtime::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf, OwnedBridgeModule};
use bp_runtime::{
BlockNumberOf, BoundedStorageValue, Chain, HashOf, HasherOf, HeaderOf, OwnedBridgeModule,
};
use finality_grandpa::voter_set::VoterSet;
use frame_support::{ensure, fail};
use frame_system::ensure_signed;
Expand Down Expand Up @@ -73,6 +75,9 @@ pub type BridgedBlockHash<T, I> = HashOf<<T as Config<I>>::BridgedChain>;
pub type BridgedBlockHasher<T, I> = HasherOf<<T as Config<I>>::BridgedChain>;
/// Header of the bridged chain.
pub type BridgedHeader<T, I> = HeaderOf<<T as Config<I>>::BridgedChain>;
/// Stored header of the bridged chain.
pub type StoredBridgedHeader<T, I> =
BoundedStorageValue<<T as Config<I>>::MaxBridgedHeaderSize, BridgedHeader<T, I>>;

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -199,8 +204,18 @@ pub mod pallet {

let is_authorities_change_enacted =
try_enact_authority_change::<T, I>(&finality_target, set_id)?;
let finality_target =
StoredBridgedHeader::<T, I>::try_from_bridged_header(*finality_target)?;
let finality_target = StoredBridgedHeader::<T, I>::try_from_inner(*finality_target)
.map_err(|e| {
log::error!(
target: LOG_TARGET,
"Size of header {:?} ({}) is larger that the configured value {}",
hash,
e.value_size,
e.maximal_size,
);

Error::<T, I>::TooLargeHeader
})?;
<RequestCount<T, I>>::mutate(|count| *count += 1);
insert_header::<T, I>(finality_target, hash);
log::info!(
Expand Down Expand Up @@ -504,7 +519,8 @@ pub mod pallet {
init_params;
let authority_set = StoredAuthoritySet::<T, I>::try_new(authority_list, set_id)
.map_err(|_| Error::TooManyAuthoritiesInSet)?;
let header = StoredBridgedHeader::<T, I>::try_from_bridged_header(*header)?;
let header = StoredBridgedHeader::<T, I>::try_from_inner(*header)
.map_err(|_| Error::<T, I>::TooLargeHeader)?;

let initial_hash = header.hash();
<InitialHash<T, I>>::put(initial_hash);
Expand Down Expand Up @@ -538,7 +554,7 @@ pub mod pallet {
);
let hash = header.hash();
insert_header::<T, I>(
StoredBridgedHeader::try_from_bridged_header(header)
StoredBridgedHeader::<T, I>::try_from_inner(header)
.expect("only used from benchmarks; benchmarks are correct; qed"),
hash,
);
Expand All @@ -553,7 +569,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// if the pallet has not been initialized yet.
pub fn best_finalized() -> Option<BridgedHeader<T, I>> {
let (_, hash) = <BestFinalized<T, I>>::get()?;
<ImportedHeaders<T, I>>::get(hash).map(|h| h.0)
<ImportedHeaders<T, I>>::get(hash).map(|h| h.into_inner())
}

/// Check if a particular header is known to the bridge pallet.
Expand Down Expand Up @@ -1103,7 +1119,7 @@ mod tests {
<BestFinalized<TestRuntime>>::put((2, hash));
<ImportedHeaders<TestRuntime>>::insert(
hash,
StoredBridgedHeader::try_from_bridged_header(header).unwrap(),
StoredBridgedHeader::<TestRuntime, ()>::try_from_inner(header).unwrap(),
);

assert_ok!(
Expand Down
46 changes: 3 additions & 43 deletions modules/grandpa/src/storage_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

//! Wrappers for public types that are implementing `MaxEncodedLen`
use crate::{BridgedHeader, Config, Error};
use crate::Config;

use bp_header_chain::AuthoritySet;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{traits::Get, BoundedVec, RuntimeDebugNoBound};
use scale_info::{Type, TypeInfo};
use frame_support::{BoundedVec, RuntimeDebugNoBound};
use scale_info::TypeInfo;
use sp_finality_grandpa::{AuthorityId, AuthorityList, AuthorityWeight, SetId};

/// A bounded list of Grandpa authorities with associated weights.
Expand Down Expand Up @@ -64,43 +64,3 @@ impl<T: Config<I>, I: 'static> From<StoredAuthoritySet<T, I>> for AuthoritySet {
AuthoritySet { authorities: t.authorities.into(), set_id: t.set_id }
}
}

/// A bounded chain header.
#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebugNoBound)]
pub struct StoredBridgedHeader<T: Config<I>, I: 'static>(pub BridgedHeader<T, I>);

impl<T: Config<I>, I: 'static> StoredBridgedHeader<T, I> {
/// Construct `StoredBridgedHeader` from the `BridgedHeader` with all required checks.
pub fn try_from_bridged_header(header: BridgedHeader<T, I>) -> Result<Self, Error<T, I>> {
// this conversion is heavy (since we do encoding here), so we may want to optimize it later
// (e.g. by introducing custom Encode implementation, and turning `StoredBridgedHeader` into
// `enum StoredBridgedHeader { Decoded(BridgedHeader), Encoded(Vec<u8>) }`)
if header.encoded_size() > T::MaxBridgedHeaderSize::get() as usize {
Err(Error::TooLargeHeader)
} else {
Ok(StoredBridgedHeader(header))
}
}
}

impl<T: Config<I>, I: 'static> sp_std::ops::Deref for StoredBridgedHeader<T, I> {
type Target = BridgedHeader<T, I>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T: Config<I>, I: 'static> TypeInfo for StoredBridgedHeader<T, I> {
type Identity = Self;

fn type_info() -> Type {
BridgedHeader::<T, I>::type_info()
}
}

impl<T: Config<I>, I: 'static> MaxEncodedLen for StoredBridgedHeader<T, I> {
fn max_encoded_len() -> usize {
T::MaxBridgedHeaderSize::get() as usize
}
}
Loading

0 comments on commit 9575229

Please sign in to comment.