Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor extrinsics + restrict weight setting #40

Merged
merged 9 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions pallets/emission0/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,4 @@ benchmarks! {
<T::Torus>::force_register_agent(&module_key2, vec![], vec![], vec![])?;

}: _(RawOrigin::Signed(module_key), module_key2.clone())

regain_weight_control {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
let module_key2: T::AccountId = account("ModuleKey2", 0, 3);

<T::Torus>::force_register_agent(&module_key, vec![], vec![], vec![])?;
<T::Torus>::force_register_agent(&module_key2, vec![], vec![], vec![])?;

Pallet::<T>::delegate_weight_control(RawOrigin::Signed(module_key.clone()).into(), module_key2.clone())?;

}: _(RawOrigin::Signed(module_key))
}
13 changes: 5 additions & 8 deletions pallets/emission0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ pub mod pallet {
/// Tried setting weights for itself.
CannotSetWeightsForSelf,

/// Tried setting weights while delegating weight control.
CannotSetWeightsWhileDelegating,

/// Tried delegating weight control to itself.
CannotDelegateWeightControlToSelf,

Expand Down Expand Up @@ -152,20 +155,14 @@ pub mod pallet {
weight_control::set_weights::<T>(origin, weights)
}

#[pallet::call_index(2)]
#[pallet::weight((T::WeightInfo::delegate_weight_control(), DispatchClass::Normal, Pays::Yes))]
#[pallet::call_index(1)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
functor-flow marked this conversation as resolved.
Show resolved Hide resolved
pub fn delegate_weight_control(
origin: OriginFor<T>,
target: AccountIdOf<T>,
) -> DispatchResult {
weight_control::delegate_weight_control::<T>(origin, target)
}

#[pallet::call_index(3)]
#[pallet::weight((T::WeightInfo::regain_weight_control(), DispatchClass::Normal, Pays::Yes))]
pub fn regain_weight_control(origin: OriginFor<T>) -> DispatchResult {
weight_control::regain_weight_control::<T>(origin)
}
}
}

Expand Down
9 changes: 0 additions & 9 deletions pallets/emission0/src/weight_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,3 @@ pub fn delegate_weight_control<T: crate::Config>(

Ok(())
}

pub fn regain_weight_control<T: crate::Config>(origin: OriginFor<T>) -> DispatchResult {
let acc_id = ensure_signed(origin)?;

crate::WeightControlDelegation::<T>::mutate(acc_id, |val| match val.take() {
Some(_) => Ok(()),
None => Err(crate::Error::<T>::AgentIsNotDelegating.into()),
})
}
23 changes: 0 additions & 23 deletions pallets/emission0/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use core::marker::PhantomData;
pub trait WeightInfo {
fn set_weights() -> Weight;
fn delegate_weight_control() -> Weight;
fn regain_weight_control() -> Weight;
}

/// Weights for `pallet_emission0` using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -78,17 +77,6 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: `Emission0::WeightControlDelegation` (r:1 w:1)
/// Proof: `Emission0::WeightControlDelegation` (`max_values`: None, `max_size`: Some(64), added: 2539, mode: `MaxEncodedLen`)
fn regain_weight_control() -> Weight {
// Proof Size summary in bytes:
// Measured: `199`
// Estimated: `3529`
// Minimum execution time: 7_000_000 picoseconds.
Weight::from_parts(8_000_000, 3529)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests.
Expand Down Expand Up @@ -125,15 +113,4 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: `Emission0::WeightControlDelegation` (r:1 w:1)
/// Proof: `Emission0::WeightControlDelegation` (`max_values`: None, `max_size`: Some(64), added: 2539, mode: `MaxEncodedLen`)
fn regain_weight_control() -> Weight {
// Proof Size summary in bytes:
// Measured: `199`
// Estimated: `3529`
// Minimum execution time: 7_000_000 picoseconds.
Weight::from_parts(8_000_000, 3529)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
25 changes: 10 additions & 15 deletions pallets/emission0/tests/weights.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use pallet_emission0::{
weight_control::{delegate_weight_control, regain_weight_control, set_weights},
weights::{delegate_weight_control, set_weights},

Check failure on line 2 in pallets/emission0/tests/weights.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved imports `pallet_emission0::weights::delegate_weight_control`, `pallet_emission0::weights::set_weights`

error[E0432]: unresolved imports `pallet_emission0::weights::delegate_weight_control`, `pallet_emission0::weights::set_weights` --> pallets/emission0/tests/weights.rs:2:15 | 2 | weights::{delegate_weight_control, set_weights}, | ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^ | | | | | no `set_weights` in `weights` | | help: a similar name exists in the module: `sp_weights` | no `delegate_weight_control` in `weights` | = help: consider importing this variant instead: pallet_emission0::Call::delegate_weight_control = help: consider importing this variant instead: pallet_emission0::Call::set_weights
functor-flow marked this conversation as resolved.
Show resolved Hide resolved
ConsensusMembers, Error, MaxAllowedWeights, MinStakePerWeight, WeightControlDelegation,
Weights,
};
use test_utils::{add_stake, get_origin, register_empty_agent, Test};
use test_utils::{
add_stake, get_origin, pallet_governance::Allocators, register_empty_agent, Test,
};

#[test]
fn delegates_and_regains_weight_control() {
Expand All @@ -21,11 +23,6 @@
Err(Error::<Test>::AgentIsNotRegistered.into())
);

assert_eq!(
regain_weight_control::<Test>(get_origin(delegator)),
Err(Error::<Test>::AgentIsNotDelegating.into())
);

register_empty_agent(delegator);

assert_eq!(
Expand All @@ -41,10 +38,6 @@
);

assert!(WeightControlDelegation::<Test>::contains_key(delegator));

assert_eq!(regain_weight_control::<Test>(get_origin(delegator)), Ok(()));

assert!(!WeightControlDelegation::<Test>::contains_key(delegator));
});
}

Expand All @@ -56,10 +49,7 @@

let validator = 0;

assert_eq!(
set_weights::<Test>(get_origin(validator), vec![(0, 0); 6]),
Err(Error::<Test>::WeightSetTooLarge.into()),
);
Allocators::<Test>::insert(0, ());

assert_eq!(
set_weights::<Test>(get_origin(validator), vec![(0, 0); 5]),
Expand All @@ -68,6 +58,11 @@

register_empty_agent(validator);

assert_eq!(
set_weights::<Test>(get_origin(validator), vec![(0, 0); 6]),
Err(Error::<Test>::WeightSetTooLarge.into()),
);

assert_eq!(
set_weights::<Test>(get_origin(validator), vec![(0, 0); 5]),
Err(Error::<Test>::NotEnoughStakeToSetWeights.into()),
Expand Down
4 changes: 3 additions & 1 deletion pallets/governance/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#![no_std]

use polkadot_sdk::sp_runtime::Percent;
use polkadot_sdk::{frame_support::dispatch::DispatchResult, sp_runtime::Percent};

pub trait GovernanceApi<AccountId> {
fn dao_treasury_address() -> AccountId;

fn treasury_emission_fee() -> Percent;

fn is_whitelisted(key: &AccountId) -> bool;

fn ensure_allocator(key: &AccountId) -> DispatchResult;
}
55 changes: 0 additions & 55 deletions pallets/governance/src/curator.rs

This file was deleted.

Loading
Loading