use alloc::vec::Vec;
use codec::{Decode, Encode};
use core::marker::PhantomData;
use frame_support::traits::Get;
use frame_system::pallet_prelude::BlockNumberFor;
use polkadot_primitives::Id as ParaId;
use polkadot_runtime_parachains::{
configuration::{self, HostConfiguration},
dmp, FeeTracker,
};
use sp_runtime::FixedPointNumber;
use xcm::prelude::*;
use xcm_builder::InspectMessageQueues;
use SendError::*;
pub trait PriceForMessageDelivery {
type Id;
fn price_for_delivery(id: Self::Id, message: &Xcm<()>) -> Assets;
}
impl PriceForMessageDelivery for () {
type Id = ();
fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> Assets {
Assets::new()
}
}
pub struct NoPriceForMessageDelivery<Id>(PhantomData<Id>);
impl<Id> PriceForMessageDelivery for NoPriceForMessageDelivery<Id> {
type Id = Id;
fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> Assets {
Assets::new()
}
}
pub struct ConstantPrice<T>(core::marker::PhantomData<T>);
impl<T: Get<Assets>> PriceForMessageDelivery for ConstantPrice<T> {
type Id = ();
fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> Assets {
T::get()
}
}
pub struct ExponentialPrice<A, B, M, F>(core::marker::PhantomData<(A, B, M, F)>);
impl<A: Get<AssetId>, B: Get<u128>, M: Get<u128>, F: FeeTracker> PriceForMessageDelivery
for ExponentialPrice<A, B, M, F>
{
type Id = F::Id;
fn price_for_delivery(id: Self::Id, msg: &Xcm<()>) -> Assets {
let msg_fee = (msg.encoded_size() as u128).saturating_mul(M::get());
let fee_sum = B::get().saturating_add(msg_fee);
let amount = F::get_fee_factor(id).saturating_mul_int(fee_sum);
(A::get(), amount).into()
}
}
pub struct ChildParachainRouter<T, W, P>(PhantomData<(T, W, P)>);
impl<T: configuration::Config + dmp::Config, W: xcm::WrapVersion, P> SendXcm
for ChildParachainRouter<T, W, P>
where
P: PriceForMessageDelivery<Id = ParaId>,
{
type Ticket = (HostConfiguration<BlockNumberFor<T>>, ParaId, Vec<u8>);
fn validate(
dest: &mut Option<Location>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(HostConfiguration<BlockNumberFor<T>>, ParaId, Vec<u8>)> {
let d = dest.take().ok_or(MissingArgument)?;
let id = if let (0, [Parachain(id)]) = d.unpack() {
*id
} else {
*dest = Some(d);
return Err(NotApplicable)
};
let xcm = msg.take().ok_or(MissingArgument)?;
let config = configuration::ActiveConfig::<T>::get();
let para = id.into();
let price = P::price_for_delivery(para, &xcm);
let versioned_xcm = W::wrap_version(&d, xcm).map_err(|()| DestinationUnsupported)?;
versioned_xcm.validate_xcm_nesting().map_err(|()| ExceedsMaxMessageSize)?;
let blob = versioned_xcm.encode();
dmp::Pallet::<T>::can_queue_downward_message(&config, ¶, &blob)
.map_err(Into::<SendError>::into)?;
Ok(((config, para, blob), price))
}
fn deliver(
(config, para, blob): (HostConfiguration<BlockNumberFor<T>>, ParaId, Vec<u8>),
) -> Result<XcmHash, SendError> {
let hash = sp_io::hashing::blake2_256(&blob[..]);
dmp::Pallet::<T>::queue_downward_message(&config, para, blob)
.map(|()| hash)
.map_err(|_| SendError::Transport(&"Error placing into DMP queue"))
}
}
impl<T: dmp::Config, W, P> InspectMessageQueues for ChildParachainRouter<T, W, P> {
fn clear_messages() {
let _ = dmp::DownwardMessageQueues::<T>::clear(u32::MAX, None);
}
fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)> {
dmp::DownwardMessageQueues::<T>::iter()
.map(|(para_id, messages)| {
let decoded_messages: Vec<VersionedXcm<()>> = messages
.iter()
.map(|downward_message| {
let message = VersionedXcm::<()>::decode(&mut &downward_message.msg[..]).unwrap();
log::trace!(target: "xcm::DownwardMessageQueues::get_messages", "Message: {:?}, sent at: {:?}", message, downward_message.sent_at);
message
})
.collect();
(VersionedLocation::from(Location::from(Parachain(para_id.into()))), decoded_messages)
})
.collect()
}
}
#[cfg(feature = "runtime-benchmarks")]
pub struct ToParachainDeliveryHelper<
XcmConfig,
ExistentialDeposit,
PriceForDelivery,
ParaId,
ToParaIdHelper,
>(
core::marker::PhantomData<(
XcmConfig,
ExistentialDeposit,
PriceForDelivery,
ParaId,
ToParaIdHelper,
)>,
);
#[cfg(feature = "runtime-benchmarks")]
impl<
XcmConfig: xcm_executor::Config,
ExistentialDeposit: Get<Option<Asset>>,
PriceForDelivery: PriceForMessageDelivery<Id = ParaId>,
Parachain: Get<ParaId>,
ToParachainHelper: EnsureForParachain,
> xcm_builder::EnsureDelivery
for ToParachainDeliveryHelper<
XcmConfig,
ExistentialDeposit,
PriceForDelivery,
Parachain,
ToParachainHelper,
>
{
fn ensure_successful_delivery(
origin_ref: &Location,
dest: &Location,
fee_reason: xcm_executor::traits::FeeReason,
) -> (Option<xcm_executor::FeesMode>, Option<Assets>) {
use xcm_executor::{
traits::{FeeManager, TransactAsset},
FeesMode,
};
if let Some(Parachain(para_id)) = dest.first_interior() {
if ParaId::from(*para_id) != Parachain::get().into() {
return (None, None)
}
} else {
return (None, None)
}
let mut fees_mode = None;
if !XcmConfig::FeeManager::is_waived(Some(origin_ref), fee_reason) {
if let Some(ed) = ExistentialDeposit::get() {
XcmConfig::AssetTransactor::deposit_asset(&ed, &origin_ref, None).unwrap();
}
let overestimated_xcm = alloc::vec![ClearOrigin; 128].into();
let overestimated_fees =
PriceForDelivery::price_for_delivery(Parachain::get(), &overestimated_xcm);
for fee in overestimated_fees.inner() {
XcmConfig::AssetTransactor::deposit_asset(&fee, &origin_ref, None).unwrap();
}
ToParachainHelper::ensure(Parachain::get());
fees_mode = Some(FeesMode { jit_withdraw: true });
}
(fees_mode, None)
}
}
#[cfg(feature = "runtime-benchmarks")]
pub trait EnsureForParachain {
fn ensure(para_id: ParaId);
}
#[cfg(feature = "runtime-benchmarks")]
impl EnsureForParachain for () {
fn ensure(_: ParaId) {
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::integration_tests::new_test_ext;
use alloc::vec;
use frame_support::{assert_ok, parameter_types};
use polkadot_runtime_parachains::FeeTracker;
use sp_runtime::FixedU128;
use xcm::MAX_XCM_DECODE_DEPTH;
parameter_types! {
pub const BaseDeliveryFee: u128 = 300_000_000;
pub const TransactionByteFee: u128 = 1_000_000;
pub FeeAssetId: AssetId = AssetId(Here.into());
}
struct TestFeeTracker;
impl FeeTracker for TestFeeTracker {
type Id = ParaId;
fn get_fee_factor(_: Self::Id) -> FixedU128 {
FixedU128::from_rational(101, 100)
}
fn increase_fee_factor(_: Self::Id, _: FixedU128) -> FixedU128 {
FixedU128::from_rational(101, 100)
}
fn decrease_fee_factor(_: Self::Id) -> FixedU128 {
FixedU128::from_rational(101, 100)
}
}
type TestExponentialPrice =
ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, TestFeeTracker>;
#[test]
fn exponential_price_correct_price_calculation() {
let id: ParaId = 123.into();
let b: u128 = BaseDeliveryFee::get();
let m: u128 = TransactionByteFee::get();
let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + m);
assert_eq!(
TestExponentialPrice::price_for_delivery(id, &Xcm(vec![])),
(FeeAssetId::get(), result).into()
);
let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (2 * m));
assert_eq!(
TestExponentialPrice::price_for_delivery(id, &Xcm(vec![ClearOrigin])),
(FeeAssetId::get(), result).into()
);
let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (4 * m));
assert_eq!(
TestExponentialPrice::price_for_delivery(
id,
&Xcm(vec![SetAppendix(Xcm(vec![ClearOrigin]))])
),
(FeeAssetId::get(), result).into()
);
}
#[test]
fn child_parachain_router_validate_nested_xcm_works() {
let dest = Parachain(5555);
type Router = ChildParachainRouter<
crate::integration_tests::Test,
(),
NoPriceForMessageDelivery<ParaId>,
>;
let mut good = Xcm(vec![ClearOrigin]);
for _ in 0..MAX_XCM_DECODE_DEPTH - 1 {
good = Xcm(vec![SetAppendix(good)]);
}
new_test_ext().execute_with(|| {
configuration::ActiveConfig::<crate::integration_tests::Test>::mutate(|c| {
c.max_downward_message_size = u32::MAX;
});
assert_ok!(<Router as SendXcm>::validate(
&mut Some(dest.into()),
&mut Some(good.clone())
));
let bad = Xcm(vec![SetAppendix(good)]);
assert_eq!(
Err(ExceedsMaxMessageSize),
<Router as SendXcm>::validate(&mut Some(dest.into()), &mut Some(bad))
);
});
}
}