Skip to content

Commit

Permalink
remove governance asset owner
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed Jan 22, 2025
1 parent 4fd2e86 commit cf918bf
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 115 deletions.
61 changes: 20 additions & 41 deletions pallets/moonbeam-foreign-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,34 +132,8 @@ pub mod pallet {
/// EVM runner
type EvmRunner: Runner<Self>;

/// Origin that is allowed to create a new foreign assets
type ForeignAssetCreatorOrigin: EnsureOriginWithArg<
Self::RuntimeOrigin,
Location,
Success = Self::AccountId,
>;

/// Origin that is allowed to freeze all tokens of a foreign asset
type ForeignAssetFreezerOrigin: EnsureOriginWithArg<
Self::RuntimeOrigin,
Location,
Success = Self::AccountId,
>;

/// Origin that is allowed to modify asset information for foreign assets
type ForeignAssetModifierOrigin: EnsureOriginWithArg<
Self::RuntimeOrigin,
Location,
Success = Self::AccountId,
>;

/// Origin that is allowed to unfreeze all tokens of a foreign asset that was previously
/// frozen
type ForeignAssetUnfreezerOrigin: EnsureOriginWithArg<
Self::RuntimeOrigin,
Location,
Success = Self::AccountId,
>;
/// Origin that is allowed to create new foreign assets
type EnsureXcmLocation: EnsureXcmLocation<Self>;

/// Hook to be called when new foreign asset is registered.
type OnForeignAssetCreated: ForeignAssetCreatedHook<Location>;
Expand Down Expand Up @@ -216,6 +190,7 @@ pub mod pallet {
/// Account has insufficient balance for locking
InsufficientBalance,
OriginIsNotAssetCreator,
CannotConvertLocationToAccount,
InvalidSymbol,
InvalidTokenName,
LocationAlreadyExists,
Expand Down Expand Up @@ -275,15 +250,9 @@ pub mod pallet {
pub type AssetsCreationDetails<T: Config> =
StorageMap<_, Blake2_128Concat, AssetId, AssetCreationDetails<T>>;

#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub enum AssetOwner<T: Config> {
Governance,
Account(T::AccountId),
}

#[derive(Clone, Decode, Encode, Eq, PartialEq, Debug, TypeInfo, MaxEncodedLen)]
pub struct AssetCreationDetails<T: Config> {
pub owner: AssetOwner<T>,
pub owner: T::AccountId,
pub deposit: Option<BalanceOf<T>>,
}

Expand Down Expand Up @@ -338,7 +307,8 @@ pub mod pallet {
let name = core::str::from_utf8(&name).map_err(|_| Error::<T>::InvalidTokenName)?;

let contract_address = EvmCaller::<T>::erc20_create(asset_id, decimals, symbol, name)?;
let owner = AssetOwner::<T>::Governance;
let owner = T::EnsureXcmLocation::account_for_location(&xcm_location)
.ok_or(Error::<T>::CannotConvertLocationToAccount)?;

// Insert the association assetId->foreigAsset
// Insert the association foreigAsset->assetId
Expand Down Expand Up @@ -427,7 +397,7 @@ pub mod pallet {
name: BoundedVec<u8, ConstU32<256>>,
) -> DispatchResult {
let owner_account =
T::ForeignAssetCreatorOrigin::ensure_origin(origin.clone(), &xcm_location)?;
T::EnsureXcmLocation::ensure_xcm_origin(origin.clone(), &xcm_location)?;

// Ensure such an assetId does not exist
ensure!(
Expand All @@ -454,7 +424,7 @@ pub mod pallet {
let name = core::str::from_utf8(&name).map_err(|_| Error::<T>::InvalidTokenName)?;
let contract_address = EvmCaller::<T>::erc20_create(asset_id, decimals, symbol, name)?;
let deposit = T::ForeignAssetCreationDeposit::get();
let owner = AssetOwner::<T>::Account(owner_account.clone());
let owner = owner_account.clone();

// Insert the association assetId->foreigAsset
// Insert the association foreigAsset->assetId
Expand Down Expand Up @@ -495,7 +465,7 @@ pub mod pallet {
new_xcm_location: Location,
) -> DispatchResult {
// Ensures that the origin is an XCM location that contains the asset
T::ForeignAssetModifierOrigin::ensure_origin(origin, &new_xcm_location)?;
T::EnsureXcmLocation::ensure_xcm_origin(origin, &new_xcm_location)?;

let previous_location =
AssetsById::<T>::get(&asset_id).ok_or(Error::<T>::AssetDoesNotExist)?;
Expand Down Expand Up @@ -535,7 +505,7 @@ pub mod pallet {

// Ensures that the origin is an XCM location that owns the asset
// represented by the assets xcm location
T::ForeignAssetFreezerOrigin::ensure_origin(origin, &xcm_location)?;
T::EnsureXcmLocation::ensure_xcm_origin(origin, &xcm_location)?;

let (_asset_id, asset_status) = AssetsByLocation::<T>::get(&xcm_location)
.ok_or(Error::<T>::CorruptedStorageOrphanLocation)?;
Expand Down Expand Up @@ -571,7 +541,7 @@ pub mod pallet {
let xcm_location =
AssetsById::<T>::get(&asset_id).ok_or(Error::<T>::AssetDoesNotExist)?;
// Ensures that the origin is an XCM location that contains the asset
T::ForeignAssetUnfreezerOrigin::ensure_origin(origin, &xcm_location)?;
T::EnsureXcmLocation::ensure_xcm_origin(origin, &xcm_location)?;

let (_asset_id, asset_status) = AssetsByLocation::<T>::get(&xcm_location)
.ok_or(Error::<T>::CorruptedStorageOrphanLocation)?;
Expand Down Expand Up @@ -689,4 +659,13 @@ pub mod pallet {
AssetsById::<T>::get(asset_id)
}
}

/// A trait to ensure that the origin is an XCM location that contains the asset
pub trait EnsureXcmLocation<T: Config> {
fn ensure_xcm_origin(
origin: T::RuntimeOrigin,
location: &Location,
) -> Result<T::AccountId, DispatchError>;
fn account_for_location(location: &Location) -> Option<T::AccountId>;
}
}
17 changes: 13 additions & 4 deletions pallets/moonbeam-foreign-assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,23 @@ parameter_types! {
pub const ForeignAssetCreationDeposit: u128 = 1;
}

pub struct ForeignAssetsEnsureXCM;

impl EnsureXcmLocation<Test> for ForeignAssetsEnsureXCM {
fn ensure_xcm_origin(origin: RuntimeOrigin, location: &Location) -> Result<AccountId, DispatchError> {
ensure_signed(origin).map_err(|_| DispatchError::BadOrigin)
}

fn account_for_location(location: &Location) -> Option<AccountId> {
Some(Alice.into())
}
}

impl crate::Config for Test {
type AccountIdToH160 = AccountIdToH160;
type AssetIdFilter = Everything;
type EvmRunner = pallet_evm::runner::stack::Runner<Self>;
type ForeignAssetCreatorOrigin = EnsureSigned<AccountId>;
type ForeignAssetFreezerOrigin = EnsureSigned<AccountId>;
type ForeignAssetModifierOrigin = EnsureSigned<AccountId>;
type ForeignAssetUnfreezerOrigin = EnsureSigned<AccountId>;
type EnsureXcmLocation = ForeignAssetsEnsureXCM;
type OnForeignAssetCreated = NoteDownHook<Location>;
type MaxForeignAssets = ConstU32<3>;
type RuntimeEvent = RuntimeEvent;
Expand Down
4 changes: 2 additions & 2 deletions pallets/moonbeam-foreign-assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn create_foreign_and_freeze_unfreeze() {
assert_eq!(
EvmForeignAssets::assets_creation_details(&1),
Some(AssetCreationDetails {
owner: AssetOwner::Account(Alice.into()),
owner: Alice.into(),
deposit: Some(ForeignAssetCreationDeposit::get())
})
);
Expand Down Expand Up @@ -85,7 +85,7 @@ fn create_foreign_and_freeze_unfreeze() {
assert_eq!(
EvmForeignAssets::assets_creation_details(&1),
Some(AssetCreationDetails {
owner: AssetOwner::Account(Alice.into()),
owner: Alice.into(),
deposit: Some(1)
})
);
Expand Down
20 changes: 15 additions & 5 deletions pallets/moonbeam-lazy-migrations/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ use frame_support::{construct_runtime, parameter_types, traits::Everything, weig
use frame_system::{EnsureRoot, EnsureSigned};
use pallet_asset_manager::AssetRegistrar;
use pallet_evm::{EnsureAddressNever, EnsureAddressRoot, FrameSystemAccountProvider};
use precompile_utils::testing::MockAccount;
use precompile_utils::testing::{Alice, MockAccount};
use sp_core::{ConstU32, H160, H256, U256};
use sp_runtime::{
traits::{BlakeTwo256, Hash, IdentityLookup},
BuildStorage, Perbill,
};
use pallet_moonbeam_foreign_assets::EnsureXcmLocation;

pub type AssetId = u128;
pub type Balance = u128;
Expand Down Expand Up @@ -288,14 +289,23 @@ impl sp_runtime::traits::Convert<AccountId, H160> for AccountIdToH160 {
}
}

pub struct ForeignAssetsEnsureXCM;

impl EnsureXcmLocation<Test> for ForeignAssetsEnsureXCM {
fn ensure_xcm_origin(origin: RuntimeOrigin, location: &Location) -> Result<AccountId, DispatchError> {
ensure_signed(origin).map_err(|_| DispatchError::BadOrigin)
}

fn account_for_location(location: &Location) -> Option<AccountId> {
Some(Alice.into())
}
}

impl pallet_moonbeam_foreign_assets::Config for Test {
type AccountIdToH160 = AccountIdToH160;
type AssetIdFilter = Everything;
type EvmRunner = pallet_evm::runner::stack::Runner<Self>;
type ForeignAssetCreatorOrigin = EnsureSigned<AccountId>;
type ForeignAssetFreezerOrigin = EnsureRoot<AccountId>;
type ForeignAssetModifierOrigin = EnsureRoot<AccountId>;
type ForeignAssetUnfreezerOrigin = EnsureRoot<AccountId>;
type EnsureXcmLocation = ForeignAssetsEnsureXCM;
type OnForeignAssetCreated = ();
type MaxForeignAssets = ConstU32<3>;
type RuntimeEvent = RuntimeEvent;
Expand Down
37 changes: 21 additions & 16 deletions runtime/moonbase/src/foreign_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,36 @@
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use crate::xcm_config::LocationToAccountId;
use crate::RuntimeOrigin;
use frame_support::traits::{EnsureOrigin, EnsureOriginWithArg, Everything};
use crate::{Runtime, RuntimeOrigin};
use frame_support::ensure;
use frame_support::traits::{EnsureOrigin, Everything};
use frame_system::ensure_signed;
use moonbeam_core_primitives::AccountId;
use pallet_moonbeam_foreign_assets::EnsureXcmLocation;
use sp_runtime::DispatchError;
use xcm::latest::Location;
use xcm_executor::traits::ConvertLocation;

// `EnsureOriginWithArg` impl for `ForeignAssetOwnerOrigin` which allows only XCM origins
// which are locations containing the class location.
pub struct ForeignAssetOwnerOrigin;
impl EnsureOriginWithArg<RuntimeOrigin, Location> for ForeignAssetOwnerOrigin {
type Success = AccountId;
pub struct ForeignAssetsEnsureXcmLocation;

fn try_origin(
impl EnsureXcmLocation<Runtime> for ForeignAssetsEnsureXcmLocation {
fn ensure_xcm_origin(
o: RuntimeOrigin,
a: &Location,
) -> core::result::Result<Self::Success, RuntimeOrigin> {
let origin_location = pallet_xcm::EnsureXcm::<Everything>::try_origin(o.clone())?;
if !a.starts_with(&origin_location) {
return Err(o);
}
LocationToAccountId::convert_location(&origin_location).ok_or(o)
location: &Location,
) -> Result<AccountId, DispatchError> {
let origin_account = ensure_signed(o.clone())?;
let origin_location = pallet_xcm::EnsureXcm::<Everything>::try_origin(o.clone())
.map_err(|_| DispatchError::BadOrigin)?;
ensure!(
location.starts_with(&origin_location),
DispatchError::BadOrigin
);
Ok(Self::account_for_location(location).unwrap_or(origin_account))
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &Location) -> Result<RuntimeOrigin, ()> {
Ok(pallet_xcm::Origin::Xcm(a.clone()).into())
fn account_for_location(location: &Location) -> Option<AccountId> {
LocationToAccountId::convert_location(location)
}
}
7 changes: 2 additions & 5 deletions runtime/moonbase/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use xcm_primitives::{
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

use crate::foreign_origin::ForeignAssetOwnerOrigin;
use crate::foreign_origin::{ForeignAssetsEnsureXcmLocation};
use crate::governance::referenda::{FastGeneralAdminOrRoot, GeneralAdminOrRoot};
use sp_core::Get;
use sp_std::{
Expand Down Expand Up @@ -707,10 +707,7 @@ impl pallet_moonbeam_foreign_assets::Config for Runtime {
type AccountIdToH160 = AccountIdToH160;
type AssetIdFilter = EvmForeignAssetIdFilter;
type EvmRunner = EvmRunnerPrecompileOrEthXcm<MoonbeamCall, Self>;
type ForeignAssetCreatorOrigin = ForeignAssetOwnerOrigin;
type ForeignAssetFreezerOrigin = ForeignAssetOwnerOrigin;
type ForeignAssetModifierOrigin = ForeignAssetOwnerOrigin;
type ForeignAssetUnfreezerOrigin = ForeignAssetOwnerOrigin;
type EnsureXcmLocation = ForeignAssetsEnsureXcmLocation;
type OnForeignAssetCreated = ();
type MaxForeignAssets = ConstU32<256>;
type RuntimeEvent = RuntimeEvent;
Expand Down
37 changes: 21 additions & 16 deletions runtime/moonbeam/src/foreign_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,36 @@
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use crate::xcm_config::LocationToAccountId;
use crate::RuntimeOrigin;
use frame_support::traits::{EnsureOrigin, EnsureOriginWithArg, Everything};
use crate::{Runtime, RuntimeOrigin};
use frame_support::ensure;
use frame_support::traits::{EnsureOrigin, Everything};
use frame_system::ensure_signed;
use moonbeam_core_primitives::AccountId;
use pallet_moonbeam_foreign_assets::EnsureXcmLocation;
use sp_runtime::DispatchError;
use xcm::latest::Location;
use xcm_executor::traits::ConvertLocation;

// `EnsureOriginWithArg` impl for `ForeignAssetOwnerOrigin` which allows only XCM origins
// which are locations containing the class location.
pub struct ForeignAssetOwnerOrigin;
impl EnsureOriginWithArg<RuntimeOrigin, Location> for ForeignAssetOwnerOrigin {
type Success = AccountId;
pub struct ForeignAssetsEnsureXcmLocation;

fn try_origin(
impl EnsureXcmLocation<Runtime> for ForeignAssetsEnsureXcmLocation {
fn ensure_xcm_origin(
o: RuntimeOrigin,
a: &Location,
) -> core::result::Result<Self::Success, RuntimeOrigin> {
let origin_location = pallet_xcm::EnsureXcm::<Everything>::try_origin(o.clone())?;
if !a.starts_with(&origin_location) {
return Err(o);
}
LocationToAccountId::convert_location(&origin_location).ok_or(o)
location: &Location,
) -> Result<AccountId, DispatchError> {
let origin_account = ensure_signed(o.clone())?;
let origin_location = pallet_xcm::EnsureXcm::<Everything>::try_origin(o.clone())
.map_err(|_| DispatchError::BadOrigin)?;
ensure!(
location.starts_with(&origin_location),
DispatchError::BadOrigin
);
Ok(Self::account_for_location(location).unwrap_or(origin_account))
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &Location) -> Result<RuntimeOrigin, ()> {
Ok(pallet_xcm::Origin::Xcm(a.clone()).into())
fn account_for_location(location: &Location) -> Option<AccountId> {
LocationToAccountId::convert_location(location)
}
}
7 changes: 2 additions & 5 deletions runtime/moonbeam/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use xcm_primitives::{
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

use crate::foreign_origin::ForeignAssetOwnerOrigin;
use crate::foreign_origin::{ForeignAssetsEnsureXcmLocation};
use crate::governance::referenda::{FastGeneralAdminOrRoot, GeneralAdminOrRoot};
use sp_core::Get;
use sp_std::{
Expand Down Expand Up @@ -692,10 +692,7 @@ impl pallet_moonbeam_foreign_assets::Config for Runtime {
type AccountIdToH160 = AccountIdToH160;
type AssetIdFilter = EvmForeignAssetIdFilter;
type EvmRunner = EvmRunnerPrecompileOrEthXcm<MoonbeamCall, Self>;
type ForeignAssetCreatorOrigin = ForeignAssetOwnerOrigin;
type ForeignAssetFreezerOrigin = ForeignAssetOwnerOrigin;
type ForeignAssetModifierOrigin = ForeignAssetOwnerOrigin;
type ForeignAssetUnfreezerOrigin = ForeignAssetOwnerOrigin;
type EnsureXcmLocation = ForeignAssetsEnsureXcmLocation;
type OnForeignAssetCreated = ();
type MaxForeignAssets = ConstU32<256>;
type RuntimeEvent = RuntimeEvent;
Expand Down
Loading

0 comments on commit cf918bf

Please sign in to comment.