diff --git a/bridges/bin/millau/runtime/src/rialto_messages.rs b/bridges/bin/millau/runtime/src/rialto_messages.rs index 2ebf17686f..5b3d08688c 100644 --- a/bridges/bin/millau/runtime/src/rialto_messages.rs +++ b/bridges/bin/millau/runtime/src/rialto_messages.rs @@ -69,7 +69,7 @@ pub type FromRialtoMessageDispatch = messages::target::FromBridgedChainMessageDi WithRialtoMessageBridge, crate::Runtime, pallet_balances::Pallet, - pallet_bridge_dispatch::DefaultInstance, + (), >; /// Millau <-> Rialto message bridge. diff --git a/bridges/bin/rialto/runtime/src/millau_messages.rs b/bridges/bin/rialto/runtime/src/millau_messages.rs index 15e1d52f25..ac79d8eca6 100644 --- a/bridges/bin/rialto/runtime/src/millau_messages.rs +++ b/bridges/bin/rialto/runtime/src/millau_messages.rs @@ -63,7 +63,7 @@ pub type FromMillauMessageDispatch = messages::target::FromBridgedChainMessageDi WithMillauMessageBridge, crate::Runtime, pallet_balances::Pallet, - pallet_bridge_dispatch::DefaultInstance, + (), >; /// Messages proof for Millau -> Rialto messages. diff --git a/bridges/bin/runtime-common/src/messages.rs b/bridges/bin/runtime-common/src/messages.rs index 08ef582993..974920c528 100644 --- a/bridges/bin/runtime-common/src/messages.rs +++ b/bridges/bin/runtime-common/src/messages.rs @@ -492,15 +492,12 @@ pub mod target { for FromBridgedChainMessageDispatch where BalanceOf>: Saturating + FixedPointOperand, - ThisDispatchInstance: frame_support::traits::Instance, + ThisDispatchInstance: 'static, ThisRuntime: pallet_bridge_dispatch::Config + pallet_transaction_payment::Config, ::OnChargeTransaction: pallet_transaction_payment::OnChargeTransaction>>, ThisCurrency: Currency>, Balance = BalanceOf>>, - >::Event: From< - pallet_bridge_dispatch::RawEvent<(LaneId, MessageNonce), AccountIdOf>, ThisDispatchInstance>, - >, pallet_bridge_dispatch::Pallet: bp_message_dispatch::MessageDispatch< AccountIdOf>, (LaneId, MessageNonce), diff --git a/bridges/modules/dispatch/src/lib.rs b/bridges/modules/dispatch/src/lib.rs index b27295481a..c68f064e9d 100644 --- a/bridges/modules/dispatch/src/lib.rs +++ b/bridges/modules/dispatch/src/lib.rs @@ -26,116 +26,124 @@ // Generated by `decl_event!` #![allow(clippy::unused_unit)] -use bp_message_dispatch::{CallOrigin, MessageDispatch, MessagePayload, SpecVersion, Weight}; +use bp_message_dispatch::{CallOrigin, MessageDispatch, MessagePayload, SpecVersion}; use bp_runtime::{ derive_account_id, messages::{DispatchFeePayment, MessageDispatchResult}, ChainId, SourceAccount, }; -use codec::{Decode, Encode}; +use codec::Encode; use frame_support::{ - decl_event, decl_module, decl_storage, - dispatch::{Dispatchable, Parameter}, + dispatch::Dispatchable, ensure, traits::{Filter, Get}, weights::{extract_actual_weight, GetDispatchInfo}, }; use frame_system::RawOrigin; -use sp_runtime::{ - traits::{BadOrigin, Convert, IdentifyAccount, MaybeDisplay, MaybeSerializeDeserialize, Member, Verify}, - DispatchResult, -}; -use sp_std::{fmt::Debug, marker::PhantomData, prelude::*}; +use sp_runtime::traits::{BadOrigin, Convert, IdentifyAccount, MaybeDisplay, Verify}; +use sp_std::{fmt::Debug, prelude::*}; -/// The module configuration trait. -pub trait Config: frame_system::Config { - /// The overarching event type. - type Event: From> + Into<::Event>; - /// Id of the message. Whenever message is passed to the dispatch module, it emits - /// event with this id + dispatch result. Could be e.g. (LaneId, MessageNonce) if - /// it comes from the messages module. - type MessageId: Parameter; - /// Type of account ID on source chain. - type SourceChainAccountId: Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + Ord + Default; - /// Type of account public key on target chain. - type TargetChainAccountPublic: Parameter + IdentifyAccount; - /// Type of signature that may prove that the message has been signed by - /// owner of `TargetChainAccountPublic`. - type TargetChainSignature: Parameter + Verify; - /// The overarching dispatch call type. - type Call: Parameter - + GetDispatchInfo - + Dispatchable< - Origin = ::Origin, - PostInfo = frame_support::dispatch::PostDispatchInfo, - >; - /// Pre-dispatch filter for incoming calls. - /// - /// The pallet will filter all incoming calls right before they're dispatched. If this filter - /// rejects the call, special event (`Event::MessageCallRejected`) is emitted. - type CallFilter: Filter<>::Call>; - /// The type that is used to wrap the `Self::Call` when it is moved over bridge. - /// - /// The idea behind this is to avoid `Call` conversion/decoding until we'll be sure - /// that all other stuff (like `spec_version`) is ok. If we would try to decode - /// `Call` which has been encoded using previous `spec_version`, then we might end - /// up with decoding error, instead of `MessageVersionSpecMismatch`. - type EncodedCall: Decode + Encode + Into>::Call, ()>>; - /// A type which can be turned into an AccountId from a 256-bit hash. - /// - /// Used when deriving target chain AccountIds from source chain AccountIds. - type AccountIdConverter: sp_runtime::traits::Convert; -} +pub use pallet::*; -decl_storage! { - trait Store for Pallet, I: Instance = DefaultInstance> as Dispatch {} -} +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; -decl_event!( - pub enum Event where - >::MessageId, - AccountId = ::AccountId, - { + #[pallet::config] + pub trait Config: frame_system::Config { + /// The overarching event type. + type Event: From> + IsType<::Event>; + /// Id of the message. Whenever message is passed to the dispatch module, it emits + /// event with this id + dispatch result. Could be e.g. (LaneId, MessageNonce) if + /// it comes from the messages module. + type MessageId: Parameter; + /// Type of account ID on source chain. + type SourceChainAccountId: Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + Ord + Default; + /// Type of account public key on target chain. + type TargetChainAccountPublic: Parameter + IdentifyAccount; + /// Type of signature that may prove that the message has been signed by + /// owner of `TargetChainAccountPublic`. + type TargetChainSignature: Parameter + Verify; + /// The overarching dispatch call type. + type Call: Parameter + + GetDispatchInfo + + Dispatchable< + Origin = ::Origin, + PostInfo = frame_support::dispatch::PostDispatchInfo, + >; + /// Pre-dispatch filter for incoming calls. + /// + /// The pallet will filter all incoming calls right before they're dispatched. If this filter + /// rejects the call, special event (`Event::MessageCallRejected`) is emitted. + type CallFilter: Filter<>::Call>; + /// The type that is used to wrap the `Self::Call` when it is moved over bridge. + /// + /// The idea behind this is to avoid `Call` conversion/decoding until we'll be sure + /// that all other stuff (like `spec_version`) is ok. If we would try to decode + /// `Call` which has been encoded using previous `spec_version`, then we might end + /// up with decoding error, instead of `MessageVersionSpecMismatch`. + type EncodedCall: Decode + Encode + Into>::Call, ()>>; + /// A type which can be turned into an AccountId from a 256-bit hash. + /// + /// Used when deriving target chain AccountIds from source chain AccountIds. + type AccountIdConverter: sp_runtime::traits::Convert; + } + + type MessageIdOf = >::MessageId; + + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(PhantomData<(T, I)>); + + #[pallet::hooks] + impl, I: 'static> Hooks> for Pallet {} + + #[pallet::call] + impl, I: 'static> Pallet {} + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + #[pallet::metadata(::AccountId = "AccountId", MessageIdOf = "MessageId")] + pub enum Event, I: 'static = ()> { /// Message has been rejected before reaching dispatch. - MessageRejected(ChainId, MessageId), + MessageRejected(ChainId, MessageIdOf), /// Message has been rejected by dispatcher because of spec version mismatch. /// Last two arguments are: expected and passed spec version. - MessageVersionSpecMismatch(ChainId, MessageId, SpecVersion, SpecVersion), + MessageVersionSpecMismatch(ChainId, MessageIdOf, SpecVersion, SpecVersion), /// Message has been rejected by dispatcher because of weight mismatch. /// Last two arguments are: expected and passed call weight. - MessageWeightMismatch(ChainId, MessageId, Weight, Weight), + MessageWeightMismatch(ChainId, MessageIdOf, Weight, Weight), /// Message signature mismatch. - MessageSignatureMismatch(ChainId, MessageId), + MessageSignatureMismatch(ChainId, MessageIdOf), /// We have failed to decode Call from the message. - MessageCallDecodeFailed(ChainId, MessageId), + MessageCallDecodeFailed(ChainId, MessageIdOf), /// The call from the message has been rejected by the call filter. - MessageCallRejected(ChainId, MessageId), + MessageCallRejected(ChainId, MessageIdOf), /// The origin account has failed to pay fee for dispatching the message. - MessageDispatchPaymentFailed(ChainId, MessageId, AccountId, Weight), + MessageDispatchPaymentFailed( + ChainId, + MessageIdOf, + ::AccountId, + Weight, + ), /// Message has been dispatched with given result. - MessageDispatched(ChainId, MessageId, DispatchResult), + MessageDispatched(ChainId, MessageIdOf, DispatchResult), /// Phantom member, never used. Needed to handle multiple pallet instances. _Dummy(PhantomData), } -); - -decl_module! { - /// Call Dispatch FRAME Pallet. - pub struct Module, I: Instance = DefaultInstance> for enum Call where origin: T::Origin { - /// Deposit one of this module's events by using the default implementation. - fn deposit_event() = default; - } } -impl, I: Instance> MessageDispatch for Pallet { +impl, I: 'static> MessageDispatch for Pallet { type Message = MessagePayload; - fn dispatch_weight(message: &Self::Message) -> Weight { + fn dispatch_weight(message: &Self::Message) -> bp_message_dispatch::Weight { message.weight } - fn dispatch Result<(), ()>>( + fn dispatch Result<(), ()>>( source_chain: ChainId, target_chain: ChainId, id: T::MessageId, @@ -152,7 +160,7 @@ impl, I: Instance> MessageDispatch for source_chain, id, ); - Self::deposit_event(RawEvent::MessageRejected(source_chain, id)); + Self::deposit_event(Event::MessageRejected(source_chain, id)); return MessageDispatchResult { dispatch_result: false, unspent_weight: 0, @@ -177,7 +185,7 @@ impl, I: Instance> MessageDispatch for expected_version, message.spec_version, ); - Self::deposit_event(RawEvent::MessageVersionSpecMismatch( + Self::deposit_event(Event::MessageVersionSpecMismatch( source_chain, id, expected_version, @@ -196,7 +204,7 @@ impl, I: Instance> MessageDispatch for source_chain, id, ); - Self::deposit_event(RawEvent::MessageCallDecodeFailed(source_chain, id)); + Self::deposit_event(Event::MessageCallDecodeFailed(source_chain, id)); return dispatch_result; } }; @@ -228,7 +236,7 @@ impl, I: Instance> MessageDispatch for target_account, target_signature, ); - Self::deposit_event(RawEvent::MessageSignatureMismatch(source_chain, id)); + Self::deposit_event(Event::MessageSignatureMismatch(source_chain, id)); return dispatch_result; } @@ -252,7 +260,7 @@ impl, I: Instance> MessageDispatch for id, call, ); - Self::deposit_event(RawEvent::MessageCallRejected(source_chain, id)); + Self::deposit_event(Event::MessageCallRejected(source_chain, id)); return dispatch_result; } @@ -270,7 +278,7 @@ impl, I: Instance> MessageDispatch for expected_weight, message.weight, ); - Self::deposit_event(RawEvent::MessageWeightMismatch( + Self::deposit_event(Event::MessageWeightMismatch( source_chain, id, expected_weight, @@ -289,7 +297,7 @@ impl, I: Instance> MessageDispatch for id, message.weight, ); - Self::deposit_event(RawEvent::MessageDispatchPaymentFailed( + Self::deposit_event(Event::MessageDispatchPaymentFailed( source_chain, id, origin_account, @@ -319,7 +327,7 @@ impl, I: Instance> MessageDispatch for result, ); - Self::deposit_event(RawEvent::MessageDispatched( + Self::deposit_event(Event::MessageDispatched( source_chain, id, result.map(drop).map_err(|e| e.error), @@ -397,6 +405,7 @@ mod tests { #![allow(clippy::from_over_into)] use super::*; + use codec::Decode; use frame_support::{parameter_types, weights::Weight}; use frame_system::{EventRecord, Phase}; use sp_core::H256;