From 893cdc0252c3e8a81307c98666c8c406b269c903 Mon Sep 17 00:00:00 2001 From: Sergej Sakac <73715684+Szegoo@users.noreply.github.com> Date: Sun, 4 Aug 2024 16:59:33 +0200 Subject: [PATCH] Make `request_region_record` an unsigned extrinsic (#223) * request_region_record with unsigned origin * fix benchmark * update weight --- pallets/regions/src/benchmarking.rs | 2 +- pallets/regions/src/lib.rs | 12 +++++------ pallets/regions/src/tests.rs | 15 +++++++------- runtime/cocos/src/weights/pallet_regions.rs | 22 ++++++++++----------- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/pallets/regions/src/benchmarking.rs b/pallets/regions/src/benchmarking.rs index 67cc057..7bdd7a1 100644 --- a/pallets/regions/src/benchmarking.rs +++ b/pallets/regions/src/benchmarking.rs @@ -64,7 +64,7 @@ mod benchmarks { assert_ok!(crate::Pallet::::mint_into(®ion_id.into(), &caller)); #[extrinsic_call] - _(RawOrigin::Signed(caller.clone()), region_id); + _(RawOrigin::None, region_id); assert!(crate::Pallet::::regions(®ion_id).unwrap().record.is_pending()); Ok(()) diff --git a/pallets/regions/src/lib.rs b/pallets/regions/src/lib.rs index bf6a680..3bf90da 100644 --- a/pallets/regions/src/lib.rs +++ b/pallets/regions/src/lib.rs @@ -126,8 +126,6 @@ pub mod pallet { RegionRecordRequested { /// The id of the region that the request was made for. region_id: RegionId, - /// The account who requested the region record. - account: T::AccountId, /// The ismp get request commitment. request_commitment: H256, }, @@ -174,16 +172,17 @@ pub mod pallet { #[pallet::call_index(1)] #[pallet::weight(T::WeightInfo::request_region_record())] pub fn request_region_record(origin: OriginFor, region_id: RegionId) -> DispatchResult { - let who = ensure_signed(origin)?; + ensure_none(origin)?; let region = Regions::::get(region_id).ok_or(Error::::UnknownRegion)?; - ensure!(region.record.is_unavailable(), Error::::NotUnavailable); - let commitment = Self::do_request_region_record(region_id, who.clone())?; + // Even though we don't know if this was requested by the region owner, we can use it + // since there is no fee charged. + let commitment = Self::do_request_region_record(region_id, region.owner.clone())?; Regions::::insert( region_id, - Region { owner: who.clone(), locked: false, record: Record::Pending(commitment) }, + Region { owner: region.owner, locked: false, record: Record::Pending(commitment) }, ); Ok(()) @@ -261,7 +260,6 @@ pub mod pallet { Self::deposit_event(Event::RegionRecordRequested { region_id, - account: who, request_commitment: commitment, }); diff --git a/pallets/regions/src/tests.rs b/pallets/regions/src/tests.rs index 183ca9a..0d5da3d 100644 --- a/pallets/regions/src/tests.rs +++ b/pallets/regions/src/tests.rs @@ -73,7 +73,7 @@ fn set_record_works() { // `set_record` succeeds: assert_ok!(Regions::mint_into(®ion_id.into(), &2)); - assert_ok!(Regions::request_region_record(RuntimeOrigin::signed(2), region_id)); + assert_ok!(Regions::request_region_record(RuntimeOrigin::none(), region_id)); assert!(Regions::regions(region_id).is_some()); let region = Regions::regions(region_id).unwrap(); @@ -101,19 +101,19 @@ fn request_region_record_works() { // fails to request unknown regions assert_noop!( - Regions::request_region_record(RuntimeOrigin::signed(1), region_id), + Regions::request_region_record(RuntimeOrigin::none(), region_id), Error::::UnknownRegion ); assert_ok!(Regions::mint_into(®ion_id.into(), &1)); - assert_ok!(Regions::request_region_record(RuntimeOrigin::signed(1), region_id)); + assert_ok!(Regions::request_region_record(RuntimeOrigin::none(), region_id)); assert!(Regions::regions(region_id).is_some()); let region = Regions::regions(region_id).unwrap(); assert!(region.record.is_pending()); // Cannot request if there is already a request pending. assert_noop!( - Regions::request_region_record(RuntimeOrigin::signed(1), region_id), + Regions::request_region_record(RuntimeOrigin::none(), region_id), Error::::NotUnavailable ); @@ -123,7 +123,7 @@ fn request_region_record_works() { *val = Some(v0); }); - assert_ok!(Regions::request_region_record(RuntimeOrigin::signed(1), region_id)); + assert_ok!(Regions::request_region_record(RuntimeOrigin::none(), region_id)); assert!(region.record.is_pending()); let request = &requests()[0]; let Request::Get(get) = request.request.clone() else { panic!("Expected GET request") }; @@ -139,7 +139,6 @@ fn request_region_record_works() { System::assert_last_event( Event::::RegionRecordRequested { region_id, - account: 1, request_commitment: Default::default(), } .into(), @@ -185,7 +184,7 @@ fn on_response_works() { let region_id = RegionId { begin: 112830, core: 72, mask: CoreMask::complete() }; assert_ok!(Regions::mint_into(®ion_id.into(), &2)); - assert_ok!(Regions::request_region_record(RuntimeOrigin::signed(2), region_id)); + assert_ok!(Regions::request_region_record(RuntimeOrigin::none(), region_id)); assert_eq!( Regions::regions(®ion_id).unwrap(), Region { owner: 2, locked: false, record: Record::Pending(Default::default()) } @@ -265,7 +264,7 @@ fn on_timeout_works() { let region_id = RegionId { begin: 0, core: 72, mask: CoreMask::complete() }; assert_ok!(Regions::mint_into(®ion_id.into(), &2)); - assert_ok!(Regions::request_region_record(RuntimeOrigin::signed(2), region_id)); + assert_ok!(Regions::request_region_record(RuntimeOrigin::none(), region_id)); assert_eq!( Regions::regions(®ion_id).unwrap(), Region { owner: 2, locked: false, record: Record::Pending(Default::default()) } diff --git a/runtime/cocos/src/weights/pallet_regions.rs b/runtime/cocos/src/weights/pallet_regions.rs index dc5d39e..0b42410 100644 --- a/runtime/cocos/src/weights/pallet_regions.rs +++ b/runtime/cocos/src/weights/pallet_regions.rs @@ -16,7 +16,7 @@ //! Autogenerated weights for `pallet_regions` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0 -//! DATE: 2024-07-17, STEPS: `20`, REPEAT: `50`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2024-08-04, STEPS: `20`, REPEAT: `50`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `cocos-instance-1`, CPU: `Intel(R) Xeon(R) CPU @ 2.80GHz` //! WASM-EXECUTION: `Compiled`, CHAIN: `Some("cocos")`, DB CACHE: `1024` @@ -60,8 +60,8 @@ impl pallet_regions::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `135` // Estimated: `3584` - // Minimum execution time: 15_244_000 picoseconds. - Weight::from_parts(15_579_000, 3584) + // Minimum execution time: 15_790_000 picoseconds. + Weight::from_parts(16_278_000, 3584) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -81,8 +81,8 @@ impl pallet_regions::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `527` // Estimated: `3992` - // Minimum execution time: 47_616_000 picoseconds. - Weight::from_parts(48_738_000, 3992) + // Minimum execution time: 48_966_000 picoseconds. + Weight::from_parts(50_182_000, 3992) .saturating_add(T::DbWeight::get().reads(6_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } @@ -90,8 +90,8 @@ impl pallet_regions::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `0` - // Minimum execution time: 752_000 picoseconds. - Weight::from_parts(836_000, 0) + // Minimum execution time: 780_000 picoseconds. + Weight::from_parts(854_000, 0) } /// Storage: `Regions::Regions` (r:1 w:1) /// Proof: `Regions::Regions` (`max_values`: None, `max_size`: Some(119), added: 2594, mode: `MaxEncodedLen`) @@ -99,8 +99,8 @@ impl pallet_regions::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `135` // Estimated: `3584` - // Minimum execution time: 12_516_000 picoseconds. - Weight::from_parts(12_883_000, 3584) + // Minimum execution time: 13_042_000 picoseconds. + Weight::from_parts(13_403_000, 3584) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -110,8 +110,8 @@ impl pallet_regions::WeightInfo for WeightInfo { // Proof Size summary in bytes: // Measured: `135` // Estimated: `3584` - // Minimum execution time: 8_165_000 picoseconds. - Weight::from_parts(8_401_000, 3584) + // Minimum execution time: 8_437_000 picoseconds. + Weight::from_parts(8_824_000, 3584) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) }