Skip to content

Commit c67d06a

Browse files
authored
ChainWithGrandpa in primitives (#1885)
* ChainWithGrandpa in primitives * clippy ++ spelling * fix benchmarks comppilation
1 parent 91e66cf commit c67d06a

File tree

34 files changed

+431
-139
lines changed

34 files changed

+431
-139
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/millau/runtime/src/lib.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -387,21 +387,6 @@ impl pallet_bridge_relayers::Config for Runtime {
387387
type WeightInfo = ();
388388
}
389389

390-
parameter_types! {
391-
/// Number of headers to keep.
392-
///
393-
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
394-
/// day.
395-
pub const HeadersToKeep: u32 = bp_rialto::DAYS;
396-
/// Maximal number of authorities at Rialto.
397-
pub const MaxAuthoritiesAtRialto: u32 = bp_rialto::MAX_AUTHORITIES_COUNT;
398-
}
399-
400-
parameter_types! {
401-
/// Maximal number of authorities at Westend.
402-
pub const MaxAuthoritiesAtWestend: u32 = bp_westend::MAX_AUTHORITIES_COUNT;
403-
}
404-
405390
pub type RialtoGrandpaInstance = ();
406391
impl pallet_bridge_grandpa::Config for Runtime {
407392
type BridgedChain = bp_rialto::Rialto;
@@ -410,19 +395,15 @@ impl pallet_bridge_grandpa::Config for Runtime {
410395
// Note that once this is hit the pallet will essentially throttle incoming requests down to one
411396
// call per block.
412397
type MaxRequests = ConstU32<50>;
413-
type HeadersToKeep = HeadersToKeep;
414-
type MaxBridgedAuthorities = MaxAuthoritiesAtRialto;
415-
398+
type HeadersToKeep = ConstU32<{ bp_rialto::DAYS }>;
416399
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<Runtime>;
417400
}
418401

419402
pub type WestendGrandpaInstance = pallet_bridge_grandpa::Instance1;
420403
impl pallet_bridge_grandpa::Config<WestendGrandpaInstance> for Runtime {
421404
type BridgedChain = bp_westend::Westend;
422405
type MaxRequests = ConstU32<50>;
423-
type HeadersToKeep = HeadersToKeep;
424-
type MaxBridgedAuthorities = MaxAuthoritiesAtWestend;
425-
406+
type HeadersToKeep = ConstU32<{ bp_westend::DAYS }>;
426407
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<Runtime>;
427408
}
428409

@@ -521,7 +502,7 @@ impl pallet_bridge_parachains::Config<WithRialtoParachainsInstance> for Runtime
521502
type ParasPalletName = RialtoParasPalletName;
522503
type ParaStoredHeaderDataBuilder =
523504
SingleParaStoredHeaderDataBuilder<bp_rialto_parachain::RialtoParachain>;
524-
type HeadsToKeep = HeadersToKeep;
505+
type HeadsToKeep = ConstU32<1024>;
525506
type MaxParaHeadDataSize = MaxRialtoParaHeadDataSize;
526507
}
527508

@@ -534,7 +515,7 @@ impl pallet_bridge_parachains::Config<WithWestendParachainsInstance> for Runtime
534515
type BridgesGrandpaPalletInstance = WestendGrandpaInstance;
535516
type ParasPalletName = WestendParasPalletName;
536517
type ParaStoredHeaderDataBuilder = SingleParaStoredHeaderDataBuilder<bp_westend::Westmint>;
537-
type HeadsToKeep = HeadersToKeep;
518+
type HeadsToKeep = ConstU32<1024>;
538519
type MaxParaHeadDataSize = MaxWestendParaHeadDataSize;
539520
}
540521

bin/rialto-parachain/runtime/src/lib.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -525,17 +525,6 @@ impl pallet_bridge_relayers::Config for Runtime {
525525
type WeightInfo = ();
526526
}
527527

