Skip to content

Commit

Permalink
Feat: allow borrow automation (#1733)
Browse files Browse the repository at this point in the history
* feat: allow pallet_xcm::send to be called by signed origin

* feat: integrate changes for LocalOriginToLocation

* feat: fix warnings

* fix: taplo

* feat: tests for account derivation

* fix: add genesis to derivation, fix dispatch info without externaltieus
  • Loading branch information
mustermeiszer authored Feb 27, 2024
1 parent 59f7f68 commit 79c0839
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 36 deletions.
7 changes: 4 additions & 3 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,17 @@ impl Contains<RuntimeCall> for BaseCallFilter {
RuntimeCall::PolkadotXcm(method) => match method {
// Block these calls when called by a signed extrinsic.
// Root will still be able to execute these.
pallet_xcm::Call::send { .. }
| pallet_xcm::Call::execute { .. }
pallet_xcm::Call::execute { .. }
| pallet_xcm::Call::teleport_assets { .. }
| pallet_xcm::Call::reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_teleport_assets { .. } => false,
pallet_xcm::Call::__Ignore { .. } => {
unimplemented!()
}
pallet_xcm::Call::force_xcm_version { .. }
// Allow all these calls. Only send(..) is callable by signed the rest needs root.
pallet_xcm::Call::send { .. }
| pallet_xcm::Call::force_xcm_version { .. }
| pallet_xcm::Call::force_suspension { .. }
| pallet_xcm::Call::force_default_xcm_version { .. }
| pallet_xcm::Call::force_subscribe_version_notify { .. }
Expand Down
12 changes: 5 additions & 7 deletions runtime/altair/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use pallet_xcm::XcmPassthrough;
use runtime_common::{
transfer_filter::PreXcmTransfer,
xcm::{
general_key, AccountIdToMultiLocation, Barrier, FixedConversionRateProvider, ToTreasury,
general_key, AccountIdToMultiLocation, Barrier, FixedConversionRateProvider,
LocalOriginToLocation, ToTreasury,
},
xcm_fees::native_per_second,
};
Expand All @@ -39,7 +40,7 @@ use xcm::{
use xcm_builder::{
ConvertedConcreteId, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FungiblesAdapter,
NoChecking, RelayChainAsNative, SiblingParachainAsNative, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation,
SovereignSignedViaLocation,
};
use xcm_executor::{traits::JustTry, XcmExecutor};

Expand Down Expand Up @@ -170,7 +171,7 @@ impl pallet_xcm::Config for Runtime {
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type Currency = crate::Balances;
type CurrencyMatcher = ();
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation<Runtime>>;
type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -179,7 +180,7 @@ impl pallet_xcm::Config for Runtime {
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation<Runtime>>;
type SovereignAccountOf = LocationToAccountId;
type TrustedLockers = ();
type UniversalLocation = UniversalLocation;
Expand All @@ -204,9 +205,6 @@ parameter_types! {
pub type CurrencyIdConvert = runtime_common::xcm::CurrencyIdConvert<Runtime>;
pub type LocationToAccountId = runtime_common::xcm::LocationToAccountId<RelayNetwork>;

/// No local origins on this chain are allowed to dispatch XCM sends/executions.
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;

/// The means for routing XCM messages which are not for local execution
/// into the right message queues.
pub type XcmRouter = (
Expand Down
7 changes: 4 additions & 3 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,17 @@ impl Contains<RuntimeCall> for BaseCallFilter {
RuntimeCall::PolkadotXcm(method) => match method {
// Block these calls when called by a signed extrinsic.
// Root will still be able to execute these.
pallet_xcm::Call::send { .. }
| pallet_xcm::Call::execute { .. }
pallet_xcm::Call::execute { .. }
| pallet_xcm::Call::teleport_assets { .. }
| pallet_xcm::Call::reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_teleport_assets { .. } => false,
pallet_xcm::Call::__Ignore { .. } => {
unimplemented!()
}
pallet_xcm::Call::force_xcm_version { .. }
// Allow all these calls. Only send(..) is callable by signed the rest needs root.
pallet_xcm::Call::send { .. }
| pallet_xcm::Call::force_xcm_version { .. }
| pallet_xcm::Call::force_suspension { .. }
| pallet_xcm::Call::force_default_xcm_version { .. }
| pallet_xcm::Call::force_subscribe_version_notify { .. }
Expand Down
11 changes: 4 additions & 7 deletions runtime/centrifuge/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use runtime_common::{
transfer_filter::PreXcmTransfer,
xcm::{
general_key, AccountIdToMultiLocation, Barrier, FixedConversionRateProvider,
LpInstanceRelayer, ToTreasury,
LocalOriginToLocation, LpInstanceRelayer, ToTreasury,
},
xcm_fees::native_per_second,
};
Expand All @@ -41,7 +41,7 @@ use xcm::{
use xcm_builder::{
ConvertedConcreteId, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FungiblesAdapter,
NoChecking, RelayChainAsNative, SiblingParachainAsNative, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation,
SovereignSignedViaLocation,
};
use xcm_executor::{traits::JustTry, XcmExecutor};

Expand Down Expand Up @@ -172,7 +172,7 @@ impl pallet_xcm::Config for Runtime {
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type Currency = crate::Balances;
type CurrencyMatcher = ();
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation<Runtime>>;
type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -181,7 +181,7 @@ impl pallet_xcm::Config for Runtime {
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation<Runtime>>;
type SovereignAccountOf = LocationToAccountId;
type TrustedLockers = ();
type UniversalLocation = UniversalLocation;
Expand All @@ -206,9 +206,6 @@ parameter_types! {
pub type CurrencyIdConvert = runtime_common::xcm::CurrencyIdConvert<Runtime>;
pub type LocationToAccountId = runtime_common::xcm::LocationToAccountId<RelayNetwork>;

/// No local origins on this chain are allowed to dispatch XCM sends/executions.
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;

/// The means for routing XCM messages which are not for local execution
/// into the right message queues.
pub type XcmRouter = (
Expand Down
31 changes: 26 additions & 5 deletions runtime/common/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ use cfg_types::{
};
use frame_support::{
sp_std::marker::PhantomData,
traits::{fungibles::Mutate, Everything},
traits::{fungibles::Mutate, Everything, Get},
};
use polkadot_parachain::primitives::Sibling;
use sp_runtime::traits::{AccountIdConversion, Convert};
use sp_runtime::traits::{AccountIdConversion, Convert, Zero};
use xcm::v3::{
prelude::*,
Junction::{AccountId32, AccountKey20, GeneralKey, Parachain},
Junctions::{X1, X2},
MultiAsset, MultiLocation, OriginKind,
MultiAsset, MultiLocation, NetworkId, OriginKind,
};
use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, ParentIsPreset, SiblingParachainConvertsVia, TakeRevenue,
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom, ParentIsPreset, SiblingParachainConvertsVia,
SignedToAccountId32, TakeRevenue, TakeWeightCredit,
};

use crate::xcm_fees::default_per_second;
Expand Down Expand Up @@ -130,6 +130,27 @@ where
}
}

/// No local origins on this chain are allowed to dispatch XCM sends/executions.
pub type LocalOriginToLocation<R> = SignedToAccountId32<
<R as frame_system::Config>::RuntimeOrigin,
AccountId,
NetworkIdByGenesis<R>,
>;

pub struct NetworkIdByGenesis<T>(sp_std::marker::PhantomData<T>);

impl<T: frame_system::Config> Get<Option<NetworkId>> for NetworkIdByGenesis<T>
where
<T as frame_system::Config>::Hash: Into<[u8; 32]>,
{
fn get() -> Option<NetworkId> {
Some(NetworkId::ByGenesis(
frame_system::BlockHash::<T>::get(<T as frame_system::Config>::BlockNumber::zero())
.into(),
))
}
}

/// CurrencyIdConvert
/// This type implements conversions from our `CurrencyId` type into
/// `MultiLocation` and vice-versa. A currency locally is identified with a
Expand Down
7 changes: 4 additions & 3 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,17 @@ impl Contains<RuntimeCall> for BaseCallFilter {
RuntimeCall::PolkadotXcm(method) => match method {
// Block these calls when called by a signed extrinsic.
// Root will still be able to execute these.
pallet_xcm::Call::send { .. }
| pallet_xcm::Call::execute { .. }
pallet_xcm::Call::execute { .. }
| pallet_xcm::Call::teleport_assets { .. }
| pallet_xcm::Call::reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_reserve_transfer_assets { .. }
| pallet_xcm::Call::limited_teleport_assets { .. } => false,
pallet_xcm::Call::__Ignore { .. } => {
unimplemented!()
}
pallet_xcm::Call::force_xcm_version { .. }
// Allow all these calls. Only send(..) is callable by signed the rest needs root.
pallet_xcm::Call::send { .. }
| pallet_xcm::Call::force_xcm_version { .. }
| pallet_xcm::Call::force_suspension { .. }
| pallet_xcm::Call::force_default_xcm_version { .. }
| pallet_xcm::Call::force_subscribe_version_notify { .. }
Expand Down
11 changes: 4 additions & 7 deletions runtime/development/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use runtime_common::{
transfer_filter::PreXcmTransfer,
xcm::{
general_key, AccountIdToMultiLocation, Barrier, FixedConversionRateProvider,
LpInstanceRelayer, ToTreasury,
LocalOriginToLocation, LpInstanceRelayer, ToTreasury,
},
xcm_fees::native_per_second,
};
Expand All @@ -37,7 +37,7 @@ use xcm::{latest::Weight as XcmWeight, prelude::*, v3::MultiLocation};
use xcm_builder::{
ConvertedConcreteId, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FungiblesAdapter,
NoChecking, RelayChainAsNative, SiblingParachainAsNative, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation,
SovereignSignedViaLocation,
};
use xcm_executor::{traits::JustTry, XcmExecutor};

Expand Down Expand Up @@ -169,7 +169,7 @@ impl pallet_xcm::Config for Runtime {
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type Currency = crate::Balances;
type CurrencyMatcher = ();
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation<Runtime>>;
type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -178,7 +178,7 @@ impl pallet_xcm::Config for Runtime {
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation<Runtime>>;
type SovereignAccountOf = LocationToAccountId;
type TrustedLockers = ();
type UniversalLocation = UniversalLocation;
Expand All @@ -203,9 +203,6 @@ parameter_types! {
pub type CurrencyIdConvert = runtime_common::xcm::CurrencyIdConvert<Runtime>;
pub type LocationToAccountId = runtime_common::xcm::LocationToAccountId<RelayNetwork>;

/// No local origins on this chain are allowed to dispatch XCM sends/executions.
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;

/// The means for routing XCM messages which are not for local execution
/// into the right message queues.
pub type XcmRouter = (
Expand Down
Loading

0 comments on commit 79c0839

Please sign in to comment.