ChainWithGrandpa in primitives (#1885)

* ChainWithGrandpa in primitives

* clippy ++ spelling

* fix benchmarks comppilation
This commit is contained in:
Svyatoslav Nikolsky
2023-02-16 15:20:45 +03:00
committed by Bastian Köcher
parent 914213d0af
commit c6c39be967
33 changed files with 421 additions and 139 deletions
+1 -2
View File
@@ -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};
@@ -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 {
+5 -9
View File
@@ -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};
@@ -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.
///
@@ -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;
}
@@ -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
+15 -3
View File
@@ -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},
@@ -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! {
@@ -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 = ();
}
@@ -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)
}
+13 -3
View File
@@ -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,
}
@@ -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)
}
}