From 862860f8a3599bc1dc6ca72b77680f3cac3da6b8 Mon Sep 17 00:00:00 2001 From: bogdanS98 Date: Fri, 19 Jul 2024 19:26:57 +0300 Subject: [PATCH 1/5] WIP --- Cargo.lock | 16 +++ Cargo.toml | 2 +- pallets/pallet-mock-skip-blocks/Cargo.toml | 25 +++++ pallets/pallet-mock-skip-blocks/src/lib.rs | 110 +++++++++++++++++++ pallets/pallet-mock-skip-blocks/src/mock.rs | 105 ++++++++++++++++++ pallets/pallet-mock-skip-blocks/src/tests.rs | 58 ++++++++++ runtime/foucoco/Cargo.toml | 5 + runtime/foucoco/src/lib.rs | 14 +++ 8 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 pallets/pallet-mock-skip-blocks/Cargo.toml create mode 100644 pallets/pallet-mock-skip-blocks/src/lib.rs create mode 100644 pallets/pallet-mock-skip-blocks/src/mock.rs create mode 100644 pallets/pallet-mock-skip-blocks/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index ff27769df..4ea5f6e00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3194,6 +3194,7 @@ dependencies = [ "pallet-democracy", "pallet-identity", "pallet-insecure-randomness-collective-flip", + "pallet-mock-skip-blocks", "pallet-multisig", "pallet-preimage", "pallet-proxy", @@ -7218,6 +7219,21 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-mock-skip-blocks" +version = "0.1.0" +dependencies = [ + "frame-support", + "frame-system", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + [[package]] name = "pallet-multisig" version = "4.0.0-dev" diff --git a/Cargo.toml b/Cargo.toml index dc0ab9f75..576207876 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ members = [ "runtime/integration-tests", "chain-extensions/token", "chain-extensions/price", - "chain-extensions/common", + "chain-extensions/common", "pallets/pallet-mock-skip-blocks", ] # need this because of bifrost farming dependency in runtime diff --git a/pallets/pallet-mock-skip-blocks/Cargo.toml b/pallets/pallet-mock-skip-blocks/Cargo.toml new file mode 100644 index 000000000..fac245ad0 --- /dev/null +++ b/pallets/pallet-mock-skip-blocks/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "pallet-mock-skip-blocks" +version = "0.1.0" +edition = "2021" + +[features] +default = ["std"] +std = [ + "frame-system/std", +] + +instant-seal = ["std"] + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "max-encoded-len"] } +scale-info = { version = "2.2.0", default-features = false, features = ["derive"] } +serde = { version = "1.0.130", default-features = false, features = ["derive"], optional = true } +frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } +frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } +sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } +sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } +sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } + +[dev-dependencies] +sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } \ No newline at end of file diff --git a/pallets/pallet-mock-skip-blocks/src/lib.rs b/pallets/pallet-mock-skip-blocks/src/lib.rs new file mode 100644 index 000000000..16cba5491 --- /dev/null +++ b/pallets/pallet-mock-skip-blocks/src/lib.rs @@ -0,0 +1,110 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(feature = "instant-seal")] +pub use pallet::*; + +#[cfg(not(feature = "instant-seal"))] +pub use dummy as pallet; + +#[cfg(not(feature = "instant-seal"))] +#[frame_support::pallet] +pub mod dummy { + use frame_support::pallet_prelude::*; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// Dummy event + DummyEvent, + } + + #[pallet::config] + pub trait Config: frame_system::Config { + /// Overarching event type + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + } + + #[pallet::call] + impl Pallet {} +} + +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod tests; + +#[cfg(feature = "instant-seal")] +#[frame_support::pallet] +pub mod pallet { + use frame_support::{dispatch::DispatchResult, pallet_prelude::*}; + use frame_system::pallet_prelude::*; + + #[pallet::pallet] + pub struct Pallet(PhantomData); + + #[pallet::error] + pub enum Error { + InvalidBlockNumber, + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// Desired block number stored + DesiredBlockStored { n: BlockNumberFor}, + /// Desired block number set + BlockSet { n: BlockNumberFor}, + /// Original block number restored + BlockReverted { n: BlockNumberFor}, + } + + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(n: T::BlockNumber) -> Weight { + let desired_block_number = DesiredBlockNumber::::get(); + OriginalBlockNumber::::put(n); + frame_system::Pallet::::set_block_number(desired_block_number); + Self::deposit_event(Event::::BlockSet { n: desired_block_number }); + 0.into() + } + + fn on_finalize(_: T::BlockNumber) { + let original_block_number = OriginalBlockNumber::::get(); + frame_system::Pallet::::set_block_number(original_block_number); + Self::deposit_event(Event::::BlockReverted { n: original_block_number }); + } + } + + #[pallet::storage] + pub type DesiredBlockNumber = StorageValue<_, T::BlockNumber, ValueQuery>; + + #[pallet::storage] + pub type OriginalBlockNumber = StorageValue<_, T::BlockNumber, ValueQuery>; + + #[pallet::config] + pub trait Config: frame_system::Config { + /// Overarching event type + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + } + + #[pallet::call] + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight((0, Pays::No))] + pub fn set_block_number(origin: OriginFor, block_number: T::BlockNumber) -> DispatchResult { + ensure_root(origin)?; + + let current_block_number = frame_system::Pallet::::block_number(); + ensure!(block_number >= current_block_number, Error::::InvalidBlockNumber); + + DesiredBlockNumber::::put(block_number); + Self::deposit_event(Event::::DesiredBlockStored { n: block_number }); + + Ok(()) + } + } +} diff --git a/pallets/pallet-mock-skip-blocks/src/mock.rs b/pallets/pallet-mock-skip-blocks/src/mock.rs new file mode 100644 index 000000000..ddc260fd5 --- /dev/null +++ b/pallets/pallet-mock-skip-blocks/src/mock.rs @@ -0,0 +1,105 @@ +#[cfg(feature = "instant-seal")] +use crate::{ + self as pallet_mock_skip_blocks, Config, +}; +#[cfg(feature = "instant-seal")] +use frame_support::{ + parameter_types, + traits::Everything, +}; +#[cfg(feature = "instant-seal")] +use sp_core::H256; +#[cfg(feature = "instant-seal")] +use sp_runtime::{ + testing::Header, + traits::{BlakeTwo256, IdentityLookup}, +}; + +#[cfg(feature = "instant-seal")] +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +#[cfg(feature = "instant-seal")] +type Block = frame_system::mocking::MockBlock; + +// Configure a mock runtime to test the pallet. +#[cfg(feature = "instant-seal")] +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Pallet, Call, Storage, Config, Event}, + MockSkipBlocks: pallet_mock_skip_blocks::{Pallet, Storage, Call, Event}, + } +); + +#[cfg(feature = "instant-seal")] +pub type AccountId = u64; +#[cfg(feature = "instant-seal")] +pub type BlockNumber = u64; +#[cfg(feature = "instant-seal")] +pub type Index = u64; + +#[cfg(feature = "instant-seal")] +parameter_types! { + pub const BlockHashCount: u64 = 250; + pub const SS58Prefix: u8 = 42; +} + +#[cfg(feature = "instant-seal")] +pub type TestEvent = RuntimeEvent; + +#[cfg(feature = "instant-seal")] +impl frame_system::Config for Test { + type BaseCallFilter = Everything; + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Index = Index; + type BlockNumber = BlockNumber; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Header = Header; + type RuntimeEvent = TestEvent; + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = SS58Prefix; + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; +} + +#[cfg(feature = "instant-seal")] +impl Config for Test { + type RuntimeEvent = RuntimeEvent; +} + +#[cfg(feature = "instant-seal")] +pub struct ExtBuilder; + +#[cfg(feature = "instant-seal")] +impl ExtBuilder { + pub fn build() -> sp_io::TestExternalities { + let storage = frame_system::GenesisConfig::default().build_storage::().unwrap(); + sp_io::TestExternalities::from(storage) + } +} + +#[cfg(feature = "instant-seal")] +pub fn run_test(test: T) +where + T: FnOnce(), +{ + ExtBuilder::build().execute_with(|| { + System::set_block_number(1); + test(); + }); +} diff --git a/pallets/pallet-mock-skip-blocks/src/tests.rs b/pallets/pallet-mock-skip-blocks/src/tests.rs new file mode 100644 index 000000000..2e403bfba --- /dev/null +++ b/pallets/pallet-mock-skip-blocks/src/tests.rs @@ -0,0 +1,58 @@ +#![cfg(test)] +#[cfg(feature = "instant-seal")] +use crate::{ + mock::*, + Error, +}; +#[cfg(feature = "instant-seal")] +use frame_support::{assert_noop, assert_ok, traits::Hooks}; + +#[cfg(feature = "instant-seal")] +#[test] +fn sets_and_reverts_block_number_success() { + run_test(|| { + // Initial block number + assert_eq!(System::block_number(), 1); + + // Set desired block number + let desired_block_number = 95; + assert_ok!(crate::Pallet::::set_block_number(RuntimeOrigin::root(), desired_block_number)); + + // Simulate block production + System::on_initialize(1); + crate::Pallet::::on_initialize(1); + assert_eq!(System::block_number(), desired_block_number); + + crate::Pallet::::on_finalize(1); + System::on_finalize(1); + assert_eq!(System::block_number(), 1); + + // Advance to the next block + System::set_block_number(2); + System::on_initialize(2); + crate::Pallet::::on_initialize(2); + assert_eq!(System::block_number(), desired_block_number); + + crate::Pallet::::on_finalize(2); + System::on_finalize(2); + assert_eq!(System::block_number(), 2); + }); +} + +#[cfg(feature = "instant-seal")] +#[test] +fn setting_block_number_to_less_than_current_fails() { + run_test(|| { + // Initial block number + assert_eq!(System::block_number(), 1); + + // Attempt to set desired block number to a value less than the current block number + assert_noop!( + crate::Pallet::::set_block_number(RuntimeOrigin::root(), 0), + Error::::InvalidBlockNumber + ); + + // Block number should remain unchanged + assert_eq!(System::block_number(), 1); + }); +} diff --git a/runtime/foucoco/Cargo.toml b/runtime/foucoco/Cargo.toml index 816f8d5fb..5971d302e 100644 --- a/runtime/foucoco/Cargo.toml +++ b/runtime/foucoco/Cargo.toml @@ -110,6 +110,7 @@ parachain-staking = { path = "../../pallets/parachain-staking", default-features orml-currencies-allowance-extension = { path = "../../pallets/orml-currencies-allowance-extension", default-features = false } orml-tokens-management-extension = { path = "../../pallets/orml-tokens-management-extension", default-features = false } treasury-buyout-extension = { path = "../../pallets/treasury-buyout-extension", default-features = false } +pallet-mock-skip-blocks = { path = "../../pallets/pallet-mock-skip-blocks", default-features = false } # DIA dia-oracle = { git = "https://github.com/pendulum-chain/oracle-pallet", default-features = false, branch = "polkadot-v0.9.42" } @@ -338,3 +339,7 @@ try-runtime = [ "bifrost-farming/try-runtime", "zenlink-protocol/try-runtime", ] + +instant-seal = [ + "pallet-mock-skip-blocks/instant-seal", +] diff --git a/runtime/foucoco/src/lib.rs b/runtime/foucoco/src/lib.rs index 039a0c3fc..6c5899da3 100644 --- a/runtime/foucoco/src/lib.rs +++ b/runtime/foucoco/src/lib.rs @@ -374,6 +374,7 @@ impl Contains for BaseFilter { | RuntimeCall::Proxy(_) | RuntimeCall::OrmlExtension(_) | RuntimeCall::TreasuryBuyoutExtension(_) + | RuntimeCall::MockSkipBlocks(_) | RuntimeCall::RewardDistribution(_) => true, // All pallets are allowed, but exhaustive match is defensive // in the case of adding new pallets. } @@ -1413,6 +1414,18 @@ impl pallet_proxy::Config for Runtime { type AnnouncementDepositFactor = AnnouncementDepositFactor; } +cfg_if::cfg_if! { + if #[cfg(feature = "instant-seal")] { + impl pallet_mock_skip_blocks::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + } + } else { + impl pallet_mock_skip_blocks::dummy::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + } + } +} + impl frame_system::offchain::SendTransactionTypes for Runtime where RuntimeCall: From, @@ -1507,6 +1520,7 @@ construct_runtime!( // Asset Metadata AssetRegistry: orml_asset_registry::{Pallet, Storage, Call, Event, Config} = 91, + MockSkipBlocks: pallet_mock_skip_blocks::{Pallet, Call, Storage, Event} = 92, } ); From 103cd1a90d23f060641c91d6e5544d23035295c8 Mon Sep 17 00:00:00 2001 From: bogdanS98 Date: Fri, 19 Jul 2024 19:38:05 +0300 Subject: [PATCH 2/5] Fmt --- pallets/pallet-mock-skip-blocks/src/lib.rs | 157 ++++++++++--------- pallets/pallet-mock-skip-blocks/src/mock.rs | 101 ++++++------ pallets/pallet-mock-skip-blocks/src/tests.rs | 88 +++++------ runtime/foucoco/src/lib.rs | 16 +- 4 files changed, 180 insertions(+), 182 deletions(-) diff --git a/pallets/pallet-mock-skip-blocks/src/lib.rs b/pallets/pallet-mock-skip-blocks/src/lib.rs index 16cba5491..78075318b 100644 --- a/pallets/pallet-mock-skip-blocks/src/lib.rs +++ b/pallets/pallet-mock-skip-blocks/src/lib.rs @@ -11,24 +11,24 @@ pub use dummy as pallet; pub mod dummy { use frame_support::pallet_prelude::*; - #[pallet::pallet] - pub struct Pallet(_); + #[pallet::pallet] + pub struct Pallet(_); #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { - /// Dummy event - DummyEvent, + pub enum Event { + /// Dummy event + DummyEvent, } - #[pallet::config] - pub trait Config: frame_system::Config { + #[pallet::config] + pub trait Config: frame_system::Config { /// Overarching event type - type RuntimeEvent: From> + IsType<::RuntimeEvent>; + type RuntimeEvent: From> + IsType<::RuntimeEvent>; } - #[pallet::call] - impl Pallet {} + #[pallet::call] + impl Pallet {} } #[cfg(test)] @@ -40,71 +40,74 @@ mod tests; #[cfg(feature = "instant-seal")] #[frame_support::pallet] pub mod pallet { - use frame_support::{dispatch::DispatchResult, pallet_prelude::*}; - use frame_system::pallet_prelude::*; - - #[pallet::pallet] - pub struct Pallet(PhantomData); - - #[pallet::error] - pub enum Error { - InvalidBlockNumber, - } - - #[pallet::event] - #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { - /// Desired block number stored - DesiredBlockStored { n: BlockNumberFor}, - /// Desired block number set - BlockSet { n: BlockNumberFor}, - /// Original block number restored - BlockReverted { n: BlockNumberFor}, - } - - #[pallet::hooks] - impl Hooks> for Pallet { - fn on_initialize(n: T::BlockNumber) -> Weight { - let desired_block_number = DesiredBlockNumber::::get(); - OriginalBlockNumber::::put(n); - frame_system::Pallet::::set_block_number(desired_block_number); - Self::deposit_event(Event::::BlockSet { n: desired_block_number }); - 0.into() - } - - fn on_finalize(_: T::BlockNumber) { - let original_block_number = OriginalBlockNumber::::get(); - frame_system::Pallet::::set_block_number(original_block_number); - Self::deposit_event(Event::::BlockReverted { n: original_block_number }); - } - } - - #[pallet::storage] - pub type DesiredBlockNumber = StorageValue<_, T::BlockNumber, ValueQuery>; - - #[pallet::storage] - pub type OriginalBlockNumber = StorageValue<_, T::BlockNumber, ValueQuery>; - - #[pallet::config] - pub trait Config: frame_system::Config { - /// Overarching event type - type RuntimeEvent: From> + IsType<::RuntimeEvent>; - } - - #[pallet::call] - impl Pallet { - #[pallet::call_index(0)] - #[pallet::weight((0, Pays::No))] - pub fn set_block_number(origin: OriginFor, block_number: T::BlockNumber) -> DispatchResult { - ensure_root(origin)?; - - let current_block_number = frame_system::Pallet::::block_number(); - ensure!(block_number >= current_block_number, Error::::InvalidBlockNumber); - - DesiredBlockNumber::::put(block_number); - Self::deposit_event(Event::::DesiredBlockStored { n: block_number }); - - Ok(()) - } - } + use frame_support::{dispatch::DispatchResult, pallet_prelude::*}; + use frame_system::pallet_prelude::*; + + #[pallet::pallet] + pub struct Pallet(PhantomData); + + #[pallet::error] + pub enum Error { + InvalidBlockNumber, + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// Desired block number stored + DesiredBlockStored { n: BlockNumberFor }, + /// Desired block number set + BlockSet { n: BlockNumberFor }, + /// Original block number restored + BlockReverted { n: BlockNumberFor }, + } + + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(n: T::BlockNumber) -> Weight { + let desired_block_number = DesiredBlockNumber::::get(); + OriginalBlockNumber::::put(n); + frame_system::Pallet::::set_block_number(desired_block_number); + Self::deposit_event(Event::::BlockSet { n: desired_block_number }); + 0.into() + } + + fn on_finalize(_: T::BlockNumber) { + let original_block_number = OriginalBlockNumber::::get(); + frame_system::Pallet::::set_block_number(original_block_number); + Self::deposit_event(Event::::BlockReverted { n: original_block_number }); + } + } + + #[pallet::storage] + pub type DesiredBlockNumber = StorageValue<_, T::BlockNumber, ValueQuery>; + + #[pallet::storage] + pub type OriginalBlockNumber = StorageValue<_, T::BlockNumber, ValueQuery>; + + #[pallet::config] + pub trait Config: frame_system::Config { + /// Overarching event type + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + } + + #[pallet::call] + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight((0, Pays::No))] + pub fn set_block_number( + origin: OriginFor, + block_number: T::BlockNumber, + ) -> DispatchResult { + ensure_root(origin)?; + + let current_block_number = frame_system::Pallet::::block_number(); + ensure!(block_number >= current_block_number, Error::::InvalidBlockNumber); + + DesiredBlockNumber::::put(block_number); + Self::deposit_event(Event::::DesiredBlockStored { n: block_number }); + + Ok(()) + } + } } diff --git a/pallets/pallet-mock-skip-blocks/src/mock.rs b/pallets/pallet-mock-skip-blocks/src/mock.rs index ddc260fd5..01118cba0 100644 --- a/pallets/pallet-mock-skip-blocks/src/mock.rs +++ b/pallets/pallet-mock-skip-blocks/src/mock.rs @@ -1,18 +1,13 @@ #[cfg(feature = "instant-seal")] -use crate::{ - self as pallet_mock_skip_blocks, Config, -}; +use crate::{self as pallet_mock_skip_blocks, Config}; #[cfg(feature = "instant-seal")] -use frame_support::{ - parameter_types, - traits::Everything, -}; +use frame_support::{parameter_types, traits::Everything}; #[cfg(feature = "instant-seal")] use sp_core::H256; #[cfg(feature = "instant-seal")] use sp_runtime::{ - testing::Header, - traits::{BlakeTwo256, IdentityLookup}, + testing::Header, + traits::{BlakeTwo256, IdentityLookup}, }; #[cfg(feature = "instant-seal")] @@ -23,14 +18,14 @@ type Block = frame_system::mocking::MockBlock; // Configure a mock runtime to test the pallet. #[cfg(feature = "instant-seal")] frame_support::construct_runtime!( - pub enum Test where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, - { - System: frame_system::{Pallet, Call, Storage, Config, Event}, - MockSkipBlocks: pallet_mock_skip_blocks::{Pallet, Storage, Call, Event}, - } + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Pallet, Call, Storage, Config, Event}, + MockSkipBlocks: pallet_mock_skip_blocks::{Pallet, Storage, Call, Event}, + } ); #[cfg(feature = "instant-seal")] @@ -42,8 +37,8 @@ pub type Index = u64; #[cfg(feature = "instant-seal")] parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: u64 = 250; + pub const SS58Prefix: u8 = 42; } #[cfg(feature = "instant-seal")] @@ -51,35 +46,35 @@ pub type TestEvent = RuntimeEvent; #[cfg(feature = "instant-seal")] impl frame_system::Config for Test { - type BaseCallFilter = Everything; - type BlockWeights = (); - type BlockLength = (); - type DbWeight = (); - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Index = Index; - type BlockNumber = BlockNumber; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = AccountId; - type Lookup = IdentityLookup; - type Header = Header; - type RuntimeEvent = TestEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; + type BaseCallFilter = Everything; + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Index = Index; + type BlockNumber = BlockNumber; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Header = Header; + type RuntimeEvent = TestEvent; + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = SS58Prefix; + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; } #[cfg(feature = "instant-seal")] impl Config for Test { - type RuntimeEvent = RuntimeEvent; + type RuntimeEvent = RuntimeEvent; } #[cfg(feature = "instant-seal")] @@ -87,19 +82,19 @@ pub struct ExtBuilder; #[cfg(feature = "instant-seal")] impl ExtBuilder { - pub fn build() -> sp_io::TestExternalities { - let storage = frame_system::GenesisConfig::default().build_storage::().unwrap(); - sp_io::TestExternalities::from(storage) - } + pub fn build() -> sp_io::TestExternalities { + let storage = frame_system::GenesisConfig::default().build_storage::().unwrap(); + sp_io::TestExternalities::from(storage) + } } #[cfg(feature = "instant-seal")] pub fn run_test(test: T) where - T: FnOnce(), + T: FnOnce(), { - ExtBuilder::build().execute_with(|| { - System::set_block_number(1); - test(); - }); + ExtBuilder::build().execute_with(|| { + System::set_block_number(1); + test(); + }); } diff --git a/pallets/pallet-mock-skip-blocks/src/tests.rs b/pallets/pallet-mock-skip-blocks/src/tests.rs index 2e403bfba..5cc26d4f0 100644 --- a/pallets/pallet-mock-skip-blocks/src/tests.rs +++ b/pallets/pallet-mock-skip-blocks/src/tests.rs @@ -1,58 +1,58 @@ #![cfg(test)] #[cfg(feature = "instant-seal")] -use crate::{ - mock::*, - Error, -}; +use crate::{mock::*, Error}; #[cfg(feature = "instant-seal")] use frame_support::{assert_noop, assert_ok, traits::Hooks}; #[cfg(feature = "instant-seal")] #[test] fn sets_and_reverts_block_number_success() { - run_test(|| { - // Initial block number - assert_eq!(System::block_number(), 1); - - // Set desired block number - let desired_block_number = 95; - assert_ok!(crate::Pallet::::set_block_number(RuntimeOrigin::root(), desired_block_number)); - - // Simulate block production - System::on_initialize(1); - crate::Pallet::::on_initialize(1); - assert_eq!(System::block_number(), desired_block_number); - - crate::Pallet::::on_finalize(1); - System::on_finalize(1); - assert_eq!(System::block_number(), 1); - - // Advance to the next block - System::set_block_number(2); - System::on_initialize(2); - crate::Pallet::::on_initialize(2); - assert_eq!(System::block_number(), desired_block_number); - - crate::Pallet::::on_finalize(2); - System::on_finalize(2); - assert_eq!(System::block_number(), 2); - }); + run_test(|| { + // Initial block number + assert_eq!(System::block_number(), 1); + + // Set desired block number + let desired_block_number = 95; + assert_ok!(crate::Pallet::::set_block_number( + RuntimeOrigin::root(), + desired_block_number + )); + + // Simulate block production + System::on_initialize(1); + crate::Pallet::::on_initialize(1); + assert_eq!(System::block_number(), desired_block_number); + + crate::Pallet::::on_finalize(1); + System::on_finalize(1); + assert_eq!(System::block_number(), 1); + + // Advance to the next block + System::set_block_number(2); + System::on_initialize(2); + crate::Pallet::::on_initialize(2); + assert_eq!(System::block_number(), desired_block_number); + + crate::Pallet::::on_finalize(2); + System::on_finalize(2); + assert_eq!(System::block_number(), 2); + }); } #[cfg(feature = "instant-seal")] #[test] fn setting_block_number_to_less_than_current_fails() { - run_test(|| { - // Initial block number - assert_eq!(System::block_number(), 1); - - // Attempt to set desired block number to a value less than the current block number - assert_noop!( - crate::Pallet::::set_block_number(RuntimeOrigin::root(), 0), - Error::::InvalidBlockNumber - ); - - // Block number should remain unchanged - assert_eq!(System::block_number(), 1); - }); + run_test(|| { + // Initial block number + assert_eq!(System::block_number(), 1); + + // Attempt to set desired block number to a value less than the current block number + assert_noop!( + crate::Pallet::::set_block_number(RuntimeOrigin::root(), 0), + Error::::InvalidBlockNumber + ); + + // Block number should remain unchanged + assert_eq!(System::block_number(), 1); + }); } diff --git a/runtime/foucoco/src/lib.rs b/runtime/foucoco/src/lib.rs index 6c5899da3..2381b631f 100644 --- a/runtime/foucoco/src/lib.rs +++ b/runtime/foucoco/src/lib.rs @@ -1415,15 +1415,15 @@ impl pallet_proxy::Config for Runtime { } cfg_if::cfg_if! { - if #[cfg(feature = "instant-seal")] { - impl pallet_mock_skip_blocks::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - } - } else { - impl pallet_mock_skip_blocks::dummy::Config for Runtime { + if #[cfg(feature = "instant-seal")] { + impl pallet_mock_skip_blocks::Config for Runtime { type RuntimeEvent = RuntimeEvent; } - } + } else { + impl pallet_mock_skip_blocks::dummy::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + } + } } impl frame_system::offchain::SendTransactionTypes for Runtime @@ -1520,7 +1520,7 @@ construct_runtime!( // Asset Metadata AssetRegistry: orml_asset_registry::{Pallet, Storage, Call, Event, Config} = 91, - MockSkipBlocks: pallet_mock_skip_blocks::{Pallet, Call, Storage, Event} = 92, + MockSkipBlocks: pallet_mock_skip_blocks::{Pallet, Call, Storage, Event} = 92, } ); From 98798036ad1402ece7725a122d8fc970ed59aad8 Mon Sep 17 00:00:00 2001 From: bogdanS98 Date: Fri, 19 Jul 2024 19:45:34 +0300 Subject: [PATCH 3/5] Remove extra flags --- pallets/pallet-mock-skip-blocks/src/lib.rs | 2 ++ pallets/pallet-mock-skip-blocks/src/mock.rs | 17 ----------------- pallets/pallet-mock-skip-blocks/src/tests.rs | 4 ---- 3 files changed, 2 insertions(+), 21 deletions(-) diff --git a/pallets/pallet-mock-skip-blocks/src/lib.rs b/pallets/pallet-mock-skip-blocks/src/lib.rs index 78075318b..11d1ecf49 100644 --- a/pallets/pallet-mock-skip-blocks/src/lib.rs +++ b/pallets/pallet-mock-skip-blocks/src/lib.rs @@ -31,9 +31,11 @@ pub mod dummy { impl Pallet {} } +#[cfg(feature = "instant-seal")] #[cfg(test)] mod mock; +#[cfg(feature = "instant-seal")] #[cfg(test)] mod tests; diff --git a/pallets/pallet-mock-skip-blocks/src/mock.rs b/pallets/pallet-mock-skip-blocks/src/mock.rs index 01118cba0..b6902e1b1 100644 --- a/pallets/pallet-mock-skip-blocks/src/mock.rs +++ b/pallets/pallet-mock-skip-blocks/src/mock.rs @@ -1,22 +1,15 @@ -#[cfg(feature = "instant-seal")] use crate::{self as pallet_mock_skip_blocks, Config}; -#[cfg(feature = "instant-seal")] use frame_support::{parameter_types, traits::Everything}; -#[cfg(feature = "instant-seal")] use sp_core::H256; -#[cfg(feature = "instant-seal")] use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, }; -#[cfg(feature = "instant-seal")] type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; -#[cfg(feature = "instant-seal")] type Block = frame_system::mocking::MockBlock; // Configure a mock runtime to test the pallet. -#[cfg(feature = "instant-seal")] frame_support::construct_runtime!( pub enum Test where Block = Block, @@ -28,23 +21,17 @@ frame_support::construct_runtime!( } ); -#[cfg(feature = "instant-seal")] pub type AccountId = u64; -#[cfg(feature = "instant-seal")] pub type BlockNumber = u64; -#[cfg(feature = "instant-seal")] pub type Index = u64; -#[cfg(feature = "instant-seal")] parameter_types! { pub const BlockHashCount: u64 = 250; pub const SS58Prefix: u8 = 42; } -#[cfg(feature = "instant-seal")] pub type TestEvent = RuntimeEvent; -#[cfg(feature = "instant-seal")] impl frame_system::Config for Test { type BaseCallFilter = Everything; type BlockWeights = (); @@ -72,15 +59,12 @@ impl frame_system::Config for Test { type MaxConsumers = frame_support::traits::ConstU32<16>; } -#[cfg(feature = "instant-seal")] impl Config for Test { type RuntimeEvent = RuntimeEvent; } -#[cfg(feature = "instant-seal")] pub struct ExtBuilder; -#[cfg(feature = "instant-seal")] impl ExtBuilder { pub fn build() -> sp_io::TestExternalities { let storage = frame_system::GenesisConfig::default().build_storage::().unwrap(); @@ -88,7 +72,6 @@ impl ExtBuilder { } } -#[cfg(feature = "instant-seal")] pub fn run_test(test: T) where T: FnOnce(), diff --git a/pallets/pallet-mock-skip-blocks/src/tests.rs b/pallets/pallet-mock-skip-blocks/src/tests.rs index 5cc26d4f0..f101a1fef 100644 --- a/pallets/pallet-mock-skip-blocks/src/tests.rs +++ b/pallets/pallet-mock-skip-blocks/src/tests.rs @@ -1,10 +1,7 @@ #![cfg(test)] -#[cfg(feature = "instant-seal")] use crate::{mock::*, Error}; -#[cfg(feature = "instant-seal")] use frame_support::{assert_noop, assert_ok, traits::Hooks}; -#[cfg(feature = "instant-seal")] #[test] fn sets_and_reverts_block_number_success() { run_test(|| { @@ -39,7 +36,6 @@ fn sets_and_reverts_block_number_success() { }); } -#[cfg(feature = "instant-seal")] #[test] fn setting_block_number_to_less_than_current_fails() { run_test(|| { From 965673e188e4c8f1ecdb754761f428f9d214159f Mon Sep 17 00:00:00 2001 From: bogdanS98 Date: Mon, 22 Jul 2024 19:05:58 +0300 Subject: [PATCH 4/5] Add missing feature flag --- Cargo.lock | 1 - node/Cargo.toml | 2 ++ pallets/pallet-mock-skip-blocks/Cargo.toml | 8 ++++---- pallets/pallet-mock-skip-blocks/src/lib.rs | 5 ++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ea5f6e00..9577b1aed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7231,7 +7231,6 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", - "sp-std", ] [[package]] diff --git a/node/Cargo.toml b/node/Cargo.toml index 09f12c5c2..3d60349c5 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -113,3 +113,5 @@ try-runtime = [ "pendulum-runtime/try-runtime", "try-runtime-cli/try-runtime" ] + +instant-seal = ["foucoco-runtime/instant-seal"] diff --git a/pallets/pallet-mock-skip-blocks/Cargo.toml b/pallets/pallet-mock-skip-blocks/Cargo.toml index fac245ad0..8ed1ec519 100644 --- a/pallets/pallet-mock-skip-blocks/Cargo.toml +++ b/pallets/pallet-mock-skip-blocks/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" default = ["std"] std = [ "frame-system/std", + "frame-support/std", ] instant-seal = ["std"] @@ -17,9 +18,8 @@ scale-info = { version = "2.2.0", default-features = false, features = ["derive" serde = { version = "1.0.130", default-features = false, features = ["derive"], optional = true } frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } -sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } -sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } -sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } [dev-dependencies] -sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } \ No newline at end of file +sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } +sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } +sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } \ No newline at end of file diff --git a/pallets/pallet-mock-skip-blocks/src/lib.rs b/pallets/pallet-mock-skip-blocks/src/lib.rs index 11d1ecf49..344c8f6f6 100644 --- a/pallets/pallet-mock-skip-blocks/src/lib.rs +++ b/pallets/pallet-mock-skip-blocks/src/lib.rs @@ -1,11 +1,10 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature = "instant-seal")] -pub use pallet::*; - #[cfg(not(feature = "instant-seal"))] pub use dummy as pallet; +pub use pallet::*; + #[cfg(not(feature = "instant-seal"))] #[frame_support::pallet] pub mod dummy { From c24503634e40471f9c807a21a17475afab11bcb0 Mon Sep 17 00:00:00 2001 From: bogdanS98 Date: Tue, 30 Jul 2024 17:28:44 +0300 Subject: [PATCH 5/5] Still WIP --- Cargo.lock | 16 +++++ pallets/pallet-mock-skip-blocks/Cargo.toml | 4 +- pallets/pallet-mock-skip-blocks/src/lib.rs | 76 ++++++++++++---------- runtime/foucoco/src/lib.rs | 30 ++++++--- 4 files changed, 82 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9577b1aed..ac8baa58e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6988,6 +6988,22 @@ dependencies = [ "syn 2.0.65", ] +[[package]] +name = "pallet-contracts-test" +version = "0.1.0" +dependencies = [ + "frame-support", + "frame-system", + "pallet-contracts", + "pallet-contracts-primitives", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core", + "sp-io", + "sp-runtime", +] + [[package]] name = "pallet-conviction-voting" version = "4.0.0-dev" diff --git a/pallets/pallet-mock-skip-blocks/Cargo.toml b/pallets/pallet-mock-skip-blocks/Cargo.toml index 8ed1ec519..373dc25cd 100644 --- a/pallets/pallet-mock-skip-blocks/Cargo.toml +++ b/pallets/pallet-mock-skip-blocks/Cargo.toml @@ -10,14 +10,16 @@ std = [ "frame-support/std", ] -instant-seal = ["std"] +instant-seal = [] [dependencies] +# hex-literal = { version = "0.3.4", optional = true } codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "max-encoded-len"] } scale-info = { version = "2.2.0", default-features = false, features = ["derive"] } serde = { version = "1.0.130", default-features = false, features = ["derive"], optional = true } frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } +sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } [dev-dependencies] sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } diff --git a/pallets/pallet-mock-skip-blocks/src/lib.rs b/pallets/pallet-mock-skip-blocks/src/lib.rs index 344c8f6f6..18bc3f839 100644 --- a/pallets/pallet-mock-skip-blocks/src/lib.rs +++ b/pallets/pallet-mock-skip-blocks/src/lib.rs @@ -1,44 +1,44 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(not(feature = "instant-seal"))] -pub use dummy as pallet; +// #[cfg(not(feature = "instant-seal"))] +// pub use dummy as pallet; pub use pallet::*; -#[cfg(not(feature = "instant-seal"))] -#[frame_support::pallet] -pub mod dummy { - use frame_support::pallet_prelude::*; - - #[pallet::pallet] - pub struct Pallet(_); - - #[pallet::event] - #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { - /// Dummy event - DummyEvent, - } - - #[pallet::config] - pub trait Config: frame_system::Config { - /// Overarching event type - type RuntimeEvent: From> + IsType<::RuntimeEvent>; - } - - #[pallet::call] - impl Pallet {} -} - -#[cfg(feature = "instant-seal")] +// #[cfg(not(feature = "instant-seal"))] +// #[frame_support::pallet] +// pub mod dummy { +// use frame_support::pallet_prelude::*; +// +// #[pallet::pallet] +// pub struct Pallet(_); +// +// #[pallet::event] +// #[pallet::generate_deposit(pub(super) fn deposit_event)] +// pub enum Event { +// /// Dummy event +// DummyEvent, +// } +// +// #[pallet::config] +// pub trait Config: frame_system::Config { +// /// Overarching event type +// type RuntimeEvent: From> + IsType<::RuntimeEvent>; +// } +// +// #[pallet::call] +// impl Pallet {} +// } + +//#[cfg(feature = "instant-seal")] #[cfg(test)] mod mock; -#[cfg(feature = "instant-seal")] +//#[cfg(feature = "instant-seal")] #[cfg(test)] mod tests; -#[cfg(feature = "instant-seal")] +//#[cfg(feature = "instant-seal")] #[frame_support::pallet] pub mod pallet { use frame_support::{dispatch::DispatchResult, pallet_prelude::*}; @@ -52,6 +52,14 @@ pub mod pallet { InvalidBlockNumber, } + //const ENCODED_KEY: &[u8] = &hex_literal::hex!("0x26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac"); + + const ENCODED_KEY: &[u8] = &[ + 0x26, 0xaa, 0x39, 0x4e, 0xea, 0x56, 0x30, 0xe0, 0x7c, 0x48, 0xae, 0x0c, + 0x95, 0x58, 0xce, 0xf7, 0x02, 0xa5, 0xc1, 0xb1, 0x9a, 0xb7, 0xa0, 0x4f, + 0x53, 0x6c, 0x51, 0x9a, 0xca, 0x49, 0x83, 0xac, + ]; + #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { @@ -68,14 +76,16 @@ pub mod pallet { fn on_initialize(n: T::BlockNumber) -> Weight { let desired_block_number = DesiredBlockNumber::::get(); OriginalBlockNumber::::put(n); - frame_system::Pallet::::set_block_number(desired_block_number); + //frame_system::Pallet::::set_block_number(desired_block_number); + sp_io::storage::set(ENCODED_KEY, &*desired_block_number.encode()); Self::deposit_event(Event::::BlockSet { n: desired_block_number }); - 0.into() + Weight::from_ref_time(0) } fn on_finalize(_: T::BlockNumber) { let original_block_number = OriginalBlockNumber::::get(); - frame_system::Pallet::::set_block_number(original_block_number); + //frame_system::Pallet::::set_block_number(original_block_number); + sp_io::storage::set(ENCODED_KEY, &*original_block_number.encode()); Self::deposit_event(Event::::BlockReverted { n: original_block_number }); } } diff --git a/runtime/foucoco/src/lib.rs b/runtime/foucoco/src/lib.rs index 2381b631f..a92f573f8 100644 --- a/runtime/foucoco/src/lib.rs +++ b/runtime/foucoco/src/lib.rs @@ -1414,18 +1414,28 @@ impl pallet_proxy::Config for Runtime { type AnnouncementDepositFactor = AnnouncementDepositFactor; } -cfg_if::cfg_if! { - if #[cfg(feature = "instant-seal")] { - impl pallet_mock_skip_blocks::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - } - } else { - impl pallet_mock_skip_blocks::dummy::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - } - } +// cfg_if::cfg_if! { +// if #[cfg(feature = "instant-seal")] { +// impl pallet_mock_skip_blocks::Config for Runtime { +// type RuntimeEvent = RuntimeEvent; +// } +// } else { +// impl pallet_mock_skip_blocks::dummy::Config for Runtime { +// type RuntimeEvent = RuntimeEvent; +// } +// } +// } + +//#[cfg(feature = "instant-seal")] +impl pallet_mock_skip_blocks::Config for Runtime { + type RuntimeEvent = RuntimeEvent; } +// #[cfg(not(feature = "instant-seal"))] +// impl pallet_mock_skip_blocks::dummy::Config for Runtime { +// type RuntimeEvent = RuntimeEvent; +// } + impl frame_system::offchain::SendTransactionTypes for Runtime where RuntimeCall: From,