From 38e64ed03d878da36c7be7f70a4e5bb5a260d26d Mon Sep 17 00:00:00 2001 From: cuteolaf Date: Wed, 17 Apr 2024 22:23:02 +0000 Subject: [PATCH 1/2] add utility pallet --- Cargo.lock | 2 ++ Cargo.toml | 1 + pallets/regions/Cargo.toml | 6 +++-- pallets/regions/src/mock.rs | 14 +++++++++--- pallets/regions/src/tests.rs | 43 +++++++++++++++++++++++++++++++++--- runtime/regionx/Cargo.toml | 4 ++++ runtime/regionx/src/ismp.rs | 10 +++++---- runtime/regionx/src/lib.rs | 9 ++++++++ 8 files changed, 77 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d201462a..65210fb2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6787,6 +6787,7 @@ dependencies = [ "pallet-balances", "pallet-broker", "pallet-ismp", + "pallet-utility", "parity-scale-codec", "scale-info", "serde", @@ -9377,6 +9378,7 @@ dependencies = [ "pallet-timestamp", "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", + "pallet-utility", "pallet-xcm", "parachains-common", "parity-scale-codec", diff --git a/Cargo.toml b/Cargo.toml index 2388c5a5..64037787 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ pallet-sudo = { version = "28.0.0", default-features = false } pallet-timestamp = { version = "27.0.0", default-features = false } pallet-transaction-payment = { version = "28.0.0", default-features = false } pallet-transaction-payment-rpc-runtime-api = { version = "28.0.0", default-features = false } +pallet-utility = { version = "28.0.0", default-features = false} pallet-message-queue = { version = "31.0.0", default-features = false } sp-api = { version = "26.0.0", default-features = false } sp-blockchain = { version = "28.0.0", default-features = false } diff --git a/pallets/regions/Cargo.toml b/pallets/regions/Cargo.toml index d24c4032..6dde62f7 100644 --- a/pallets/regions/Cargo.toml +++ b/pallets/regions/Cargo.toml @@ -34,11 +34,12 @@ sp-runtime = { workspace = true, default-features = false } hex = { workspace = true } serde = { workspace = true } pallet-balances = { workspace = true, default-features = false } +pallet-utility = { workspace = true, default-features = false} ismp-testsuite = { workspace = true } [features] default = ["std"] -runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] +runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks", "pallet-utility/runtime-benchmarks"] std = [ "log/std", "parity-scale-codec/std", @@ -50,5 +51,6 @@ std = [ "frame-system/std", "pallet-balances/std", "pallet-ismp/std", + "pallet-utility/std" ] -try-runtime = [ "frame-support/try-runtime" ] +try-runtime = [ "frame-support/try-runtime", "pallet-utility/try-runtime" ] diff --git a/pallets/regions/src/mock.rs b/pallets/regions/src/mock.rs index 807246ef..0abd91c7 100644 --- a/pallets/regions/src/mock.rs +++ b/pallets/regions/src/mock.rs @@ -31,6 +31,7 @@ frame_support::construct_runtime!( { System: frame_system::{Pallet, Call, Config, Storage, Event}, Balances: pallet_balances, + Utility: pallet_utility, Regions: crate::{Pallet, Call, Storage, Event}, } ); @@ -84,6 +85,13 @@ impl pallet_balances::Config for Test { type MaxFreezes = (); } +impl pallet_utility::Config for Test { + type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; + type PalletsOrigin = OriginCaller; + type WeightInfo = pallet_utility::weights::SubstrateWeight; +} + parameter_types! { pub const CoretimeChain: StateMachine = StateMachine::Kusama(1005); // coretime-kusama } @@ -108,7 +116,7 @@ impl crate::Config for Test { // Build genesis storage according to the mock runtime. pub fn new_test_ext() -> sp_io::TestExternalities { let t = frame_system::GenesisConfig::::default().build_storage().unwrap(); - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); - ext + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext } diff --git a/pallets/regions/src/tests.rs b/pallets/regions/src/tests.rs index 5112e425..c93b03f0 100644 --- a/pallets/regions/src/tests.rs +++ b/pallets/regions/src/tests.rs @@ -14,8 +14,8 @@ // along with RegionX. If not, see . use crate::{ - ismp_mock::requests, mock::*, pallet::Regions as RegionsStorage, utils, Error, Event, - IsmpCustomError, IsmpModuleCallback, Record, Region, + ismp_mock::requests, mock::*, pallet::Regions as RegionsStorage, utils, Call as RegionsCall, + Error, Event, IsmpCustomError, IsmpModuleCallback, Record, Region, }; use frame_support::{assert_err, assert_ok, pallet_prelude::*, traits::nonfungible::Mutate}; use ismp::{ @@ -108,7 +108,9 @@ fn request_region_record_works() { assert_ok!(Regions::request_region_record(RuntimeOrigin::signed(1), region_id)); - System::assert_last_event(Event::::RegionRecordRequested { region_id, account: 1 }.into()); + System::assert_last_event( + Event::::RegionRecordRequested { region_id, account: 1 }.into(), + ); }); } @@ -244,6 +246,41 @@ fn on_accept_works() { }); } +#[test] +fn utlity_pallet_works() { + new_test_ext().execute_with(|| { + let region1_id = RegionId { begin: 112830, core: 81, mask: CoreMask::complete() }; + let region2_id = RegionId { begin: 112830, core: 82, mask: CoreMask::complete() }; + + assert_ok!(Regions::mint_into(®ion1_id.into(), &1)); + assert_ok!(Regions::mint_into(®ion2_id.into(), &1)); + + assert_ok!(Utility::batch( + RuntimeOrigin::signed(1), + vec![ + RuntimeCall::Regions(RegionsCall::transfer { + region_id: region1_id.clone(), + new_owner: 2 + }), + RuntimeCall::Regions(RegionsCall::transfer { + region_id: region2_id.clone(), + new_owner: 2 + }) + ] + )); + + // check the new owners + assert_eq!( + Regions::regions(®ion1_id).unwrap(), + Region { owner: 2, record: Record::Pending } + ); + assert_eq!( + Regions::regions(®ion2_id).unwrap(), + Region { owner: 2, record: Record::Pending } + ); + }); +} + #[test] fn utils_read_value_works() { new_test_ext().execute_with(|| { diff --git a/runtime/regionx/Cargo.toml b/runtime/regionx/Cargo.toml index 57bd086d..71a31879 100644 --- a/runtime/regionx/Cargo.toml +++ b/runtime/regionx/Cargo.toml @@ -52,6 +52,7 @@ pallet-sudo = { workspace = true } pallet-timestamp = { workspace = true } pallet-transaction-payment = { workspace = true } pallet-transaction-payment-rpc-runtime-api = { workspace = true } +pallet-utility = { workspace = true } sp-api = { workspace = true } sp-block-builder = { workspace = true } sp-consensus-aura = { workspace = true } @@ -126,6 +127,7 @@ std = [ "pallet-timestamp/std", "pallet-transaction-payment-rpc-runtime-api/std", "pallet-transaction-payment/std", + "pallet-utility/std", "pallet-xcm/std", "parachain-info/std", "parachains-common/std", @@ -170,6 +172,7 @@ runtime-benchmarks = [ "pallet-regions/runtime-benchmarks", "pallet-sudo/runtime-benchmarks", "pallet-timestamp/runtime-benchmarks", + "pallet-utility/runtime-benchmarks", "pallet-xcm/runtime-benchmarks", "parachains-common/runtime-benchmarks", "polkadot-parachain-primitives/runtime-benchmarks", @@ -203,6 +206,7 @@ try-runtime = [ "pallet-sudo/try-runtime", "pallet-timestamp/try-runtime", "pallet-transaction-payment/try-runtime", + "pallet-utility/try-runtime", "pallet-xcm/try-runtime", "parachain-info/try-runtime", "polkadot-runtime-common/try-runtime", diff --git a/runtime/regionx/src/ismp.rs b/runtime/regionx/src/ismp.rs index 914651d7..a3541dbe 100644 --- a/runtime/regionx/src/ismp.rs +++ b/runtime/regionx/src/ismp.rs @@ -85,8 +85,9 @@ impl IsmpModule for ProxyModule { #[allow(clippy::match_single_binding)] match pallet_id { - pallet_regions::PALLET_ID => - pallet_regions::IsmpModuleCallback::::default().on_response(response), + pallet_regions::PALLET_ID => { + pallet_regions::IsmpModuleCallback::::default().on_response(response) + }, _ => Err(Error::ImplementationSpecific("Destination module not found".to_string())), } } @@ -103,8 +104,9 @@ impl IsmpModule for ProxyModule { #[allow(clippy::match_single_binding)] match pallet_id { - pallet_regions::PALLET_ID => - pallet_regions::IsmpModuleCallback::::default().on_timeout(timeout), + pallet_regions::PALLET_ID => { + pallet_regions::IsmpModuleCallback::::default().on_timeout(timeout) + }, // instead of returning an error, do nothing. The timeout is for a connected chain. _ => Ok(()), } diff --git a/runtime/regionx/src/lib.rs b/runtime/regionx/src/lib.rs index caee0147..f0ef5053 100644 --- a/runtime/regionx/src/lib.rs +++ b/runtime/regionx/src/lib.rs @@ -334,6 +334,13 @@ impl pallet_timestamp::Config for Runtime { type WeightInfo = (); } +impl pallet_utility::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; + type PalletsOrigin = OriginCaller; + type WeightInfo = pallet_utility::weights::SubstrateWeight; +} + impl pallet_authorship::Config for Runtime { type FindAuthor = pallet_session::FindAccountFromAuthorIndex; type EventHandler = (CollatorSelection,); @@ -588,6 +595,7 @@ construct_runtime!( ParachainSystem: cumulus_pallet_parachain_system = 1, Timestamp: pallet_timestamp = 2, ParachainInfo: parachain_info = 3, + Utility: pallet_utility = 4, // Monetary stuff. Balances: pallet_balances = 10, @@ -628,6 +636,7 @@ mod benches { [pallet_balances, Balances] [pallet_session, SessionBench::] [pallet_timestamp, Timestamp] + [pallet_utility, Utility], [pallet_sudo, Sudo] [pallet_collator_selection, CollatorSelection] [cumulus_pallet_xcmp_queue, XcmpQueue] From 9faccb413c06f29ca078ae5d8f2db1a4d15a0635 Mon Sep 17 00:00:00 2001 From: cuteolaf Date: Wed, 17 Apr 2024 23:22:12 +0000 Subject: [PATCH 2/2] cargo fmt --- runtime/regionx/src/ismp.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/runtime/regionx/src/ismp.rs b/runtime/regionx/src/ismp.rs index a3541dbe..914651d7 100644 --- a/runtime/regionx/src/ismp.rs +++ b/runtime/regionx/src/ismp.rs @@ -85,9 +85,8 @@ impl IsmpModule for ProxyModule { #[allow(clippy::match_single_binding)] match pallet_id { - pallet_regions::PALLET_ID => { - pallet_regions::IsmpModuleCallback::::default().on_response(response) - }, + pallet_regions::PALLET_ID => + pallet_regions::IsmpModuleCallback::::default().on_response(response), _ => Err(Error::ImplementationSpecific("Destination module not found".to_string())), } } @@ -104,9 +103,8 @@ impl IsmpModule for ProxyModule { #[allow(clippy::match_single_binding)] match pallet_id { - pallet_regions::PALLET_ID => { - pallet_regions::IsmpModuleCallback::::default().on_timeout(timeout) - }, + pallet_regions::PALLET_ID => + pallet_regions::IsmpModuleCallback::::default().on_timeout(timeout), // instead of returning an error, do nothing. The timeout is for a connected chain. _ => Ok(()), }