Skip to content

Commit 3a666b6

Browse files
Change update_moving_price() function
- change the function to use FirstEmissionBlockNumber parameter instead of NetworkRegisteredAt.
1 parent c3d85bd commit 3a666b6

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Diff for: pallets/subtensor/src/staking/stake_utils.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,24 @@ impl<T: Config> Pallet<T> {
5858
}
5959
}
6060
pub fn update_moving_price(netuid: u16) {
61-
let blocks_since_registration = U96F32::saturating_from_num(
62-
Self::get_current_block_as_u64().saturating_sub(NetworkRegisteredAt::<T>::get(netuid)),
63-
);
61+
let blocks_since_start_call = U96F32::saturating_from_num({
62+
// We expect FirstEmissionBlockNumber to be set earlier, and we take the block when
63+
// `start_call` was called (first block before FirstEmissionBlockNumber).
64+
let start_call_block = FirstEmissionBlockNumber::<T>::get(netuid)
65+
.unwrap_or_default()
66+
.saturating_sub(1);
67+
68+
Self::get_current_block_as_u64().saturating_sub(start_call_block)
69+
});
6470

6571
// Use halving time hyperparameter. The meaning of this parameter can be best explained under
6672
// the assumption of a constant price and SubnetMovingAlpha == 0.5: It is how many blocks it
6773
// will take in order for the distance between current EMA of price and current price to shorten
6874
// by half.
6975
let halving_time = EMAPriceHalvingBlocks::<T>::get(netuid);
7076
let current_ma_unsigned = U96F32::saturating_from_num(SubnetMovingAlpha::<T>::get());
71-
let alpha: U96F32 = current_ma_unsigned.saturating_mul(blocks_since_registration.safe_div(
72-
blocks_since_registration.saturating_add(U96F32::saturating_from_num(halving_time)),
77+
let alpha: U96F32 = current_ma_unsigned.saturating_mul(blocks_since_start_call.safe_div(
78+
blocks_since_start_call.saturating_add(U96F32::saturating_from_num(halving_time)),
7379
));
7480
// Because alpha = b / (b + h), where b and h > 0, alpha < 1, so 1 - alpha > 0.
7581
// We can use unsigned type here: U96F32

Diff for: pallets/subtensor/src/tests/coinbase.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn test_coinbase_moving_prices() {
190190
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
191191
SubnetMechanism::<Test>::insert(netuid, 1);
192192
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(1));
193-
NetworkRegisteredAt::<Test>::insert(netuid, 1);
193+
FirstEmissionBlockNumber::<Test>::insert(netuid, 1);
194194

195195
// Updating the moving price keeps it the same.
196196
assert_eq!(
@@ -250,7 +250,7 @@ fn test_update_moving_price_initial() {
250250

251251
// Registered recently
252252
System::set_block_number(510);
253-
NetworkRegisteredAt::<Test>::insert(netuid, 500);
253+
FirstEmissionBlockNumber::<Test>::insert(netuid, 500);
254254

255255
SubtensorModule::update_moving_price(netuid);
256256

@@ -275,7 +275,7 @@ fn test_update_moving_price_after_time() {
275275

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

280280
SubtensorModule::update_moving_price(netuid);
281281

0 commit comments

Comments
 (0)