diff --git a/pallets/subtensor/src/migrations/migrate_rao.rs b/pallets/subtensor/src/migrations/migrate_rao.rs index d6183c2d1d..ba9e217dc2 100644 --- a/pallets/subtensor/src/migrations/migrate_rao.rs +++ b/pallets/subtensor/src/migrations/migrate_rao.rs @@ -3,6 +3,7 @@ use alloc::string::String; use frame_support::IterableStorageMap; use frame_support::{traits::Get, weights::Weight}; use sp_runtime::format; +use substrate_fixed::types::I96F32; use substrate_fixed::types::U64F64; use super::*; @@ -88,6 +89,12 @@ pub fn migrate_rao() -> Weight { let remaining_lock = lock.saturating_sub(pool_initial_tao); // Refund the owner for the remaining lock. + SubnetMovingPrice::::insert( + netuid, + I96F32::from_num(EmissionValues::::get(netuid)) + .checked_div(I96F32::from_num(1_000_000_000)) + .unwrap_or(I96F32::from_num(0.0)), + ); Pallet::::add_balance_to_coldkey_account(&owner, remaining_lock); SubnetLocked::::insert(netuid, 0); // Clear lock amount. SubnetTAO::::insert(netuid, pool_initial_tao); diff --git a/pallets/subtensor/src/tests/migration.rs b/pallets/subtensor/src/tests/migration.rs index 025f77e981..6d45cbe0be 100644 --- a/pallets/subtensor/src/tests/migration.rs +++ b/pallets/subtensor/src/tests/migration.rs @@ -15,6 +15,18 @@ use sp_core::{crypto::Ss58Codec, H256, U256}; use sp_io::hashing::twox_128; use sp_runtime::traits::Zero; use substrate_fixed::types::extra::U2; +use substrate_fixed::types::I96F32; + +#[allow(clippy::arithmetic_side_effects)] +fn close(value: u64, target: u64, eps: u64) { + assert!( + (value as i64 - target as i64).abs() < eps as i64, + "Assertion failed: value = {}, target = {}, eps = {}", + value, + target, + eps + ) +} #[test] fn test_initialise_ti() { @@ -39,226 +51,6 @@ fn test_initialise_ti() { }); } -// #[test] -// fn test_migration_fix_total_stake_maps() { -// new_test_ext(1).execute_with(|| { - -// let ck1 = U256::from(1); -// let ck2 = U256::from(2); -// let ck3 = U256::from(3); - -// let hk1 = U256::from(1 + 100); -// let hk2 = U256::from(2 + 100); - -// let mut total_stake_amount = 0; - -// // Give each coldkey some stake in the maps -// SubtensorModule::increase_stake_on_coldkey_hotkey_account(&ck1, &hk1, 100); -// total_stake_amount += 100; - -// SubtensorModule::increase_stake_on_coldkey_hotkey_account(&ck2, &hk1, 10_101); -// total_stake_amount += 10_101; - -// SubtensorModule::increase_stake_on_coldkey_hotkey_account(&ck3, &hk2, 100_000_000); -// total_stake_amount += 100_000_000; - -// SubtensorModule::increase_stake_on_coldkey_hotkey_account(&ck1, &hk2, 1_123_000_000); -// total_stake_amount += 1_123_000_000; - -// // Check that the total stake is correct -// assert_eq!(SubtensorModule::get_total_stake(), total_stake_amount); - -// // Check that the total coldkey stake is correct -// assert_eq!( -// SubtensorModule::get_total_stake_for_coldkey(&ck1), -// 100 + 1_123_000_000 -// ); -// assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&ck2), 10_101); -// assert_eq!( -// SubtensorModule::get_total_stake_for_coldkey(&ck3), -// 100_000_000 -// ); - -// // Check that the total hotkey stake is correct -// assert_eq!( -// SubtensorModule::get_total_stake_for_hotkey(&hk1), -// 100 + 10_101 -// ); -// assert_eq!( -// SubtensorModule::get_total_stake_for_hotkey(&hk2), -// 100_000_000 + 1_123_000_000 -// ); - -// // Mess up the total coldkey stake -// crate::TotalColdkeyStake::::insert(ck1, 0); -// // Verify that the total coldkey stake is now 0 for ck1 -// assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&ck1), 0); - -// // Mess up the total stake -// crate::TotalStake::::put(123_456_789); -// // Verify that the total stake is now wrong -// assert_ne!(SubtensorModule::get_total_stake(), total_stake_amount); - -// // Run the migration to fix the total stake maps -// crate::migrations::migrate_to_v2_fixed_total_stake::migrate_to_v2_fixed_total_stake::( -// ); - -// // Verify that the total stake is now correct -// assert_eq!(SubtensorModule::get_total_stake(), total_stake_amount); -// // Verify that the total coldkey stake is now correct for each coldkey -// assert_eq!( -// SubtensorModule::get_total_stake_for_coldkey(&ck1), -// 100 + 1_123_000_000 -// ); -// assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&ck2), 10_101); -// assert_eq!( -// SubtensorModule::get_total_stake_for_coldkey(&ck3), -// 100_000_000 -// ); - -// // Verify that the total hotkey stake is STILL correct for each hotkey -// assert_eq!( -// SubtensorModule::get_total_stake_for_hotkey(&hk1), -// 100 + 10_101 -// ); -// assert_eq!( -// SubtensorModule::get_total_stake_for_hotkey(&hk2), -// 100_000_000 + 1_123_000_000 -// ); - -// // Verify that the Stake map has no extra entries -// assert_eq!(crate::Stake::::iter().count(), 4); // 4 entries total -// assert_eq!(crate::Stake::::iter_key_prefix(hk1).count(), 2); // 2 stake entries for hk1 -// assert_eq!(crate::Stake::::iter_key_prefix(hk2).count(), 2); // 2 stake entries for hk2 -// }) -// } - -// #[test] -// // To run this test with cargo, use the following command: -// // cargo test --package pallet-subtensor --test migration test_migrate_total_issuance -// fn test_migrate_total_issuance() { -// new_test_ext(1).execute_with(|| { -// // Run the migration to check total issuance. -// let test: bool = true; - -// assert_eq!(SubtensorModule::get_total_issuance(), 0); -// crate::migrations::migrate_total_issuance::migrate_total_issuance::(test); -// assert_eq!(SubtensorModule::get_total_issuance(), 0); - -// SubtensorModule::add_balance_to_coldkey_account(&U256::from(1), 10000); -// assert_eq!(SubtensorModule::get_total_issuance(), 0); -// crate::migrations::migrate_total_issuance::migrate_total_issuance::(test); -// assert_eq!(SubtensorModule::get_total_issuance(), 10000); - -// SubtensorModule::increase_stake_on_coldkey_hotkey_account( -// &U256::from(1), -// &U256::from(1), -// 30000, -// ); -// assert_eq!(SubtensorModule::get_total_issuance(), 10000); -// crate::migrations::migrate_total_issuance::migrate_total_issuance::(test); -// assert_eq!(SubtensorModule::get_total_issuance(), 10000 + 30000); -// }) -// } - -//#[test] -// To run this test with cargo, use the following command: -// cargo test --package pallet-subtensor --test migration test_total_issuance_global -// fn test_total_issuance_global() { -// new_test_ext(0).execute_with(|| { - -// // Initialize network unique identifier and keys for testing. -// let netuid: u16 = 1; // Network unique identifier set to 1 for testing. -// let coldkey = U256::from(0); // Coldkey initialized to 0, representing an account's public key for non-transactional operations. -// let hotkey = U256::from(0); // Hotkey initialized to 0, representing an account's public key for transactional operations. -// let owner: U256 = U256::from(0); - -// let lockcost: u64 = SubtensorModule::get_network_lock_cost(); -// SubtensorModule::add_balance_to_coldkey_account(&owner, lockcost); // Add a balance of 20000 to the coldkey account. -// assert_eq!(SubtensorModule::get_total_issuance(), 0); // initial is zero. -// assert_ok!(SubtensorModule::register_network( -// <::RuntimeOrigin>::signed(owner), -// )); -// SubtensorModule::set_max_allowed_uids(netuid, 1); // Set the maximum allowed unique identifiers for the network to 1. -// assert_eq!(SubtensorModule::get_total_issuance(), 0); // initial is zero. -// crate::migrations::migrate_total_issuance::migrate_total_issuance::(true); // Pick up lock. -// assert_eq!(SubtensorModule::get_total_issuance(), lockcost); // Verify the total issuance is updated to 20000 after migration. -// assert!(SubtensorModule::if_subnet_exist(netuid)); - -// // Test the migration's effect on total issuance after adding balance to a coldkey account. -// let account_balance: u64 = 20000; -// let _hotkey_account_id_1 = U256::from(1); // Define a hotkey account ID for further operations. -// let _coldkey_account_id_1 = U256::from(1); // Define a coldkey account ID for further operations. -// assert_eq!(SubtensorModule::get_total_issuance(), lockcost); // Ensure the total issuance starts at 0 before the migration. -// SubtensorModule::add_balance_to_coldkey_account(&coldkey, account_balance); // Add a balance of 20000 to the coldkey account. -// crate::migrations::migrate_total_issuance::migrate_total_issuance::(true); // Execute the migration to update total issuance. -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost -// ); // Verify the total issuance is updated to 20000 after migration. - -// // Test the effect of burning on total issuance. -// let burn_cost: u64 = 10000; -// SubtensorModule::set_burn(netuid, burn_cost); // Set the burn amount to 10000 for the network. -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost -// ); // Confirm the total issuance remains 20000 before burning. -// assert_ok!(SubtensorModule::burned_register( -// <::RuntimeOrigin>::signed(hotkey), -// netuid, -// hotkey -// )); // Execute the burn operation, reducing the total issuance. -// assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 1); // Ensure the subnetwork count increases to 1 after burning -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost - burn_cost -// ); // Verify the total issuance is reduced to 10000 after burning. -// crate::migrations::migrate_total_issuance::migrate_total_issuance::(true); // Execute the migration to update total issuance. -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost - burn_cost -// ); // Verify the total issuance is updated to 10000 nothing changes - -// // Test staking functionality and its effect on total issuance. -// let new_stake: u64 = 10000; -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost - burn_cost -// ); // Same -// SubtensorModule::increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, new_stake); // Stake an additional 10000 to the coldkey-hotkey account. This is i -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost - burn_cost -// ); // Same -// crate::migrations::migrate_total_issuance::migrate_total_issuance::(true); // Fix issuance -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost - burn_cost + new_stake -// ); // New - -// // Set emission values for the network and verify. -// let emission: u64 = 1_000_000_000; -// SubtensorModule::set_tempo(netuid, 1); -// SubtensorModule::set_emission_values(&[netuid], vec![emission]).unwrap(); // Set the emission value for the network to 1_000_000_000. -// assert_eq!(SubtensorModule::get_subnet_emission_value(netuid), emission); // Verify the emission value is set correctly for the network. -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost - burn_cost + new_stake -// ); -// run_to_block(2); // Advance to block number 2 to trigger the emission through the subnet. -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost - burn_cost + new_stake + emission -// ); // Verify the total issuance reflects the staked amount and emission value that has been put through the epoch. -// crate::migrations::migrate_total_issuance::migrate_total_issuance::(true); // Test migration does not change amount. -// assert_eq!( -// SubtensorModule::get_total_issuance(), -// account_balance + lockcost - burn_cost + new_stake + emission -// ); // Verify the total issuance reflects the staked amount and emission value that has been put through the epoch. -// }) -// } - #[test] fn test_migration_transfer_nets_to_foundation() { new_test_ext(1).execute_with(|| { @@ -307,148 +99,6 @@ fn test_migration_delete_subnet_21() { }) } -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake --exact --nocapture -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake --exact --nocapture -// Deprecated -// #[test] -// fn test_migrate_fix_total_coldkey_stake() { -// new_test_ext(1).execute_with(|| { -// assert!(false); - -// let _migration_name = "fix_total_coldkey_stake_v7"; -// let coldkey = U256::from(0); -// TotalColdkeyStake::::insert(coldkey, 0); -// StakingHotkeys::::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]); -// Stake::::insert(U256::from(1), U256::from(0), 10000); -// Stake::::insert(U256::from(2), U256::from(0), 10000); -// Stake::::insert(U256::from(3), U256::from(0), 10000); -// crate::migrations::migrate_fix_total_coldkey_stake::do_migrate_fix_total_coldkey_stake::< -// Test, -// >(); -// assert_eq!(TotalColdkeyStake::::get(coldkey), 30000); -// }) -// } - -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_value_already_in_total --exact --nocapture -// Deprecated -// #[test] -// fn test_migrate_fix_total_coldkey_stake_value_already_in_total() { -// new_test_ext(1).execute_with(|| { - -// let _migration_name = "fix_total_coldkey_stake_v7"; -// let coldkey = U256::from(0); -// TotalColdkeyStake::::insert(coldkey, 100000000); -// StakingHotkeys::::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]); -// Stake::::insert(U256::from(1), U256::from(0), 10000); -// Stake::::insert(U256::from(2), U256::from(0), 10000); -// Stake::::insert(U256::from(3), U256::from(0), 10000); -// crate::migrations::migrate_fix_total_coldkey_stake::do_migrate_fix_total_coldkey_stake::< -// Test, -// >(); -// assert_eq!(TotalColdkeyStake::::get(coldkey), 30000); -// }) -// } - -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_no_entry --exact --nocapture -// Deprecated -// #[test] -// fn test_migrate_fix_total_coldkey_stake_no_entry() { -// new_test_ext(1).execute_with(|| { - -// let _migration_name = "fix_total_coldkey_stake_v7"; -// let coldkey = U256::from(0); -// StakingHotkeys::::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]); -// Stake::::insert(U256::from(1), U256::from(0), 10000); -// Stake::::insert(U256::from(2), U256::from(0), 10000); -// Stake::::insert(U256::from(3), U256::from(0), 10000); -// crate::migrations::migrate_fix_total_coldkey_stake::do_migrate_fix_total_coldkey_stake::< -// Test, -// >(); -// assert_eq!(TotalColdkeyStake::::get(coldkey), 30000); -// }) -// } - -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys --exact --nocapture -// Deprecated -// #[test] -// fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys() { -// new_test_ext(1).execute_with(|| { -// let _migration_name = "fix_total_coldkey_stake_v7"; -// let coldkey = U256::from(0); -// TotalColdkeyStake::::insert(coldkey, 100000000); -// StakingHotkeys::::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]); -// crate::migrations::migrate_fix_total_coldkey_stake::do_migrate_fix_total_coldkey_stake::< -// Test, -// >(); -// assert_eq!(TotalColdkeyStake::::get(coldkey), 0); -// }) -// } - -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing --exact --nocapture -// Deprecated -// #[test] -// fn test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing() { -// new_test_ext(1).execute_with(|| { - -// let _migration_name = "fix_total_coldkey_stake_v7"; -// let coldkey = U256::from(0); -// TotalColdkeyStake::::insert(coldkey, 100000000); -// StakingHotkeys::::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]); -// Stake::::insert(U256::from(1), U256::from(0), 10000); -// Stake::::insert(U256::from(2), U256::from(0), 10000); -// crate::migrations::migrate_fix_total_coldkey_stake::do_migrate_fix_total_coldkey_stake::< -// Test, -// >(); -// assert_eq!(TotalColdkeyStake::::get(coldkey), 20000); -// }) -// } - -// New test to check if migration runs only once -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_runs_once --exact --nocapture -// Deprecated -// #[test] -// fn test_migrate_fix_total_coldkey_stake_runs_once() { -// new_test_ext(1).execute_with(|| { - -// let migration_name = "fix_total_coldkey_stake_v7"; -// let coldkey = U256::from(0); -// TotalColdkeyStake::::insert(coldkey, 0); -// StakingHotkeys::::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]); -// Stake::::insert(U256::from(1), coldkey, 10000); -// Stake::::insert(U256::from(2), coldkey, 10000); -// Stake::::insert(U256::from(3), coldkey, 10000); - -// // First run -// let first_weight = run_migration_and_check(migration_name); -// assert!(first_weight != Weight::zero()); -// assert_eq!(TotalColdkeyStake::::get(coldkey), 30000); - -// // Second run -// let second_weight = run_migration_and_check(migration_name); -// assert_eq!(second_weight, Weight::zero()); -// assert_eq!(TotalColdkeyStake::::get(coldkey), 30000); -// }) -// } - -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries --exact --nocapture -// Deprecated -// #[test] -// fn test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries() { -// new_test_ext(1).execute_with(|| { - -// let migration_name = "fix_total_coldkey_stake_v7"; -// let coldkey = U256::from(0); -// TotalColdkeyStake::::insert(coldkey, 123_456_789); - -// // Notably, coldkey has no stake map or staking_hotkeys map entries - -// let weight = run_migration_and_check(migration_name); -// assert!(weight != Weight::zero()); -// // Therefore 0 -// assert_eq!(TotalColdkeyStake::::get(coldkey), 123_456_789); -// }) -// } - fn run_migration_and_check(migration_name: &'static str) -> frame_support::weights::Weight { // Execute the migration and store its weight let weight: frame_support::weights::Weight = @@ -558,6 +208,8 @@ fn test_migrate_rao() { // Setup initial state let netuid_0: u16 = 0; let netuid_1: u16 = 1; + let netuid_2: u16 = 2; + let netuid_3: u16 = 3; let hotkey1 = U256::from(1); let hotkey2 = U256::from(2); let coldkey1 = U256::from(3); @@ -570,11 +222,17 @@ fn test_migrate_rao() { // Add networks root and alpha add_network(netuid_0, 1, 0); add_network(netuid_1, 1, 0); + add_network(netuid_2, 1, 0); + add_network(netuid_3, 1, 0); // Set subnet lock SubnetLocked::::insert(netuid_1, lock_amount); // Add some initial stake + EmissionValues::::insert(netuid_1, 1_000_000_000); + EmissionValues::::insert(netuid_2, 2_000_000_000); + EmissionValues::::insert(netuid_3, 3_000_000_000); + Owner::::insert(hotkey1, coldkey1); Owner::::insert(hotkey2, coldkey2); Stake::::insert(hotkey1, coldkey1, stake_amount); @@ -710,6 +368,25 @@ fn test_migrate_rao() { SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey1, netuid_1), stake_amount ); + + // Run the coinbase + let emission: u64 = 1_000_000_000; + SubtensorModule::run_coinbase(I96F32::from_num(emission)); + close( + SubnetTaoInEmission::::get(netuid_1), + emission / 6, + 100, + ); + close( + SubnetTaoInEmission::::get(netuid_2), + 2 * (emission / 6), + 100, + ); + close( + SubnetTaoInEmission::::get(netuid_3), + 3 * (emission / 6), + 100, + ); }); } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 03ade34d32..40e5c54f02 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -229,7 +229,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: 231, + spec_version: 232, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1,