Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use defaultConfig for pallet_contracts #1817

Merged
merged 6 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use defaultConfig for pallet_contracts
  • Loading branch information
pgherveou committed Oct 8, 2023
commit 810127361f4b507ea1a3cd0196d6b252e46ae6d1
67 changes: 63 additions & 4 deletions substrate/frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,10 @@ pub mod pallet {
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::config]
#[pallet::config(with_default)]
pub trait Config: frame_system::Config {
/// The time implementation used to supply timestamps to contracts through `seal_now`.
#[pallet::no_default]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not possible to provide a default here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you would most likely want to override it, if you do anything that use that type in your tests, but I can provide an implementation that is unimplented!

type Time: Time;

/// The generator used to supply randomness to contracts through `seal_random`.
Expand All @@ -244,22 +245,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]
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self>>;

/// The fungible in which fees are paid and contract balances are held.
#[pallet::no_default]
type Currency: Inspect<Self::AccountId>
+ Mutate<Self::AccountId>
+ MutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>;

/// The overarching event type.
#[pallet::no_default_bounds]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// The overarching call type.
#[pallet::no_default_bounds]
type RuntimeCall: Dispatchable<RuntimeOrigin = Self::RuntimeOrigin, PostInfo = PostDispatchInfo>
+ GetDispatchInfo
+ codec::Decode
+ IsType<<Self as frame_system::Config>::RuntimeCall>;

/// Overarching hold reason.
#[pallet::no_default_bounds]
type RuntimeHoldReason: From<HoldReason>;

/// Filter that is applied to calls dispatched by contracts.
///
/// Use this filter to control which dispatchables are callable by contracts.
Expand All @@ -279,21 +288,25 @@ pub mod pallet {
/// Therefore please make sure to be restrictive about which dispatchables are allowed
/// in order to not introduce a new DoS vector like memory allocation patterns that can
/// be exploited to drive the runtime into a panic.
#[pallet::no_default_bounds]
type CallFilter: Contains<<Self as frame_system::Config>::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]
type WeightPrice: Convert<Weight, BalanceOf<Self>>;

/// Describes the weights of the dispatchables of this module and is also used to
/// construct a default cost schedule.
type WeightInfo: WeightInfo;

/// Type that allows the runtime authors to add new host functions for a contract to call.
#[pallet::no_default]
type ChainExtension: chain_extension::ChainExtension<Self> + Default;

/// Cost schedule and limits.
#[pallet::constant]
#[pallet::no_default]
pgherveou marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried with no_default_bounds?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type Schedule: Get<Schedule<Self>>;

