Skip to content

Commit a27fff2

Browse files
authored
Merge pull request #1371 from opentensor/fix/slow-down-price-ema
Initial slow down for price ema
2 parents 17b6ef3 + b4bab28 commit a27fff2

File tree

3 files changed

+78
-17
lines changed

3 files changed

+78
-17
lines changed

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ impl<T: Config> Pallet<T> {
5757
}
5858
}
5959
pub fn update_moving_price(netuid: u16) {
60-
let alpha: I96F32 = SubnetMovingAlpha::<T>::get();
60+
let blocks_since_registration = I96F32::saturating_from_num(
61+
Self::get_current_block_as_u64().saturating_sub(NetworkRegisteredAt::<T>::get(netuid)),
62+
);
63+
// 7200 * 14 = 100_800 is the halving time
64+
let alpha: I96F32 =
65+
SubnetMovingAlpha::<T>::get().saturating_mul(blocks_since_registration.safe_div(
66+
blocks_since_registration.saturating_add(I96F32::saturating_from_num(100_800)),
67+
));
6168
let minus_alpha: I96F32 = I96F32::saturating_from_num(1.0).saturating_sub(alpha);
6269
let current_price: I96F32 = alpha
6370
.saturating_mul(Self::get_alpha_price(netuid).min(I96F32::saturating_from_num(1.0)));

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,16 @@ 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);
194+
193195
// Updating the moving price keeps it the same.
194196
assert_eq!(
195197
SubtensorModule::get_moving_alpha_price(netuid),
196198
I96F32::from_num(1)
197199
);
200+
// Skip some blocks so that EMA price is not slowed down
201+
System::set_block_number(7_200_000);
202+
198203
SubtensorModule::update_moving_price(netuid);
199204
assert_eq!(
200205
SubtensorModule::get_moving_alpha_price(netuid),
@@ -206,29 +211,78 @@ fn test_coinbase_moving_prices() {
206211
SubnetMovingAlpha::<Test>::set(I96F32::from_num(1.0));
207212
// Run moving 1 times.
208213
SubtensorModule::update_moving_price(netuid);
209-
// Assert price is == 100% of the real price.
210-
assert_eq!(
211-
SubtensorModule::get_moving_alpha_price(netuid),
212-
I96F32::from_num(1.0)
213-
);
214+
// Assert price is ~ 100% of the real price.
215+
assert!(I96F32::from_num(1.0) - SubtensorModule::get_moving_alpha_price(netuid) < 0.05);
214216
// Set price to zero.
215217
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
216218
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.1));
217-
// Run moving 6 times.
218-
SubtensorModule::update_moving_price(netuid);
219-
SubtensorModule::update_moving_price(netuid);
220-
SubtensorModule::update_moving_price(netuid);
221-
SubtensorModule::update_moving_price(netuid);
222-
SubtensorModule::update_moving_price(netuid);
223-
SubtensorModule::update_moving_price(netuid);
219+
220+
// EMA price 14 days after registration
221+
System::set_block_number(7_200 * 14);
222+
223+
// Run moving 14 times.
224+
for _ in 0..14 {
225+
SubtensorModule::update_moving_price(netuid);
226+
}
227+
224228
// Assert price is > 50% of the real price.
225-
assert_eq!(
226-
SubtensorModule::get_moving_alpha_price(netuid),
227-
I96F32::from_num(0.468559)
229+
assert!(
230+
(I96F32::from_num(0.512325) - SubtensorModule::get_moving_alpha_price(netuid)).abs()
231+
< 0.001
228232
);
229233
});
230234
}
231235

236+
// Test moving price updates slow down at the beginning.
237+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_update_moving_price_initial --exact --show-output --nocapture
238+
#[test]
239+
fn test_update_moving_price_initial() {
240+
new_test_ext(1).execute_with(|| {
241+
let netuid: u16 = 1;
242+
add_network(netuid, 1, 0);
243+
// Set current price to 1.0
244+
SubnetTAO::<Test>::insert(netuid, 1_000_000);
245+
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
246+
SubnetMechanism::<Test>::insert(netuid, 1);
247+
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.5));
248+
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
249+
250+
// Registered recently
251+
System::set_block_number(510);
252+
NetworkRegisteredAt::<Test>::insert(netuid, 500);
253+
254+
SubtensorModule::update_moving_price(netuid);
255+
256+
let new_price = SubnetMovingPrice::<Test>::get(netuid);
257+
assert!(new_price.to_num::<f64>() < 0.001);
258+
});
259+
}
260+
261+
// Test moving price updates slow down at the beginning.
262+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_update_moving_price_after_time --exact --show-output --nocapture
263+
#[test]
264+
fn test_update_moving_price_after_time() {
265+
new_test_ext(1).execute_with(|| {
266+
let netuid: u16 = 1;
267+
add_network(netuid, 1, 0);
268+
// Set current price to 1.0
269+
SubnetTAO::<Test>::insert(netuid, 1_000_000);
270+
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
271+
SubnetMechanism::<Test>::insert(netuid, 1);
272+
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.5));
273+
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
274+
275+
// Registered long time ago
276+
System::set_block_number(72_000_500);
277+
NetworkRegisteredAt::<Test>::insert(netuid, 500);
278+
279+
SubtensorModule::update_moving_price(netuid);
280+
281+
let new_price = SubnetMovingPrice::<Test>::get(netuid);
282+
assert!((new_price.to_num::<f64>() - 0.5).abs() < 0.001);
283+
});
284+
}
285+
232286
// Test basic alpha issuance in coinbase mechanism.
233287
// This test verifies that:
234288
// - Alpha issuance is initialized to 0 for new subnets

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
228228
// `spec_version`, and `authoring_version` are the same between Wasm and native.
229229
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
230230
// the compatible custom types.
231-
spec_version: 245,
231+
spec_version: 246,
232232
impl_version: 1,
233233
apis: RUNTIME_API_VERSIONS,
234234
transaction_version: 1,

0 commit comments

Comments
 (0)