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

Adjusts POW and BURN based on a moving average #157

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions pallets/subtensor/src/block_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ impl<T: Config> Pallet<T> {
registrations_this_interval: u16,
target_registrations_per_interval: u16
) -> u64 {
let next_value: I110F18 = I110F18::from_num( current_difficulty ) * I110F18::from_num( registrations_this_interval + target_registrations_per_interval ) / I110F18::from_num( target_registrations_per_interval + target_registrations_per_interval );
let updated_difficulty: I110F18 = I110F18::from_num( current_difficulty ) * I110F18::from_num( registrations_this_interval + target_registrations_per_interval ) / I110F18::from_num( target_registrations_per_interval + target_registrations_per_interval );
let alpha: I110F18 = I110F18::from_num( Self::get_adjustment_alpha( netuid) ) / I110F18::from_num( u64::MAX );
let next_value:I110F18 = alpha * current_difficulty + ( 1 - alpha ) * updated_difficulty
if next_value >= I110F18::from_num( Self::get_max_difficulty( netuid ) ){
return Self::get_max_difficulty( netuid );
} else if next_value <= I110F18::from_num( Self::get_min_difficulty( netuid ) ) {
Expand All @@ -314,7 +316,9 @@ impl<T: Config> Pallet<T> {
registrations_this_interval: u16,
target_registrations_per_interval: u16
) -> u64 {
let next_value: I110F18 = I110F18::from_num( current_burn ) * I110F18::from_num( registrations_this_interval + target_registrations_per_interval ) / I110F18::from_num( target_registrations_per_interval + target_registrations_per_interval );
let updated_burn: I110F18 = I110F18::from_num( current_burn ) * I110F18::from_num( registrations_this_interval + target_registrations_per_interval ) / I110F18::from_num( target_registrations_per_interval + target_registrations_per_interval );
let alpha: I110F18 = I110F18::from_num( Self::get_adjustment_alpha( netuid ) ) / I110F18::from_num( u64::MAX );
let next_value: I110F18 = alpha * current_burn + ( 1 - alpha ) * updated_burn
if next_value >= I110F18::from_num( Self::get_max_burn_as_u64( netuid ) ){
return Self::get_max_burn_as_u64( netuid );
} else if next_value <= I110F18::from_num( Self::get_min_burn_as_u64( netuid ) ) {
Expand Down
7 changes: 7 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ pub mod pallet {
type InitialTxRateLimit: Get<u64>;
#[pallet::constant] // Initial percentage of total stake required to join senate.
type InitialSenateRequiredStakePercentage: Get<u64>;
#[pallet::constant] // Initial adjustment alpha on burn and pow.
type InitialAdjustmentAlpha: Get<u64>;
}

pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
Expand Down Expand Up @@ -450,6 +452,8 @@ pub mod pallet {
pub fn DefaultSynergyScalingLawPower<T: Config>() -> u16 { T::InitialSynergyScalingLawPower::get() }
#[pallet::type_value]
pub fn DefaultTargetRegistrationsPerInterval<T: Config>() -> u16 { T::InitialTargetRegistrationsPerInterval::get() }
#[pallet::type_value]
pub fn DefaultAdjustmentAlpha<T: Config>() -> u16 { T::InitialAdjustmentAlpha::get() }


#[pallet::storage] // --- MAP ( netuid ) --> Rho
Expand Down Expand Up @@ -506,6 +510,8 @@ pub mod pallet {
pub type TargetRegistrationsPerInterval<T> = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultTargetRegistrationsPerInterval<T> >;
#[pallet::storage] // --- DMAP ( netuid, uid ) --> block_at_registration
pub type BlockAtRegistration<T:Config> = StorageDoubleMap<_, Identity, u16, Identity, u16, u64, ValueQuery, DefaultBlockAtRegistration<T> >;
#[pallet::storage] // --- DMAP ( netuid ) --> adjustment_alpha
pub type AdjustmentAlpha<T:Config> = StorageDoubleMap<_, Identity, u16, Identity, u16, u64, ValueQuery, DefaultAdjustmentAlpha<T> >;

// =======================================
// ==== Subnetwork Consensus Storage ====
Expand Down Expand Up @@ -616,6 +622,7 @@ pub mod pallet {
TempoSet(u16, u16), // --- Event created when setting tempo on a network
RAORecycledForRegistrationSet( u16, u64 ), // Event created when setting the RAO recycled for registration.
SenateRequiredStakePercentSet( u64 ), // Event created when setting the minimum required stake amount for senate registration.
AdjustmentAlphaSet( u16, u64 ), // Event created when setting the adjustment alpha on a subnet.
}

// Errors inform users that something went wrong.
Expand Down
11 changes: 11 additions & 0 deletions pallets/subtensor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ impl<T: Config> Pallet<T> {
// ========================
// ==== Sudo calls ========
// ========================
pub fn get_adjustment_alpha( netuid: u16 ) -> u64 { AdjustmentAlpha::<T>::get(netuid) }
pub fn set_adjustment_alpha( netuid: u16, adjustment_alpha: u64 ) { AdjustmentAlpha::<T>::insert( netuid, adjustment_alpha ) }
pub fn do_sudo_set_adjustment_alpha( origin: T::RuntimeOrigin, netuid: u16, adjustment_alpha: u64 ) -> DispatchResult {
ensure_root( origin )?;
Self::set_adjustment_alpha( netuid, adjustment_alpha );
log::info!("AdjustmentAlphaSet( adjustment_alpha: {:?} ) ", adjustment_alpha );
Self::deposit_event( Event::AdjustmentAlphaSet( netuid, adjustment_alpha ) );
Ok(())
}


pub fn get_default_take() -> u16 { DefaultTake::<T>::get() }
pub fn set_default_take( default_take: u16 ) { DefaultTake::<T>::put( default_take ) }
pub fn do_sudo_set_default_take( origin: T::RuntimeOrigin, default_take: u16 ) -> DispatchResult {
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ parameter_types! {
pub const InitialDifficulty: u64 = 10000;
pub const InitialActivityCutoff: u16 = 5000;
pub const InitialAdjustmentInterval: u16 = 100;
pub const InitialAdjustmentAlpha: u16 = u64::MAX;
pub const InitialMaxRegistrationsPerBlock: u16 = 3;
pub const InitialTargetRegistrationsPerInterval: u16 = 2;
pub const InitialPruningScore : u16 = u16::MAX;
Expand Down Expand Up @@ -312,6 +313,7 @@ impl pallet_subtensor::Config for Test {
type InitialTempo = InitialTempo;
type InitialDifficulty = InitialDifficulty;
type InitialAdjustmentInterval = InitialAdjustmentInterval;
type InitialAdjustmentAlpha = InitialAdjustmentAlpha;
type InitialTargetRegistrationsPerInterval = InitialTargetRegistrationsPerInterval;
type InitialRho = InitialRho;
type InitialKappa = InitialKappa;
Expand Down
4 changes: 3 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 125,
spec_version: 126,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -513,6 +513,7 @@ parameter_types! {
pub const SubtensorInitialTempo: u16 = 99;
pub const SubtensorInitialDifficulty: u64 = 10_000_000;
pub const SubtensorInitialAdjustmentInterval: u16 = 100;
pub const SubtensorInitialAdjustmentAlpha: u64 = u64::MAX;
pub const SubtensorInitialTargetRegistrationsPerInterval: u16 = 2;
pub const SubtensorInitialImmunityPeriod: u16 = 4096;
pub const SubtensorInitialActivityCutoff: u16 = 5000;
Expand Down Expand Up @@ -560,6 +561,7 @@ impl pallet_subtensor::Config for Runtime {
type InitialTempo = SubtensorInitialTempo;
type InitialDifficulty = SubtensorInitialDifficulty;
type InitialAdjustmentInterval = SubtensorInitialAdjustmentInterval;
type InitialAdjustmentAlpha = SubtensorInitialAdjustmentAlpha;
type InitialTargetRegistrationsPerInterval = SubtensorInitialTargetRegistrationsPerInterval;
type InitialImmunityPeriod = SubtensorInitialImmunityPeriod;
type InitialActivityCutoff = SubtensorInitialActivityCutoff;
Expand Down
Loading