File tree Expand file tree Collapse file tree 3 files changed +39
-5
lines changed Expand file tree Collapse file tree 3 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -1669,3 +1669,32 @@ fn test_sudo_set_subnet_owner_hotkey() {
1669
1669
) ;
1670
1670
} ) ;
1671
1671
}
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
+ }
Original file line number Diff line number Diff line change @@ -60,10 +60,15 @@ impl<T: Config> Pallet<T> {
60
60
let blocks_since_registration = I96F32 :: saturating_from_num (
61
61
Self :: get_current_block_as_u64 ( ) . saturating_sub ( NetworkRegisteredAt :: < T > :: get ( netuid) ) ,
62
62
) ;
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) ;
64
69
let alpha: I96F32 =
65
70
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 ) ) ,
67
72
) ) ;
68
73
let minus_alpha: I96F32 = I96F32 :: saturating_from_num ( 1.0 ) . saturating_sub ( alpha) ;
69
74
let current_price: I96F32 = alpha
Original file line number Diff line number Diff line change @@ -218,8 +218,8 @@ fn test_coinbase_moving_prices() {
218
218
SubnetMovingPrice :: < Test > :: insert ( netuid, I96F32 :: from_num ( 0 ) ) ;
219
219
SubnetMovingAlpha :: < Test > :: set ( I96F32 :: from_num ( 0.1 ) ) ;
220
220
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 ) ;
223
223
224
224
// Run moving 14 times.
225
225
for _ in 0 ..14 {
@@ -274,7 +274,7 @@ fn test_update_moving_price_after_time() {
274
274
SubnetMovingPrice :: < Test > :: insert ( netuid, I96F32 :: from_num ( 0 ) ) ;
275
275
276
276
// Registered long time ago
277
- System :: set_block_number ( 72_000_500 ) ;
277
+ System :: set_block_number ( 144_000_500 ) ;
278
278
NetworkRegisteredAt :: < Test > :: insert ( netuid, 500 ) ;
279
279
280
280
SubtensorModule :: update_moving_price ( netuid) ;
You can’t perform that action at this time.
0 commit comments