Skip to content

Commit

Permalink
chore: remove old commune stuff (#80)
Browse files Browse the repository at this point in the history
Removes unused storage values from Commune times.

Storage values removed:
* `MinAllowedWeights`,
* `MaxAllowedWeights`,
* `MinStakePerWeight`,
* `RegistrationBlock` (in favor of `registration_block` within the Agent
value).
 
Fields removed from `GlobalParamsData`:
* `max_allowed_weights`,
* `min_stake_per_weight`.
  • Loading branch information
saiintbrisson authored Feb 11, 2025
1 parent 2091d69 commit 151076a
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* @supremesource @saiintbrisson @steinerkelvin @devwckd
* @functor-flow @saiintbrisson @steinerkelvin @devwckd

/.github/workflows/* @steinerkelvin @daviptrs @saiintbrisson
/docker/**/* @steinerkelvin @daviptrs @saiintbrisson
15 changes: 0 additions & 15 deletions pallets/emission0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,6 @@ pub mod pallet {
pub type WeightControlDelegation<T: Config> =
StorageMap<_, Identity, T::AccountId, T::AccountId>;

// TODO: remove
#[pallet::storage]
pub type MinAllowedWeights<T: Config> =
StorageValue<_, u16, ValueQuery, T::DefaultMinAllowedWeights>;

/// Maximum number of weights a validator can set.
#[pallet::storage]
pub type MaxAllowedWeights<T: Config> =
StorageValue<_, u16, ValueQuery, T::DefaultMaxAllowedWeights>;

// TODO: cap weights on distribution.
/// Minimum stake a validator needs for each weight it sets.
#[pallet::storage]
pub type MinStakePerWeight<T> = StorageValue<_, BalanceOf<T>, ValueQuery>;

/// Percentage of issued tokens to be burned every epoch.
#[pallet::storage]
pub type EmissionRecyclingPercentage<T: Config> =
Expand Down
15 changes: 5 additions & 10 deletions pallets/emission0/src/weight_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ pub fn set_weights<T: crate::Config>(
let acc_id = ensure_signed(origin)?;
<T::Governance>::ensure_allocator(&acc_id)?;

ensure!(
weights.len() <= crate::MaxAllowedWeights::<T>::get() as usize,
crate::Error::<T>::WeightSetTooLarge
);

ensure!(
!crate::WeightControlDelegation::<T>::contains_key(&acc_id),
crate::Error::<T>::CannotSetWeightsWhileDelegating,
Expand All @@ -37,12 +32,8 @@ pub fn set_weights<T: crate::Config>(
.iter()
.map(|(_, stake)| *stake)
.sum();
let min_stake_for_weights = crate::MinStakePerWeight::<T>::get()
.checked_mul(weights.len() as u128)
.unwrap_or_default();

ensure!(
total_stake >= min_stake_for_weights,
total_stake >= <T::Torus>::min_validator_stake(),
crate::Error::<T>::NotEnoughStakeToSetWeights
);

Expand Down Expand Up @@ -95,6 +86,10 @@ pub fn delegate_weight_control<T: crate::Config>(
crate::Error::<T>::AgentIsNotRegistered
);

// At the current network stage, it only makes sense to delegate weight control
// to allocators.
<T::Governance>::ensure_allocator(&target)?;

crate::WeightControlDelegation::<T>::set(&acc_id, Some(target.clone()));

crate::Pallet::<T>::deposit_event(crate::Event::<T>::DelegatedWeightControl(acc_id, target));
Expand Down
4 changes: 3 additions & 1 deletion pallets/emission0/tests/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use polkadot_sdk::{
use substrate_fixed::{traits::ToFixed, types::I96F32};
use test_utils::{
add_balance, add_stake, get_origin,
pallet_governance::TreasuryEmissionFee,
pallet_governance::{Allocators, TreasuryEmissionFee},
pallet_torus0::{
stake::sum_staked_by, Agents, FeeConstraints, MaxAllowedValidators, MinAllowedStake,
MinValidatorStake, StakedBy,
Expand Down Expand Up @@ -489,6 +489,8 @@ fn pays_weight_control_fee_and_dividends_to_stakers() {
register_empty_agent(id);
}

Allocators::<Test>::set(val_1, Some(()));

pallet_emission0::weight_control::delegate_weight_control::<Test>(get_origin(val_2), val_1)
.expect("failed to delegate weight control");

Expand Down
23 changes: 11 additions & 12 deletions pallets/emission0/tests/weights.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use pallet_emission0::{
weight_control::{delegate_weight_control, regain_weight_control, set_weights},
ConsensusMembers, Error, MaxAllowedWeights, MinStakePerWeight, WeightControlDelegation,
Weights,
ConsensusMembers, Error, WeightControlDelegation, Weights,
};
use test_utils::{
add_stake, get_origin, pallet_governance::Allocators, register_empty_agent, Test,
add_stake, get_origin, pallet_governance::Allocators, pallet_torus0::MinValidatorStake,
register_empty_agent, Test,
};

#[test]
Expand Down Expand Up @@ -37,6 +37,11 @@ fn delegates_and_regains_weight_control() {

register_empty_agent(delegated);

delegate_weight_control::<Test>(get_origin(delegator), delegated)
.expect_err("cannot delegate to not-allocator");

Allocators::<Test>::set(delegated, Some(()));

assert_eq!(
delegate_weight_control::<Test>(get_origin(delegator), delegated),
Ok(())
Expand All @@ -52,9 +57,6 @@ fn delegates_and_regains_weight_control() {
#[test]
fn sets_weights_correctly() {
test_utils::new_test_ext().execute_with(|| {
MaxAllowedWeights::<Test>::set(5);
MinStakePerWeight::<Test>::set(1);

let validator = 0;

assert_eq!(
Expand All @@ -71,17 +73,12 @@ fn sets_weights_correctly() {

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()),
);

add_stake(validator, validator, 3);
add_stake(validator, validator, MinValidatorStake::<Test>::get());

assert_eq!(
set_weights::<Test>(get_origin(validator), vec![(0, 0); 5]),
Expand All @@ -96,6 +93,8 @@ fn sets_weights_correctly() {
register_empty_agent(1);
register_empty_agent(2);

Allocators::<Test>::set(1, Some(()));

delegate_weight_control::<Test>(get_origin(validator), 1)
.expect("failed to delegate weight control");

Expand Down
14 changes: 5 additions & 9 deletions pallets/governance/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use polkadot_sdk::{
traits::{Currency, WithdrawReasons},
DebugNoBound,
},
polkadot_sdk_frame::prelude::BlockNumberFor,
sp_runtime::BoundedVec,
sp_std::vec::Vec,
};
use scale_info::TypeInfo;

use crate::{
frame::traits::ExistenceRequirement, whitelist, AccountIdOf, AgentApplications, BalanceOf,
Block,
};

/// Decentralized autonomous organization application, it's used to do agent
Expand All @@ -27,7 +27,7 @@ pub struct AgentApplication<T: crate::Config> {
pub agent_key: AccountIdOf<T>,
pub data: BoundedVec<u8, T::MaxApplicationDataLength>,
pub cost: BalanceOf<T>,
pub expires_at: Block,
pub expires_at: BlockNumberFor<T>,
pub action: ApplicationAction,
pub status: ApplicationStatus,
}
Expand Down Expand Up @@ -104,12 +104,8 @@ pub fn submit_application<T: crate::Config>(
return Err(crate::Error::<T>::InvalidApplicationDataLength.into());
}

let current_block: u64 =
TryInto::try_into(<polkadot_sdk::frame_system::Pallet<T>>::block_number())
.ok()
.expect("blockchain will not exceed 2^64 blocks; QED.");

let expires_at = current_block + config.agent_application_expiration;
let expires_at = <polkadot_sdk::frame_system::Pallet<T>>::block_number()
+ config.agent_application_expiration;

let next_id = AgentApplications::<T>::iter()
.count()
Expand Down Expand Up @@ -198,7 +194,7 @@ pub fn deny_application<T: crate::Config>(application_id: u32) -> DispatchResult
/// Iterates through all open applications checking if the current block is
/// greater or equal to the former's expiration block. If so, marks the
/// application as Expired.
pub(crate) fn resolve_expired_applications<T: crate::Config>(current_block: Block) {
pub(crate) fn resolve_expired_applications<T: crate::Config>(current_block: BlockNumberFor<T>) {
for application in crate::AgentApplications::<T>::iter_values() {
if current_block < application.expires_at
|| !matches!(application.status, ApplicationStatus::Open)
Expand Down
4 changes: 1 addition & 3 deletions pallets/governance/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::*;
fn create_application<T: Config>(module_key: &T::AccountId) {
let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.agent_application_cost;
let _ = <T as crate::Config>::Currency::deposit_creating(&module_key, cost);
let _ = <T as crate::Config>::Currency::deposit_creating(module_key, cost);

let min_data_len = T::MinApplicationDataLength::get();
let data = vec![0; min_data_len as usize];
Expand Down Expand Up @@ -112,8 +112,6 @@ benchmarks! {
min_name_length: 2,
max_name_length: T::MaxAgentNameLengthConstraint::get() as u16 - 1,
max_allowed_agents: 1,
max_allowed_weights: 1,
min_stake_per_weight: 0,
min_weight_control_fee: 1,
min_staking_fee: 1,
dividends_participation_weight: Percent::zero(),
Expand Down
11 changes: 6 additions & 5 deletions pallets/governance/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use codec::{Decode, Encode, MaxEncodedLen};
use polkadot_sdk::{
frame_election_provider_support::Get, frame_support::DebugNoBound, sp_runtime::Percent,
frame_election_provider_support::Get, frame_support::DebugNoBound,
polkadot_sdk_frame::prelude::BlockNumberFor, sp_runtime::Percent,
};
use scale_info::TypeInfo;

use crate::{BalanceOf, BlockAmount};
use crate::BalanceOf;

#[derive(Clone, TypeInfo, Decode, Encode, PartialEq, Eq, DebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct GovernanceConfiguration<T: crate::Config> {
pub proposal_cost: BalanceOf<T>,
pub proposal_expiration: BlockAmount,
pub proposal_expiration: BlockNumberFor<T>,
pub agent_application_cost: BalanceOf<T>,
pub agent_application_expiration: BlockAmount,
pub agent_application_expiration: BlockNumberFor<T>,
pub proposal_reward_treasury_allocation: Percent,
pub max_proposal_reward_treasury_allocation: BalanceOf<T>,
pub proposal_reward_interval: BlockAmount,
pub proposal_reward_interval: BlockNumberFor<T>,
}

impl<T: crate::Config> Default for GovernanceConfiguration<T> {
Expand Down
4 changes: 0 additions & 4 deletions pallets/governance/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ pub(super) type BalanceOf<T> = <<T as crate::Config>::Currency as Currency<
>>::Balance;

pub(super) type AccountIdOf<T> = <T as polkadot_sdk::frame_system::Config>::AccountId;

pub(super) type Block = u64;

pub(super) type BlockAmount = u64;
24 changes: 12 additions & 12 deletions pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ pub mod pallet {
type MaxApplicationDataLength: Get<u32>;

#[pallet::constant]
type ApplicationExpiration: Get<BlockAmount>;
#[pallet::no_default_bounds]
type ApplicationExpiration: Get<BlockNumberFor<Self>>;

#[pallet::constant]
type MaxPenaltyPercentage: Get<Percent>;
Expand All @@ -132,14 +133,16 @@ pub mod pallet {
type DefaultProposalCost: Get<BalanceOf<Self>>;

#[pallet::constant]
type DefaultProposalExpiration: Get<BlockAmount>;
#[pallet::no_default_bounds]
type DefaultProposalExpiration: Get<BlockNumberFor<Self>>;

#[pallet::constant]
#[pallet::no_default_bounds]
type DefaultAgentApplicationCost: Get<BalanceOf<Self>>;

#[pallet::constant]
type DefaultAgentApplicationExpiration: Get<BlockAmount>;
#[pallet::no_default_bounds]
type DefaultAgentApplicationExpiration: Get<BlockNumberFor<Self>>;

#[pallet::constant]
type DefaultProposalRewardTreasuryAllocation: Get<Percent>;
Expand All @@ -149,7 +152,8 @@ pub mod pallet {
type DefaultMaxProposalRewardTreasuryAllocation: Get<BalanceOf<Self>>;

#[pallet::constant]
type DefaultProposalRewardInterval: Get<BlockAmount>;
#[pallet::no_default_bounds]
type DefaultProposalRewardInterval: Get<BlockNumberFor<Self>>;

#[pallet::no_default_bounds]
type RuntimeEvent: From<Event<Self>>
Expand All @@ -167,14 +171,10 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(block_number: BlockNumberFor<T>) -> Weight {
let current_block: u64 = block_number
.try_into()
.ok()
.expect("blockchain won't pass 2 ^ 64 blocks");

application::resolve_expired_applications::<T>(current_block);
proposal::tick_proposals::<T>(current_block);
proposal::tick_proposal_rewards::<T>(current_block);
application::resolve_expired_applications::<T>(block_number);

proposal::tick_proposals::<T>(block_number);
proposal::tick_proposal_rewards::<T>(block_number);

Weight::zero()
}
Expand Down
Loading

0 comments on commit 151076a

Please sign in to comment.