Skip to content

Commit 9d018c0

Browse files
committed
Enforce ema price halving hyperparameter and add tests
1 parent 0479ad1 commit 9d018c0

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

pallets/admin-utils/src/tests/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,3 +1669,32 @@ fn test_sudo_set_subnet_owner_hotkey() {
16691669
);
16701670
});
16711671
}
1672+
1673+
// cargo test --package pallet-admin-utils --lib -- tests::test_sudo_set_ema_halving --exact --show-output
1674+
#[test]
1675+
fn test_sudo_set_ema_halving() {
1676+
new_test_ext().execute_with(|| {
1677+
let netuid: u16 = 1;
1678+
let to_be_set: u64 = 10;
1679+
add_network(netuid, 10);
1680+
1681+
let value_before: u64 = pallet_subtensor::EMAPriceHalvingBlocks::<Test>::get(netuid);
1682+
assert_eq!(
1683+
AdminUtils::sudo_set_ema_price_halving_period(
1684+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1685+
netuid,
1686+
to_be_set
1687+
),
1688+
Err(DispatchError::BadOrigin)
1689+
);
1690+
let value_after_1: u64 = pallet_subtensor::EMAPriceHalvingBlocks::<Test>::get(netuid);
1691+
assert_eq!(value_after_1, value_before);
1692+
assert_ok!(AdminUtils::sudo_set_ema_price_halving_period(
1693+
<<Test as Config>::RuntimeOrigin>::root(),
1694+
netuid,
1695+
to_be_set
1696+
));
1697+
let value_after_2: u64 = pallet_subtensor::EMAPriceHalvingBlocks::<Test>::get(netuid);
1698+
assert_eq!(value_after_2, to_be_set);
1699+
});
1700+
}

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ impl<T: Config> Pallet<T> {
6060
let blocks_since_registration = I96F32::saturating_from_num(
6161
Self::get_current_block_as_u64().saturating_sub(NetworkRegisteredAt::<T>::get(netuid)),
6262
);
63-
// 7200 * 14 = 100_800 is the halving time
63+
64+
// Use halving time hyperparameter. The meaning of this parameter can be best explained under
65+
// the assumption of a constant price and SubnetMovingAlpha == 0.5: It is how many blocks it
66+
// will take in order for the distance between current EMA of price and current price to shorten
67+
// by half.
68+
let halving_time = EMAPriceHalvingBlocks::<T>::get(netuid);
6469
let alpha: I96F32 =
6570
SubnetMovingAlpha::<T>::get().saturating_mul(blocks_since_registration.safe_div(
66-
blocks_since_registration.saturating_add(I96F32::saturating_from_num(100_800)),
71+
blocks_since_registration.saturating_add(I96F32::saturating_from_num(halving_time)),
6772
));
6873
let minus_alpha: I96F32 = I96F32::saturating_from_num(1.0).saturating_sub(alpha);
6974
let current_price: I96F32 = alpha

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ fn test_coinbase_moving_prices() {
218218
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
219219
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.1));
220220

221-
// EMA price 14 days after registration
222-
System::set_block_number(7_200 * 14);
221+
// EMA price 28 days after registration
222+
System::set_block_number(7_200 * 28);
223223

224224
// Run moving 14 times.
225225
for _ in 0..14 {
@@ -274,7 +274,7 @@ fn test_update_moving_price_after_time() {
274274
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
275275

276276
// Registered long time ago
277-
System::set_block_number(72_000_500);
277+
System::set_block_number(144_000_500);
278278
NetworkRegisteredAt::<Test>::insert(netuid, 500);
279279

280280
SubtensorModule::update_moving_price(netuid);

0 commit comments

Comments
 (0)