Skip to content

Commit

Permalink
fix: add missing weight to coretime calls
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoaguirre committed Nov 29, 2024
1 parent e63ea59 commit b755fa2
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion polkadot/runtime/parachains/src/coretime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ pub mod pallet {
type AssetTransactor: TransactAsset;
/// AccountId to Location converter
type AccountToLocation: for<'a> TryConvert<&'a Self::AccountId, Location>;

/// Maximum weight for any XCM transact call that should be executed on the coretime chain.
///
/// Basically should be `max_weight(set_leases, reserve, notify_core_count)`.
type MaxXcmTransactWeight: Get<Weight>;
}

#[pallet::event]
Expand Down Expand Up @@ -333,7 +338,7 @@ impl<T: Config> OnNewSession<BlockNumberFor<T>> for Pallet<T> {
fn mk_coretime_call<T: Config>(call: crate::coretime::CoretimeCalls) -> Instruction<()> {
Instruction::Transact {
origin_kind: OriginKind::Superuser,
fallback_max_weight: None,
fallback_max_weight: Some(T::MaxXcmTransactWeight::get()),
call: BrokerRuntimePallets::Broker(call).encode().into(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ impl assigner_coretime::Config for Test {}

parameter_types! {
pub const BrokerId: u32 = 10u32;
pub MaxXcmTransactWeight: Weight = Weight::from_parts(10_000_000, 10_000);
}

pub struct BrokerPot;
Expand All @@ -437,6 +438,7 @@ impl coretime::Config for Test {
type BrokerId = BrokerId;
type WeightInfo = crate::coretime::TestWeightInfo;
type SendXcm = DummyXcmSender;
type MaxXcmTransactWeight = MaxXcmTransactWeight;
type BrokerPotLocation = BrokerPot;
type AssetTransactor = ();
type AccountToLocation = ();
Expand Down
14 changes: 11 additions & 3 deletions polkadot/runtime/rococo/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::marker::PhantomData;
use frame_support::pallet_prelude::DispatchResult;
use frame_system::RawOrigin;
use polkadot_primitives::Balance;
use polkadot_runtime_common::identity_migrator::OnReapIdentity;
use polkadot_runtime_common::identity_migrator::{OnReapIdentity, WeightInfo};
use rococo_runtime_constants::currency::*;
use xcm::{latest::prelude::*, VersionedLocation, VersionedXcm};
use xcm_executor::traits::TransactAsset;
Expand Down Expand Up @@ -88,7 +88,10 @@ where
AccountId: Into<[u8; 32]> + Clone + Encode,
{
fn on_reap_identity(who: &AccountId, fields: u32, subs: u32) -> DispatchResult {
use crate::impls::IdentityMigratorCalls::PokeDeposit;
use crate::{
impls::IdentityMigratorCalls::PokeDeposit,
weights::polkadot_runtime_common_identity_migrator::WeightInfo as MigratorWeights,
};

let total_to_send = Self::calculate_remote_deposit(fields, subs);

Expand Down Expand Up @@ -141,6 +144,7 @@ where
.into();

let poke = PeopleRuntimePallets::<AccountId>::IdentityMigrator(PokeDeposit(who.clone()));
let remote_weight_limit = MigratorWeights::<Runtime>::poke_deposit().saturating_mul(2);

// Actual program to execute on People Chain.
let program: Xcm<()> = Xcm(vec![
Expand All @@ -157,7 +161,11 @@ where
.into(),
},
// Poke the deposit to reserve the appropriate amount on the parachain.
Transact { origin_kind: OriginKind::Superuser, call: poke.encode().into() },
Transact {
origin_kind: OriginKind::Superuser,
fallback_max_weight: Some(remote_weight_limit),
call: poke.encode().into(),
},
]);

// send
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ impl parachains_scheduler::Config for Runtime {
parameter_types! {
pub const BrokerId: u32 = BROKER_ID;
pub const BrokerPalletId: PalletId = PalletId(*b"py/broke");
pub MaxXcmTransactWeight: Weight = Weight::from_parts(200_000_000, 20_000);
}

pub struct BrokerPot;
Expand All @@ -1123,6 +1124,7 @@ impl coretime::Config for Runtime {
xcm_config::ThisNetwork,
<Runtime as frame_system::Config>::AccountId,
>;
type MaxXcmTransactWeight = MaxXcmTransactWeight;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ impl parachains_paras::Config for Runtime {

parameter_types! {
pub const BrokerId: u32 = 10u32;
pub MaxXcmTransactWeight: Weight = Weight::from_parts(10_000_000, 10_000);
}

pub struct BrokerPot;
Expand Down Expand Up @@ -657,6 +658,7 @@ impl coretime::Config for Runtime {
type BrokerId = BrokerId;
type WeightInfo = crate::coretime::TestWeightInfo;
type SendXcm = DummyXcmSender;
type MaxXcmTransactWeight = MaxXcmTransactWeight;
type BrokerPotLocation = BrokerPot;
type AssetTransactor = ();
type AccountToLocation = ();
Expand Down
10 changes: 7 additions & 3 deletions polkadot/runtime/westend/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::marker::PhantomData;
use frame_support::pallet_prelude::DispatchResult;
use frame_system::RawOrigin;
use polkadot_primitives::Balance;
use polkadot_runtime_common::identity_migrator::OnReapIdentity;
use polkadot_runtime_common::identity_migrator::{OnReapIdentity, WeightInfo};
use westend_runtime_constants::currency::*;
use xcm::{latest::prelude::*, VersionedLocation, VersionedXcm};
use xcm_executor::traits::TransactAsset;
Expand Down Expand Up @@ -88,7 +88,10 @@ where
AccountId: Into<[u8; 32]> + Clone + Encode,
{
fn on_reap_identity(who: &AccountId, fields: u32, subs: u32) -> DispatchResult {
use crate::impls::IdentityMigratorCalls::PokeDeposit;
use crate::{
impls::IdentityMigratorCalls::PokeDeposit,
weights::polkadot_runtime_common_identity_migrator::WeightInfo as MigratorWeights,
};

let total_to_send = Self::calculate_remote_deposit(fields, subs);

Expand Down Expand Up @@ -141,6 +144,7 @@ where
.into();

let poke = PeopleRuntimePallets::<AccountId>::IdentityMigrator(PokeDeposit(who.clone()));
let remote_weight_limit = MigratorWeights::<Runtime>::poke_deposit().saturating_mul(2);

// Actual program to execute on People Chain.
let program: Xcm<()> = Xcm(vec![
Expand All @@ -160,7 +164,7 @@ where
Transact {
origin_kind: OriginKind::Superuser,
call: poke.encode().into(),
fallback_max_weight: None,
fallback_max_weight: Some(remote_weight_limit),
},
]);

Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,7 @@ impl parachains_scheduler::Config for Runtime {
parameter_types! {
pub const BrokerId: u32 = BROKER_ID;
pub const BrokerPalletId: PalletId = PalletId(*b"py/broke");
pub MaxXcmTransactWeight: Weight = Weight::from_parts(200_000_000, 20_000);
}

pub struct BrokerPot;
Expand All @@ -1349,6 +1350,7 @@ impl coretime::Config for Runtime {
xcm_config::ThisNetwork,
<Runtime as frame_system::Config>::AccountId,
>;
type MaxXcmTransactWeight = MaxXcmTransactWeight;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions polkadot/xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ schemars = { default-features = true, optional = true, workspace = true }
xcm-procedural = { workspace = true, default-features = true }
environmental = { workspace = true }
hex-literal = { workspace = true, default-features = true }
frame-support = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true, default-features = true }
Expand All @@ -36,6 +37,7 @@ std = [
"bounded-collections/std",
"codec/std",
"environmental/std",
"frame-support/std",
"log/std",
"scale-info/std",
"serde/std",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ mod benchmarks {
let instruction = Instruction::Transact {
origin_kind: OriginKind::SovereignAccount,
call: double_encoded_noop_call,
fallback_xcm_weight: None,
};
let xcm = Xcm(vec![instruction]);
#[block]
Expand Down
17 changes: 9 additions & 8 deletions polkadot/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern crate alloc;

use codec::{Decode, DecodeLimit, Encode, Error as CodecError, Input, MaxEncodedLen};
use derivative::Derivative;
use frame_support::dispatch::GetDispatchInfo;
use scale_info::TypeInfo;

pub mod v3;
Expand Down Expand Up @@ -325,7 +326,7 @@ pub enum VersionedXcm<RuntimeCall> {
V5(v5::Xcm<RuntimeCall>),
}

impl<C> IntoVersion for VersionedXcm<C> {
impl<C: Decode + GetDispatchInfo> IntoVersion for VersionedXcm<C> {
fn into_version(self, n: Version) -> Result<Self, ()> {
Ok(match n {
3 => Self::V3(self.try_into()?),
Expand Down Expand Up @@ -381,7 +382,7 @@ impl<RuntimeCall> From<v5::Xcm<RuntimeCall>> for VersionedXcm<RuntimeCall> {
}
}

impl<Call> TryFrom<VersionedXcm<Call>> for v3::Xcm<Call> {
impl<Call: Decode + GetDispatchInfo> TryFrom<VersionedXcm<Call>> for v3::Xcm<Call> {
type Error = ();
fn try_from(x: VersionedXcm<Call>) -> Result<Self, ()> {
use VersionedXcm::*;
Expand All @@ -396,7 +397,7 @@ impl<Call> TryFrom<VersionedXcm<Call>> for v3::Xcm<Call> {
}
}

impl<Call> TryFrom<VersionedXcm<Call>> for v4::Xcm<Call> {
impl<Call: Decode + GetDispatchInfo> TryFrom<VersionedXcm<Call>> for v4::Xcm<Call> {
type Error = ();
fn try_from(x: VersionedXcm<Call>) -> Result<Self, ()> {
use VersionedXcm::*;
Expand All @@ -408,7 +409,7 @@ impl<Call> TryFrom<VersionedXcm<Call>> for v4::Xcm<Call> {
}
}

impl<Call> TryFrom<VersionedXcm<Call>> for v5::Xcm<Call> {
impl<Call: Decode + GetDispatchInfo> TryFrom<VersionedXcm<Call>> for v5::Xcm<Call> {
type Error = ();
fn try_from(x: VersionedXcm<Call>) -> Result<Self, ()> {
use VersionedXcm::*;
Expand All @@ -426,7 +427,7 @@ impl<Call> TryFrom<VersionedXcm<Call>> for v5::Xcm<Call> {
/// Convert an `Xcm` datum into a `VersionedXcm`, based on a destination `Location` which will
/// interpret it.
pub trait WrapVersion {
fn wrap_version<RuntimeCall>(
fn wrap_version<RuntimeCall: Decode + GetDispatchInfo>(
dest: &latest::Location,
xcm: impl Into<VersionedXcm<RuntimeCall>>,
) -> Result<VersionedXcm<RuntimeCall>, ()>;
Expand Down Expand Up @@ -459,7 +460,7 @@ impl WrapVersion for () {
/// wrapping it.
pub struct AlwaysV3;
impl WrapVersion for AlwaysV3 {
fn wrap_version<Call>(
fn wrap_version<Call: Decode + GetDispatchInfo>(
_: &latest::Location,
xcm: impl Into<VersionedXcm<Call>>,
) -> Result<VersionedXcm<Call>, ()> {
Expand All @@ -476,7 +477,7 @@ impl GetVersion for AlwaysV3 {
/// wrapping it.
pub struct AlwaysV4;
impl WrapVersion for AlwaysV4 {
fn wrap_version<Call>(
fn wrap_version<Call: Decode + GetDispatchInfo>(
_: &latest::Location,
xcm: impl Into<VersionedXcm<Call>>,
) -> Result<VersionedXcm<Call>, ()> {
Expand All @@ -493,7 +494,7 @@ impl GetVersion for AlwaysV4 {
/// wrapping it.
pub struct AlwaysV5;
impl WrapVersion for AlwaysV5 {
fn wrap_version<Call>(
fn wrap_version<Call: Decode + GetDispatchInfo>(
_: &latest::Location,
xcm: impl Into<VersionedXcm<Call>>,
) -> Result<VersionedXcm<Call>, ()> {
Expand Down
21 changes: 17 additions & 4 deletions polkadot/xcm/src/v4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use codec::{
};
use core::{fmt::Debug, result};
use derivative::Derivative;
use frame_support::dispatch::GetDispatchInfo;
use scale_info::TypeInfo;

mod asset;
Expand Down Expand Up @@ -1269,15 +1270,15 @@ impl<Call> TryFrom<OldXcm<Call>> for Xcm<Call> {
}

// Convert from a v5 XCM to a v4 XCM.
impl<Call> TryFrom<NewXcm<Call>> for Xcm<Call> {
impl<Call: Decode + GetDispatchInfo> TryFrom<NewXcm<Call>> for Xcm<Call> {
type Error = ();
fn try_from(new_xcm: NewXcm<Call>) -> result::Result<Self, Self::Error> {
Ok(Xcm(new_xcm.0.into_iter().map(TryInto::try_into).collect::<result::Result<_, _>>()?))
}
}

// Convert from a v5 instruction to a v4 instruction.
impl<Call> TryFrom<NewInstruction<Call>> for Instruction<Call> {
impl<Call: Decode + GetDispatchInfo> TryFrom<NewInstruction<Call>> for Instruction<Call> {
type Error = ();
fn try_from(new_instruction: NewInstruction<Call>) -> result::Result<Self, Self::Error> {
use NewInstruction::*;
Expand Down Expand Up @@ -1313,8 +1314,20 @@ impl<Call> TryFrom<NewInstruction<Call>> for Instruction<Call> {
HrmpChannelAccepted { recipient } => Self::HrmpChannelAccepted { recipient },
HrmpChannelClosing { initiator, sender, recipient } =>
Self::HrmpChannelClosing { initiator, sender, recipient },
Transact { origin_kind, call, fallback_max_weight } => {
let require_weight_at_most = fallback_max_weight.unwrap_or(Weight::MAX);
Transact { origin_kind, mut call, fallback_max_weight } => {
// We first try to decode the call, if we can't, we use the fallback weight,
// if there's no fallback, we just return `Weight::MAX`.
let require_weight_at_most = match call.take_decoded() {
Ok(decoded) => decoded.get_dispatch_info().call_weight,
Err(error) => {
log::error!(
target: "xcm::versions::v5Tov4",
"Couldn't decode call in Transact: {:?}, using fallback weight.",
error,
);
fallback_max_weight.unwrap_or(Weight::MAX)
}
};
Self::Transact { origin_kind, require_weight_at_most, call: call.into() }
},
ReportError(response_info) => Self::ReportError(QueryResponseInfo {
Expand Down
19 changes: 15 additions & 4 deletions polkadot/xcm/src/v5/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,15 +1597,15 @@ mod tests {
WithdrawAsset((Here, 1u128).into()),
Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![].into(),
call: vec![200, 200, 200].into(),
fallback_max_weight: Some(Weight::from_parts(1_000_000, 1_024)),
},
]);
let old_xcm = OldXcm::<()>(vec![
OldInstruction::WithdrawAsset((OldHere, 1u128).into()),
OldInstruction::Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![].into(),
call: vec![200, 200, 200].into(),
require_weight_at_most: Weight::from_parts(1_000_000, 1_024),
},
]);
Expand All @@ -1618,18 +1618,29 @@ mod tests {
WithdrawAsset((Here, 1u128).into()),
Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![].into(),
call: vec![200, 200, 200].into(),
fallback_max_weight: None,
},
]);
let old_xcm = OldXcm::<()>(vec![
OldInstruction::WithdrawAsset((OldHere, 1u128).into()),
OldInstruction::Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![].into(),
call: vec![200, 200, 200].into(),
require_weight_at_most: Weight::MAX,
},
]);
assert_eq!(old_xcm, OldXcm::<()>::try_from(xcm_without_fallback.clone()).unwrap());
let new_xcm: Xcm<()> = old_xcm.try_into().unwrap();
let xcm_with_max_weight_fallback = Xcm::<()>(vec![
WithdrawAsset((Here, 1u128).into()),
Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![200, 200, 200].into(),
fallback_max_weight: Some(Weight::MAX),
},
]);
assert_eq!(new_xcm, xcm_with_max_weight_fallback);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion polkadot/xcm/xcm-executor/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn transact_recursion_limit_works() {
Xcm(vec![
WithdrawAsset((Here, 1_000).into()),
BuyExecution { fees: (Here, 1).into(), weight_limit: Unlimited },
Transact { origin_kind: OriginKind::Native, call: call.encode().into() },
Transact { origin_kind: OriginKind::Native, call: call.encode().into(), fallback_max_weight: None },
])
};
let mut call: Option<polkadot_test_runtime::RuntimeCall> = None;
Expand Down

0 comments on commit b755fa2

Please sign in to comment.