Skip to content

Commit

Permalink
Adjusts POW and BURN based on a moving average (clean) (#158)
Browse files Browse the repository at this point in the history
* initial commit

* fix tests

---------

Co-authored-by: unconst <jake@bittensor.com>
  • Loading branch information
opentaco and unconst committed Jul 10, 2023
1 parent ac34bd7 commit ae01a71
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 3 deletions.
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 * I110F18::from_num(current_difficulty) + ( I110F18::from_num(1.0) - 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 * I110F18::from_num(current_burn) + ( I110F18::from_num(1.0) - 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
16 changes: 16 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>() -> u64 { 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> = StorageMap<_, 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 Expand Up @@ -1718,6 +1725,15 @@ pub mod pallet {
pub fn sudo_remove_votes(origin: OriginFor<T>, who: T::AccountId ) -> DispatchResult {
Self::do_remove_votes(origin, &who)
}

#[pallet::call_index(58)]
#[pallet::weight((Weight::from_ref_time(14_000_000)
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Operational, Pays::No))]
pub fn sudo_set_adjustment_alpha( origin:OriginFor<T>, netuid: u16, adjustment_alpha: u64 ) -> DispatchResult {
Self::do_sudo_set_adjustment_alpha( origin, netuid, adjustment_alpha )
}

}

// ---- Subtensor helper functions.
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 @@ -204,6 +204,17 @@ impl<T: Config> Pallet<T> {
Ok(())
}

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 )?;
ensure!(Self::if_subnet_exist(netuid), Error::<T>::NetworkDoesNotExist);
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_validator_exclude_quantile( netuid: u16 ) -> u16 { ValidatorExcludeQuantile::<T>::get( netuid ) }
pub fn set_validator_exclude_quantile( netuid: u16, validator_exclude_quantile: u16 ) { ValidatorExcludeQuantile::<T>::insert( netuid, validator_exclude_quantile ); }
pub fn do_sudo_set_validator_exclude_quantile( origin:T::RuntimeOrigin, netuid: u16, validator_exclude_quantile: u16 ) -> DispatchResult {
Expand Down
37 changes: 37 additions & 0 deletions pallets/subtensor/tests/block_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,43 @@ fn test_burn_adjustment() {
});
}

#[test]
fn test_burn_adjustment_with_moving_average() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let tempo: u16 = 13;
let burn_cost:u64 = 1000;
let adjustment_interval = 1;
let target_registrations_per_interval = 1;
SubtensorModule::set_burn( netuid, burn_cost);
SubtensorModule::set_adjustment_interval( netuid, adjustment_interval );
SubtensorModule::set_target_registrations_per_interval( netuid, target_registrations_per_interval);
// Set alpha here.
add_network(netuid, tempo, 0);
SubtensorModule::set_adjustment_alpha( netuid, u64::MAX/2 );

// Register key 1.
let hotkey_account_id_1 = U256::from(1);
let coldkey_account_id_1 = U256::from(1);
SubtensorModule::add_balance_to_coldkey_account( &coldkey_account_id_1, 10000 );
assert_ok!(SubtensorModule::burned_register(<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id_1), netuid, hotkey_account_id_1));

// Register key 2.
let hotkey_account_id_2 =U256::from(2);
let coldkey_account_id_2 = U256::from(2);
SubtensorModule::add_balance_to_coldkey_account( &coldkey_account_id_2, 10000 );
assert_ok!(SubtensorModule::burned_register(<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id_2), netuid, hotkey_account_id_2));

// We are over the number of regs allowed this interval.
// Step the block and trigger the adjustment.
step_block( 1 );

// Check the adjusted burn.
// 0.5 * 1000 + 0.5 * 1500 = 1250
assert_eq!(SubtensorModule::get_burn_as_u64(netuid), 1250);
});
}

#[test]
#[allow(unused_assignments)]
fn test_burn_adjustment_case_a() {
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: u64 = 0; // no weight to previous value.
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
33 changes: 33 additions & 0 deletions pallets/subtensor/tests/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,39 @@ fn test_sudo_set_adjustment_interval() {
});
}

#[test]
fn test_sudo_set_adjustment_alpha() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u64 = 10;
let init_value: u64 = SubtensorModule::get_adjustment_alpha(netuid);
add_network(netuid, 10, 0);
assert_eq!(
SubtensorModule::sudo_set_adjustment_alpha(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
netuid,
to_be_set
),
Err(DispatchError::BadOrigin.into())
);
assert_eq!(
SubtensorModule::sudo_set_adjustment_alpha(
<<Test as Config>::RuntimeOrigin>::root(),
netuid + 1,
to_be_set
),
Err(Error::<Test>::NetworkDoesNotExist.into())
);
assert_eq!(SubtensorModule::get_adjustment_alpha(netuid), init_value);
assert_ok!(SubtensorModule::sudo_set_adjustment_alpha(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));
assert_eq!(SubtensorModule::get_adjustment_alpha(netuid), to_be_set);
});
}

#[test]
fn test_sudo_set_validator_exclude_quantile() {
new_test_ext().execute_with(|| {
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 = 0; // no weight to previous value.
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

0 comments on commit ae01a71

Please sign in to comment.