diff --git a/bridges/bin/rialto/runtime/src/lib.rs b/bridges/bin/rialto/runtime/src/lib.rs index 0c9a973185..0674965da5 100644 --- a/bridges/bin/rialto/runtime/src/lib.rs +++ b/bridges/bin/rialto/runtime/src/lib.rs @@ -953,7 +953,7 @@ impl_runtime_apis! { Default::default(), ); - prepare_message_proof::( + prepare_message_proof::( params, make_millau_message_key, make_millau_outbound_lane_data_key, @@ -986,7 +986,7 @@ impl_runtime_apis! { }; use sp_runtime::traits::Header; - prepare_message_delivery_proof::( + prepare_message_delivery_proof::( params, |lane_id| pallet_message_lane::storage_keys::inbound_lane_data_key::< Runtime, diff --git a/bridges/bin/runtime-common/src/messages_benchmarking.rs b/bridges/bin/runtime-common/src/messages_benchmarking.rs index 73bdd5755f..56b427cb0e 100644 --- a/bridges/bin/runtime-common/src/messages_benchmarking.rs +++ b/bridges/bin/runtime-common/src/messages_benchmarking.rs @@ -62,7 +62,7 @@ pub fn ed25519_sign(target_call: &impl Encode, source_account_id: &impl Encode) } /// Prepare proof of messages for the `receive_messages_proof` call. -pub fn prepare_message_proof( +pub fn prepare_message_proof( params: MessageProofParams, make_bridged_message_storage_key: MM, make_bridged_outbound_lane_data_key: ML, @@ -73,7 +73,8 @@ pub fn prepare_message_proof( where B: MessageBridge, H: Hasher, - R: pallet_finality_verifier::Config, + R: pallet_finality_verifier::Config, + FI: 'static, ::Hash: Into>>, MM: Fn(MessageKey) -> Vec, ML: Fn(LaneId) -> Vec, @@ -129,7 +130,7 @@ where // prepare Bridged chain header and insert it into the Substrate pallet let bridged_header = make_bridged_header(root); let bridged_header_hash = bridged_header.hash(); - pallet_finality_verifier::initialize_for_benchmarks::(bridged_header); + pallet_finality_verifier::initialize_for_benchmarks::(bridged_header); ( FromBridgedChainMessagesProof { @@ -146,7 +147,7 @@ where } /// Prepare proof of messages delivery for the `receive_messages_delivery_proof` call. -pub fn prepare_message_delivery_proof( +pub fn prepare_message_delivery_proof( params: MessageDeliveryProofParams>>, make_bridged_inbound_lane_data_key: ML, make_bridged_header: MH, @@ -154,7 +155,8 @@ pub fn prepare_message_delivery_proof( where B: MessageBridge, H: Hasher, - R: pallet_finality_verifier::Config, + R: pallet_finality_verifier::Config, + FI: 'static, ::Hash: Into>>, ML: Fn(LaneId) -> Vec, MH: Fn(H::Out) -> ::Header, @@ -181,7 +183,7 @@ where // prepare Bridged chain header and insert it into the Substrate pallet let bridged_header = make_bridged_header(root); let bridged_header_hash = bridged_header.hash(); - pallet_finality_verifier::initialize_for_benchmarks::(bridged_header); + pallet_finality_verifier::initialize_for_benchmarks::(bridged_header); FromBridgedChainMessagesDeliveryProof { bridged_header_hash: bridged_header_hash.into(), diff --git a/bridges/modules/finality-verifier/src/lib.rs b/bridges/modules/finality-verifier/src/lib.rs index a072fc537b..8127daba3e 100644 --- a/bridges/modules/finality-verifier/src/lib.rs +++ b/bridges/modules/finality-verifier/src/lib.rs @@ -55,13 +55,13 @@ mod mock; pub use pallet::*; /// Block number of the bridged chain. -pub type BridgedBlockNumber = BlockNumberOf<::BridgedChain>; +pub type BridgedBlockNumber = BlockNumberOf<>::BridgedChain>; /// Block hash of the bridged chain. -pub type BridgedBlockHash = HashOf<::BridgedChain>; +pub type BridgedBlockHash = HashOf<>::BridgedChain>; /// Hasher of the bridged chain. -pub type BridgedBlockHasher = HasherOf<::BridgedChain>; +pub type BridgedBlockHasher = HasherOf<>::BridgedChain>; /// Header of the bridged chain. -pub type BridgedHeader = HeaderOf<::BridgedChain>; +pub type BridgedHeader = HeaderOf<>::BridgedChain>; #[frame_support::pallet] pub mod pallet { @@ -70,7 +70,7 @@ pub mod pallet { use frame_system::pallet_prelude::*; #[pallet::config] - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config { /// The chain we are bridging to here. type BridgedChain: Chain; @@ -85,12 +85,12 @@ pub mod pallet { } #[pallet::pallet] - pub struct Pallet(PhantomData); + pub struct Pallet(PhantomData<(T, I)>); #[pallet::hooks] - impl Hooks> for Pallet { + impl, I: 'static> Hooks> for Pallet { fn on_initialize(_n: T::BlockNumber) -> frame_support::weights::Weight { - >::mutate(|count| *count = count.saturating_sub(1)); + >::mutate(|count| *count = count.saturating_sub(1)); (0_u64) .saturating_add(T::DbWeight::get().reads(1)) @@ -99,7 +99,7 @@ pub mod pallet { } #[pallet::call] - impl Pallet { + impl, I: 'static> Pallet { /// Verify a target header is finalized according to the given finality proof. /// /// It will use the underlying storage pallet to fetch information about the current @@ -110,21 +110,21 @@ pub mod pallet { #[pallet::weight(0)] pub fn submit_finality_proof( origin: OriginFor, - finality_target: BridgedHeader, + finality_target: BridgedHeader, justification: Vec, ) -> DispatchResultWithPostInfo { - ensure_operational::()?; + ensure_operational::()?; let _ = ensure_signed(origin)?; ensure!( Self::request_count() < T::MaxRequests::get(), - >::TooManyRequests + >::TooManyRequests ); let (hash, number) = (finality_target.hash(), finality_target.number()); log::trace!("Going to try and finalize header {:?}", finality_target); - let best_finalized = >::get(>::get()).expect( + let best_finalized = >::get(>::get()).expect( "In order to reach this point the bridge must have been initialized. Afterwards, every time `BestFinalized` is updated `ImportedHeaders` is also updated. Therefore `ImportedHeaders` must contain an entry for `BestFinalized`.", @@ -132,14 +132,14 @@ pub mod pallet { // We do a quick check here to ensure that our header chain is making progress and isn't // "travelling back in time" (which could be indicative of something bad, e.g a hard-fork). - ensure!(best_finalized.number() < number, >::OldHeader); + ensure!(best_finalized.number() < number, >::OldHeader); - verify_justification::(&justification, hash, *number)?; + verify_justification::(&justification, hash, *number)?; - try_enact_authority_change::(&finality_target)?; - >::put(hash); - >::insert(hash, finality_target); - >::mutate(|count| *count += 1); + try_enact_authority_change::(&finality_target)?; + >::put(hash); + >::insert(hash, finality_target); + >::mutate(|count| *count += 1); log::info!("Succesfully imported finalized header with hash {:?}!", hash); @@ -158,13 +158,13 @@ pub mod pallet { #[pallet::weight((T::DbWeight::get().reads_writes(2, 5), DispatchClass::Operational))] pub fn initialize( origin: OriginFor, - init_data: super::InitializationData>, + init_data: super::InitializationData>, ) -> DispatchResultWithPostInfo { - ensure_owner_or_root::(origin)?; + ensure_owner_or_root::(origin)?; - let init_allowed = !>::exists(); - ensure!(init_allowed, >::AlreadyInitialized); - initialize_bridge::(init_data.clone()); + let init_allowed = !>::exists(); + ensure!(init_allowed, >::AlreadyInitialized); + initialize_bridge::(init_data.clone()); log::info!( "Pallet has been initialized with the following parameters: {:?}", @@ -179,14 +179,14 @@ pub mod pallet { /// May only be called either by root, or by `ModuleOwner`. #[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))] pub fn set_owner(origin: OriginFor, new_owner: Option) -> DispatchResultWithPostInfo { - ensure_owner_or_root::(origin)?; + ensure_owner_or_root::(origin)?; match new_owner { Some(new_owner) => { - ModuleOwner::::put(&new_owner); + ModuleOwner::::put(&new_owner); log::info!("Setting pallet Owner to: {:?}", new_owner); } None => { - ModuleOwner::::kill(); + ModuleOwner::::kill(); log::info!("Removed Owner of pallet."); } } @@ -199,8 +199,8 @@ pub mod pallet { /// May only be called either by root, or by `ModuleOwner`. #[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))] pub fn set_operational(origin: OriginFor, operational: bool) -> DispatchResultWithPostInfo { - ensure_owner_or_root::(origin)?; - >::put(operational); + ensure_owner_or_root::(origin)?; + >::put(operational); if operational { log::info!("Resuming pallet operations."); @@ -221,23 +221,25 @@ pub mod pallet { /// that the pallet can always make progress. #[pallet::storage] #[pallet::getter(fn request_count)] - pub(super) type RequestCount = StorageValue<_, u32, ValueQuery>; + pub(super) type RequestCount, I: 'static = ()> = StorageValue<_, u32, ValueQuery>; /// Hash of the header used to bootstrap the pallet. #[pallet::storage] - pub(super) type InitialHash = StorageValue<_, BridgedBlockHash, ValueQuery>; + pub(super) type InitialHash, I: 'static = ()> = StorageValue<_, BridgedBlockHash, ValueQuery>; /// Hash of the best finalized header. #[pallet::storage] - pub(super) type BestFinalized = StorageValue<_, BridgedBlockHash, ValueQuery>; + pub(super) type BestFinalized, I: 'static = ()> = StorageValue<_, BridgedBlockHash, ValueQuery>; /// Headers which have been imported into the pallet. #[pallet::storage] - pub(super) type ImportedHeaders = StorageMap<_, Identity, BridgedBlockHash, BridgedHeader>; + pub(super) type ImportedHeaders, I: 'static = ()> = + StorageMap<_, Identity, BridgedBlockHash, BridgedHeader>; /// The current GRANDPA Authority set. #[pallet::storage] - pub(super) type CurrentAuthoritySet = StorageValue<_, bp_header_chain::AuthoritySet, ValueQuery>; + pub(super) type CurrentAuthoritySet, I: 'static = ()> = + StorageValue<_, bp_header_chain::AuthoritySet, ValueQuery>; /// Optional pallet owner. /// @@ -246,20 +248,20 @@ pub mod pallet { /// runtime methods may still be used to do that (i.e. democracy::referendum to update halt /// flag directly or call the `halt_operations`). #[pallet::storage] - pub(super) type ModuleOwner = StorageValue<_, T::AccountId, OptionQuery>; + pub(super) type ModuleOwner, I: 'static = ()> = StorageValue<_, T::AccountId, OptionQuery>; /// If true, all pallet transactions are failed immediately. #[pallet::storage] - pub(super) type IsHalted = StorageValue<_, bool, ValueQuery>; + pub(super) type IsHalted, I: 'static = ()> = StorageValue<_, bool, ValueQuery>; #[pallet::genesis_config] - pub struct GenesisConfig { + pub struct GenesisConfig, I: 'static = ()> { owner: Option, - init_data: Option>>, + init_data: Option>>, } #[cfg(feature = "std")] - impl Default for GenesisConfig { + impl, I: 'static> Default for GenesisConfig { fn default() -> Self { Self { owner: None, @@ -269,24 +271,24 @@ pub mod pallet { } #[pallet::genesis_build] - impl GenesisBuild for GenesisConfig { + impl, I: 'static> GenesisBuild for GenesisConfig { fn build(&self) { if let Some(ref owner) = self.owner { - >::put(owner); + >::put(owner); } if let Some(init_data) = self.init_data.clone() { - initialize_bridge::(init_data); + initialize_bridge::(init_data); } else { // Since the bridge hasn't been initialized we shouldn't allow anyone to perform // transactions. - >::put(true); + >::put(true); } } } #[pallet::error] - pub enum Error { + pub enum Error { /// The given justification is invalid for the given header. InvalidJustification, /// The authority set from the underlying header chain is invalid. @@ -314,20 +316,20 @@ pub mod pallet { /// /// This function does not support forced changes, or scheduled changes with delays /// since these types of changes are indicitive of abnormal behaviour from GRANDPA. - pub(crate) fn try_enact_authority_change( - header: &BridgedHeader, + pub(crate) fn try_enact_authority_change, I: 'static>( + header: &BridgedHeader, ) -> Result<(), sp_runtime::DispatchError> { // We don't support forced changes - at that point governance intervention is required. ensure!( super::find_forced_change(header).is_none(), - >::UnsupportedScheduledChange + >::UnsupportedScheduledChange ); if let Some(change) = super::find_scheduled_change(header) { // GRANDPA only includes a `delay` for forced changes, so this isn't valid. - ensure!(change.delay == Zero::zero(), >::UnsupportedScheduledChange); + ensure!(change.delay == Zero::zero(), >::UnsupportedScheduledChange); - let current_set_id = >::get().set_id; + let current_set_id = >::get().set_id; // TODO [#788]: Stop manually increasing the `set_id` here. let next_authorities = bp_header_chain::AuthoritySet { authorities: change.next_authorities, @@ -336,7 +338,7 @@ pub mod pallet { // Since our header schedules a change and we know the delay is 0, it must also enact // the change. - >::put(&next_authorities); + >::put(&next_authorities); log::info!( "Transitioned from authority set {} to {}! New authorities are: {:?}", @@ -352,22 +354,22 @@ pub mod pallet { /// Verify a GRANDPA justification (finality proof) for a given header. /// /// Will use the GRANDPA current authorities known to the pallet. - pub(crate) fn verify_justification( + pub(crate) fn verify_justification, I: 'static>( justification: &[u8], - hash: BridgedBlockHash, - number: BridgedBlockNumber, + hash: BridgedBlockHash, + number: BridgedBlockNumber, ) -> Result<(), sp_runtime::DispatchError> { use bp_header_chain::justification::verify_justification; - let authority_set = >::get(); - let voter_set = VoterSet::new(authority_set.authorities).ok_or(>::InvalidAuthoritySet)?; + let authority_set = >::get(); + let voter_set = VoterSet::new(authority_set.authorities).ok_or(>::InvalidAuthoritySet)?; let set_id = authority_set.set_id; Ok( - verify_justification::>((hash, number), set_id, voter_set, &justification).map_err( + verify_justification::>((hash, number), set_id, voter_set, &justification).map_err( |e| { log::error!("Received invalid justification for {:?}: {:?}", hash, e); - >::InvalidJustification + >::InvalidJustification }, )?, ) @@ -375,7 +377,9 @@ pub mod pallet { /// Since this writes to storage with no real checks this should only be used in functions that /// were called by a trusted origin. - pub(crate) fn initialize_bridge(init_params: super::InitializationData>) { + pub(crate) fn initialize_bridge, I: 'static>( + init_params: super::InitializationData>, + ) { let super::InitializationData { header, authority_list, @@ -384,44 +388,44 @@ pub mod pallet { } = init_params; let initial_hash = header.hash(); - >::put(initial_hash); - >::put(initial_hash); - >::insert(initial_hash, header); + >::put(initial_hash); + >::put(initial_hash); + >::insert(initial_hash, header); let authority_set = bp_header_chain::AuthoritySet::new(authority_list, set_id); - >::put(authority_set); + >::put(authority_set); - >::put(is_halted); + >::put(is_halted); } /// Ensure that the origin is either root, or `ModuleOwner`. - fn ensure_owner_or_root(origin: T::Origin) -> Result<(), BadOrigin> { + fn ensure_owner_or_root, I: 'static>(origin: T::Origin) -> Result<(), BadOrigin> { match origin.into() { Ok(RawOrigin::Root) => Ok(()), - Ok(RawOrigin::Signed(ref signer)) if Some(signer) == >::get().as_ref() => Ok(()), + Ok(RawOrigin::Signed(ref signer)) if Some(signer) == >::get().as_ref() => Ok(()), _ => Err(BadOrigin), } } /// Ensure that the pallet is in operational mode (not halted). - fn ensure_operational() -> Result<(), Error> { - if >::get() { - Err(>::Halted) + fn ensure_operational, I: 'static>() -> Result<(), Error> { + if >::get() { + Err(>::Halted) } else { Ok(()) } } } -impl Pallet { +impl, I: 'static> Pallet { /// Get the best finalized header the pallet knows of. /// /// Returns a dummy header if there is no best header. This can only happen /// if the pallet has not been initialized yet. - pub fn best_finalized() -> BridgedHeader { - let hash = >::get(); - >::get(hash).unwrap_or_else(|| { - >::new( + pub fn best_finalized() -> BridgedHeader { + let hash = >::get(); + >::get(hash).unwrap_or_else(|| { + >::new( Default::default(), Default::default(), Default::default(), @@ -432,21 +436,21 @@ impl Pallet { } /// Check if a particular header is known to the bridge pallet. - pub fn is_known_header(hash: BridgedBlockHash) -> bool { - >::contains_key(hash) + pub fn is_known_header(hash: BridgedBlockHash) -> bool { + >::contains_key(hash) } /// Verify that the passed storage proof is valid, given it is crafted using /// known finalized header. If the proof is valid, then the `parse` callback /// is called and the function returns its result. pub fn parse_finalized_storage_proof( - hash: BridgedBlockHash, + hash: BridgedBlockHash, storage_proof: sp_trie::StorageProof, - parse: impl FnOnce(bp_runtime::StorageProofChecker>) -> R, + parse: impl FnOnce(bp_runtime::StorageProofChecker>) -> R, ) -> Result { - let header = >::get(hash).ok_or(Error::::UnknownHeader)?; + let header = >::get(hash).ok_or(Error::::UnknownHeader)?; let storage_proof_checker = bp_runtime::StorageProofChecker::new(*header.state_root(), storage_proof) - .map_err(|_| Error::::StorageRootMismatch)?; + .map_err(|_| Error::::StorageRootMismatch)?; Ok(parse(storage_proof_checker)) } @@ -504,8 +508,8 @@ pub(crate) fn find_forced_change( /// (Re)initialize bridge with given header for using it in external benchmarks. #[cfg(feature = "runtime-benchmarks")] -pub fn initialize_for_benchmarks(header: BridgedHeader) { - initialize_bridge::(InitializationData { +pub fn initialize_for_benchmarks, I: 'static>(header: BridgedHeader) { + initialize_bridge::(InitializationData { header, authority_list: Vec::new(), // we don't verify any proofs in external benchmarks set_id: 0, @@ -603,7 +607,7 @@ mod tests { run_test(|| { assert_eq!( BestFinalized::::get(), - BridgedBlockHash::::default() + BridgedBlockHash::::default() ); assert_eq!(Module::::best_finalized(), test_header(0)); diff --git a/bridges/modules/finality-verifier/src/mock.rs b/bridges/modules/finality-verifier/src/mock.rs index f80a374f37..59f658e539 100644 --- a/bridges/modules/finality-verifier/src/mock.rs +++ b/bridges/modules/finality-verifier/src/mock.rs @@ -26,9 +26,9 @@ use sp_runtime::{ }; pub type AccountId = u64; -pub type TestHeader = crate::BridgedHeader; -pub type TestNumber = crate::BridgedBlockNumber; -pub type TestHash = crate::BridgedBlockHash; +pub type TestHeader = crate::BridgedHeader; +pub type TestNumber = crate::BridgedBlockNumber; +pub type TestHash = crate::BridgedBlockHash; type Block = frame_system::mocking::MockBlock; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic;