Remove Default bound on AccountId types under the xcm directory (#4712)

* Refactor ParentIsDefault to ParentIsAllZeroes

* Remove Default bound on all AccountId types under the xcm directory

* Change to ParentIs<A: Get<AccountId>, AccountId>

* Provide a better account for ParentIs

* Fixes

* Fixes

* Fixes

* Fixes

* Update xcm/xcm-builder/src/currency_adapter.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Use preset account ID value for parent MultiLocations

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Keith Yeung
2022-01-21 03:03:39 -08:00
committed by GitHub
parent 3d61cc01db
commit efe290490a
7 changed files with 36 additions and 25 deletions
@@ -70,7 +70,7 @@ use xcm::latest::prelude::*;
use xcm_builder::{ use xcm_builder::{
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, CurrencyAdapter, AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, CurrencyAdapter,
EnsureXcmOrigin, FixedWeightBounds, IsConcrete, LocationInverter, NativeAsset, EnsureXcmOrigin, FixedWeightBounds, IsConcrete, LocationInverter, NativeAsset,
ParentAsSuperuser, ParentIsDefault, RelayChainAsNative, SiblingParachainAsNative, ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeWeightCredit, UsingComponents, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents,
}; };
@@ -294,8 +294,8 @@ parameter_types! {
/// when determining ownership of accounts for asset transacting and when attempting to use XCM /// when determining ownership of accounts for asset transacting and when attempting to use XCM
/// `Transact` in order to determine the dispatch Origin. /// `Transact` in order to determine the dispatch Origin.
pub type LocationToAccountId = ( pub type LocationToAccountId = (
// The parent (Relay-chain) origin converts to the default `AccountId`. // The parent (Relay-chain) origin converts to the parent `AccountId`.
ParentIsDefault<AccountId>, ParentIsPreset<AccountId>,
// Sibling parachain origins convert to AccountId via the `ParaId::into`. // Sibling parachain origins convert to AccountId via the `ParaId::into`.
SiblingParachainConvertsVia<Sibling, AccountId>, SiblingParachainConvertsVia<Sibling, AccountId>,
// Straight up local `AccountId32` origins just alias directly to `AccountId`. // Straight up local `AccountId32` origins just alias directly to `AccountId`.
@@ -17,6 +17,7 @@
//! A mock runtime for XCM benchmarking. //! A mock runtime for XCM benchmarking.
use crate::{generic, mock::*, *}; use crate::{generic, mock::*, *};
use codec::Decode;
use frame_support::{ use frame_support::{
parameter_types, parameter_types,
traits::{Everything, OriginTrait}, traits::{Everything, OriginTrait},
@@ -24,7 +25,7 @@ use frame_support::{
use sp_core::H256; use sp_core::H256;
use sp_runtime::{ use sp_runtime::{
testing::Header, testing::Header,
traits::{BlakeTwo256, IdentityLookup}, traits::{BlakeTwo256, IdentityLookup, TrailingZeroInput},
BuildStorage, BuildStorage,
}; };
use xcm_builder::{ use xcm_builder::{
@@ -161,12 +162,15 @@ pub struct AlwaysSignedByDefault<Origin>(core::marker::PhantomData<Origin>);
impl<Origin> ConvertOrigin<Origin> for AlwaysSignedByDefault<Origin> impl<Origin> ConvertOrigin<Origin> for AlwaysSignedByDefault<Origin>
where where
Origin: OriginTrait, Origin: OriginTrait,
<Origin as OriginTrait>::AccountId: Default, <Origin as OriginTrait>::AccountId: Decode,
{ {
fn convert_origin( fn convert_origin(
_origin: impl Into<MultiLocation>, _origin: impl Into<MultiLocation>,
_kind: OriginKind, _kind: OriginKind,
) -> Result<Origin, MultiLocation> { ) -> Result<Origin, MultiLocation> {
Ok(Origin::signed(Default::default())) Ok(Origin::signed(
<Origin as OriginTrait>::AccountId::decode(&mut TrailingZeroInput::zeroes())
.expect("infinite length input; no invalid inputs for type; qed"),
))
} }
} }
@@ -52,9 +52,11 @@ impl From<Error> for XcmError {
/// ///
/// # Example /// # Example
/// ``` /// ```
/// use frame_support::parameter_types; /// use parity_scale_codec::Decode;
/// use frame_support::{parameter_types, PalletId};
/// use sp_runtime::traits::{AccountIdConversion, TrailingZeroInput};
/// use xcm::latest::prelude::*; /// use xcm::latest::prelude::*;
/// use xcm_builder::{ParentIsDefault, CurrencyAdapter, IsConcrete}; /// use xcm_builder::{ParentIsPreset, CurrencyAdapter, IsConcrete};
/// ///
/// /// Our chain's account id. /// /// Our chain's account id.
/// type AccountId = sp_runtime::AccountId32; /// type AccountId = sp_runtime::AccountId32;
@@ -62,12 +64,12 @@ impl From<Error> for XcmError {
/// /// Our relay chain's location. /// /// Our relay chain's location.
/// parameter_types! { /// parameter_types! {
/// pub RelayChain: MultiLocation = Parent.into(); /// pub RelayChain: MultiLocation = Parent.into();
/// pub CheckingAccount: AccountId = Default::default(); /// pub CheckingAccount: AccountId = PalletId(*b"checking").into_account();
/// } /// }
/// ///
/// /// Some items that implement `Convert<MultiLocation, AccountId>`. Can be more, but for now we just assume we accept /// /// Some items that implement `Convert<MultiLocation, AccountId>`. Can be more, but for now we just assume we accept
/// /// messages from the parent (relay chain). /// /// messages from the parent (relay chain).
/// pub type LocationConvertor = (ParentIsDefault<RelayChain>); /// pub type LocationConverter = (ParentIsPreset<AccountId>);
/// ///
/// /// Final currency adapter. This can be used in `xcm::Config` to specify how asset related transactions happen. /// /// Final currency adapter. This can be used in `xcm::Config` to specify how asset related transactions happen.
/// pub type AssetTransactor = CurrencyAdapter< /// pub type AssetTransactor = CurrencyAdapter<
@@ -75,8 +77,8 @@ impl From<Error> for XcmError {
/// u128, /// u128,
/// // The matcher: use the currency when the asset is a concrete asset in our relay chain. /// // The matcher: use the currency when the asset is a concrete asset in our relay chain.
/// IsConcrete<RelayChain>, /// IsConcrete<RelayChain>,
/// // The local convertor: default account of the parent relay chain. /// // The local converter: default account of the parent relay chain.
/// LocationConvertor, /// LocationConverter,
/// // Our chain's account ID type. /// // Our chain's account ID type.
/// AccountId, /// AccountId,
/// // The checking account. Can be any deterministic inaccessible account. /// // The checking account. Can be any deterministic inaccessible account.
+1 -1
View File
@@ -31,7 +31,7 @@ pub mod test_utils;
mod location_conversion; mod location_conversion;
pub use location_conversion::{ pub use location_conversion::{
Account32Hash, AccountId32Aliases, AccountKey20Aliases, ChildParachainConvertsVia, Account32Hash, AccountId32Aliases, AccountKey20Aliases, ChildParachainConvertsVia,
LocationInverter, ParentIsDefault, SiblingParachainConvertsVia, LocationInverter, ParentIsPreset, SiblingParachainConvertsVia,
}; };
mod origin_conversion; mod origin_conversion;
@@ -15,9 +15,9 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. // along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use frame_support::traits::Get; use frame_support::traits::Get;
use parity_scale_codec::Encode; use parity_scale_codec::{Decode, Encode};
use sp_io::hashing::blake2_256; use sp_io::hashing::blake2_256;
use sp_runtime::traits::AccountIdConversion; use sp_runtime::traits::{AccountIdConversion, TrailingZeroInput};
use sp_std::{borrow::Borrow, marker::PhantomData}; use sp_std::{borrow::Borrow, marker::PhantomData};
use xcm::latest::{Junction::*, Junctions::*, MultiLocation, NetworkId, Parent}; use xcm::latest::{Junction::*, Junctions::*, MultiLocation, NetworkId, Parent};
use xcm_executor::traits::{Convert, InvertLocation}; use xcm_executor::traits::{Convert, InvertLocation};
@@ -36,21 +36,26 @@ impl<Network: Get<NetworkId>, AccountId: From<[u8; 32]> + Into<[u8; 32]> + Clone
} }
/// A [`MultiLocation`] consisting of a single `Parent` [`Junction`] will be converted to the /// A [`MultiLocation`] consisting of a single `Parent` [`Junction`] will be converted to the
/// default value of `AccountId` (e.g. all zeros for `AccountId32`). /// parent `AccountId`.
pub struct ParentIsDefault<AccountId>(PhantomData<AccountId>); pub struct ParentIsPreset<AccountId>(PhantomData<AccountId>);
impl<AccountId: Default + Eq + Clone> Convert<MultiLocation, AccountId> impl<AccountId: Decode + Eq + Clone> Convert<MultiLocation, AccountId>
for ParentIsDefault<AccountId> for ParentIsPreset<AccountId>
{ {
fn convert_ref(location: impl Borrow<MultiLocation>) -> Result<AccountId, ()> { fn convert_ref(location: impl Borrow<MultiLocation>) -> Result<AccountId, ()> {
if location.borrow().contains_parents_only(1) { if location.borrow().contains_parents_only(1) {
Ok(AccountId::default()) Ok(b"Parent"
.using_encoded(|b| AccountId::decode(&mut TrailingZeroInput::new(b)))
.expect("infinite length input; no invalid inputs for type; qed"))
} else { } else {
Err(()) Err(())
} }
} }
fn reverse_ref(who: impl Borrow<AccountId>) -> Result<MultiLocation, ()> { fn reverse_ref(who: impl Borrow<AccountId>) -> Result<MultiLocation, ()> {
if who.borrow() == &AccountId::default() { let parent_account = b"Parent"
.using_encoded(|b| AccountId::decode(&mut TrailingZeroInput::new(b)))
.expect("infinite length input; no invalid inputs for type; qed");
if who.borrow() == &parent_account {
Ok(Parent.into()) Ok(Parent.into())
} else { } else {
Err(()) Err(())
@@ -39,7 +39,7 @@ use xcm::{latest::prelude::*, VersionedXcm};
use xcm_builder::{ use xcm_builder::{
AccountId32Aliases, AllowUnpaidExecutionFrom, CurrencyAdapter as XcmCurrencyAdapter, AccountId32Aliases, AllowUnpaidExecutionFrom, CurrencyAdapter as XcmCurrencyAdapter,
EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, IsConcrete, LocationInverter, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, IsConcrete, LocationInverter,
NativeAsset, ParentIsDefault, SiblingParachainConvertsVia, SignedAccountId32AsNative, NativeAsset, ParentIsPreset, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, SignedToAccountId32, SovereignSignedViaLocation,
}; };
use xcm_executor::{Config, XcmExecutor}; use xcm_executor::{Config, XcmExecutor};
@@ -108,7 +108,7 @@ parameter_types! {
} }
pub type LocationToAccountId = ( pub type LocationToAccountId = (
ParentIsDefault<AccountId>, ParentIsPreset<AccountId>,
SiblingParachainConvertsVia<Sibling, AccountId>, SiblingParachainConvertsVia<Sibling, AccountId>,
AccountId32Aliases<RelayNetwork, AccountId>, AccountId32Aliases<RelayNetwork, AccountId>,
); );
@@ -39,7 +39,7 @@ use xcm::{latest::prelude::*, VersionedXcm};
use xcm_builder::{ use xcm_builder::{
AccountId32Aliases, AllowUnpaidExecutionFrom, CurrencyAdapter as XcmCurrencyAdapter, AccountId32Aliases, AllowUnpaidExecutionFrom, CurrencyAdapter as XcmCurrencyAdapter,
EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, IsConcrete, LocationInverter, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, IsConcrete, LocationInverter,
NativeAsset, ParentIsDefault, SiblingParachainConvertsVia, SignedAccountId32AsNative, NativeAsset, ParentIsPreset, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, SignedToAccountId32, SovereignSignedViaLocation,
}; };
use xcm_executor::{Config, XcmExecutor}; use xcm_executor::{Config, XcmExecutor};
@@ -108,7 +108,7 @@ parameter_types! {
} }
pub type LocationToAccountId = ( pub type LocationToAccountId = (
ParentIsDefault<AccountId>, ParentIsPreset<AccountId>,
SiblingParachainConvertsVia<Sibling, AccountId>, SiblingParachainConvertsVia<Sibling, AccountId>,
AccountId32Aliases<RelayNetwork, AccountId>, AccountId32Aliases<RelayNetwork, AccountId>,
); );