Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Introduce transactional processing for xcm instructions #6951

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ea646b5
introduce transactional processing for xcm instructions
vstam1 Mar 24, 2023
cb8930d
merge master
vstam1 Mar 31, 2023
334307b
add Executor rollback on instruction error
vstam1 Apr 5, 2023
b1c1686
add transactional processing on xcm instrutions
vstam1 Apr 5, 2023
672012b
fix tests
vstam1 Apr 5, 2023
2f249f6
add clone to FixedRateOfFungiable
vstam1 Apr 5, 2023
b97a9ef
fix tests
vstam1 Apr 5, 2023
3524945
add FrameTransactionalProcessor to xcm-builder mock
vstam1 Apr 6, 2023
3dbe380
fmt
vstam1 Apr 6, 2023
497d955
more tests
vstam1 Apr 6, 2023
f2abce5
merge master
vstam1 May 2, 2023
c65ef90
small comment
vstam1 May 2, 2023
91c29de
move to unsafe transactional processing for frame
vstam1 Jun 1, 2023
358d025
change FrameTransactionProcessor and merge master
vstam1 Jun 13, 2023
fa4ea4a
Merge remote-tracking branch 'origin/master' into vstam1/xcm-transact…
Jun 13, 2023
d1d97f5
merge master
vstam1 Aug 2, 2023
e1b0bd2
fmt
vstam1 Aug 2, 2023
1b24683
add bool to processing trait to consider the rollback of registers
vstam1 Aug 3, 2023
68bea8a
add comments
vstam1 Aug 3, 2023
9aa5831
Merge remote-tracking branch 'origin/master' into vstam1/xcm-transact…
Aug 3, 2023
8fbd67b
address feedback
vstam1 Aug 3, 2023
49359bc
address feedback on function name
vstam1 Aug 4, 2023
3fe888e
fix register cloning
vstam1 Aug 4, 2023
3f5a2ff
Merge remote-tracking branch 'origin/master' into vstam1/xcm-transact…
Aug 7, 2023
908fbf0
address feedback
vstam1 Aug 7, 2023
c4120b5
update vm cloning to only cloning specific registers
vstam1 Aug 24, 2023
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
9 changes: 5 additions & 4 deletions runtime/kusama/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, ChildParachainAsNative,
ChildParachainConvertsVia, ChildSystemParachainAsSuperuser,
CurrencyAdapter as XcmCurrencyAdapter, IsChildSystemParachain, IsConcrete, MintLocation,
OriginToPluralityVoice, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId, UsingComponents,
WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
CurrencyAdapter as XcmCurrencyAdapter, FrameTransactionalProcessor, IsChildSystemParachain,
IsConcrete, MintLocation, OriginToPluralityVoice, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId,
UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
};
use xcm_executor::traits::WithOriginFilter;

Expand Down Expand Up @@ -346,6 +346,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = WithOriginFilter<SafeCallFilter>;
type SafeCallFilter = SafeCallFilter;
type Aliasers = Nothing;
type TransactionalProcessor = FrameTransactionalProcessor;
}

parameter_types! {
Expand Down
7 changes: 4 additions & 3 deletions runtime/polkadot/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, BackingToPlurality,
ChildParachainAsNative, ChildParachainConvertsVia, CurrencyAdapter as XcmCurrencyAdapter,
IsConcrete, MintLocation, OriginToPluralityVoice, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents,
WeightInfoBounds, WithComputedOrigin,
FrameTransactionalProcessor, IsConcrete, MintLocation, OriginToPluralityVoice,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
UsingComponents, WeightInfoBounds, WithComputedOrigin,
};
use xcm_executor::traits::WithOriginFilter;

Expand Down Expand Up @@ -347,6 +347,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = WithOriginFilter<SafeCallFilter>;
type SafeCallFilter = SafeCallFilter;
type Aliasers = Nothing;
type TransactionalProcessor = FrameTransactionalProcessor;
}

parameter_types! {
Expand Down
9 changes: 5 additions & 4 deletions runtime/rococo/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, BackingToPlurality,
ChildParachainAsNative, ChildParachainConvertsVia, ChildSystemParachainAsSuperuser,
CurrencyAdapter as XcmCurrencyAdapter, FixedWeightBounds, IsChildSystemParachain, IsConcrete,
MintLocation, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
TakeWeightCredit, TrailingSetTopicAsId, UsingComponents, WeightInfoBounds, WithComputedOrigin,
WithUniqueTopic,
CurrencyAdapter as XcmCurrencyAdapter, FixedWeightBounds, FrameTransactionalProcessor,
IsChildSystemParachain, IsConcrete, MintLocation, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId,
UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};

