Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial slow down for price ema #1371

Merged
merged 4 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ impl<T: Config> Pallet<T> {
}
}
pub fn update_moving_price(netuid: u16) {
let alpha: I96F32 = SubnetMovingAlpha::<T>::get();
let blocks_since_registration = I96F32::saturating_from_num(
Self::get_current_block_as_u64().saturating_sub(NetworkRegisteredAt::<T>::get(netuid)),
);
// 7200 * 14 = 100_800 is the halving time
let alpha: I96F32 =
SubnetMovingAlpha::<T>::get().saturating_mul(blocks_since_registration.safe_div(
blocks_since_registration.saturating_add(I96F32::saturating_from_num(100_800)),
));
let minus_alpha: I96F32 = I96F32::saturating_from_num(1.0).saturating_sub(alpha);
let current_price: I96F32 = alpha
.saturating_mul(Self::get_alpha_price(netuid).min(I96F32::saturating_from_num(1.0)));
Expand Down
84 changes: 69 additions & 15 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,16 @@ fn test_coinbase_moving_prices() {
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
SubnetMechanism::<Test>::insert(netuid, 1);
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(1));
NetworkRegisteredAt::<Test>::insert(netuid, 1);

// Updating the moving price keeps it the same.
assert_eq!(
SubtensorModule::get_moving_alpha_price(netuid),
I96F32::from_num(1)
);
// Skip some blocks so that EMA price is not slowed down
System::set_block_number(7_200_000);

SubtensorModule::update_moving_price(netuid);
assert_eq!(
SubtensorModule::get_moving_alpha_price(netuid),
Expand All @@ -206,29 +211,78 @@ fn test_coinbase_moving_prices() {
SubnetMovingAlpha::<Test>::set(I96F32::from_num(1.0));
// Run moving 1 times.
SubtensorModule::update_moving_price(netuid);
// Assert price is == 100% of the real price.
assert_eq!(
SubtensorModule::get_moving_alpha_price(netuid),
I96F32::from_num(1.0)
);
// Assert price is ~ 100% of the real price.
assert!(I96F32::from_num(1.0) - SubtensorModule::get_moving_alpha_price(netuid) < 0.05);
// Set price to zero.
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.1));
// Run moving 6 times.
SubtensorModule::update_moving_price(netuid);
SubtensorModule::update_moving_price(netuid);
SubtensorModule::update_moving_price(netuid);
SubtensorModule::update_moving_price(netuid);
SubtensorModule::update_moving_price(netuid);
SubtensorModule::update_moving_price(netuid);

// EMA price 14 days after registration
System::set_block_number(7_200 * 14);

// Run moving 14 times.
for _ in 0..14 {
SubtensorModule::update_moving_price(netuid);
}

// Assert price is > 50% of the real price.
assert_eq!(
SubtensorModule::get_moving_alpha_price(netuid),
I96F32::from_num(0.468559)
assert!(
(I96F32::from_num(0.512325) - SubtensorModule::get_moving_alpha_price(netuid)).abs()
< 0.001
);
});
}

// Test moving price updates slow down at the beginning.
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_update_moving_price_initial --exact --show-output --nocapture
#[test]
fn test_update_moving_price_initial() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
// Set current price to 1.0
SubnetTAO::<Test>::insert(netuid, 1_000_000);
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
SubnetMechanism::<Test>::insert(netuid, 1);
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.5));
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));

// Registered recently
System::set_block_number(510);
NetworkRegisteredAt::<Test>::insert(netuid, 500);

SubtensorModule::update_moving_price(netuid);

let new_price = SubnetMovingPrice::<Test>::get(netuid);
assert!(new_price.to_num::<f64>() < 0.001);
});
}

// Test moving price updates slow down at the beginning.
// 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
#[test]
fn test_update_moving_price_after_time() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 1, 0);
// Set current price to 1.0
SubnetTAO::<Test>::insert(netuid, 1_000_000);
SubnetAlphaIn::<Test>::insert(netuid, 1_000_000);
SubnetMechanism::<Test>::insert(netuid, 1);
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.5));
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));

// Registered long time ago
System::set_block_number(72_000_500);
NetworkRegisteredAt::<Test>::insert(netuid, 500);

SubtensorModule::update_moving_price(netuid);

let new_price = SubnetMovingPrice::<Test>::get(netuid);
assert!((new_price.to_num::<f64>() - 0.5).abs() < 0.001);
});
}

// Test basic alpha issuance in coinbase mechanism.
// This test verifies that:
// - Alpha issuance is initialized to 0 for new subnets
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 245,
spec_version: 246,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading