diff --git a/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs b/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs index 3c06131dd6088..bf3c00b3ff1f5 100644 --- a/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs +++ b/substrate/frame/contracts/mock-network/src/parachain/contracts_config.rs @@ -15,87 +15,19 @@ // along with Polkadot. If not, see . use super::{Balances, Runtime, RuntimeCall, RuntimeEvent}; -use crate::{ - parachain, - parachain::RuntimeHoldReason, - primitives::{Balance, CENTS}, -}; -use frame_support::{ - parameter_types, - traits::{ConstBool, ConstU32, Contains, Randomness}, - weights::Weight, -}; -use frame_system::{pallet_prelude::BlockNumberFor, EnsureSigned}; -use pallet_xcm::BalanceOf; -use sp_runtime::{traits::Convert, Perbill}; - -pub const fn deposit(items: u32, bytes: u32) -> Balance { - items as Balance * 1 * CENTS + (bytes as Balance) * 1 * CENTS -} +use crate::parachain::RuntimeHoldReason; +use frame_support::{derive_impl, parameter_types}; parameter_types! { - pub const DepositPerItem: Balance = deposit(1, 0); - pub const DepositPerByte: Balance = deposit(0, 1); - pub const DefaultDepositLimit: Balance = deposit(1024, 1024 * 1024); pub Schedule: pallet_contracts::Schedule = Default::default(); - pub const CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0); - pub const MaxDelegateDependencies: u32 = 32; -} - -pub struct DummyRandomness(sp_std::marker::PhantomData); - -impl Randomness> for DummyRandomness { - fn random(_subject: &[u8]) -> (T::Hash, BlockNumberFor) { - (Default::default(), Default::default()) - } -} - -impl Convert> for Runtime { - fn convert(w: Weight) -> BalanceOf { - w.ref_time().into() - } -} - -#[derive(Clone, Default)] -pub struct Filters; - -impl Contains for Filters { - fn contains(call: &RuntimeCall) -> bool { - match call { - parachain::RuntimeCall::Contracts(_) => true, - _ => false, - } - } } +#[derive_impl(pallet_contracts::config_preludes::TestDefaultConfig)] impl pallet_contracts::Config for Runtime { type AddressGenerator = pallet_contracts::DefaultAddressGenerator; - type CallFilter = Filters; type CallStack = [pallet_contracts::Frame; 5]; - type ChainExtension = (); - type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; type Currency = Balances; - type DefaultDepositLimit = DefaultDepositLimit; - type DepositPerByte = DepositPerByte; - type DepositPerItem = DepositPerItem; - type MaxCodeLen = ConstU32<{ 123 * 1024 }>; - type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>; - type MaxDelegateDependencies = MaxDelegateDependencies; - type MaxStorageKeyLen = ConstU32<128>; - type Migrations = (); - type Randomness = DummyRandomness; - type RuntimeCall = RuntimeCall; - type RuntimeEvent = RuntimeEvent; - type RuntimeHoldReason = RuntimeHoldReason; type Schedule = Schedule; type Time = super::Timestamp; - type UnsafeUnstableInterface = ConstBool; - type UploadOrigin = EnsureSigned; - type InstantiateOrigin = EnsureSigned; - type WeightInfo = (); - type WeightPrice = Self; - type Debug = (); - type Environment = (); - type ApiVersion = (); type Xcm = pallet_xcm::Pallet; } diff --git a/substrate/frame/contracts/src/lib.rs b/substrate/frame/contracts/src/lib.rs index 73c70a7704e20..edc4c872bfce1 100644 --- a/substrate/frame/contracts/src/lib.rs +++ b/substrate/frame/contracts/src/lib.rs @@ -250,7 +250,7 @@ pub mod pallet { #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); - #[pallet::config] + #[pallet::config(with_default)] pub trait Config: frame_system::Config { /// The time implementation used to supply timestamps to contracts through `seal_now`. type Time: Time; @@ -263,22 +263,30 @@ pub mod pallet { /// be instantiated from existing codes that use this deprecated functionality. It will /// be removed eventually. Hence for new `pallet-contracts` deployments it is okay /// to supply a dummy implementation for this type (because it is never used). + #[pallet::no_default_bounds] type Randomness: Randomness>; /// The fungible in which fees are paid and contract balances are held. + #[pallet::no_default] type Currency: Inspect + Mutate + MutateHold; /// The overarching event type. + #[pallet::no_default_bounds] type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The overarching call type. + #[pallet::no_default_bounds] type RuntimeCall: Dispatchable + GetDispatchInfo + codec::Decode + IsType<::RuntimeCall>; + /// Overarching hold reason. + #[pallet::no_default_bounds] + type RuntimeHoldReason: From; + /// Filter that is applied to calls dispatched by contracts. /// /// Use this filter to control which dispatchables are callable by contracts. @@ -301,10 +309,12 @@ pub mod pallet { /// /// This filter does not apply to XCM transact calls. To impose restrictions on XCM transact /// calls, you must configure them separately within the XCM pallet itself. + #[pallet::no_default_bounds] type CallFilter: Contains<::RuntimeCall>; /// Used to answer contracts' queries regarding the current weight price. This is **not** /// used to calculate the actual fee and is only for informational purposes. + #[pallet::no_default_bounds] type WeightPrice: Convert>; /// Describes the weights of the dispatchables of this module and is also used to @@ -312,10 +322,12 @@ pub mod pallet { type WeightInfo: WeightInfo; /// Type that allows the runtime authors to add new host functions for a contract to call. + #[pallet::no_default_bounds] type ChainExtension: chain_extension::ChainExtension + Default; /// Cost schedule and limits. #[pallet::constant] + #[pallet::no_default] type Schedule: Get>; /// The type of the call stack determines the maximum nesting depth of contract calls. @@ -326,6 +338,7 @@ pub mod pallet { /// /// This setting along with [`MaxCodeLen`](#associatedtype.MaxCodeLen) directly affects /// memory usage of your runtime. + #[pallet::no_default] type CallStack: Array>; /// The amount of balance a caller has to pay for each byte of storage. @@ -334,10 +347,12 @@ pub mod pallet { /// /// Changing this value for an existing chain might need a storage migration. #[pallet::constant] + #[pallet::no_default_bounds] type DepositPerByte: Get>; /// Fallback value to limit the storage deposit if it's not being set by the caller. #[pallet::constant] + #[pallet::no_default_bounds] type DefaultDepositLimit: Get>; /// The amount of balance a caller has to pay for each storage item. @@ -346,6 +361,7 @@ pub mod pallet { /// /// Changing this value for an existing chain might need a storage migration. #[pallet::constant] + #[pallet::no_default_bounds] type DepositPerItem: Get>; /// The percentage of the storage deposit that should be held for using a code hash. @@ -356,6 +372,7 @@ pub mod pallet { type CodeHashLockupDepositPercent: Get; /// The address generator used to generate the addresses of contracts. + #[pallet::no_default_bounds] type AddressGenerator: AddressGenerator; /// The maximum length of a contract code in bytes. @@ -395,6 +412,7 @@ pub mod pallet { /// /// By default, it is safe to set this to `EnsureSigned`, allowing anyone to upload contract /// code. + #[pallet::no_default_bounds] type UploadOrigin: EnsureOrigin; /// Origin allowed to instantiate code. @@ -407,11 +425,9 @@ pub mod pallet { /// /// By default, it is safe to set this to `EnsureSigned`, allowing anyone to instantiate /// contract code. + #[pallet::no_default_bounds] type InstantiateOrigin: EnsureOrigin; - /// Overarching hold reason. - type RuntimeHoldReason: From; - /// The sequence of migration steps that will be applied during a migration. /// /// # Examples @@ -435,6 +451,7 @@ pub mod pallet { /// For most production chains, it's recommended to use the `()` implementation of this /// trait. This implementation offers additional logging when the log target /// "runtime::contracts" is set to trace. + #[pallet::no_default_bounds] type Debug: Debugger; /// Type that bundles together all the runtime configurable interface types. @@ -442,16 +459,19 @@ pub mod pallet { /// This is not a real config. We just mention the type here as constant so that /// its type appears in the metadata. Only valid value is `()`. #[pallet::constant] + #[pallet::no_default_bounds] type Environment: Get>; /// The version of the HostFn APIs that are available in the runtime. /// /// Only valid value is `()`. #[pallet::constant] + #[pallet::no_default_bounds] type ApiVersion: Get; /// A type that exposes XCM APIs, allowing contracts to interact with other parachains, and /// execute XCM programs. + #[pallet::no_default_bounds] type Xcm: xcm_builder::Controller< OriginFor, ::RuntimeCall, @@ -459,6 +479,95 @@ pub mod pallet { >; } + /// Container for different types that implement [`DefaultConfig`]` of this pallet. + pub mod config_preludes { + use super::*; + use frame_support::{ + derive_impl, + traits::{ConstBool, ConstU32}, + }; + use frame_system::EnsureSigned; + use sp_core::parameter_types; + + type AccountId = sp_runtime::AccountId32; + type Balance = u64; + const UNITS: Balance = 10_000_000_000; + const CENTS: Balance = UNITS / 100; + + const fn deposit(items: u32, bytes: u32) -> Balance { + items as Balance * 1 * CENTS + (bytes as Balance) * 1 * CENTS + } + + parameter_types! { + pub const DepositPerItem: Balance = deposit(1, 0); + pub const DepositPerByte: Balance = deposit(0, 1); + pub const DefaultDepositLimit: Balance = deposit(1024, 1024 * 1024); + pub const CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0); + pub const MaxDelegateDependencies: u32 = 32; + } + + /// A type providing default configurations for this pallet in testing environment. + pub struct TestDefaultConfig; + + impl Randomness for TestDefaultConfig { + fn random(_subject: &[u8]) -> (Output, BlockNumber) { + unimplemented!("No default `random` implementation in `TestDefaultConfig`, provide a custom `T::Randomness` type.") + } + } + + impl Time for TestDefaultConfig { + type Moment = u64; + fn now() -> Self::Moment { + unimplemented!("No default `now` implementation in `TestDefaultConfig` provide a custom `T::Time` type.") + } + } + + impl> Convert for TestDefaultConfig { + fn convert(w: Weight) -> T { + w.ref_time().into() + } + } + + #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig, no_aggregated_types)] + impl frame_system::DefaultConfig for TestDefaultConfig {} + + #[frame_support::register_default_impl(TestDefaultConfig)] + impl DefaultConfig for TestDefaultConfig { + #[inject_runtime_type] + type RuntimeEvent = (); + + #[inject_runtime_type] + type RuntimeHoldReason = (); + + #[inject_runtime_type] + type RuntimeCall = (); + + type AddressGenerator = DefaultAddressGenerator; + type CallFilter = (); + type ChainExtension = (); + type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; + type DefaultDepositLimit = DefaultDepositLimit; + type DepositPerByte = DepositPerByte; + type DepositPerItem = DepositPerItem; + type MaxCodeLen = ConstU32<{ 123 * 1024 }>; + type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>; + type MaxDelegateDependencies = MaxDelegateDependencies; + type MaxStorageKeyLen = ConstU32<128>; + type Migrations = (); + type Time = Self; + type Randomness = Self; + type UnsafeUnstableInterface = ConstBool; + type UploadOrigin = EnsureSigned; + type InstantiateOrigin = EnsureSigned; + type WeightInfo = (); + type WeightPrice = Self; + type Debug = (); + type Environment = (); + type ApiVersion = (); + type Xcm = (); + } + } + #[pallet::hooks] impl Hooks> for Pallet { fn on_idle(_block: BlockNumberFor, limit: Weight) -> Weight { diff --git a/substrate/frame/contracts/src/tests.rs b/substrate/frame/contracts/src/tests.rs index 0b83358a7f56a..57b804a51e417 100644 --- a/substrate/frame/contracts/src/tests.rs +++ b/substrate/frame/contracts/src/tests.rs @@ -334,34 +334,24 @@ parameter_types! { #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for Test { + type Block = Block; type AccountId = AccountId32; type Lookup = IdentityLookup; - type Block = Block; type AccountData = pallet_balances::AccountData; } + impl pallet_insecure_randomness_collective_flip::Config for Test {} + +#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = u64; - type RuntimeEvent = RuntimeEvent; - type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; + type ReserveIdentifier = [u8; 8]; type AccountStore = System; - type WeightInfo = (); - type FreezeIdentifier = (); - type MaxFreezes = (); - type RuntimeHoldReason = RuntimeHoldReason; - type RuntimeFreezeReason = RuntimeFreezeReason; } -impl pallet_timestamp::Config for Test { - type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = ConstU64<1>; - type WeightInfo = (); -} +#[derive_impl(pallet_timestamp::config_preludes::TestDefaultConfig)] +impl pallet_timestamp::Config for Test {} + impl pallet_utility::Config for Test { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; @@ -467,16 +457,13 @@ parameter_types! { pub static UnstableInterface: bool = true; } +#[derive_impl(crate::config_preludes::TestDefaultConfig)] impl Config for Test { type Time = Timestamp; type Randomness = Randomness; type Currency = Balances; - type RuntimeEvent = RuntimeEvent; - type RuntimeCall = RuntimeCall; type CallFilter = TestFilter; type CallStack = [Frame; 5]; - type WeightPrice = Self; - type WeightInfo = (); type ChainExtension = (TestExtension, DisabledExtension, RevertingExtension, TempStorageExtension); type Schedule = MySchedule; @@ -484,20 +471,13 @@ impl Config for Test { type DepositPerItem = DepositPerItem; type DefaultDepositLimit = DefaultDepositLimit; type AddressGenerator = DefaultAddressGenerator; - type MaxCodeLen = ConstU32<{ 123 * 1024 }>; - type MaxStorageKeyLen = ConstU32<128>; type UnsafeUnstableInterface = UnstableInterface; type UploadOrigin = EnsureAccount; type InstantiateOrigin = EnsureAccount; - type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>; - type RuntimeHoldReason = RuntimeHoldReason; type Migrations = crate::migration::codegen::BenchMigrations; type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; type MaxDelegateDependencies = MaxDelegateDependencies; type Debug = TestDebug; - type Environment = (); - type ApiVersion = (); - type Xcm = (); } pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]);