Skip to content

Commit

Permalink
ChainWithGrandpa in primitives (#1885)
Browse files Browse the repository at this point in the history
* ChainWithGrandpa in primitives

* clippy ++ spelling

* fix benchmarks comppilation
  • Loading branch information
svyatonik committed Feb 16, 2023
1 parent 91e66cf commit c67d06a
Show file tree
Hide file tree
Showing 34 changed files with 431 additions and 139 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 4 additions & 23 deletions bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,21 +387,6 @@ impl pallet_bridge_relayers::Config for Runtime {
type WeightInfo = ();
}

parameter_types! {
/// Number of headers to keep.
///
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
/// day.
pub const HeadersToKeep: u32 = bp_rialto::DAYS;
/// Maximal number of authorities at Rialto.
pub const MaxAuthoritiesAtRialto: u32 = bp_rialto::MAX_AUTHORITIES_COUNT;
}

parameter_types! {
/// Maximal number of authorities at Westend.
pub const MaxAuthoritiesAtWestend: u32 = bp_westend::MAX_AUTHORITIES_COUNT;
}

pub type RialtoGrandpaInstance = ();
impl pallet_bridge_grandpa::Config for Runtime {
type BridgedChain = bp_rialto::Rialto;
Expand All @@ -410,19 +395,15 @@ impl pallet_bridge_grandpa::Config for Runtime {
// Note that once this is hit the pallet will essentially throttle incoming requests down to one
// call per block.
type MaxRequests = ConstU32<50>;
type HeadersToKeep = HeadersToKeep;
type MaxBridgedAuthorities = MaxAuthoritiesAtRialto;

type HeadersToKeep = ConstU32<{ bp_rialto::DAYS }>;
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<Runtime>;
}

pub type WestendGrandpaInstance = pallet_bridge_grandpa::Instance1;
impl pallet_bridge_grandpa::Config<WestendGrandpaInstance> for Runtime {
type BridgedChain = bp_westend::Westend;
type MaxRequests = ConstU32<50>;
type HeadersToKeep = HeadersToKeep;
type MaxBridgedAuthorities = MaxAuthoritiesAtWestend;

type HeadersToKeep = ConstU32<{ bp_westend::DAYS }>;
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<Runtime>;
}

Expand Down Expand Up @@ -521,7 +502,7 @@ impl pallet_bridge_parachains::Config<WithRialtoParachainsInstance> for Runtime
type ParasPalletName = RialtoParasPalletName;
type ParaStoredHeaderDataBuilder =
SingleParaStoredHeaderDataBuilder<bp_rialto_parachain::RialtoParachain>;
type HeadsToKeep = HeadersToKeep;
type HeadsToKeep = ConstU32<1024>;
type MaxParaHeadDataSize = MaxRialtoParaHeadDataSize;
}

Expand All @@ -534,7 +515,7 @@ impl pallet_bridge_parachains::Config<WithWestendParachainsInstance> for Runtime
type BridgesGrandpaPalletInstance = WestendGrandpaInstance;
type ParasPalletName = WestendParasPalletName;
type ParaStoredHeaderDataBuilder = SingleParaStoredHeaderDataBuilder<bp_westend::Westmint>;
type HeadsToKeep = HeadersToKeep;
type HeadsToKeep = ConstU32<1024>;
type MaxParaHeadDataSize = MaxWestendParaHeadDataSize;
}

Expand Down
14 changes: 1 addition & 13 deletions bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,17 +525,6 @@ impl pallet_bridge_relayers::Config for Runtime {
type WeightInfo = ();
}

parameter_types! {
/// Number of headers to keep.
///
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
/// day.
pub const HeadersToKeep: u32 = bp_millau::DAYS as u32;

/// Maximal number of authorities at Millau.
pub const MaxAuthoritiesAtMillau: u32 = bp_millau::MAX_AUTHORITIES_COUNT;
}

