Skip to content

Commit 009961d

Browse files
author
unconst
committed
fix test
2 parents 8af65df + 1ffefe1 commit 009961d

File tree

7 files changed

+196
-185
lines changed

7 files changed

+196
-185
lines changed

pallets/subtensor/src/coinbase/block_emission.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ impl<T: Config> Pallet<T> {
3030
tao_emission: u64,
3131
alpha_block_emission: u64,
3232
) -> (u64, u64, u64) {
33-
3433
// Init terms.
3534
let mut tao_in_emission: I96F32 = I96F32::saturating_from_num(tao_emission);
3635
let float_alpha_block_emission: I96F32 = I96F32::saturating_from_num(alpha_block_emission);

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 84 additions & 81 deletions
Large diffs are not rendered by default.

pallets/subtensor/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -736,18 +736,18 @@ pub mod pallet {
736736
/// Default moving alpha for the moving price.
737737
pub fn DefaultMovingAlpha<T: Config>() -> I96F32 {
738738
// Moving average take 30 days to reach 50% of the price
739-
// and 3.5 months to reach 90%.
740-
I96F32::saturating_from_num( 0.000003 )
739+
// and 3.5 months to reach 90%.
740+
I96F32::saturating_from_num(0.000003)
741741
}
742742
#[pallet::type_value]
743743
/// Default subnet moving price.
744744
pub fn DefaultMovingPrice<T: Config>() -> I96F32 {
745-
I96F32::saturating_from_num( 0.0 )
745+
I96F32::saturating_from_num(0.0)
746746
}
747747
#[pallet::type_value]
748748
/// Default value for Share Pool variables
749749
pub fn DefaultSharePoolZero<T: Config>() -> U64F64 {
750-
U64F64::saturating_from_num( 0 )
750+
U64F64::saturating_from_num(0)
751751
}
752752

753753
#[pallet::type_value]
@@ -925,7 +925,8 @@ pub mod pallet {
925925
#[pallet::storage] // --- ITEM ( moving_alpha ) -- subnet moving alpha.
926926
pub type SubnetMovingAlpha<T> = StorageValue<_, I96F32, ValueQuery, DefaultMovingAlpha<T>>;
927927
#[pallet::storage] // --- MAP ( netuid ) --> moving_price | The subnet moving price.
928-
pub type SubnetMovingPrice<T: Config> = StorageMap<_, Identity, u16, I96F32, ValueQuery, DefaultMovingPrice<T>>;
928+
pub type SubnetMovingPrice<T: Config> =
929+
StorageMap<_, Identity, u16, I96F32, ValueQuery, DefaultMovingPrice<T>>;
929930
#[pallet::storage] // --- MAP ( netuid ) --> total_volume | The total amount of TAO bought and sold since the start of the network.
930931
pub type SubnetVolume<T: Config> =
931932
StorageMap<_, Identity, u16, u128, ValueQuery, DefaultZeroU128<T>>;

pallets/subtensor/src/staking/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ impl<T: Config> Pallet<T> {
121121
Delegates::<T>::get(hotkey)
122122
}
123123
pub fn get_hotkey_take_float(hotkey: &T::AccountId) -> I96F32 {
124-
I96F32::from_num( Self::get_hotkey_take(hotkey) )
125-
.checked_div( I96F32::from_num(u16::MAX) )
126-
.unwrap_or( I96F32::from_num(0.0) )
124+
I96F32::saturating_from_num(Self::get_hotkey_take(hotkey))
125+
.checked_div(I96F32::saturating_from_num(u16::MAX))
126+
.unwrap_or(I96F32::saturating_from_num(0.0))
127127
}
128128

129129
/// Returns true if the hotkey account has been created.

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,23 @@ impl<T: Config> Pallet<T> {
4747
}
4848
pub fn get_moving_alpha_price(netuid: u16) -> I96F32 {
4949
if netuid == Self::get_root_netuid() {
50-
return I96F32::saturating_from_num(1.0); // Root.
51-
}
52-
else if SubnetMechanism::<T>::get(netuid) == 0 {
53-
return I96F32::saturating_from_num(1.0); // Stable
54-
}
55-
else {
56-
return SubnetMovingPrice::<T>::get( netuid );
50+
// Root.
51+
I96F32::saturating_from_num(1.0)
52+
} else if SubnetMechanism::<T>::get(netuid) == 0 {
53+
// Stable
54+
I96F32::saturating_from_num(1.0)
55+
} else {
56+
SubnetMovingPrice::<T>::get(netuid)
5757
}
5858
}
59-
pub fn update_moving_price( netuid: u16 ) {
59+
pub fn update_moving_price(netuid: u16) {
6060
let alpha: I96F32 = SubnetMovingAlpha::<T>::get();
6161
let minus_alpha: I96F32 = I96F32::saturating_from_num(1.0).saturating_sub(alpha);
62-
let current_price: I96F32 = alpha.saturating_mul( Self::get_alpha_price(netuid) );
63-
let current_moving: I96F32 = minus_alpha.saturating_mul( Self::get_moving_alpha_price(netuid) );
64-
let new_moving: I96F32 = current_price.saturating_add( current_moving );
65-
SubnetMovingPrice::<T>::insert( netuid, new_moving);
62+
let current_price: I96F32 = alpha.saturating_mul(Self::get_alpha_price(netuid));
63+
let current_moving: I96F32 =
64+
minus_alpha.saturating_mul(Self::get_moving_alpha_price(netuid));
65+
let new_moving: I96F32 = current_price.saturating_add(current_moving);
66+
SubnetMovingPrice::<T>::insert(netuid, new_moving);
6667
}
6768

6869
/// Retrieves the global global weight as a normalized value between 0 and 1.

pallets/subtensor/src/tests/children.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,10 @@ fn test_get_stake_for_hotkey_on_subnet_single_parent_child() {
18411841
register_ok_neuron(netuid, child, coldkey, 0);
18421842

18431843
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1844-
&parent, &coldkey, netuid, 1_000_000_000,
1844+
&parent,
1845+
&coldkey,
1846+
netuid,
1847+
1_000_000_000,
18451848
);
18461849

18471850
mock_set_children_no_epochs(netuid, &parent, &[(u64::MAX, child)]);
@@ -2013,11 +2016,7 @@ fn test_get_stake_for_hotkey_on_subnet_edge_cases() {
20132016
);
20142017

20152018
// Test with 0% and 100% stake allocation
2016-
mock_set_children_no_epochs(
2017-
netuid,
2018-
&parent,
2019-
&[(0, child1), (u64::MAX, child2)],
2020-
);
2019+
mock_set_children_no_epochs(netuid, &parent, &[(0, child1), (u64::MAX, child2)]);
20212020

20222021
let parent_stake = SubtensorModule::get_inherited_for_hotkey_on_subnet(&parent, netuid);
20232022
let child1_stake = SubtensorModule::get_inherited_for_hotkey_on_subnet(&child1, netuid);
@@ -2709,8 +2708,10 @@ fn test_set_children_rate_limit_fail_then_succeed() {
27092708
#[test]
27102709
fn test_childkey_set_weights_single_parent() {
27112710
new_test_ext(1).execute_with(|| {
2712-
let netuid: u16 = 1;
2713-
add_network(netuid, 1, 0);
2711+
let subnet_owner_coldkey = U256::from(1001);
2712+
let subnet_owner_hotkey = U256::from(1002);
2713+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
2714+
Tempo::<Test>::insert(netuid, 1);
27142715

27152716
// Define hotkeys
27162717
let parent: U256 = U256::from(1);
@@ -2750,14 +2751,14 @@ fn test_childkey_set_weights_single_parent() {
27502751
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
27512752

27522753
// Set parent-child relationship
2753-
mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX, child)]);
2754+
mock_set_children_no_epochs(netuid, &parent, &[(u64::MAX, child)]);
27542755

2755-
step_block(7200 + 1);
27562756
// Set weights on the child using the weight_setter account
27572757
let origin = RuntimeOrigin::signed(weight_setter);
27582758
let uids: Vec<u16> = vec![1]; // Only set weight for the child (UID 1)
27592759
let values: Vec<u16> = vec![u16::MAX]; // Use maximum value for u16
27602760
let version_key = SubtensorModule::get_weights_version_key(netuid);
2761+
ValidatorPermit::<Test>::insert(netuid, vec![true, true, true, true]);
27612762
assert_ok!(SubtensorModule::set_weights(
27622763
origin,
27632764
netuid,
@@ -3191,15 +3192,9 @@ fn test_parent_child_chain_emission() {
31913192
log::info!("rel_stake_a: {:?}", rel_stake_a); // 0.6666 -> 2/3
31923193
log::info!("rel_stake_b: {:?}", rel_stake_b); // 0.2222 -> 2/9
31933194
log::info!("rel_stake_c: {:?}", rel_stake_c); // 0.1111 -> 1/9
3194-
assert!(
3195-
(rel_stake_a - I96F32::from_num(stake_a) / total_tao).abs() < 0.001
3196-
);
3197-
assert!(
3198-
(rel_stake_b - I96F32::from_num(stake_b) / total_tao).abs() < 0.001
3199-
);
3200-
assert!(
3201-
(rel_stake_c - I96F32::from_num(stake_c) / total_tao).abs() < 0.001
3202-
);
3195+
assert!((rel_stake_a - I96F32::from_num(stake_a) / total_tao).abs() < 0.001);
3196+
assert!((rel_stake_b - I96F32::from_num(stake_b) / total_tao).abs() < 0.001);
3197+
assert!((rel_stake_c - I96F32::from_num(stake_c) / total_tao).abs() < 0.001);
32033198

32043199
// Set parent-child relationships
32053200
// A -> B (50% of A's stake)
@@ -3228,13 +3223,13 @@ fn test_parent_child_chain_emission() {
32283223
// Set the weight of root TAO to be 0%, so only alpha is effective.
32293224
SubtensorModule::set_tao_weight(0);
32303225

3231-
let hardcoded_emission: I96F32 = I96F32::from_num(1_000_000); // 1 million (adjust as needed)
3226+
let emission: I96F32 = I96F32::from_num(SubtensorModule::get_block_emission().unwrap_or(0));
32323227

32333228
// Set pending emission to 0
32343229
PendingEmission::<Test>::insert(netuid, 0);
32353230

3236-
// Run epoch with a hardcoded emission value
3237-
SubtensorModule::run_coinbase(hardcoded_emission);
3231+
// Run epoch with emission value
3232+
SubtensorModule::run_coinbase(emission);
32383233

32393234
// Log new stake
32403235
let stake_a_new: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_a);
@@ -3262,7 +3257,7 @@ fn test_parent_child_chain_emission() {
32623257

32633258
// Verify the final stake distribution
32643259
let stake_inc_eps: I96F32 = I96F32::from_num(1e-4); // 4 decimal places
3265-
3260+
32663261
// Each child has chk_take take
32673262
let expected_a = I96F32::from_num(2_f64 / 3_f64)
32683263
* (I96F32::from_num(1_f64) - (I96F32::from_num(1_f64 / 2_f64) * chk_take));
@@ -3315,8 +3310,8 @@ fn test_parent_child_chain_emission() {
33153310

33163311
assert_abs_diff_eq!(
33173312
total_stake_inc.to_num::<u64>(),
3318-
hardcoded_emission.to_num::<u64>(),
3319-
epsilon = 10_000,
3313+
emission.to_num::<u64>(),
3314+
epsilon = emission.to_num::<u64>() / 1000,
33203315
);
33213316
});
33223317
}

0 commit comments

Comments
 (0)