/// The type of the call stack determines the maximum nesting depth of contract calls.
Expand All @@ -304,6 +317,7 @@ pub mod pallet {
///
/// This setting along with [`MaxCodeLen`](#associatedtype.MaxCodeLen) directly affects
/// memory usage of your runtime.
#[pallet::no_default]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, the trait is generic over T

type CallStack: Array<Item = Frame<Self>>;

/// The amount of balance a caller has to pay for each byte of storage.
Expand All @@ -312,10 +326,12 @@ pub mod pallet {
///
/// Changing this value for an existing chain might need a storage migration.
#[pallet::constant]
#[pallet::no_default]
type DepositPerByte: Get<BalanceOf<Self>>;
pgherveou marked this conversation as resolved.
Show resolved Hide resolved

/// Fallback value to limit the storage deposit if it's not being set by the caller.
#[pallet::constant]
#[pallet::no_default]
type DefaultDepositLimit: Get<BalanceOf<Self>>;

/// The amount of balance a caller has to pay for each storage item.
Expand All @@ -324,6 +340,7 @@ pub mod pallet {
///
/// Changing this value for an existing chain might need a storage migration.
#[pallet::constant]
#[pallet::no_default]
type DepositPerItem: Get<BalanceOf<Self>>;

/// The percentage of the storage deposit that should be held for using a code hash.
Expand All @@ -334,6 +351,7 @@ pub mod pallet {
type CodeHashLockupDepositPercent: Get<Perbill>;

/// The address generator used to generate the addresses of contracts.
#[pallet::no_default_bounds]
type AddressGenerator: AddressGenerator<Self>;

/// The maximum length of a contract code in bytes.
Expand Down Expand Up @@ -369,9 +387,6 @@ pub mod pallet {
#[pallet::constant]
type MaxDebugBufferLen: Get<u32>;

/// Overarching hold reason.
type RuntimeHoldReason: From<HoldReason>;

/// The sequence of migration steps that will be applied during a migration.
///
/// # Examples
Expand All @@ -395,16 +410,60 @@ 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<Self>;

/// Type that bundles together all the runtime configurable interface types.
///
/// 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]
type Environment: Get<Environment<Self>>;
}

/// 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 sp_core::parameter_types;

parameter_types! {
pub const CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0);
}

/// A type providing default configurations for this pallet in testing environment.
pub struct TestDefaultConfig;

#[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 RuntimeCall = ();
#[inject_runtime_type]
type RuntimeHoldReason = ();

type CallFilter = ();
type AddressGenerator = DefaultAddressGenerator;
type MaxCodeLen = ConstU32<{ 123 * 1024 }>;
type MaxStorageKeyLen = ConstU32<128>;
type UnsafeUnstableInterface = ConstBool<true>;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type MaxDelegateDependencies = ConstU32<32>;
type Migrations = ();
type WeightInfo = ();
type Debug = ();
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_idle(_block: BlockNumberFor<T>, mut remaining_weight: Weight) -> Weight {
Expand Down
50 changes: 11 additions & 39 deletions substrate/frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ use crate::{
wasm::{Determinism, ReturnCode as RuntimeReturnCode},
weights::WeightInfo,
BalanceOf, Code, CodeHash, CodeInfoOf, CollectEvents, Config, ContractInfo, ContractInfoOf,
DebugInfo, DefaultAddressGenerator, DeletionQueueCounter, Error, HoldReason,
MigrationInProgress, Origin, Pallet, PristineCode, Schedule,
DebugInfo, DeletionQueueCounter, Error, HoldReason, MigrationInProgress, Origin, Pallet,
PristineCode, Schedule,
};
use assert_matches::assert_matches;
use codec::Encode;
use frame_support::{
assert_err, assert_err_ignore_postinfo, assert_err_with_weight, assert_noop, assert_ok,
derive_impl,
dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo},
parameter_types,
storage::child,
Expand Down Expand Up @@ -331,45 +332,23 @@ parameter_types! {
);
pub static ExistentialDeposit: u64 = 1;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = BlockWeights;
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type Hash = H256;
type RuntimeCall = RuntimeCall;
type Hashing = BlakeTwo256;
type Block = Block;
type AccountId = AccountId32;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl pallet_insecure_randomness_collective_flip::Config for Test {}

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)]
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 MaxHolds = ConstU32<1>;
}

Expand Down Expand Up @@ -457,28 +436,21 @@ parameter_types! {
pub static UnstableInterface: bool = true;
}

#[derive_impl(crate::config_preludes::TestDefaultConfig as crate::DefaultConfig)]
impl Config for Test {
pgherveou marked this conversation as resolved.
Show resolved Hide resolved
type Time = Timestamp;
type Randomness = Randomness;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type CallFilter = TestFilter;
type CallStack = [Frame<Self>; 5];
type WeightPrice = Self;
type WeightInfo = ();
type ChainExtension =
(TestExtension, DisabledExtension, RevertingExtension, TempStorageExtension);
type Schedule = MySchedule;
type DepositPerByte = DepositPerByte;
type DepositPerItem = DepositPerItem;
type DefaultDepositLimit = DefaultDepositLimit;
type AddressGenerator = DefaultAddressGenerator;
type MaxCodeLen = ConstU32<{ 123 * 1024 }>;
type MaxStorageKeyLen = ConstU32<128>;
type UnsafeUnstableInterface = UnstableInterface;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
type RuntimeHoldReason = RuntimeHoldReason;
type Migrations = crate::migration::codegen::BenchMigrations;
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type MaxDelegateDependencies = MaxDelegateDependencies;
Expand Down