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

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

Merged
merged 10 commits into from
Jan 21, 2022
6 changes: 3 additions & 3 deletions bridges/bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<AccountId>,
// The parent (Relay-chain) origin converts to the parent `AccountId`.
ParentIsPreset<AccountId>,
// Sibling parachain origins convert to AccountId via the `ParaId::into`.
SiblingParachainConvertsVia<Sibling, AccountId>,
// Straight up local `AccountId32` origins just alias directly to `AccountId`.
Expand Down
10 changes: 7 additions & 3 deletions xcm/pallet-xcm-benchmarks/src/generic/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
//! A mock runtime for XCM benchmarking.

use crate::{generic, mock::*, *};
use codec::Decode;
use frame_support::{
parameter_types,
traits::{Everything, OriginTrait},
};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
traits::{BlakeTwo256, IdentityLookup, TrailingZeroInput},
BuildStorage,
};
use xcm_builder::{
Expand Down Expand Up @@ -161,12 +162,15 @@ pub struct AlwaysSignedByDefault<Origin>(core::marker::PhantomData<Origin>);
impl<Origin> ConvertOrigin<Origin> for AlwaysSignedByDefault<Origin>
where
Origin: OriginTrait,
<Origin as OriginTrait>::AccountId: Default,
<Origin as OriginTrait>::AccountId: Decode,
{
fn convert_origin(
_origin: impl Into<MultiLocation>,
_kind: OriginKind,
) -> 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"),
))
}
}
14 changes: 8 additions & 6 deletions xcm/xcm-builder/src/currency_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,33 @@ impl From<Error> 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;
///
/// /// 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<MultiLocation, AccountId>`. Can be more, but for now we just assume we accept
/// /// 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.
/// pub type AssetTransactor = CurrencyAdapter<
/// // Use this balance type:
/// u128,
/// // The matcher: use the currency when the asset is a concrete asset in our relay chain.
/// IsConcrete<RelayChain>,
/// // 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.
Expand Down
2 changes: 1 addition & 1 deletion xcm/xcm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 13 additions & 8 deletions xcm/xcm-builder/src/location_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

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};
Expand All @@ -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
/// default value of `AccountId` (e.g. all zeros for `AccountId32`).
pub struct ParentIsDefault<AccountId>(PhantomData<AccountId>);
impl<AccountId: Default + Eq + Clone> Convert<MultiLocation, AccountId>
for ParentIsDefault<AccountId>
/// parent `AccountId`.
pub struct ParentIsPreset<AccountId>(PhantomData<AccountId>);
impl<AccountId: Decode + Eq + Clone> Convert<MultiLocation, AccountId>
for ParentIsPreset<AccountId>
{
fn convert_ref(location: impl Borrow<MultiLocation>) -> Result<AccountId, ()> {
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<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())
} else {
Err(())
Expand Down
4 changes: 2 additions & 2 deletions xcm/xcm-simulator/example/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -108,7 +108,7 @@ parameter_types! {
}

pub type LocationToAccountId = (
ParentIsDefault<AccountId>,
ParentIsPreset<AccountId>,
SiblingParachainConvertsVia<Sibling, AccountId>,
AccountId32Aliases<RelayNetwork, AccountId>,
);
Expand Down
4 changes: 2 additions & 2 deletions xcm/xcm-simulator/fuzzer/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -108,7 +108,7 @@ parameter_types! {
}

pub type LocationToAccountId = (
ParentIsDefault<AccountId>,
ParentIsPreset<AccountId>,
SiblingParachainConvertsVia<Sibling, AccountId>,
AccountId32Aliases<RelayNetwork, AccountId>,
);
Expand Down