Skip to content

Commit

Permalink
add utility pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteolaf committed Apr 17, 2024
1 parent 958c988 commit 38e64ed
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 4 additions & 2 deletions pallets/regions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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" ]
14 changes: 11 additions & 3 deletions pallets/regions/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ frame_support::construct_runtime!(
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Balances: pallet_balances,
Utility: pallet_utility,
Regions: crate::{Pallet, Call, Storage, Event<T>},
}
);
Expand Down Expand Up @@ -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<Test>;
}

parameter_types! {
pub const CoretimeChain: StateMachine = StateMachine::Kusama(1005); // coretime-kusama
}
Expand All @@ -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::<Test>::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
}
43 changes: 40 additions & 3 deletions pallets/regions/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// along with RegionX. If not, see <https://www.gnu.org/licenses/>.

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::{
Expand Down Expand Up @@ -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::<Test>::RegionRecordRequested { region_id, account: 1 }.into());
System::assert_last_event(
Event::<Test>::RegionRecordRequested { region_id, account: 1 }.into(),
);
});
}

Expand Down Expand Up @@ -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(&region1_id.into(), &1));
assert_ok!(Regions::mint_into(&region2_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(&region1_id).unwrap(),
Region { owner: 2, record: Record::Pending }
);
assert_eq!(
Regions::regions(&region2_id).unwrap(),
Region { owner: 2, record: Record::Pending }
);
});
}

#[test]
fn utils_read_value_works() {
new_test_ext().execute_with(|| {
Expand Down
4 changes: 4 additions & 0 deletions runtime/regionx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions runtime/regionx/src/ismp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ impl IsmpModule for ProxyModule {

#[allow(clippy::match_single_binding)]
match pallet_id {
pallet_regions::PALLET_ID =>
pallet_regions::IsmpModuleCallback::<Runtime>::default().on_response(response),
pallet_regions::PALLET_ID => {
pallet_regions::IsmpModuleCallback::<Runtime>::default().on_response(response)
},
_ => Err(Error::ImplementationSpecific("Destination module not found".to_string())),
}
}
Expand All @@ -103,8 +104,9 @@ impl IsmpModule for ProxyModule {

#[allow(clippy::match_single_binding)]
match pallet_id {
pallet_regions::PALLET_ID =>
pallet_regions::IsmpModuleCallback::<Runtime>::default().on_timeout(timeout),
pallet_regions::PALLET_ID => {
pallet_regions::IsmpModuleCallback::<Runtime>::default().on_timeout(timeout)
},
// instead of returning an error, do nothing. The timeout is for a connected chain.
_ => Ok(()),
}
Expand Down
9 changes: 9 additions & 0 deletions runtime/regionx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime>;
}

impl pallet_authorship::Config for Runtime {
type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Aura>;
type EventHandler = (CollatorSelection,);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -628,6 +636,7 @@ mod benches {
[pallet_balances, Balances]
[pallet_session, SessionBench::<Runtime>]
[pallet_timestamp, Timestamp]
[pallet_utility, Utility],
[pallet_sudo, Sudo]
[pallet_collator_selection, CollatorSelection]
[cumulus_pallet_xcmp_queue, XcmpQueue]
Expand Down

0 comments on commit 38e64ed

Please sign in to comment.