diff --git a/Cargo.toml b/Cargo.toml index 83efe30..5b520ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink" license = "Apache-2.0" readme = "README.md" repository = "https://github.com/Cardinal-Cryptography/drink" -version = "0.8.0" +version = "0.8.1" [workspace.dependencies] anyhow = { version = "1.0.71" } @@ -56,5 +56,5 @@ sp-runtime-interface = { version = "19.0.0" } # Local dependencies -drink = { version = "0.8.0", path = "drink" } -drink-test-macro = { version = "0.8.0", path = "drink/test-macro" } +drink = { version = "0.8.1", path = "drink" } +drink-test-macro = { version = "0.8.1", path = "drink/test-macro" } diff --git a/drink/src/lib.rs b/drink/src/lib.rs index 7c16fa9..62f0c9f 100644 --- a/drink/src/lib.rs +++ b/drink/src/lib.rs @@ -26,6 +26,8 @@ pub use mock::{mock_message, ContractMock, MessageMock, MockedCallResult, Select use pallet_contracts::debug::ExecResult; use pallet_contracts_primitives::{ExecReturnValue, ReturnFlags}; use parity_scale_codec::{Decode, Encode}; +/// Export pallets that are used in the minimal runtime. +pub use {frame_support, frame_system, pallet_balances, pallet_contracts, pallet_timestamp}; use crate::{ errors::MessageResult, diff --git a/drink/src/runtime.rs b/drink/src/runtime.rs index c52e053..3f4b5b7 100644 --- a/drink/src/runtime.rs +++ b/drink/src/runtime.rs @@ -15,11 +15,6 @@ pub type AccountIdFor = ::AccountId; /// The type of a hash. pub type HashFor = ::Hash; -/// Export pallets that are used in the runtime. -pub use frame_system; -pub use pallet_balances; -pub use pallet_contracts; - /// A runtime to use. pub trait Runtime: frame_system::Config { /// Initialize the storage at the genesis block. diff --git a/drink/src/runtime/minimal.rs b/drink/src/runtime/minimal.rs index 3051d82..041550f 100644 --- a/drink/src/runtime/minimal.rs +++ b/drink/src/runtime/minimal.rs @@ -1,142 +1,163 @@ #![allow(missing_docs)] // `construct_macro` doesn't allow doc comments for the runtime type. -use std::time::SystemTime; - -use frame_metadata::RuntimeMetadataPrefixed; -use frame_support::{ - parameter_types, - sp_runtime::{ - testing::H256, - traits::{BlakeTwo256, Convert, Dispatchable, IdentityLookup}, - AccountId32, BuildStorage, Perbill, Storage, - }, - traits::{ConstBool, ConstU128, ConstU32, ConstU64, Currency, Hooks, Randomness}, - weights::Weight, -}; -// Re-export all pallets. -pub use frame_system; -use frame_system::{pallet_prelude::BlockNumberFor, Config}; -pub use pallet_balances; -pub use pallet_contracts; -use pallet_contracts::{DefaultAddressGenerator, Frame, Schedule}; -pub use pallet_timestamp; - -use crate::{ - runtime::{pallet_contracts_debugging::DrinkDebug, AccountIdFor}, - Runtime, -}; +#[macro_export] +macro_rules! create_minimal_runtime { + ($name:ident) => { + create_minimal_runtime!($name, ()); + }; + ($name:ident, $chain_extension: ty) => { + +// ------------ Put all the boilerplate into an auxiliary module ----------------------------------- +mod construct_runtime { + + // ------------ Bring some common types into the scope ----------------------------------------- + use $crate::frame_support::{ + construct_runtime, + parameter_types, + sp_runtime::{ + testing::H256, + traits::{BlakeTwo256, Convert, IdentityLookup}, + AccountId32, Perbill, + }, + traits::{ConstBool, ConstU128, ConstU32, ConstU64, Currency, Randomness}, + weights::Weight, + }; + use $crate::runtime::pallet_contracts_debugging::DrinkDebug; + + // ------------ Define the runtime type as a collection of pallets ----------------------------- + construct_runtime!( + pub enum $name { + System: $crate::frame_system, + Balances: $crate::pallet_balances, + Timestamp: $crate::pallet_timestamp, + Contracts: $crate::pallet_contracts, + } + ); + + // ------------ Configure pallet system -------------------------------------------------------- + impl $crate::frame_system::Config for $name { + type BaseCallFilter = $crate::frame_support::traits::Everything; + type BlockWeights = (); + type BlockLength = (); + type Block = $crate::frame_system::mocking::MockBlockU32<$name>; + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Nonce = u64; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId32; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = ConstU32<250>; + type DbWeight = (); + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = $crate::pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = ConstU32<16>; + } -type Block = frame_system::mocking::MockBlockU32; + // ------------ Configure pallet balances ------------------------------------------------------ + impl $crate::pallet_balances::Config for $name { + type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); + type Balance = u128; + type DustRemoval = (); + type ExistentialDeposit = ConstU128<1>; + type AccountStore = System; + type ReserveIdentifier = [u8; 8]; + type FreezeIdentifier = (); + type MaxLocks = (); + type MaxReserves = (); + type MaxHolds = ConstU32<1>; + type MaxFreezes = (); + type RuntimeHoldReason = RuntimeHoldReason; + } -frame_support::construct_runtime!( - pub enum MinimalRuntime { - System: frame_system, - Balances: pallet_balances, - Timestamp: pallet_timestamp, - Contracts: pallet_contracts, + // ------------ Configure pallet timestamp ----------------------------------------------------- + impl $crate::pallet_timestamp::Config for $name { + type Moment = u64; + type OnTimestampSet = (); + type MinimumPeriod = ConstU64<1>; + type WeightInfo = (); } -); - -impl frame_system::Config for MinimalRuntime { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - type Block = Block; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Nonce = u64; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = AccountId32; - type Lookup = IdentityLookup; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = ConstU32<250>; - type DbWeight = (); - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); - type OnSetCode = (); - type MaxConsumers = ConstU32<16>; -} -impl pallet_balances::Config for MinimalRuntime { - type RuntimeEvent = RuntimeEvent; - type WeightInfo = (); - type Balance = u128; - type DustRemoval = (); - type ExistentialDeposit = ConstU128<1>; - type AccountStore = System; - type ReserveIdentifier = [u8; 8]; - type FreezeIdentifier = (); - type MaxLocks = (); - type MaxReserves = (); - type MaxHolds = ConstU32<1>; - type MaxFreezes = (); - type RuntimeHoldReason = RuntimeHoldReason; -} + // ------------ Configure pallet contracts ----------------------------------------------------- + pub enum SandboxRandomness {} + impl Randomness for SandboxRandomness { + fn random(_subject: &[u8]) -> (H256, u32) { + unreachable!("No randomness") + } + } -impl pallet_timestamp::Config for MinimalRuntime { - type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = ConstU64<1>; - type WeightInfo = (); -} + type BalanceOf = >::Balance; + impl Convert for $name { + fn convert(w: Weight) -> BalanceOf { + w.ref_time().into() + } + } -pub enum SandboxRandomness {} -impl Randomness for SandboxRandomness { - fn random(_subject: &[u8]) -> (H256, u32) { - todo!("No randomness") + parameter_types! { + pub SandboxSchedule: $crate::pallet_contracts::Schedule<$name> = { + <$crate::pallet_contracts::Schedule<$name>>::default() + }; + pub DeletionWeightLimit: Weight = Weight::zero(); + pub DefaultDepositLimit: BalanceOf = 10_000_000; + pub CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0); + pub MaxDelegateDependencies: u32 = 32; } -} -type BalanceOf = >::Balance; -impl Convert for MinimalRuntime { - fn convert(w: Weight) -> BalanceOf { - w.ref_time().into() + impl $crate::pallet_contracts::Config for $name { + type Time = Timestamp; + type Randomness = SandboxRandomness; + type Currency = Balances; + type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; + type CallFilter = (); + type WeightPrice = Self; + type WeightInfo = (); + type ChainExtension = $chain_extension; + type Schedule = SandboxSchedule; + type CallStack = [$crate::pallet_contracts::Frame; 5]; + type DepositPerByte = ConstU128<1>; + type DepositPerItem = ConstU128<1>; + type AddressGenerator = $crate::pallet_contracts::DefaultAddressGenerator; + type MaxCodeLen = ConstU32<{ 123 * 1024 }>; + type MaxStorageKeyLen = ConstU32<128>; + type UnsafeUnstableInterface = ConstBool; + type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>; + type Migrations = (); + type DefaultDepositLimit = DefaultDepositLimit; + type Debug = DrinkDebug; + type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; + type MaxDelegateDependencies = MaxDelegateDependencies; + type RuntimeHoldReason = RuntimeHoldReason; + type Environment = (); } } -parameter_types! { - pub SandboxSchedule: Schedule = { - >::default() +// ------------ Export runtime type itself, pallets and useful types from the auxiliary module ----- +pub use construct_runtime::{ + $name, Balances, Contracts, PalletInfo, RuntimeCall, RuntimeEvent, RuntimeHoldReason, + RuntimeOrigin, System, Timestamp, +}; }; - pub DeletionWeightLimit: Weight = Weight::zero(); - pub DefaultDepositLimit: BalanceOf = 10_000_000; - pub CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0); - pub MaxDelegateDependencies: u32 = 32; } -impl pallet_contracts::Config for MinimalRuntime { - type Time = Timestamp; - type Randomness = SandboxRandomness; - type Currency = Balances; - type RuntimeEvent = RuntimeEvent; - type RuntimeCall = RuntimeCall; - type CallFilter = (); - type WeightPrice = Self; - type WeightInfo = (); - type ChainExtension = (); - type Schedule = SandboxSchedule; - type CallStack = [Frame; 5]; - type DepositPerByte = ConstU128<1>; - type DepositPerItem = ConstU128<1>; - type AddressGenerator = DefaultAddressGenerator; - type MaxCodeLen = ConstU32<{ 123 * 1024 }>; - type MaxStorageKeyLen = ConstU32<128>; - type UnsafeUnstableInterface = ConstBool; - type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>; - type Migrations = (); - type DefaultDepositLimit = DefaultDepositLimit; - type Debug = DrinkDebug; - type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; - type MaxDelegateDependencies = MaxDelegateDependencies; - type RuntimeHoldReason = RuntimeHoldReason; - type Environment = (); -} +create_minimal_runtime!(MinimalRuntime); + +use std::time::SystemTime; + +use frame_support::{ + sp_runtime::{testing::H256, traits::Dispatchable, AccountId32, BuildStorage, Storage}, + traits::Hooks, +}; + +use crate::{AccountIdFor, Runtime, RuntimeMetadataPrefixed}; /// Default initial balance for the default account. pub const INITIAL_BALANCE: u128 = 1_000_000_000_000_000; @@ -149,7 +170,10 @@ impl Runtime for MinimalRuntime { .assimilate_storage(storage) } - fn initialize_block(height: BlockNumberFor, parent_hash: H256) -> Result<(), String> { + fn initialize_block( + height: frame_system::pallet_prelude::BlockNumberFor, + parent_hash: H256, + ) -> Result<(), String> { System::reset_events(); System::initialize(&height, &parent_hash, &Default::default()); @@ -168,7 +192,9 @@ impl Runtime for MinimalRuntime { Ok(()) } - fn finalize_block(height: BlockNumberFor) -> Result { + fn finalize_block( + height: frame_system::pallet_prelude::BlockNumberFor, + ) -> Result { Contracts::on_finalize(height); Timestamp::on_finalize(height); Balances::on_finalize(height); @@ -186,7 +212,7 @@ impl Runtime for MinimalRuntime { fn convert_account_to_origin( account: AccountIdFor, - ) -> <::RuntimeCall as Dispatchable>::RuntimeOrigin { + ) -> <::RuntimeCall as Dispatchable>::RuntimeOrigin { Some(account).into() } } diff --git a/drink/src/sandbox/balance_api.rs b/drink/src/sandbox/balance_api.rs index 7706035..9174b16 100644 --- a/drink/src/sandbox/balance_api.rs +++ b/drink/src/sandbox/balance_api.rs @@ -2,10 +2,7 @@ use frame_support::{sp_runtime::DispatchError, traits::fungible::Mutate}; use super::Sandbox; -use crate::{ - runtime::{AccountIdFor, *}, - BalanceOf, -}; +use crate::{runtime::AccountIdFor, BalanceOf}; impl Sandbox { /// Mint tokens to an account.