Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xcm revamp about buy weight for transact #223

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ pub mod pallet {

/// Weight information for the extrinsics in this module.
type WeightInfo: WeightInfo;

type SelfParaId: Get<u32>;

#[pallet::constant]
type BaseXcmWeight: Get<u64>;

#[pallet::constant]
type ContributionWeight: Get<u64>;

#[pallet::constant]
type WithdrawWeight: Get<u64>;
}

#[pallet::pallet]
Expand Down Expand Up @@ -739,7 +750,7 @@ pub mod pallet {

ensure!(owner == fund.depositor, Error::<T>::UnauthorizedAccount);

Self::xcm_ump_withdraw(index).map_err(|_| Error::<T>::XcmFailed)?;
Self::xcm_ump_withdraw(origin, index).map_err(|_| Error::<T>::XcmFailed)?;

Self::deposit_event(Event::Withdrawing(owner, index, fund.raised));

Expand Down Expand Up @@ -1175,12 +1186,13 @@ pub mod pallet {
T::BifrostXcmExecutor::ump_transact(
MultiLocation::X1(Junction::Parachain(index)),
call,
T::ContributionWeight::get(),
false,
)
}

fn xcm_ump_withdraw(index: ParaId) -> XcmResult {
let who: AccountIdOf<T> = PolkadotParaId::from(index).into_account();
fn xcm_ump_withdraw(_origin: OriginFor<T>, index: ParaId) -> XcmResult {
let who: AccountIdOf<T> = PolkadotParaId::from(T::SelfParaId::get()).into_account();

let withdraw = Withdraw { who, index };
let call = CrowdloanWithdrawCall::CrowdloanWithdraw(WithdrawCall::Withdraw(withdraw))
Expand All @@ -1190,6 +1202,7 @@ pub mod pallet {
T::BifrostXcmExecutor::ump_transact(
MultiLocation::X1(Junction::Parachain(index)),
call,
T::WithdrawWeight::get(),
false,
)
}
Expand Down
19 changes: 18 additions & 1 deletion pallets/salp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ parameter_types! {
pub const ReleaseRatio: Percent = Percent::from_percent(50);
pub const DepositTokenType: CurrencyId = CurrencyId::Token(TokenSymbol::ASG);
pub const XcmTransferOrigin: TransferOriginType = TransferOriginType::FromSelf;
pub BaseXcmWeight:u64 = 1_000_000_000 as u64;
pub ContributionWeight:u64 = 1_000_000_000 as u64;
pub WithdrawWeight:u64 = 1_000_000_000 as u64;
pub const SelfParaId: u32 = 2001;
}

parameter_types! {
Expand Down Expand Up @@ -198,6 +202,10 @@ impl salp::Config for Test {
type VSBondValidPeriod = VSBondValidPeriod;
type XcmTransferOrigin = XcmTransferOrigin;
type WeightInfo = salp::TestWeightInfo;
type SelfParaId = SelfParaId;
type BaseXcmWeight = BaseXcmWeight;
type ContributionWeight = ContributionWeight;
type WithdrawWeight = WithdrawWeight;
}

// To control the result returned by `MockXcmExecutor`
Expand All @@ -207,7 +215,16 @@ pub(crate) static mut MOCK_XCM_RESULT: (bool, bool) = (true, true);
pub struct MockXcmExecutor;

impl BifrostXcmExecutor for MockXcmExecutor {
fn ump_transact(_origin: MultiLocation, _call: DoubleEncoded<()>, _relayer: bool) -> XcmResult {
fn transact_weight() -> u64 {
return 0;
}

fn ump_transact(
_origin: MultiLocation,
_call: DoubleEncoded<()>,
_weight: u64,
_relayer: bool,
) -> XcmResult {
let result = unsafe { MOCK_XCM_RESULT.0 };

match result {
Expand Down
10 changes: 8 additions & 2 deletions runtime/asgard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ parameter_types! {
pub const NativeCurrencyId: CurrencyId = CurrencyId::Native(TokenSymbol::ASG);
pub const RelayCurrencyId: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
pub const StableCurrencyId: CurrencyId = CurrencyId::Stable(TokenSymbol::AUSD);
pub SelfParaId: u32 = ParachainInfo::parachain_id().into();
}

impl frame_system::Config for Runtime {
Expand Down Expand Up @@ -955,6 +956,8 @@ parameter_types! {
pub const SlotLength: BlockNumber = 8u32 as BlockNumber;
pub const XcmTransferOrigin: TransferOriginType = TransferOriginType::FromRelayChain;
pub XcmWeight: XcmBaseWeight = XCM_WEIGHT.into();
pub ContributionWeight:u64 = XCM_WEIGHT.into();
pub WithdrawWeight:u64 = XCM_WEIGHT.into();
}

impl bifrost_salp::Config for Runtime {
Expand All @@ -975,7 +978,11 @@ impl bifrost_salp::Config for Runtime {
type SubmissionDeposit = SubmissionDeposit;
type VSBondValidPeriod = VSBondValidPeriod;
type XcmTransferOrigin = XcmTransferOrigin;
type WeightInfo = weights::bifrost_salp::WeightInfo<Runtime>; // bifrost_salp::TestWeightInfo;
type WeightInfo = weights::bifrost_salp::WeightInfo<Runtime>;
type SelfParaId = SelfParaId;
type ContributionWeight = ContributionWeight;
type WithdrawWeight = WithdrawWeight;
type BaseXcmWeight = XcmWeight;
}

parameter_types! {
Expand Down Expand Up @@ -1010,7 +1017,6 @@ impl bifrost_vsbond_auction::Config for Runtime {
// zenlink runtime start
parameter_types! {
pub const ZenlinkPalletId: PalletId = PalletId(*b"/zenlink");
pub SelfParaId: u32 = ParachainInfo::parachain_id().into();
pub const GetExchangeFee: (u32, u32) = (3, 1000); // 0.3%

// xcm
Expand Down
8 changes: 7 additions & 1 deletion runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,8 @@ parameter_types! {
pub const SlotLength: BlockNumber = 8u32 as BlockNumber;
pub const XcmTransferOrigin: TransferOriginType = TransferOriginType::FromRelayChain;
pub XcmWeight: XcmBaseWeight = XCM_WEIGHT.into();
pub ContributionWeight:u64 = XCM_WEIGHT.into();
pub WithdrawWeight:u64 = XCM_WEIGHT.into();
}

impl bifrost_salp::Config for Runtime {
Expand All @@ -946,7 +948,11 @@ impl bifrost_salp::Config for Runtime {
type SubmissionDeposit = SubmissionDeposit;
type VSBondValidPeriod = VSBondValidPeriod;
type XcmTransferOrigin = XcmTransferOrigin;
type WeightInfo = weights::bifrost_salp::WeightInfo<Runtime>; // bifrost_salp::TestWeightInfo;
type WeightInfo = weights::bifrost_salp::WeightInfo<Runtime>;
type SelfParaId = SelfParaId;
type ContributionWeight = ContributionWeight;
type WithdrawWeight = WithdrawWeight;
type BaseXcmWeight = XcmWeight;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions xcm-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
paste = "1.0.5"
smallvec = "1.6.1"
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }
Expand All @@ -31,6 +32,7 @@ parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polk
pallet-xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8" }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8" }
polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8" }
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.8" }

[features]
default = ["std"]
Expand Down
31 changes: 26 additions & 5 deletions xcm-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,32 @@ pub struct BifrostXcmAdaptor<XcmSender, BaseXcmWeight>(PhantomData<(XcmSender, B
impl<XcmSender: SendXcm, BaseXcmWeight: Get<u64>> BifrostXcmExecutor
for BifrostXcmAdaptor<XcmSender, BaseXcmWeight>
{
fn ump_transact(origin: MultiLocation, call: DoubleEncoded<()>, relay: bool) -> XcmResult {
let mut message = Xcm::Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: u64::MAX,
call,
fn transact_weight() -> u64 {
return 4 * BaseXcmWeight::get();
}

fn ump_transact(
origin: MultiLocation,
call: DoubleEncoded<()>,
weight: u64,
relay: bool,
) -> XcmResult {
let mut message = Xcm::WithdrawAsset {
assets: vec![MultiAsset::ConcreteFungible {
id: MultiLocation::Null,
amount: (weight + Self::transact_weight()) as u128,
}],
effects: vec![Order::BuyExecution {
fees: MultiAsset::All,
weight: weight + 2 * BaseXcmWeight::get(),
debt: 2 * BaseXcmWeight::get(),
halt_on_error: true,
xcm: vec![Xcm::Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: u64::MAX,
call,
}],
}],
};

if relay {
Expand Down
51 changes: 43 additions & 8 deletions xcm-support/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,24 @@ pub mod para {

pub mod relay {
use cumulus_primitives_core::ParaId;
use frame_support::{construct_runtime, parameter_types, traits::All, weights::Weight};
use frame_support::{
construct_runtime, parameter_types,
traits::{All, OnUnbalanced},
weights::{Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial},
};
use pallet_balances::NegativeImbalance;
use polkadot_primitives;
use polkadot_runtime_parachains::{configuration, origin, shared, ump};
use smallvec::smallvec;
use sp_core::H256;
use sp_runtime::{testing::Header, traits::IdentityLookup, AccountId32};
use sp_runtime::{testing::Header, traits::IdentityLookup, AccountId32, Perbill};
use xcm::v0::{MultiAsset, MultiLocation, NetworkId};
use xcm_builder::{
AccountId32Aliases, AllowUnpaidExecutionFrom, ChildParachainAsNative,
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, ChildParachainAsNative,
ChildParachainConvertsVia, ChildSystemParachainAsSuperuser,
CurrencyAdapter as XcmCurrencyAdapter, FixedRateOfConcreteFungible, FixedWeightBounds,
IsConcrete, LocationInverter, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation,
CurrencyAdapter as XcmCurrencyAdapter, FixedWeightBounds, IsConcrete, LocationInverter,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
TakeWeightCredit, UsingComponents,
};
use xcm_executor::{Config, XcmExecutor};

Expand All @@ -521,6 +528,33 @@ pub mod relay {
pub const BlockHashCount: u64 = 250;
}

/// Logic for the author to get a portion of fees.
pub struct ToAuthor<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<NegativeImbalance<R>> for ToAuthor<R>
where
R: pallet_balances::Config,
<R as frame_system::Config>::AccountId: From<polkadot_primitives::v1::AccountId>,
<R as frame_system::Config>::AccountId: Into<polkadot_primitives::v1::AccountId>,
<R as frame_system::Config>::Event: From<pallet_balances::Event<R>>,
{
fn on_nonzero_unbalanced(_amount: NegativeImbalance<R>) {}
}

pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
let p = 1_000_000_00;
let q = 1_000_000;
smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

impl frame_system::Config for Runtime {
type AccountData = pallet_balances::AccountData<Balance>;
type AccountId = AccountId;
Expand Down Expand Up @@ -598,7 +632,7 @@ pub mod relay {
}

pub type XcmRouter = crate::mock::RelayChainXcmRouter;
pub type Barrier = AllowUnpaidExecutionFrom<All<MultiLocation>>;
pub type Barrier = (TakeWeightCredit, AllowTopLevelPaidExecutionFrom<All<MultiLocation>>);

pub struct XcmConfig;
impl Config for XcmConfig {
Expand All @@ -610,7 +644,8 @@ pub mod relay {
type LocationInverter = LocationInverter<Ancestry>;
type OriginConverter = LocalOriginConverter;
type ResponseHandler = ();
type Trader = FixedRateOfConcreteFungible<KsmPerSecond, ()>;
type Trader =
UsingComponents<WeightToFee, KsmLocation, AccountId, Balances, ToAuthor<Runtime>>;
type Weigher = FixedWeightBounds<BaseXcmWeight, Call>;
type XcmSender = XcmRouter;
}
Expand Down
44 changes: 42 additions & 2 deletions xcm-support/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ mod tests {
use codec::Encode;
use frame_support::assert_ok;
use xcm::v0::{
Junction::{self, Parachain, Parent},
Junction,
Junction::{Parachain, Parent},
MultiAsset,
MultiAsset::*,
MultiLocation,
MultiLocation::*,
NetworkId, OriginKind,
NetworkId, OriginKind, Xcm,
Xcm::*,
};

Expand Down Expand Up @@ -112,6 +115,43 @@ mod tests {
});
}

#[test]
fn ump_transact_buy_weight() {
MockNet::reset();

let remark =
relay::Call::System(frame_system::Call::<relay::Runtime>::remark_with_event(vec![
1, 2, 3,
]));
ParaA::execute_with(|| {
assert_ok!(ParachainPalletXcm::send_xcm(
Null,
X1(Parent),
Xcm::WithdrawAsset {
assets: vec![MultiAsset::ConcreteFungible {
id: MultiLocation::Null,
amount: 0
}],
effects: vec![Order::BuyExecution {
fees: MultiAsset::All,
weight: INITIAL_BALANCE as u64,
debt: INITIAL_BALANCE as u64,
halt_on_error: false,
xcm: vec![Xcm::Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: u64::MAX,
call: remark.encode().into(),
}],
}],
}
));
});

Relay::execute_with(|| {
print_events::<relay::Runtime>("RelayChain");
});
}

#[test]
fn xcmp() {
MockNet::reset();
Expand Down
9 changes: 8 additions & 1 deletion xcm-support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ pub trait HandleXcmpMessage {

/// Bifrost Xcm Executor
pub trait BifrostXcmExecutor {
fn ump_transact(origin: MultiLocation, call: DoubleEncoded<()>, relay: bool) -> XcmResult;
fn transact_weight() -> u64;

fn ump_transact(
origin: MultiLocation,
call: DoubleEncoded<()>,
weight: u64,
relay: bool,
) -> XcmResult;

fn ump_transfer_asset(
origin: MultiLocation,
Expand Down