Skip to content

Commit

Permalink
Make request_region_record an unsigned extrinsic (#223)
Browse files Browse the repository at this point in the history
* request_region_record with unsigned origin

* fix benchmark

* update weight
  • Loading branch information
Szegoo authored Aug 4, 2024
1 parent 68449f7 commit 893cdc0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pallets/regions/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod benchmarks {
assert_ok!(crate::Pallet::<T>::mint_into(&region_id.into(), &caller));

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), region_id);
_(RawOrigin::None, region_id);
assert!(crate::Pallet::<T>::regions(&region_id).unwrap().record.is_pending());

Ok(())
Expand Down
12 changes: 5 additions & 7 deletions pallets/regions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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<T>, region_id: RegionId) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure_none(origin)?;

let region = Regions::<T>::get(region_id).ok_or(Error::<T>::UnknownRegion)?;

ensure!(region.record.is_unavailable(), Error::<T>::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::<T>::insert(
region_id,
Region { owner: who.clone(), locked: false, record: Record::Pending(commitment) },
Region { owner: region.owner, locked: false, record: Record::Pending(commitment) },
);

Ok(())
Expand Down Expand Up @@ -261,7 +260,6 @@ pub mod pallet {

Self::deposit_event(Event::RegionRecordRequested {
region_id,
account: who,
request_commitment: commitment,
});

Expand Down
15 changes: 7 additions & 8 deletions pallets/regions/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn set_record_works() {
// `set_record` succeeds:

assert_ok!(Regions::mint_into(&region_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();
Expand Down Expand Up @@ -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::<Test>::UnknownRegion
);

assert_ok!(Regions::mint_into(&region_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::<Test>::NotUnavailable
);

Expand All @@ -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") };
Expand All @@ -139,7 +139,6 @@ fn request_region_record_works() {
System::assert_last_event(
Event::<Test>::RegionRecordRequested {
region_id,
account: 1,
request_commitment: Default::default(),
}
.into(),
Expand Down Expand Up @@ -185,7 +184,7 @@ fn on_response_works() {
let region_id = RegionId { begin: 112830, core: 72, mask: CoreMask::complete() };

assert_ok!(Regions::mint_into(&region_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(&region_id).unwrap(),
Region { owner: 2, locked: false, record: Record::Pending(Default::default()) }
Expand Down Expand Up @@ -265,7 +264,7 @@ fn on_timeout_works() {
let region_id = RegionId { begin: 0, core: 72, mask: CoreMask::complete() };

assert_ok!(Regions::mint_into(&region_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(&region_id).unwrap(),
Region { owner: 2, locked: false, record: Record::Pending(Default::default()) }
Expand Down
22 changes: 11 additions & 11 deletions runtime/cocos/src/weights/pallet_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -60,8 +60,8 @@ impl<T: frame_system::Config> pallet_regions::WeightInfo for WeightInfo<T> {
// 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))
}
Expand All @@ -81,26 +81,26 @@ impl<T: frame_system::Config> pallet_regions::WeightInfo for WeightInfo<T> {
// 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))
}
fn on_accept() -> Weight {
// 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`)
fn on_response() -> Weight {
// 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))
}
Expand All @@ -110,8 +110,8 @@ impl<T: frame_system::Config> pallet_regions::WeightInfo for WeightInfo<T> {
// 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))
}
Expand Down

0 comments on commit 893cdc0

Please sign in to comment.