Expand Down Expand Up @@ -320,6 +320,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = WithOriginFilter<SafeCallFilter>;
type SafeCallFilter = SafeCallFilter;
type Aliasers = Nothing;
type TransactionalProcessor = FrameTransactionalProcessor;
}

parameter_types! {
Expand Down
6 changes: 4 additions & 2 deletions runtime/test-runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use frame_support::{
use frame_system::EnsureRoot;
use xcm::latest::prelude::*;
use xcm_builder::{
AllowUnpaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds, SignedAccountId32AsNative,
SignedToAccountId32,
AllowUnpaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds, FrameTransactionalProcessor,
SignedAccountId32AsNative, SignedToAccountId32,
};
use xcm_executor::{
traits::{TransactAsset, WeightTrader},
Expand Down Expand Up @@ -74,6 +74,7 @@ impl TransactAsset for DummyAssetTransactor {
}
}

#[derive(Clone)]
pub struct DummyWeightTrader;
impl WeightTrader for DummyWeightTrader {
fn new() -> Self {
Expand Down Expand Up @@ -121,6 +122,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = super::RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
type TransactionalProcessor = FrameTransactionalProcessor;
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
8 changes: 5 additions & 3 deletions runtime/westend/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, ChildParachainAsNative,
ChildParachainConvertsVia, ChildSystemParachainAsSuperuser,
CurrencyAdapter as XcmCurrencyAdapter, IsChildSystemParachain, IsConcrete, MintLocation,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
TrailingSetTopicAsId, UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
CurrencyAdapter as XcmCurrencyAdapter, FrameTransactionalProcessor, IsChildSystemParachain,
IsConcrete, MintLocation, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId, UsingComponents,
WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};

Expand Down Expand Up @@ -269,6 +270,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = WithOriginFilter<SafeCallFilter>;
type SafeCallFilter = SafeCallFilter;
type Aliasers = Nothing;
type TransactionalProcessor = FrameTransactionalProcessor;
}

/// Type to convert an `Origin` type value into a `MultiLocation` value which represents an interior location
Expand Down
3 changes: 2 additions & 1 deletion xcm/pallet-xcm-benchmarks/src/fungible/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use frame_support::{
use sp_core::H256;
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
use xcm::latest::prelude::*;
use xcm_builder::{AllowUnpaidExecutionFrom, MintLocation};
use xcm_builder::{AllowUnpaidExecutionFrom, FrameTransactionalProcessor, MintLocation};

type Block = frame_system::mocking::MockBlock<Test>;

Expand Down Expand Up @@ -152,6 +152,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
type TransactionalProcessor = FrameTransactionalProcessor;
}

impl crate::Config for Test {
Expand Down
3 changes: 2 additions & 1 deletion xcm/pallet-xcm-benchmarks/src/generic/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use xcm_builder::{
Assets, TestAssetExchanger, TestAssetLocker, TestAssetTrap, TestSubscriptionService,
TestUniversalAliases,
},
AliasForeignAccountId32, AllowUnpaidExecutionFrom,
AliasForeignAccountId32, AllowUnpaidExecutionFrom, FrameTransactionalProcessor,
};
use xcm_executor::traits::ConvertOrigin;

Expand Down Expand Up @@ -131,6 +131,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Aliasers;
type TransactionalProcessor = FrameTransactionalProcessor;
}

impl crate::Config for Test {
Expand Down
5 changes: 3 additions & 2 deletions xcm/pallet-xcm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, Case, ChildParachainAsNative, ChildParachainConvertsVia,
ChildSystemParachainAsSuperuser, CurrencyAdapter as XcmCurrencyAdapter, FixedRateOfFungible,
FixedWeightBounds, IsConcrete, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeWeightCredit,
FixedWeightBounds, FrameTransactionalProcessor, IsConcrete, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
};
use xcm_executor::XcmExecutor;

Expand Down Expand Up @@ -306,6 +306,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
type TransactionalProcessor = FrameTransactionalProcessor;
}

pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, AnyNetwork>;
Expand Down
3 changes: 3 additions & 0 deletions xcm/xcm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@ pub use origin_aliases::AliasForeignAccountId32;

mod pay;
pub use pay::{FixedLocation, LocatableAssetId, PayAccountId32OnChainOverXcm, PayOverXcm};