528-
parameter_types! {
529-
/// Number of headers to keep.
530-
///
531-
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
532-
/// day.
533-
pub const HeadersToKeep: u32 = bp_millau::DAYS as u32;
534-
535-
/// Maximal number of authorities at Millau.
536-
pub const MaxAuthoritiesAtMillau: u32 = bp_millau::MAX_AUTHORITIES_COUNT;
537-
}
538-
539528
pub type MillauGrandpaInstance = ();
540529
impl pallet_bridge_grandpa::Config for Runtime {
541530
type BridgedChain = bp_millau::Millau;
@@ -544,8 +533,7 @@ impl pallet_bridge_grandpa::Config for Runtime {
544533
/// Note that once this is hit the pallet will essentially throttle incoming requests down to
545534
/// one call per block.
546535
type MaxRequests = ConstU32<50>;
547-
type HeadersToKeep = HeadersToKeep;
548-
type MaxBridgedAuthorities = MaxAuthoritiesAtMillau;
536+
type HeadersToKeep = ConstU32<{ bp_millau::DAYS as u32 }>;
549537
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<Runtime>;
550538
}
551539

bin/rialto/runtime/src/lib.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,6 @@ impl pallet_bridge_relayers::Config for Runtime {
397397
type WeightInfo = ();
398398
}
399399

400-
parameter_types! {
401-
/// Number of headers to keep.
402-
///
403-
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
404-
/// day.
405-
pub const HeadersToKeep: u32 = bp_rialto::DAYS;
406-
407-
/// Maximal number of authorities at Millau.
408-
pub const MaxAuthoritiesAtMillau: u32 = bp_millau::MAX_AUTHORITIES_COUNT;
409-
}
410-
411400
pub type MillauGrandpaInstance = ();
412401
impl pallet_bridge_grandpa::Config for Runtime {
413402
type BridgedChain = bp_millau::Millau;
@@ -416,8 +405,7 @@ impl pallet_bridge_grandpa::Config for Runtime {
416405
/// Note that once this is hit the pallet will essentially throttle incoming requests down to
417406
/// one call per block.
418407
type MaxRequests = ConstU32<50>;
419-
type HeadersToKeep = HeadersToKeep;
420-
type MaxBridgedAuthorities = MaxAuthoritiesAtMillau;
408+
type HeadersToKeep = ConstU32<{ bp_millau::DAYS as u32 }>;
421409
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<Runtime>;
422410
}
423411

bin/runtime-common/src/mock.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::messages::{
3232
BridgedChainWithMessages, HashOf, MessageBridge, ThisChainWithMessages,
3333
};
3434

35-
use bp_header_chain::HeaderChain;
35+
use bp_header_chain::{ChainWithGrandpa, HeaderChain};
3636
use bp_messages::{target_chain::ForbidInboundMessages, LaneId, MessageNonce};
3737
use bp_parachains::SingleParaStoredHeaderDataBuilder;
3838
use bp_runtime::{Chain, ChainId, Parachain, UnderlyingChainProvider};
@@ -195,7 +195,6 @@ impl pallet_bridge_grandpa::Config for TestRuntime {
195195
type BridgedChain = BridgedUnderlyingChain;
196196
type MaxRequests = ConstU32<50>;
197197
type HeadersToKeep = ConstU32<8>;
198-
type MaxBridgedAuthorities = ConstU32<1024>;
199198
type WeightInfo = pallet_bridge_grandpa::weights::BridgeWeight<TestRuntime>;
200199
}
201200

@@ -372,6 +371,14 @@ impl Chain for BridgedUnderlyingChain {
372371
}
373372
}
374373

374+
impl ChainWithGrandpa for BridgedUnderlyingChain {
375+
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = "";
376+
const MAX_AUTHORITIES_COUNT: u32 = 16;
377+
const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 8;
378+
const MAX_HEADER_SIZE: u32 = 256;
379+
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64;
380+
}
381+
375382
impl Chain for BridgedUnderlyingParachain {
376383
type BlockNumber = BridgedChainBlockNumber;
377384
type Hash = BridgedChainHash;

modules/grandpa/src/benchmarking.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use bp_test_utils::{
4747
TEST_GRANDPA_SET_ID,
4848
};
4949
use frame_benchmarking::{benchmarks_instance_pallet, whitelisted_caller};
50-
use frame_support::traits::Get;
5150
use frame_system::RawOrigin;
5251
use sp_finality_grandpa::AuthorityId;
5352
use sp_runtime::traits::{One, Zero};
@@ -68,7 +67,7 @@ const MAX_VOTE_ANCESTRIES_RANGE_END: u32 =
6867

6968
// the same with validators - if there are too much validators, let's run benchmarks on subrange
7069
fn validator_set_range_end<T: Config<I>, I: 'static>() -> u32 {
71-
let max_bridged_authorities = T::MaxBridgedAuthorities::get();
70+
let max_bridged_authorities = T::BridgedChain::MAX_AUTHORITIES_COUNT;
7271
if max_bridged_authorities > 128 {
7372
sp_std::cmp::max(128, max_bridged_authorities / 5)
7473
} else {

modules/grandpa/src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
use storage_types::StoredAuthoritySet;
4040

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

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

121-
/// Max number of authorities at the bridged chain.
122-
#[pallet::constant]
123-
type MaxBridgedAuthorities: Get<u32>;
124-
125121
/// Weights gathered through benchmarking.
126122
type WeightInfo: WeightInfo;
127123
}
@@ -513,7 +509,7 @@ pub mod pallet {
513509
target: LOG_TARGET,
514510
"Failed to initialize bridge. Number of authorities in the set {} is larger than the configured value {}",
515511
authority_set_length,
516-
T::MaxBridgedAuthorities::get(),
512+
T::BridgedChain::MAX_AUTHORITIES_COUNT,
517513
);
518514

519515
Error::TooManyAuthoritiesInSet

modules/grandpa/src/mock.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
// From construct_runtime macro
1818
#![allow(clippy::from_over_into)]
1919

20+
use bp_header_chain::ChainWithGrandpa;
2021
use bp_runtime::Chain;
21-
use frame_support::{construct_runtime, parameter_types, traits::ConstU64, weights::Weight};
22+
use frame_support::{
23+
construct_runtime, parameter_types,
24+
traits::{ConstU32, ConstU64},
25+
weights::Weight,
26+
};
2227
use sp_core::sr25519::Signature;
2328
use sp_runtime::{
2429
testing::{Header, H256},
@@ -78,7 +83,7 @@ impl frame_system::Config for TestRuntime {
7883
type BlockLength = ();
7984
type SS58Prefix = ();
8085
type OnSetCode = ();
81-
type MaxConsumers = frame_support::traits::ConstU32<16>;
86+
type MaxConsumers = ConstU32<16>;
8287
}
8388

8489
parameter_types! {
@@ -92,7 +97,6 @@ impl grandpa::Config for TestRuntime {
9297
type BridgedChain = TestBridgedChain;
9398
type MaxRequests = MaxRequests;
9499
type HeadersToKeep = HeadersToKeep;
95-
type MaxBridgedAuthorities = frame_support::traits::ConstU32<MAX_BRIDGED_AUTHORITIES>;
96100
type WeightInfo = ();
97101
}
98102

@@ -118,6 +122,14 @@ impl Chain for TestBridgedChain {
118122
}
119123
}
120124

125+
impl ChainWithGrandpa for TestBridgedChain {
126+
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = "";
127+
const MAX_AUTHORITIES_COUNT: u32 = MAX_BRIDGED_AUTHORITIES;
128+
const REASONABLE_HEADERS_IN_JUSTIFICATON_ANCESTRY: u32 = 8;
129+
const MAX_HEADER_SIZE: u32 = 256;
130+
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32 = 64;
131+
}
132+
121133
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
122134
sp_io::TestExternalities::new(Default::default()).execute_with(test)
123135
}

modules/grandpa/src/storage_types.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,32 @@
1818
1919
use crate::Config;
2020

21-
use bp_header_chain::AuthoritySet;
21+
use bp_header_chain::{AuthoritySet, ChainWithGrandpa};
2222
use codec::{Decode, Encode, MaxEncodedLen};
2323
use frame_support::{traits::Get, BoundedVec, RuntimeDebugNoBound};
2424
use scale_info::TypeInfo;
2525
use sp_finality_grandpa::{AuthorityId, AuthorityList, AuthorityWeight, SetId};
26+
use sp_std::marker::PhantomData;
2627

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

32+
/// Adapter for using `T::BridgedChain::MAX_BRIDGED_AUTHORITIES` in `BoundedVec`.
33+
pub struct StoredAuthorityListLimit<T, I>(PhantomData<(T, I)>);
34+
35+
impl<T: Config<I>, I: 'static> Get<u32> for StoredAuthorityListLimit<T, I> {
36+
fn get() -> u32 {
37+
T::BridgedChain::MAX_AUTHORITIES_COUNT
38+
}
39+
}
40+
3141
/// A bounded GRANDPA Authority List and ID.
3242
#[derive(Clone, Decode, Encode, Eq, TypeInfo, MaxEncodedLen, RuntimeDebugNoBound)]
3343
#[scale_info(skip_type_params(T, I))]
3444
pub struct StoredAuthoritySet<T: Config<I>, I: 'static> {
3545
/// List of GRANDPA authorities for the current round.
36-
pub authorities: StoredAuthorityList<<T as Config<I>>::MaxBridgedAuthorities>,
46+
pub authorities: StoredAuthorityList<StoredAuthorityListLimit<T, I>>,
3747
/// Monotonic identifier of the current GRANDPA authority set.
3848
pub set_id: SetId,
3949
}
@@ -60,7 +70,7 @@ impl<T: Config<I>, I: 'static> StoredAuthoritySet<T, I> {
6070
let single_authority_max_encoded_len =
6171
<(AuthorityId, AuthorityWeight)>::max_encoded_len() as u64;
6272
let extra_authorities =
63-
T::MaxBridgedAuthorities::get().saturating_sub(self.authorities.len() as _);
73+
T::BridgedChain::MAX_AUTHORITIES_COUNT.saturating_sub(self.authorities.len() as _);
6474
single_authority_max_encoded_len.saturating_mul(extra_authorities as u64)
6575
}
6676
}

0 commit comments

Comments
 (0)