Skip to content

Commit

Permalink
Merge pull request #22 from agicommies/feb28
Browse files Browse the repository at this point in the history
Adding dynamic burn
  • Loading branch information
Supremesource authored Mar 3, 2024
2 parents 833cefe + ce59e93 commit bcca9c1
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 21 deletions.
63 changes: 61 additions & 2 deletions pallets/subspace/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::voting::AUTHORITY_MODE;
// TODO: deposit events on sets

use super::*;
use frame_support::pallet_prelude::DispatchResult;
Expand All @@ -12,13 +13,17 @@ impl<T: Config> Pallet<T> {
max_allowed_subnets: Self::get_global_max_allowed_subnets(),
max_allowed_modules: Self::get_max_allowed_modules(),
max_registrations_per_block: Self::get_max_registrations_per_block(),
target_registrations_interval: Self::get_target_registrations_interval(),
target_registrations_per_interval: Self::get_target_registrations_per_interval(),
unit_emission: Self::get_unit_emission(),
tx_rate_limit: Self::get_tx_rate_limit(),
vote_threshold: Self::get_global_vote_threshold(),
max_proposals: Self::get_max_proposals(),
vote_mode: Self::get_vote_mode_global(),
burn_rate: Self::get_burn_rate(),
min_burn: Self::get_min_burn(),
max_burn: Self::get_max_burn(),
adjustment_alpha: Self::get_adjustment_alpha(),
min_stake: Self::get_min_stake_global(),
min_weight_stake: Self::get_min_weight_stake(),
max_allowed_weights: Self::get_max_allowed_weights_global(),
Expand Down Expand Up @@ -47,6 +52,11 @@ impl<T: Config> Pallet<T> {
Error::<T>::InvalidMaxRegistrationsPerBlock
);

ensure!(
params.target_registrations_interval > 0,
Error::<T>::InvalidTargetRegistrationsInterval
);

ensure!(
params.vote_threshold < 100,
Error::<T>::InvalidVoteThreshold
Expand All @@ -73,17 +83,33 @@ impl<T: Config> Pallet<T> {
Self::set_global_max_allowed_subnets(params.max_allowed_subnets);
Self::set_max_allowed_modules(params.max_allowed_modules);
Self::set_max_registrations_per_block(params.max_registrations_per_block);
Self::set_target_registrations_interval(params.target_registrations_interval);
Self::set_target_registrations_per_interval(params.target_registrations_per_interval);
Self::set_adjustment_alpha(params.adjustment_alpha);
Self::set_unit_emission(params.unit_emission);
Self::set_tx_rate_limit(params.tx_rate_limit);
Self::set_global_vote_threshold(params.vote_threshold);
Self::set_max_proposals(params.max_proposals);
Self::set_vote_mode_global(params.vote_mode);
Self::set_burn_rate(params.burn_rate);
Self::set_min_burn(params.min_burn);
Self::set_max_burn(params.max_burn);
Self::set_min_weight_stake(params.min_weight_stake);
Self::set_min_stake_global(params.min_stake);
}

pub fn get_registrations_this_interval() -> u16 {
RegistrationsThisInterval::<T>::get()
}

pub fn get_target_registrations_per_interval() -> u16 {
TargetRegistrationsPerInterval::<T>::get()
}

pub fn set_target_registrations_per_interval(target_interval: u16) {
TargetRegistrationsPerInterval::<T>::set(target_interval)
}

pub fn get_min_weight_stake() -> u64 {
MinWeightStake::<T>::get()
}
Expand Down Expand Up @@ -113,6 +139,14 @@ impl<T: Config> Pallet<T> {
BurnRate::<T>::get().min(100)
}

pub fn get_burn() -> u64 {
Burn::<T>::get()
}

pub fn set_burn(burn: u64) {
Burn::<T>::set(burn)
}

pub fn set_burn_rate(burn_rate: u16) {
BurnRate::<T>::put(burn_rate.min(100));
}
Expand All @@ -134,6 +168,15 @@ impl<T: Config> Pallet<T> {
pub fn get_max_registrations_per_block() -> u16 {
MaxRegistrationsPerBlock::<T>::get()
}
pub fn set_max_registrations_per_block(max_registrations_per_block: u16) {
MaxRegistrationsPerBlock::<T>::set(max_registrations_per_block);
}
pub fn get_target_registrations_interval() -> u16 {
TargetRegistrationsInterval::<T>::get()
}
pub fn set_target_registrations_interval(target_registrations_interval: u16) {
TargetRegistrationsInterval::<T>::set(target_registrations_interval);
}
pub fn get_global_max_name_length() -> u16 {
MaxNameLength::<T>::get()
}
Expand Down Expand Up @@ -180,11 +223,27 @@ impl<T: Config> Pallet<T> {
TxRateLimit::<T>::put(tx_rate_limit)
}

pub fn get_min_burn() -> u64 {
MinBurn::<T>::get()
}

pub fn set_min_burn(min_burn: u64) {
MinBurn::<T>::put(min_burn);
}

pub fn get_min_burn() -> u64 {
MinBurn::<T>::get()
pub fn get_max_burn() -> u64 {
MaxBurn::<T>::get()
}

pub fn set_max_burn(max_burn: u64) {
MaxBurn::<T>::put(max_burn);
}

pub fn get_adjustment_alpha() -> u64 {
AdjustmentAlpha::<T>::get()
}

pub fn set_adjustment_alpha(adjustment_alpha: u64) {
AdjustmentAlpha::<T>::put(adjustment_alpha);
}
}
80 changes: 72 additions & 8 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,27 @@ pub mod pallet {

#[pallet::type_value]
pub fn DefaultMinBurn<T: Config>() -> u64 {
0
200_000_000 // 2 $COMAI
}
#[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights
#[pallet::storage] // --- MinBurn
pub type MinBurn<T> = StorageValue<_, u64, ValueQuery, DefaultMinBurn<T>>;

#[pallet::type_value]
pub fn DefaultMaxBurn<T: Config>() -> u64 {
250_000_000_000 // 250 $COMAI
}

#[pallet::type_value]
pub fn DefaultAdjustmentAlpha<T: Config>() -> u64 {
0 // u64::MAX / 2
}

#[pallet::storage] // --- adjusment alpha
pub type AdjustmentAlpha<T> = StorageValue<_, u64, ValueQuery, DefaultAdjustmentAlpha<T>>;

#[pallet::storage] // --- MaxBurn
pub type MaxBurn<T> = StorageValue<_, u64, ValueQuery, DefaultMaxBurn<T>>;

#[pallet::type_value]
pub fn DefaultLastTxBlock<T: Config>() -> u64 {
0
Expand All @@ -156,6 +172,23 @@ pub mod pallet {
pub(super) type MaxAllowedSubnets<T: Config> =
StorageValue<_, u16, ValueQuery, DefaultMaxAllowedSubnets<T>>;

#[pallet::type_value]
pub fn DefaultRegistrationsThisInterval<T: Config>() -> u16 {
0
}

#[pallet::storage] // --- ITEM ( registrations_this_interval )
pub(super) type RegistrationsThisInterval<T: Config> =
StorageValue<_, u16, ValueQuery, DefaultRegistrationsThisInterval<T>>;

#[pallet::type_value]
pub fn DefaultBurn<T: Config>() -> u64 {
0
}

#[pallet::storage] // --- ITEM ( burn )
pub(super) type Burn<T: Config> = StorageValue<_, u64, ValueQuery, DefaultBurn<T>>;

#[pallet::type_value]
pub fn DefaultMaxAllowedModules<T: Config>() -> u16 {
10_000
Expand All @@ -180,6 +213,23 @@ pub mod pallet {
pub type MaxRegistrationsPerBlock<T> =
StorageValue<_, u16, ValueQuery, DefaultMaxRegistrationsPerBlock<T>>;

#[pallet::type_value]
pub fn DefaultTargetRegistrationsPerInterval<T: Config>() -> u16 {
DefaultTargetRegistrationsInterval::<T>::get() / 2
}
#[pallet::storage] // --- ITEM( global_target_registrations_interval )
pub type TargetRegistrationsPerInterval<T> =
StorageValue<_, u16, ValueQuery, DefaultTargetRegistrationsPerInterval<T>>;

#[pallet::type_value] // --- ITEM( global_target_registrations_interval ) Measured in the number of blocks
pub fn DefaultTargetRegistrationsInterval<T: Config>() -> u16 {
DefaultTempo::<T>::get() * 2 // 2 times the epoch
}

#[pallet::storage] // --- ITEM( global_target_registrations_interval )
pub type TargetRegistrationsInterval<T> =
StorageValue<_, u16, ValueQuery, DefaultTargetRegistrationsInterval<T>>;

#[pallet::type_value]
pub fn DefaultMinStakeGlobal<T: Config>() -> u64 {
100
Expand Down Expand Up @@ -225,14 +275,19 @@ pub mod pallet {

// mins
pub min_burn: u64, // min burn required
pub max_burn: u64, // max burn allowed
pub min_stake: u64, // min stake required
pub min_weight_stake: u64, // min weight stake required

// other
pub unit_emission: u64, // emission per block
pub tx_rate_limit: u64, // tx rate limit
pub vote_threshold: u16, // out of 100
pub vote_mode: Vec<u8>, // out of 100
pub target_registrations_per_interval: u16, // desired number of registrations per interval
pub target_registrations_interval: u16, /* the number of blocks that defines the
* registration interval */
pub adjustment_alpha: u64, // adjustment alpha
pub unit_emission: u64, // emission per block
pub tx_rate_limit: u64, // tx rate limit
pub vote_threshold: u16, // out of 100
pub vote_mode: Vec<u8>, // out of 100
}

#[pallet::type_value]
Expand All @@ -243,11 +298,15 @@ pub mod pallet {
max_allowed_modules: DefaultMaxAllowedModules::<T>::get(),
max_allowed_weights: DefaultMaxAllowedWeightsGlobal::<T>::get(),
max_registrations_per_block: DefaultMaxRegistrationsPerBlock::<T>::get(),
target_registrations_per_interval: DefaultTargetRegistrationsPerInterval::<T>::get(),
target_registrations_interval: DefaultTargetRegistrationsInterval::<T>::get(),
max_name_length: DefaultMaxNameLength::<T>::get(),
max_proposals: DefaultMaxProposals::<T>::get(),
min_burn: DefaultMinBurn::<T>::get(),
max_burn: DefaultMaxBurn::<T>::get(),
min_stake: DefaultMinStakeGlobal::<T>::get(),
min_weight_stake: DefaultMinWeightStake::<T>::get(),
adjustment_alpha: DefaultAdjustmentAlpha::<T>::get(),
unit_emission: DefaultUnitEmission::<T>::get(),
tx_rate_limit: DefaultTxRateLimit::<T>::get(),
vote_threshold: DefaultVoteThreshold::<T>::get(),
Expand Down Expand Up @@ -724,6 +783,8 @@ pub mod pallet {
* weights on a subnetwork. */
ModuleRegistered(u16, u16, T::AccountId), /* --- Event created when a new module
* account has been registered to the chain. */
ModuleDeregistered(u16, u16, T::AccountId), /* --- Event created when a module account
* has been deregistered from the chain. */
BulkModulesRegistered(u16, u16), /* --- Event created when multiple uids have been
* concurrently registered. */
BulkBalancesSet(u16, u16),
Expand All @@ -742,8 +803,8 @@ pub mod pallet {
MaxNameLengthSet(u16), // --- Event created when setting the maximum network name length
MaxAllowedSubnetsSet(u16), // --- Event created when setting the maximum allowed subnets
MaxAllowedModulesSet(u16), // --- Event created when setting the maximum allowed modules
MaxRegistrationsPerBlockSet(u16), /* --- Event created when we set max registrations
* per block */
MaxRegistrationsPerBlockSet(u16), // --- Event created when we set max registrations
target_registrations_intervalSet(u16), // --- Event created when we set target registrations
GlobalUpdate(u16, u16, u16, u16, u64, u64),
GlobalProposalAccepted(u64), // (id)
CustomProposalAccepted(u64), // (id)
Expand Down Expand Up @@ -848,6 +909,7 @@ pub mod pallet {
InvalidMaxAllowedSubnets,
InvalidMaxAllowedModules,
InvalidMaxRegistrationsPerBlock,
InvalidTargetRegistrationsInterval,
InvalidVoteThreshold,
InvalidMaxProposals,
InvalidUnitEmission,
Expand Down Expand Up @@ -1136,6 +1198,7 @@ pub mod pallet {
max_name_length: u16,
max_proposals: u64,
max_registrations_per_block: u16,
target_registrations_interval: u16,
min_burn: u64,
min_stake: u64,
min_weight_stake: u64,
Expand All @@ -1152,6 +1215,7 @@ pub mod pallet {
params.max_name_length = max_name_length;
params.max_proposals = max_proposals;
params.max_registrations_per_block = max_registrations_per_block;
params.target_registrations_interval = target_registrations_interval;
params.min_burn = min_burn;
params.min_stake = min_stake;
params.min_weight_stake = min_weight_stake;
Expand Down
32 changes: 27 additions & 5 deletions pallets/subspace/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<T: Config> Pallet<T> {

// --- 5. Ensure the caller has enough stake to register.
let min_stake: u64 = MinStake::<T>::get(netuid);
let min_burn: u64 = Self::get_min_burn();
let min_burn: u64 = Self::get_burn();

// also ensures that in the case min_burn is present, the stake is enough
// as burn, will be decreased from the stake on the module
Expand All @@ -54,12 +54,18 @@ impl<T: Config> Pallet<T> {
Error::<T>::NotEnoughStakeToRegister
);

// --- 6. Ensure the module key is not already registered.
// --- 6. Ensure the module key is not already registered,
// and namespace is not already taken.
ensure!(
!Self::key_registered(netuid, &key),
Error::<T>::KeyAlreadyRegistered
);

ensure!(
!Self::does_module_name_exist(netuid, name.clone()),
Error::<T>::NameAlreadyRegistered
);

// --- 7. Check if we are exceeding the max allowed modules per network.
// If we do deregister slot.
Self::check_module_limits(netuid);
Expand All @@ -74,10 +80,24 @@ impl<T: Config> Pallet<T> {
if min_burn > 0 {
// if min burn is present, decrease the stake by the min burn
Self::decrease_stake(netuid, &key, &module_key, min_burn);

// Ensure that the stake decreased after the burn.
let current_stake: u64 = Self::get_total_stake_to(netuid, &key);
ensure!(
current_stake == stake_amount.saturating_sub(min_burn),
Error::<T>::NotEnoughStakeToRegister
);
}

// --- 10. Increment the number of registrations per block.
// Make sure that the registration went through.
ensure!(
Self::key_registered(netuid, &key),
Error::<T>::NotRegistered
);

// --- 10. Increment the number of registrations.
RegistrationsPerBlock::<T>::mutate(|val| *val += 1);
RegistrationsThisInterval::<T>::mutate(|val| *val += 1);

// --- Deposit successful event.
Self::deposit_event(Event::ModuleRegistered(netuid, uid, module_key.clone()));
Expand All @@ -104,7 +124,9 @@ impl<T: Config> Pallet<T> {
Error::<T>::StillRegistered
);

// --- 5. Ok and done.
// --- Deposit successful event.
Self::deposit_event(Event::ModuleDeregistered(netuid, uid, key));
// --- 3. Ok and done.
Ok(())
}

Expand All @@ -127,7 +149,7 @@ impl<T: Config> Pallet<T> {
if (uid as usize) < vec.len() {
vec[uid as usize]
} else {
0_u64
0u64
}
}
pub fn get_lowest_uid(netuid: u16) -> u16 {
Expand Down
1 change: 0 additions & 1 deletion pallets/subspace/src/staking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::*;

// import vec
use sp_arithmetic::per_things::Percent;
use sp_std::vec::Vec;

Expand Down
Loading

0 comments on commit bcca9c1

Please sign in to comment.