Skip to content

Commit a3d9024

Browse files
committed
Initial slow down for price ema
1 parent 17b6ef3 commit a3d9024

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ 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+
let alpha: I96F32 =
64+
SubnetMovingAlpha::<T>::get().saturating_mul(blocks_since_registration.safe_div(
65+
blocks_since_registration.saturating_add(I96F32::saturating_from_num(7200)),
66+
));
6167
let minus_alpha: I96F32 = I96F32::saturating_from_num(1.0).saturating_sub(alpha);
6268
let current_price: I96F32 = alpha
6369
.saturating_mul(Self::get_alpha_price(netuid).min(I96F32::saturating_from_num(1.0)));

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ fn test_coinbase_moving_prices() {
195195
SubtensorModule::get_moving_alpha_price(netuid),
196196
I96F32::from_num(1)
197197
);
198+
// Skip some blocks so that EMA price is now slowed down
199+
System::set_block_number(7_200_000);
200+
198201
SubtensorModule::update_moving_price(netuid);
199202
assert_eq!(
200203
SubtensorModule::get_moving_alpha_price(netuid),
@@ -207,10 +210,7 @@ fn test_coinbase_moving_prices() {
207210
// Run moving 1 times.
208211
SubtensorModule::update_moving_price(netuid);
209212
// 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-
);
213+
assert!(I96F32::from_num(1.0) - SubtensorModule::get_moving_alpha_price(netuid) < 0.001);
214214
// Set price to zero.
215215
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
216216
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.1));
@@ -222,13 +222,63 @@ fn test_coinbase_moving_prices() {
222222
SubtensorModule::update_moving_price(netuid);
223223
SubtensorModule::update_moving_price(netuid);
224224
// Assert price is > 50% of the real price.
225-
assert_eq!(
226-
SubtensorModule::get_moving_alpha_price(netuid),
227-
I96F32::from_num(0.468559)
225+
assert!(
226+
(I96F32::from_num(0.468559) - SubtensorModule::get_moving_alpha_price(netuid)).abs()
227+
< 0.001
228228
);
229229
});
230230
}
231231

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

0 commit comments

Comments
 (0)