pub type MillauGrandpaInstance = ();
impl pallet_bridge_grandpa::Config for Runtime {
type BridgedChain = bp_millau::Millau;
Expand All @@ -544,8 +533,7 @@ impl pallet_bridge_grandpa::Config for Runtime {
/// Note that once this is hit the pallet will essentially throttle incoming requests down to
/// one call per block.
type MaxRequests = ConstU32<50>;
type HeadersToKeep = HeadersToKeep;
type MaxBridgedAuthorities = MaxAuthoritiesAtMillau;
type HeadersToKeep = ConstU32<{ bp_millau::DAYS as u32 }>;
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<Runtime>;
}

Expand Down
14 changes: 1 addition & 13 deletions bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,6 @@ impl pallet_bridge_relayers::Config for Runtime {
type WeightInfo = ();
}

parameter_types! {
/// Number of headers to keep.
///
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
/// day.
pub const HeadersToKeep: u32 = bp_rialto::DAYS;

/// Maximal number of authorities at Millau.
pub const MaxAuthoritiesAtMillau: u32 = bp_millau::MAX_AUTHORITIES_COUNT;
}

pub type MillauGrandpaInstance = ();
impl pallet_bridge_grandpa::Config for Runtime {
type BridgedChain = bp_millau::Millau;
Expand All @@ -416,8 +405,7 @@ impl pallet_bridge_grandpa::Config for Runtime {
/// Note that once this is hit the pallet will essentially throttle incoming requests down to
/// one call per block.
type MaxRequests = ConstU32<50>;
type HeadersToKeep = HeadersToKeep;
type MaxBridgedAuthorities = MaxAuthoritiesAtMillau;
type HeadersToKeep = ConstU32<{ bp_millau::DAYS as u32 }>;
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<Runtime>;
}

Expand Down
11 changes: 9 additions & 2 deletions bin/runtime-common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::messages::{
BridgedChainWithMessages, HashOf, MessageBridge, ThisChainWithMessages,
};

use bp_header_chain::HeaderChain;
use bp_header_chain::{ChainWithGrandpa, HeaderChain};
use bp_messages::{target_chain::ForbidInboundMessages, LaneId, MessageNonce};
use bp_parachains::SingleParaStoredHeaderDataBuilder;
use bp_runtime::{Chain, ChainId, Parachain, UnderlyingChainProvider};
Expand Down Expand Up @@ -195,7 +195,6 @@ impl pallet_bridge_grandpa::Config for TestRuntime {
type BridgedChain = BridgedUnderlyingChain;
type MaxRequests = ConstU32<50>;
type HeadersToKeep = ConstU32<8>;
type MaxBridgedAuthorities = ConstU32<1024>;
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<TestRuntime>;
}

Expand Down Expand Up @@ -372,6 +371,14 @@ impl Chain for BridgedUnderlyingChain {
}
}

impl ChainWithGrandpa for BridgedUnderlyingChain {
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = "";
const MAX_AUTHORITIES_COUNT: u32 = 16;
const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 8;
const MAX_HEADER_SIZE: u32 = 256;
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64;
}

impl Chain for BridgedUnderlyingParachain {
type BlockNumber = BridgedChainBlockNumber;
type Hash = BridgedChainHash;
Expand Down
3 changes: 1 addition & 2 deletions modules/grandpa/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ use bp_test_utils::{
TEST_GRANDPA_SET_ID,
};
use frame_benchmarking::{benchmarks_instance_pallet, whitelisted_caller};
use frame_support::traits::Get;
use frame_system::RawOrigin;
use sp_finality_grandpa::AuthorityId;
use sp_runtime::traits::{One, Zero};
Expand All @@ -68,7 +67,7 @@ const MAX_VOTE_ANCESTRIES_RANGE_END: u32 =

// the same with validators - if there are too much validators, let's run benchmarks on subrange
fn validator_set_range_end<T: Config<I>, I: 'static>() -> u32 {
let max_bridged_authorities = T::MaxBridgedAuthorities::get();
let max_bridged_authorities = T::BridgedChain::MAX_AUTHORITIES_COUNT;
if max_bridged_authorities > 128 {
sp_std::cmp::max(128, max_bridged_authorities / 5)
} else {
Expand Down
14 changes: 5 additions & 9 deletions modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
use storage_types::StoredAuthoritySet;

use bp_header_chain::{
justification::GrandpaJustification, HeaderChain, InitializationData, StoredHeaderData,
StoredHeaderDataBuilder,
justification::GrandpaJustification, ChainWithGrandpa, HeaderChain, InitializationData,
StoredHeaderData, StoredHeaderDataBuilder,
};
use bp_runtime::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderId, HeaderOf, OwnedBridgeModule};
use bp_runtime::{BlockNumberOf, HashOf, HasherOf, HeaderId, HeaderOf, OwnedBridgeModule};
use finality_grandpa::voter_set::VoterSet;
use frame_support::{dispatch::PostDispatchInfo, ensure};
use sp_finality_grandpa::{ConsensusLog, GRANDPA_ENGINE_ID};
Expand Down Expand Up @@ -97,7 +97,7 @@ pub mod pallet {
#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
/// The chain we are bridging to here.
type BridgedChain: Chain;
type BridgedChain: ChainWithGrandpa;

/// The upper bound on the number of requests allowed by the pallet.
///
Expand All @@ -118,10 +118,6 @@ pub mod pallet {
#[pallet::constant]
type HeadersToKeep: Get<u32>;

/// Max number of authorities at the bridged chain.
#[pallet::constant]
type MaxBridgedAuthorities: Get<u32>;

/// Weights gathered through benchmarking.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -513,7 +509,7 @@ pub mod pallet {
target: LOG_TARGET,
"Failed to initialize bridge. Number of authorities in the set {} is larger than the configured value {}",
authority_set_length,
T::MaxBridgedAuthorities::get(),
T::BridgedChain::MAX_AUTHORITIES_COUNT,
);

Error::TooManyAuthoritiesInSet
Expand Down
18 changes: 15 additions & 3 deletions modules/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
// From construct_runtime macro
#![allow(clippy::from_over_into)]

use bp_header_chain::ChainWithGrandpa;
use bp_runtime::Chain;
use frame_support::{construct_runtime, parameter_types, traits::ConstU64, weights::Weight};
use frame_support::{
construct_runtime, parameter_types,
traits::{ConstU32, ConstU64},
weights::Weight,
};
use sp_core::sr25519::Signature;
use sp_runtime::{
testing::{Header, H256},
Expand Down Expand Up @@ -78,7 +83,7 @@ impl frame_system::Config for TestRuntime {
type BlockLength = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type MaxConsumers = ConstU32<16>;
}

parameter_types! {
Expand All @@ -92,7 +97,6 @@ impl grandpa::Config for TestRuntime {
type BridgedChain = TestBridgedChain;
type MaxRequests = MaxRequests;
type HeadersToKeep = HeadersToKeep;
type MaxBridgedAuthorities = frame_support::traits::ConstU32<MAX_BRIDGED_AUTHORITIES>;
type WeightInfo = ();
}

Expand All @@ -118,6 +122,14 @@ impl Chain for TestBridgedChain {
}
}

impl ChainWithGrandpa for TestBridgedChain {
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = "";
const MAX_AUTHORITIES_COUNT: u32 = MAX_BRIDGED_AUTHORITIES;
const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 8;
const MAX_HEADER_SIZE: u32 = 256;
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64;
}

pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
sp_io::TestExternalities::new(Default::default()).execute_with(test)
}
Expand Down
16 changes: 13 additions & 3 deletions modules/grandpa/src/storage_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,32 @@

use crate::Config;

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

/// A bounded list of Grandpa authorities with associated weights.
pub type StoredAuthorityList<MaxBridgedAuthorities> =
BoundedVec<(AuthorityId, AuthorityWeight), MaxBridgedAuthorities>;

/// Adapter for using `T::BridgedChain::MAX_BRIDGED_AUTHORITIES` in `BoundedVec`.
pub struct StoredAuthorityListLimit<T, I>(PhantomData<(T, I)>);

impl<T: Config<I>, I: 'static> Get<u32> for StoredAuthorityListLimit<T, I> {
fn get() -> u32 {
T::BridgedChain::MAX_AUTHORITIES_COUNT
}
}

/// A bounded GRANDPA Authority List and ID.
#[derive(Clone, Decode, Encode, Eq, TypeInfo, MaxEncodedLen, RuntimeDebugNoBound)]
#[scale_info(skip_type_params(T, I))]
pub struct StoredAuthoritySet<T: Config<I>, I: 'static> {
/// List of GRANDPA authorities for the current round.
pub authorities: StoredAuthorityList<<T as Config<I>>::MaxBridgedAuthorities>,
pub authorities: StoredAuthorityList<StoredAuthorityListLimit<T, I>>,
/// Monotonic identifier of the current GRANDPA authority set.
pub set_id: SetId,
}
Expand All @@ -60,7 +70,7 @@ impl<T: Config<I>, I: 'static> StoredAuthoritySet<T, I> {
let single_authority_max_encoded_len =
<(AuthorityId, AuthorityWeight)>::max_encoded_len() as u64;
let extra_authorities =
T::MaxBridgedAuthorities::get().saturating_sub(self.authorities.len() as _);
T::BridgedChain::MAX_AUTHORITIES_COUNT.saturating_sub(self.authorities.len() as _);
single_authority_max_encoded_len.saturating_mul(extra_authorities as u64)
}
}
Expand Down
Loading

0 comments on commit c67d06a

Please sign in to comment.