lib.rs 97.7 KiB
Newer Older
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
Gavin Wood's avatar
Gavin Wood committed
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
Gavin Wood's avatar
Gavin Wood committed
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
Gavin Wood's avatar
Gavin Wood committed

//! Pallet to handle XCM messages.

#![cfg_attr(not(feature = "std"), no_std)]

Gavin Wood's avatar
Gavin Wood committed
#[cfg(feature = "runtime-benchmarks")]
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

Gavin Wood's avatar
Gavin Wood committed
pub mod migration;

use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use frame_support::{
	dispatch::GetDispatchInfo,
	pallet_prelude::*,
	traits::{
		Contains, ContainsPair, Currency, Defensive, EnsureOrigin, Get, LockableCurrency,
		OriginTrait, WithdrawReasons,
	},
	PalletId,
Gavin Wood's avatar
Gavin Wood committed
};
use frame_system::pallet_prelude::{BlockNumberFor, *};
pub use pallet::*;
use sp_runtime::{
Gavin Wood's avatar
Gavin Wood committed
	traits::{
		AccountIdConversion, BadOrigin, BlakeTwo256, BlockNumberProvider, Dispatchable, Hash,
		Saturating, Zero,
Gavin Wood's avatar
Gavin Wood committed
	},
	RuntimeDebug,
};
use sp_std::{boxed::Box, marker::PhantomData, prelude::*, result::Result, vec};
Gavin Wood's avatar
Gavin Wood committed
use xcm::{latest::QueryResponseInfo, prelude::*};
use xcm_builder::{
	ExecuteController, ExecuteControllerWeightInfo, QueryController, QueryControllerWeightInfo,
	SendController, SendControllerWeightInfo,
Gavin Wood's avatar
Gavin Wood committed
};
use xcm_executor::{
	traits::{
		AssetTransferError, CheckSuspension, ClaimAssets, ConvertLocation, ConvertOrigin,
		DropAssets, MatchesFungible, OnResponse, Properties, QueryHandler, QueryResponseStatus,
		TransactAsset, TransferType, VersionChangeNotifier, WeightBounds, XcmAssetTransfers,
Gavin Wood's avatar
Gavin Wood committed
	},
	Assets,
};

pub trait WeightInfo {
	fn send() -> Weight;
	fn teleport_assets() -> Weight;
	fn reserve_transfer_assets() -> Weight;
	fn execute() -> Weight;
	fn force_xcm_version() -> Weight;
	fn force_default_xcm_version() -> Weight;
	fn force_subscribe_version_notify() -> Weight;
	fn force_unsubscribe_version_notify() -> Weight;
	fn force_suspension() -> Weight;
Gavin Wood's avatar
Gavin Wood committed
	fn migrate_supported_version() -> Weight;
	fn migrate_version_notifiers() -> Weight;
	fn already_notified_target() -> Weight;
	fn notify_current_targets() -> Weight;
	fn notify_target_migration_fail() -> Weight;
	fn migrate_version_notify_targets() -> Weight;
	fn migrate_and_notify_old_targets() -> Weight;
	fn new_query() -> Weight;
	fn take_response() -> Weight;
Gavin Wood's avatar
Gavin Wood committed
}

/// fallback implementation
pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
	fn send() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn teleport_assets() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn reserve_transfer_assets() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn execute() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn force_xcm_version() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn force_default_xcm_version() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn force_subscribe_version_notify() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn force_unsubscribe_version_notify() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn force_suspension() -> Weight {
		Weight::from_parts(100_000_000, 0)
	}

Gavin Wood's avatar
Gavin Wood committed
	fn migrate_supported_version() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn migrate_version_notifiers() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn already_notified_target() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn notify_current_targets() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn notify_target_migration_fail() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn migrate_version_notify_targets() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn migrate_and_notify_old_targets() -> Weight {
		Weight::from_parts(100_000_000, 0)
Gavin Wood's avatar
Gavin Wood committed
	}

	fn new_query() -> Weight {
		Weight::from_parts(100_000_000, 0)
	}

	fn take_response() -> Weight {
		Weight::from_parts(100_000_000, 0)
	}
Gavin Wood's avatar
Gavin Wood committed
}
Gavin Wood's avatar
Gavin Wood committed

#[frame_support::pallet]
pub mod pallet {
	use super::*;
		dispatch::{GetDispatchInfo, PostDispatchInfo},
		parameter_types,
Gavin Wood's avatar
Gavin Wood committed
	use frame_system::Config as SysConfig;
	use sp_runtime::traits::Dispatchable;
Gavin Wood's avatar
Gavin Wood committed
	use xcm_executor::traits::{MatchesFungible, WeightBounds};
	parameter_types! {
		/// An implementation of `Get<u32>` which just returns the latest XCM version which we can
		/// support.
		pub const CurrentXcmVersion: u32 = XCM_VERSION;
	}

	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

Gavin Wood's avatar
Gavin Wood committed
	#[pallet::pallet]
	#[pallet::storage_version(STORAGE_VERSION)]
	#[pallet::without_storage_info]
Gavin Wood's avatar
Gavin Wood committed
	pub struct Pallet<T>(_);

Gavin Wood's avatar
Gavin Wood committed
	pub type BalanceOf<T> =
		<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

Gavin Wood's avatar
Gavin Wood committed
	#[pallet::config]
	/// The module configuration trait.
	pub trait Config: frame_system::Config {
		/// The overarching event type.
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
Gavin Wood's avatar
Gavin Wood committed
		/// A lockable currency.
		// TODO: We should really use a trait which can handle multiple currencies.
		type Currency: LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>;
Gavin Wood's avatar
Gavin Wood committed

		/// The `MultiAsset` matcher for `Currency`.
Loading full blame...