mod transactional;
pub use transactional::FrameTransactionalProcessor;
3 changes: 2 additions & 1 deletion xcm/xcm-builder/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
pub use crate::{
AliasForeignAccountId32, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, FixedRateOfFungible,
FixedWeightBounds, TakeWeightCredit,
FixedWeightBounds, FrameTransactionalProcessor, TakeWeightCredit,
};
use frame_support::traits::{ContainsPair, Everything};
pub use frame_support::{
Expand Down Expand Up @@ -685,6 +685,7 @@ impl Config for TestConfig {
type CallDispatcher = TestCall;
type SafeCallFilter = Everything;
type Aliasers = AliasForeignAccountId32<SiblingPrefix>;
type TransactionalProcessor = ();
}

pub fn fungible_multi_asset(location: MultiLocation, amount: u128) -> MultiAsset {
Expand Down
39 changes: 39 additions & 0 deletions xcm/xcm-builder/src/transactional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.

use frame_support::storage::{with_transaction_unchecked, TransactionOutcome};
use xcm::latest::prelude::*;
use xcm_executor::traits::ProcessTransaction;

/// Transactional processor implementation using frame transactional layers.
pub struct FrameTransactionalProcessor;
impl ProcessTransaction for FrameTransactionalProcessor {
vstam1 marked this conversation as resolved.
Show resolved Hide resolved
const IS_TRANSACTIONAL: bool = true;

fn process_transaction<F>(f: F) -> Result<(), XcmError>
where
F: FnOnce() -> Result<(), XcmError>,
{
// Is safe to use because of the `MAX_XCM_DECODE_DEPTH`.
with_transaction_unchecked(|| -> TransactionOutcome<Result<(), XcmError>> {
let outcome = f();
match &outcome {
Ok(_) => TransactionOutcome::Commit(Ok(())),
Err(err) => TransactionOutcome::Rollback(Err(*err)),
}
})
}
}
23 changes: 23 additions & 0 deletions xcm/xcm-builder/src/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ impl<T: Get<(AssetId, u128, u128)>, R: TakeRevenue> Drop for FixedRateOfFungible
}
}

impl<T, R> Clone for FixedRateOfFungible<T, R>
where
T: Get<(AssetId, u128, u128)>,
R: TakeRevenue,
{
fn clone(&self) -> FixedRateOfFungible<T, R> {
Self(self.0, self.1, PhantomData)
}
}

/// Weight trader which uses the configured `WeightToFee` to set the right price for weight and then
/// places any weight bought into the right account.
pub struct UsingComponents<
Expand Down Expand Up @@ -257,3 +267,16 @@ impl<
OnUnbalanced::on_unbalanced(Currency::issue(self.1));
}
}

impl<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced> Clone
for UsingComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced>
where
WeightToFee: WeightToFeeT<Balance = Currency::Balance>,
AssetId: Get<MultiLocation>,
Currency: CurrencyT<AccountId>,
OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>,
{
fn clone(&self) -> UsingComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced> {
Self(self.0, self.1, PhantomData)
}
}
6 changes: 4 additions & 2 deletions xcm/xcm-builder/tests/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ use xcm_builder::{
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom,
ChildParachainAsNative, ChildParachainConvertsVia, ChildSystemParachainAsSuperuser,
CurrencyAdapter as XcmCurrencyAdapter, FixedRateOfFungible, FixedWeightBounds,
IsChildSystemParachain, IsConcrete, MintLocation, RespectSuspension, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
FrameTransactionalProcessor, IsChildSystemParachain, IsConcrete, MintLocation,
RespectSuspension, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
TakeWeightCredit,
};

pub type AccountId = AccountId32;
Expand Down Expand Up @@ -203,6 +204,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
type TransactionalProcessor = FrameTransactionalProcessor;
}

pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, KusamaNetwork>;
Expand Down
9 changes: 6 additions & 3 deletions xcm/xcm-executor/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

use crate::traits::{
AssetExchange, AssetLock, CallDispatcher, ClaimAssets, ConvertOrigin, DropAssets, ExportXcm,
FeeManager, OnResponse, ShouldExecute, TransactAsset, VersionChangeNotifier, WeightBounds,
WeightTrader,
FeeManager, OnResponse, ProcessTransaction, ShouldExecute, TransactAsset,
VersionChangeNotifier, WeightBounds, WeightTrader,
};
use frame_support::{
dispatch::{Dispatchable, GetDispatchInfo, Parameter, PostDispatchInfo},
Expand Down Expand Up @@ -59,7 +59,7 @@ pub trait Config {
type Weigher: WeightBounds<Self::RuntimeCall>;

/// The means of purchasing weight credit for XCM execution.
type Trader: WeightTrader;
type Trader: WeightTrader + Clone;

/// What to do when a response of a query is found.
type ResponseHandler: OnResponse;
Expand Down Expand Up @@ -110,4 +110,7 @@ pub trait Config {
/// Use this type to explicitly whitelist calls that cannot undergo recursion. This is a
/// temporary measure until we properly account for proof size weights for XCM instructions.
type SafeCallFilter: Contains<Self::RuntimeCall>;

/// Transactional processor for XCM instructions.
type TransactionalProcessor: ProcessTransaction;
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
}
Loading