From efe290490a407f0c3f78c7c4954229f9e810cb03 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Fri, 21 Jan 2022 03:03:39 -0800 Subject: [PATCH] Remove `Default` bound on `AccountId` types under the xcm directory (#4712) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor ParentIsDefault to ParentIsAllZeroes * Remove Default bound on all AccountId types under the xcm directory * Change to ParentIs, 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 * Use preset account ID value for parent MultiLocations Co-authored-by: Bastian Köcher --- .../bin/rialto-parachain/runtime/src/lib.rs | 6 +++--- .../pallet-xcm-benchmarks/src/generic/mock.rs | 10 ++++++--- .../xcm/xcm-builder/src/currency_adapter.rs | 14 +++++++------ polkadot/xcm/xcm-builder/src/lib.rs | 2 +- .../xcm-builder/src/location_conversion.rs | 21 ++++++++++++------- .../xcm-simulator/example/src/parachain.rs | 4 ++-- .../xcm/xcm-simulator/fuzzer/src/parachain.rs | 4 ++-- 7 files changed, 36 insertions(+), 25 deletions(-) diff --git a/polkadot/bridges/bin/rialto-parachain/runtime/src/lib.rs b/polkadot/bridges/bin/rialto-parachain/runtime/src/lib.rs index e4ac28d2ad..407af35727 100644 --- a/polkadot/bridges/bin/rialto-parachain/runtime/src/lib.rs +++ b/polkadot/bridges/bin/rialto-parachain/runtime/src/lib.rs @@ -70,7 +70,7 @@ use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, CurrencyAdapter, EnsureXcmOrigin, FixedWeightBounds, IsConcrete, LocationInverter, NativeAsset, - ParentAsSuperuser, ParentIsDefault, RelayChainAsNative, SiblingParachainAsNative, + ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents, }; @@ -294,8 +294,8 @@ parameter_types! { /// when determining ownership of accounts for asset transacting and when attempting to use XCM /// `Transact` in order to determine the dispatch Origin. pub type LocationToAccountId = ( - // The parent (Relay-chain) origin converts to the default `AccountId`. - ParentIsDefault, + // The parent (Relay-chain) origin converts to the parent `AccountId`. + ParentIsPreset, // Sibling parachain origins convert to AccountId via the `ParaId::into`. SiblingParachainConvertsVia, // Straight up local `AccountId32` origins just alias directly to `AccountId`. diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs index 0ad1f72831..7782ba1d90 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs @@ -17,6 +17,7 @@ //! A mock runtime for XCM benchmarking. use crate::{generic, mock::*, *}; +use codec::Decode; use frame_support::{ parameter_types, traits::{Everything, OriginTrait}, @@ -24,7 +25,7 @@ use frame_support::{ use sp_core::H256; use sp_runtime::{ testing::Header, - traits::{BlakeTwo256, IdentityLookup}, + traits::{BlakeTwo256, IdentityLookup, TrailingZeroInput}, BuildStorage, }; use xcm_builder::{ @@ -161,12 +162,15 @@ pub struct AlwaysSignedByDefault(core::marker::PhantomData); impl ConvertOrigin for AlwaysSignedByDefault where Origin: OriginTrait, - ::AccountId: Default, + ::AccountId: Decode, { fn convert_origin( _origin: impl Into, _kind: OriginKind, ) -> Result { - Ok(Origin::signed(Default::default())) + Ok(Origin::signed( + ::AccountId::decode(&mut TrailingZeroInput::zeroes()) + .expect("infinite length input; no invalid inputs for type; qed"), + )) } } diff --git a/polkadot/xcm/xcm-builder/src/currency_adapter.rs b/polkadot/xcm/xcm-builder/src/currency_adapter.rs index 23da4fcfc8..86a2f54901 100644 --- a/polkadot/xcm/xcm-builder/src/currency_adapter.rs +++ b/polkadot/xcm/xcm-builder/src/currency_adapter.rs @@ -52,9 +52,11 @@ impl From for XcmError { /// /// # 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_builder::{ParentIsDefault, CurrencyAdapter, IsConcrete}; +/// use xcm_builder::{ParentIsPreset, CurrencyAdapter, IsConcrete}; /// /// /// Our chain's account id. /// type AccountId = sp_runtime::AccountId32; @@ -62,12 +64,12 @@ impl From for XcmError { /// /// Our relay chain's location. /// parameter_types! { /// pub RelayChain: MultiLocation = Parent.into(); -/// pub CheckingAccount: AccountId = Default::default(); +/// pub CheckingAccount: AccountId = PalletId(*b"checking").into_account(); /// } /// /// /// Some items that implement `Convert`. Can be more, but for now we just assume we accept /// /// messages from the parent (relay chain). -/// pub type LocationConvertor = (ParentIsDefault); +/// pub type LocationConverter = (ParentIsPreset); /// /// /// Final currency adapter. This can be used in `xcm::Config` to specify how asset related transactions happen. /// pub type AssetTransactor = CurrencyAdapter< @@ -75,8 +77,8 @@ impl From for XcmError { /// u128, /// // The matcher: use the currency when the asset is a concrete asset in our relay chain. /// IsConcrete, -/// // The local convertor: default account of the parent relay chain. -/// LocationConvertor, +/// // The local converter: default account of the parent relay chain. +/// LocationConverter, /// // Our chain's account ID type. /// AccountId, /// // The checking account. Can be any deterministic inaccessible account. diff --git a/polkadot/xcm/xcm-builder/src/lib.rs b/polkadot/xcm/xcm-builder/src/lib.rs index 7d94312a46..e3473d55d5 100644 --- a/polkadot/xcm/xcm-builder/src/lib.rs +++ b/polkadot/xcm/xcm-builder/src/lib.rs @@ -31,7 +31,7 @@ pub mod test_utils; mod location_conversion; pub use location_conversion::{ Account32Hash, AccountId32Aliases, AccountKey20Aliases, ChildParachainConvertsVia, - LocationInverter, ParentIsDefault, SiblingParachainConvertsVia, + LocationInverter, ParentIsPreset, SiblingParachainConvertsVia, }; mod origin_conversion; diff --git a/polkadot/xcm/xcm-builder/src/location_conversion.rs b/polkadot/xcm/xcm-builder/src/location_conversion.rs index a6933b5266..a5bb40c87e 100644 --- a/polkadot/xcm/xcm-builder/src/location_conversion.rs +++ b/polkadot/xcm/xcm-builder/src/location_conversion.rs @@ -15,9 +15,9 @@ // along with Polkadot. If not, see . use frame_support::traits::Get; -use parity_scale_codec::Encode; +use parity_scale_codec::{Decode, Encode}; 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 xcm::latest::{Junction::*, Junctions::*, MultiLocation, NetworkId, Parent}; use xcm_executor::traits::{Convert, InvertLocation}; @@ -36,21 +36,26 @@ impl, AccountId: From<[u8; 32]> + Into<[u8; 32]> + Clone } /// A [`MultiLocation`] consisting of a single `Parent` [`Junction`] will be converted to the -/// default value of `AccountId` (e.g. all zeros for `AccountId32`). -pub struct ParentIsDefault(PhantomData); -impl Convert - for ParentIsDefault +/// parent `AccountId`. +pub struct ParentIsPreset(PhantomData); +impl Convert + for ParentIsPreset { fn convert_ref(location: impl Borrow) -> Result { 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 { Err(()) } } fn reverse_ref(who: impl Borrow) -> Result { - 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()) } else { Err(()) diff --git a/polkadot/xcm/xcm-simulator/example/src/parachain.rs b/polkadot/xcm/xcm-simulator/example/src/parachain.rs index 94c3c9d543..5bde85e39f 100644 --- a/polkadot/xcm/xcm-simulator/example/src/parachain.rs +++ b/polkadot/xcm/xcm-simulator/example/src/parachain.rs @@ -39,7 +39,7 @@ use xcm::{latest::prelude::*, VersionedXcm}; use xcm_builder::{ AccountId32Aliases, AllowUnpaidExecutionFrom, CurrencyAdapter as XcmCurrencyAdapter, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, IsConcrete, LocationInverter, - NativeAsset, ParentIsDefault, SiblingParachainConvertsVia, SignedAccountId32AsNative, + NativeAsset, ParentIsPreset, SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, }; use xcm_executor::{Config, XcmExecutor}; @@ -108,7 +108,7 @@ parameter_types! { } pub type LocationToAccountId = ( - ParentIsDefault, + ParentIsPreset, SiblingParachainConvertsVia, AccountId32Aliases, ); diff --git a/polkadot/xcm/xcm-simulator/fuzzer/src/parachain.rs b/polkadot/xcm/xcm-simulator/fuzzer/src/parachain.rs index 2e82d9a199..bfc7e4858a 100644 --- a/polkadot/xcm/xcm-simulator/fuzzer/src/parachain.rs +++ b/polkadot/xcm/xcm-simulator/fuzzer/src/parachain.rs @@ -39,7 +39,7 @@ use xcm::{latest::prelude::*, VersionedXcm}; use xcm_builder::{ AccountId32Aliases, AllowUnpaidExecutionFrom, CurrencyAdapter as XcmCurrencyAdapter, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, IsConcrete, LocationInverter, - NativeAsset, ParentIsDefault, SiblingParachainConvertsVia, SignedAccountId32AsNative, + NativeAsset, ParentIsPreset, SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, }; use xcm_executor::{Config, XcmExecutor}; @@ -108,7 +108,7 @@ parameter_types! { } pub type LocationToAccountId = ( - ParentIsDefault, + ParentIsPreset, SiblingParachainConvertsVia, AccountId32Aliases, );