// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see .
//! Runtime module which takes care of dispatching messages received over the bridge.
//!
//! The messages are interpreted directly as runtime `Call`. We attempt to decode
//! them and then dispatch as usual. To prevent compatibility issues, the Calls have
//! to include a `spec_version`. This will be checked before dispatch. In the case of
//! a successful dispatch an event is emitted.
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
// Generated by `decl_event!`
#![allow(clippy::unused_unit)]
use bp_message_dispatch::{CallOrigin, MessageDispatch, MessagePayload, SpecVersion};
use bp_runtime::{
derive_account_id,
messages::{DispatchFeePayment, MessageDispatchResult},
ChainId, SourceAccount,
};
use codec::Encode;
use frame_support::{
dispatch::Dispatchable,
ensure,
traits::{Filter, Get},
weights::{extract_actual_weight, GetDispatchInfo},
};
use frame_system::RawOrigin;
use sp_runtime::traits::{BadOrigin, Convert, IdentifyAccount, MaybeDisplay, Verify};
use sp_std::{fmt::Debug, prelude::*};
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[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, MessageIdOf),
/// Message has been rejected by dispatcher because of spec version mismatch.
/// Last two arguments are: expected and passed spec version.
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, MessageIdOf, Weight, Weight),
/// Message signature mismatch.
MessageSignatureMismatch(ChainId, MessageIdOf),
/// We have failed to decode Call from the message.
MessageCallDecodeFailed(ChainId, MessageIdOf),
/// The call from the message has been rejected by the call filter.
MessageCallRejected(ChainId, MessageIdOf),
/// The origin account has failed to pay fee for dispatching the message.
MessageDispatchPaymentFailed(
ChainId,
MessageIdOf,
::AccountId,
Weight,
),
/// Message has been dispatched with given result.
MessageDispatched(ChainId, MessageIdOf, DispatchResult),
/// Phantom member, never used. Needed to handle multiple pallet instances.
_Dummy(PhantomData),
}
}
impl, I: 'static> MessageDispatch for Pallet {
type Message =
MessagePayload;
fn dispatch_weight(message: &Self::Message) -> bp_message_dispatch::Weight {
message.weight
}
fn dispatch Result<(), ()>>(
source_chain: ChainId,
target_chain: ChainId,
id: T::MessageId,
message: Result,
pay_dispatch_fee: P,
) -> MessageDispatchResult {
// emit special even if message has been rejected by external component
let message = match message {
Ok(message) => message,
Err(_) => {
log::trace!(
target: "runtime::bridge-dispatch",
"Message {:?}/{:?}: rejected before actual dispatch",
source_chain,
id,
);
Self::deposit_event(Event::MessageRejected(source_chain, id));
return MessageDispatchResult {
dispatch_result: false,
unspent_weight: 0,
dispatch_fee_paid_during_dispatch: false,
};
}
};
// verify spec version
// (we want it to be the same, because otherwise we may decode Call improperly)
let mut dispatch_result = MessageDispatchResult {
dispatch_result: false,
unspent_weight: message.weight,
dispatch_fee_paid_during_dispatch: false,
};
let expected_version = ::Version::get().spec_version;
if message.spec_version != expected_version {
log::trace!(
"Message {:?}/{:?}: spec_version mismatch. Expected {:?}, got {:?}",
source_chain,
id,
expected_version,
message.spec_version,
);
Self::deposit_event(Event::MessageVersionSpecMismatch(
source_chain,
id,
expected_version,
message.spec_version,
));
return dispatch_result;
}
// now that we have spec version checked, let's decode the call
let call = match message.call.into() {
Ok(call) => call,
Err(_) => {
log::trace!(
target: "runtime::bridge-dispatch",
"Failed to decode Call from message {:?}/{:?}",
source_chain,
id,
);
Self::deposit_event(Event::MessageCallDecodeFailed(source_chain, id));
return dispatch_result;
}
};
// prepare dispatch origin
let origin_account = match message.origin {
CallOrigin::SourceRoot => {
let hex_id = derive_account_id::(source_chain, SourceAccount::Root);
let target_id = T::AccountIdConverter::convert(hex_id);
log::trace!(target: "runtime::bridge-dispatch", "Root Account: {:?}", &target_id);
target_id
}
CallOrigin::TargetAccount(source_account_id, target_public, target_signature) => {
let digest = account_ownership_digest(
&call,
source_account_id,
message.spec_version,
source_chain,
target_chain,
);
let target_account = target_public.into_account();
if !target_signature.verify(&digest[..], &target_account) {
log::trace!(
target: "runtime::bridge-dispatch",
"Message {:?}/{:?}: origin proof is invalid. Expected account: {:?} from signature: {:?}",
source_chain,
id,
target_account,
target_signature,
);
Self::deposit_event(Event::MessageSignatureMismatch(source_chain, id));
return dispatch_result;
}
log::trace!(target: "runtime::bridge-dispatch", "Target Account: {:?}", &target_account);
target_account
}
CallOrigin::SourceAccount(source_account_id) => {
let hex_id = derive_account_id(source_chain, SourceAccount::Account(source_account_id));
let target_id = T::AccountIdConverter::convert(hex_id);
log::trace!(target: "runtime::bridge-dispatch", "Source Account: {:?}", &target_id);
target_id
}
};
// filter the call
if !T::CallFilter::filter(&call) {
log::trace!(
target: "runtime::bridge-dispatch",
"Message {:?}/{:?}: the call ({:?}) is rejected by filter",
source_chain,
id,
call,
);
Self::deposit_event(Event::MessageCallRejected(source_chain, id));
return dispatch_result;
}
// verify weight
// (we want passed weight to be at least equal to pre-dispatch weight of the call
// because otherwise Calls may be dispatched at lower price)
let dispatch_info = call.get_dispatch_info();
let expected_weight = dispatch_info.weight;
if message.weight < expected_weight {
log::trace!(
target: "runtime::bridge-dispatch",
"Message {:?}/{:?}: passed weight is too low. Expected at least {:?}, got {:?}",
source_chain,
id,
expected_weight,
message.weight,
);
Self::deposit_event(Event::MessageWeightMismatch(
source_chain,
id,
expected_weight,
message.weight,
));
return dispatch_result;
}
// pay dispatch fee right before dispatch
let pay_dispatch_fee_at_target_chain = message.dispatch_fee_payment == DispatchFeePayment::AtTargetChain;
if pay_dispatch_fee_at_target_chain && pay_dispatch_fee(&origin_account, message.weight).is_err() {
log::trace!(
target: "runtime::bridge-dispatch",
"Failed to pay dispatch fee for dispatching message {:?}/{:?} with weight {}",
source_chain,
id,
message.weight,
);
Self::deposit_event(Event::MessageDispatchPaymentFailed(
source_chain,
id,
origin_account,
message.weight,
));
return dispatch_result;
}
dispatch_result.dispatch_fee_paid_during_dispatch = pay_dispatch_fee_at_target_chain;
// finally dispatch message
let origin = RawOrigin::Signed(origin_account).into();
log::trace!(target: "runtime::bridge-dispatch", "Message being dispatched is: {:.4096?}", &call);
let result = call.dispatch(origin);
let actual_call_weight = extract_actual_weight(&result, &dispatch_info);
dispatch_result.dispatch_result = result.is_ok();
dispatch_result.unspent_weight = message.weight.saturating_sub(actual_call_weight);
log::trace!(
target: "runtime::bridge-dispatch",
"Message {:?}/{:?} has been dispatched. Weight: {} of {}. Result: {:?}. Call dispatch result: {:?}",
source_chain,
id,
actual_call_weight,
message.weight,
dispatch_result,
result,
);
Self::deposit_event(Event::MessageDispatched(
source_chain,
id,
result.map(drop).map_err(|e| e.error),
));
dispatch_result
}
}
/// Check if the message is allowed to be dispatched on the target chain given the sender's origin
/// on the source chain.
///
/// For example, if a message is sent from a "regular" account on the source chain it will not be
/// allowed to be dispatched as Root on the target chain. This is a useful check to do on the source
/// chain _before_ sending a message whose dispatch will be rejected on the target chain.
pub fn verify_message_origin(
sender_origin: &RawOrigin,
message: &MessagePayload,
) -> Result