use codec::Decode;
use core::{marker::PhantomData, result::Result};
use frame_support::{
dispatch::GetDispatchInfo,
traits::{
fungible::{Balanced, Credit, Inspect},
Get, OnUnbalanced as OnUnbalancedT,
},
weights::{
constants::{WEIGHT_PROOF_SIZE_PER_MB, WEIGHT_REF_TIME_PER_SECOND},
WeightToFee as WeightToFeeT,
},
};
use sp_runtime::traits::{SaturatedConversion, Saturating, Zero};
use xcm::latest::{prelude::*, GetWeight, Weight};
use xcm_executor::{
traits::{WeightBounds, WeightTrader},
AssetsInHolding,
};
pub struct FixedWeightBounds<T, C, M>(PhantomData<(T, C, M)>);
impl<T: Get<Weight>, C: Decode + GetDispatchInfo, M: Get<u32>> WeightBounds<C>
for FixedWeightBounds<T, C, M>
{
fn weight(message: &mut Xcm<C>) -> Result<Weight, ()> {
log::trace!(target: "xcm::weight", "FixedWeightBounds message: {:?}", message);
let mut instructions_left = M::get();
Self::weight_with_limit(message, &mut instructions_left)
}
fn instr_weight(instruction: &mut Instruction<C>) -> Result<Weight, ()> {
Self::instr_weight_with_limit(instruction, &mut u32::max_value())
}
}
impl<T: Get<Weight>, C: Decode + GetDispatchInfo, M> FixedWeightBounds<T, C, M> {
fn weight_with_limit(message: &mut Xcm<C>, instrs_limit: &mut u32) -> Result<Weight, ()> {
let mut r: Weight = Weight::zero();
*instrs_limit = instrs_limit.checked_sub(message.0.len() as u32).ok_or(())?;
for instruction in message.0.iter_mut() {
r = r
.checked_add(&Self::instr_weight_with_limit(instruction, instrs_limit)?)
.ok_or(())?;
}
Ok(r)
}
fn instr_weight_with_limit(
instruction: &mut Instruction<C>,
instrs_limit: &mut u32,
) -> Result<Weight, ()> {
let instr_weight = match instruction {
Transact { ref mut call, .. } => call.ensure_decoded()?.get_dispatch_info().call_weight,
SetErrorHandler(xcm) | SetAppendix(xcm) | ExecuteWithOrigin { xcm, .. } =>
Self::weight_with_limit(xcm, instrs_limit)?,
_ => Weight::zero(),
};
T::get().checked_add(&instr_weight).ok_or(())
}
}
pub struct WeightInfoBounds<W, C, M>(PhantomData<(W, C, M)>);
impl<W, C, M> WeightBounds<C> for WeightInfoBounds<W, C, M>
where
W: XcmWeightInfo<C>,
C: Decode + GetDispatchInfo,
M: Get<u32>,
Instruction<C>: xcm::latest::GetWeight<W>,
{
fn weight(message: &mut Xcm<C>) -> Result<Weight, ()> {
log::trace!(target: "xcm::weight", "WeightInfoBounds message: {:?}", message);
let mut instructions_left = M::get();
Self::weight_with_limit(message, &mut instructions_left)
}
fn instr_weight(instruction: &mut Instruction<C>) -> Result<Weight, ()> {
Self::instr_weight_with_limit(instruction, &mut u32::max_value())
}
}
impl<W, C, M> WeightInfoBounds<W, C, M>
where
W: XcmWeightInfo<C>,
C: Decode + GetDispatchInfo,
M: Get<u32>,
Instruction<C>: xcm::latest::GetWeight<W>,
{
fn weight_with_limit(message: &mut Xcm<C>, instrs_limit: &mut u32) -> Result<Weight, ()> {
let mut r: Weight = Weight::zero();
*instrs_limit = instrs_limit.checked_sub(message.0.len() as u32).ok_or(())?;
for instruction in message.0.iter_mut() {
r = r
.checked_add(&Self::instr_weight_with_limit(instruction, instrs_limit)?)
.ok_or(())?;
}
Ok(r)
}
fn instr_weight_with_limit(
instruction: &mut Instruction<C>,
instrs_limit: &mut u32,
) -> Result<Weight, ()> {
let instr_weight = match instruction {
Transact { ref mut call, .. } => call.ensure_decoded()?.get_dispatch_info().call_weight,
SetErrorHandler(xcm) | SetAppendix(xcm) => Self::weight_with_limit(xcm, instrs_limit)?,
_ => Weight::zero(),
};
instruction.weight().checked_add(&instr_weight).ok_or(())
}
}
pub trait TakeRevenue {
fn take_revenue(revenue: Asset);
}
impl TakeRevenue for () {
fn take_revenue(_revenue: Asset) {}
}
pub struct FixedRateOfFungible<T: Get<(AssetId, u128, u128)>, R: TakeRevenue>(
Weight,
u128,
PhantomData<(T, R)>,
);
impl<T: Get<(AssetId, u128, u128)>, R: TakeRevenue> WeightTrader for FixedRateOfFungible<T, R> {
fn new() -> Self {
Self(Weight::zero(), 0, PhantomData)
}
fn buy_weight(
&mut self,
weight: Weight,
payment: AssetsInHolding,
context: &XcmContext,
) -> Result<AssetsInHolding, XcmError> {
log::trace!(
target: "xcm::weight",
"FixedRateOfFungible::buy_weight weight: {:?}, payment: {:?}, context: {:?}",
weight, payment, context,
);
let (id, units_per_second, units_per_mb) = T::get();
let amount = (units_per_second * (weight.ref_time() as u128) /
(WEIGHT_REF_TIME_PER_SECOND as u128)) +
(units_per_mb * (weight.proof_size() as u128) / (WEIGHT_PROOF_SIZE_PER_MB as u128));
if amount == 0 {
return Ok(payment)
}
let unused =
payment.checked_sub((id, amount).into()).map_err(|_| XcmError::TooExpensive)?;
self.0 = self.0.saturating_add(weight);
self.1 = self.1.saturating_add(amount);
Ok(unused)
}
fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option<Asset> {
log::trace!(target: "xcm::weight", "FixedRateOfFungible::refund_weight weight: {:?}, context: {:?}", weight, context);
let (id, units_per_second, units_per_mb) = T::get();
let weight = weight.min(self.0);
let amount = (units_per_second * (weight.ref_time() as u128) /
(WEIGHT_REF_TIME_PER_SECOND as u128)) +
(units_per_mb * (weight.proof_size() as u128) / (WEIGHT_PROOF_SIZE_PER_MB as u128));
self.0 -= weight;
self.1 = self.1.saturating_sub(amount);
if amount > 0 {
Some((id, amount).into())
} else {
None
}
}
}
impl<T: Get<(AssetId, u128, u128)>, R: TakeRevenue> Drop for FixedRateOfFungible<T, R> {
fn drop(&mut self) {
if self.1 > 0 {
R::take_revenue((T::get().0, self.1).into());
}
}
}
pub struct UsingComponents<
WeightToFee: WeightToFeeT<Balance = <Fungible as Inspect<AccountId>>::Balance>,
AssetIdValue: Get<Location>,
AccountId,
Fungible: Balanced<AccountId> + Inspect<AccountId>,
OnUnbalanced: OnUnbalancedT<Credit<AccountId, Fungible>>,
>(
Weight,
Fungible::Balance,
PhantomData<(WeightToFee, AssetIdValue, AccountId, Fungible, OnUnbalanced)>,
);
impl<
WeightToFee: WeightToFeeT<Balance = <Fungible as Inspect<AccountId>>::Balance>,
AssetIdValue: Get<Location>,
AccountId,
Fungible: Balanced<AccountId> + Inspect<AccountId>,
OnUnbalanced: OnUnbalancedT<Credit<AccountId, Fungible>>,
> WeightTrader for UsingComponents<WeightToFee, AssetIdValue, AccountId, Fungible, OnUnbalanced>
{
fn new() -> Self {
Self(Weight::zero(), Zero::zero(), PhantomData)
}
fn buy_weight(
&mut self,
weight: Weight,
payment: AssetsInHolding,
context: &XcmContext,
) -> Result<AssetsInHolding, XcmError> {
log::trace!(target: "xcm::weight", "UsingComponents::buy_weight weight: {:?}, payment: {:?}, context: {:?}", weight, payment, context);
let amount = WeightToFee::weight_to_fee(&weight);
let u128_amount: u128 = amount.try_into().map_err(|_| XcmError::Overflow)?;
let required = Asset { id: AssetId(AssetIdValue::get()), fun: Fungible(u128_amount) };
let unused = payment.checked_sub(required).map_err(|_| XcmError::TooExpensive)?;
self.0 = self.0.saturating_add(weight);
self.1 = self.1.saturating_add(amount);
Ok(unused)
}
fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option<Asset> {
log::trace!(target: "xcm::weight", "UsingComponents::refund_weight weight: {:?}, context: {:?}, available weight: {:?}, available amount: {:?}", weight, context, self.0, self.1);
let weight = weight.min(self.0);
let amount = WeightToFee::weight_to_fee(&weight);
self.0 -= weight;
self.1 = self.1.saturating_sub(amount);
let amount: u128 = amount.saturated_into();
log::trace!(target: "xcm::weight", "UsingComponents::refund_weight amount to refund: {:?}", amount);
if amount > 0 {
Some((AssetIdValue::get(), amount).into())
} else {
None
}
}
}
impl<
WeightToFee: WeightToFeeT<Balance = <Fungible as Inspect<AccountId>>::Balance>,
AssetId: Get<Location>,
AccountId,
Fungible: Balanced<AccountId> + Inspect<AccountId>,
OnUnbalanced: OnUnbalancedT<Credit<AccountId, Fungible>>,
> Drop for UsingComponents<WeightToFee, AssetId, AccountId, Fungible, OnUnbalanced>
{
fn drop(&mut self) {
OnUnbalanced::on_unbalanced(Fungible::issue(self.1));
}
}