Skip to content

Commit 719a6f5

Browse files
authored
Merge pull request #1510 from opentensor/ema-hotfix
Change update_moving_price() function
2 parents 8de0d23 + ccaa561 commit 719a6f5

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

.github/workflows/try-runtime.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ env:
99
jobs:
1010
check-devnet:
1111
name: check devnet
12+
if: github.base_ref != 'main'
1213
runs-on: SubtensorCI
1314
steps:
1415
- name: Checkout sources
@@ -29,7 +30,7 @@ jobs:
2930

3031
check-testnet:
3132
name: check testnet
32-
# if: github.base_ref == 'testnet' || github.base_ref == 'devnet' || github.base_ref == 'main'
33+
if: github.base_ref != 'main'
3334
runs-on: SubtensorCI
3435
steps:
3536
- name: Checkout sources

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,30 @@ impl<T: Config> Pallet<T> {
5656
SubnetMovingPrice::<T>::get(netuid)
5757
}
5858
}
59+
5960
pub fn update_moving_price(netuid: u16) {
60-
let blocks_since_registration = I96F32::saturating_from_num(
61-
Self::get_current_block_as_u64().saturating_sub(NetworkRegisteredAt::<T>::get(netuid)),
62-
);
61+
let blocks_since_start_call = I96F32::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+
});
6370

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.
6871
let halving_time = EMAPriceHalvingBlocks::<T>::get(netuid);
6972
let alpha: I96F32 =
70-
SubnetMovingAlpha::<T>::get().saturating_mul(blocks_since_registration.safe_div(
71-
blocks_since_registration.saturating_add(I96F32::saturating_from_num(halving_time)),
73+
SubnetMovingAlpha::<T>::get().saturating_mul(blocks_since_start_call.safe_div(
74+
blocks_since_start_call.saturating_add(I96F32::saturating_from_num(halving_time)),
7275
));
7376
let minus_alpha: I96F32 = I96F32::saturating_from_num(1.0).saturating_sub(alpha);
7477
let current_price: I96F32 = alpha
7578
.saturating_mul(Self::get_alpha_price(netuid).min(I96F32::saturating_from_num(1.0)));
7679
let current_moving: I96F32 =
7780
minus_alpha.saturating_mul(Self::get_moving_alpha_price(netuid));
7881
let new_moving: I96F32 = current_price.saturating_add(current_moving);
82+
7983
SubnetMovingPrice::<T>::insert(netuid, new_moving);
8084
}
8185

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn test_coinbase_moving_prices() {
191191
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
192192
SubnetMechanism::<Test>::insert(netuid, 1);
193193
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(1));
194-
NetworkRegisteredAt::<Test>::insert(netuid, 1);
194+
FirstEmissionBlockNumber::<Test>::insert(netuid, 1);
195195

196196
// Updating the moving price keeps it the same.
197197
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

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
207207
// `spec_version`, and `authoring_version` are the same between Wasm and native.
208208
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
209209
// the compatible custom types.
210-
spec_version: 257,
210+
spec_version: 258,
211211
impl_version: 1,
212212
apis: RUNTIME_API_VERSIONS,
213213
transaction_version: 1,

0 commit comments

Comments
 (0)