Skip to content

Commit 8f33f8c

Browse files
authored
Merge pull request #2192 from opentensor/hotfix/sn-to-emit-to
Hotfix/sn to emit to
2 parents b179867 + d968a93 commit 8f33f8c

File tree

11 files changed

+516
-30
lines changed

11 files changed

+516
-30
lines changed

evm-tests/test/neuron.precompile.emission-check.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe("Test the Neuron precompile with emission", () => {
6363

6464
let i = 0;
6565
while (i < 10) {
66-
const emission = await api.query.SubtensorModule.PendingEmission.getValue(netuid)
66+
const emission = await api.query.SubtensorModule.ServerEmission.getValue(netuid)
6767

6868
console.log("emission is ", emission);
6969
await new Promise((resolve) => setTimeout(resolve, 2000));

evm-tests/test/staking.precompile.reward.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ describe("Test neuron precompile reward", () => {
7474

7575
let index = 0;
7676
while (index < 60) {
77-
const pending = await api.query.SubtensorModule.PendingEmission.getValue(netuid);
77+
const pending = await api.query.SubtensorModule.ValidatorEmission.getValue(netuid);
7878
if (pending > 0) {
7979
console.log("pending amount is ", pending);
8080
break;
8181
}
8282

8383
await new Promise((resolve) => setTimeout(resolve, 1000));
84-
console.log("wait for the pendingEmission update");
84+
console.log("wait for the ValidatorEmission update");
8585
index += 1;
8686
}
8787

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ impl<T: Config> Pallet<T> {
3131
.collect();
3232
log::debug!("All subnet netuids: {subnets:?}");
3333

34+
let subnets_to_emit_to: Vec<NetUid> = Self::get_subnets_to_emit_to(&subnets);
35+
log::debug!("Subnets to emit to: {subnets_to_emit_to:?}");
36+
3437
// 2. Get subnets to emit to and emissions
35-
let subnet_emissions = Self::get_subnet_block_emissions(&subnets, block_emission);
36-
let subnets_to_emit_to: Vec<NetUid> = subnet_emissions.keys().copied().collect();
38+
let subnet_emissions =
39+
Self::get_subnet_block_emissions(&subnets_to_emit_to, block_emission);
3740
let root_sell_flag = Self::get_network_root_sell_flag(&subnets_to_emit_to);
3841

3942
// --- 3. Get subnet terms (tao_in, alpha_in, and alpha_out)

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ mod hooks {
7272
.saturating_add(migrations::migrate_delete_subnet_21::migrate_delete_subnet_21::<T>())
7373
// Storage version v4 -> v5
7474
.saturating_add(migrations::migrate_delete_subnet_3::migrate_delete_subnet_3::<T>())
75-
// Doesn't check storage version. TODO: Remove after upgrade
76-
// Storage version v5 -> v6
77-
.saturating_add(migrations::migrate_total_issuance::migrate_total_issuance::<T>(false))
7875
// Populate OwnedHotkeys map for coldkey swap. Doesn't update storage vesion.
7976
// Storage version v6 -> v7
8077
.saturating_add(migrations::migrate_populate_owned_hotkeys::migrate_populate_owned::<T>())
@@ -163,7 +160,9 @@ mod hooks {
163160
// Re-init tao flows
164161
.saturating_add(migrations::migrate_init_tao_flow::migrate_init_tao_flow::<T>())
165162
// Migrate pending emissions
166-
.saturating_add(migrations::migrate_pending_emissions::migrate_pending_emissions::<T>());
163+
.saturating_add(migrations::migrate_pending_emissions::migrate_pending_emissions::<T>())
164+
// Reset unactive subnets
165+
.saturating_add(migrations::migrate_reset_unactive_sn::migrate_reset_unactive_sn::<T>());
167166
weight
168167
}
169168

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use super::*;
2+
3+
pub fn get_unactive_sn_netuids<T: Config>(
4+
pool_initial_alpha: AlphaCurrency,
5+
) -> (Vec<NetUid>, Weight) {
6+
// Loop over all subnets, if the AlphaIssuance is > pool_initial_alpha
7+
// but FirstEmissionBlockNumber is None
8+
// then this subnet should be reset
9+
let mut weight = T::DbWeight::get().reads(1);
10+
let unactive_netuids = Pallet::<T>::get_all_subnet_netuids()
11+
.iter()
12+
.filter(|&netuid| !netuid.is_root())
13+
.filter(|&netuid| {
14+
let alpha_issuance = Pallet::<T>::get_alpha_issuance(*netuid);
15+
let first_emission_block_number = FirstEmissionBlockNumber::<T>::get(*netuid);
16+
alpha_issuance != pool_initial_alpha && first_emission_block_number.is_none()
17+
})
18+
.copied()
19+
.collect::<Vec<_>>();
20+
weight = weight
21+
.saturating_add(T::DbWeight::get().reads(unactive_netuids.len().saturating_mul(3) as u64));
22+
23+
(unactive_netuids, weight)
24+
}
25+
26+
pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
27+
let migration_name = b"migrate_reset_unactive_sn".to_vec();
28+
let mut weight: Weight = T::DbWeight::get().reads(1);
29+
30+
// Skip if already executed
31+
if HasMigrationRun::<T>::get(&migration_name) {
32+
log::info!(
33+
target: "runtime",
34+
"Migration '{}' already run - skipping.",
35+
String::from_utf8_lossy(&migration_name)
36+
);
37+
return weight;
38+
}
39+
log::info!(
40+
"Running migration '{}'",
41+
String::from_utf8_lossy(&migration_name)
42+
);
43+
44+
// From init_new_network
45+
let pool_initial_tao: TaoCurrency = Pallet::<T>::get_network_min_lock();
46+
let pool_initial_alpha: AlphaCurrency = pool_initial_tao.to_u64().into();
47+
48+
let (unactive_netuids, w) = get_unactive_sn_netuids::<T>(pool_initial_alpha);
49+
weight = weight.saturating_add(w);
50+
51+
for netuid in unactive_netuids.iter() {
52+
// Reset the subnet as it shouldn't have any emissions
53+
PendingServerEmission::<T>::remove(*netuid);
54+
PendingValidatorEmission::<T>::remove(*netuid);
55+
PendingRootAlphaDivs::<T>::remove(*netuid);
56+
PendingOwnerCut::<T>::remove(*netuid);
57+
SubnetTaoInEmission::<T>::remove(*netuid);
58+
SubnetAlphaInEmission::<T>::remove(*netuid);
59+
SubnetAlphaOutEmission::<T>::remove(*netuid);
60+
weight = weight.saturating_add(T::DbWeight::get().writes(7));
61+
}
62+
63+
// Mark Migration as Completed
64+
HasMigrationRun::<T>::insert(&migration_name, true);
65+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
66+
67+
log::info!(
68+
"Migration '{:?}' completed successfully.",
69+
String::from_utf8_lossy(&migration_name)
70+
);
71+
72+
weight
73+
}

pallets/subtensor/src/migrations/migrate_total_issuance.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ pub fn migrate_total_issuance<T: Config>(test: bool) -> Weight {
5151
T::DbWeight::get().reads((Owner::<T>::iter().count() as u64).saturating_mul(2)),
5252
);
5353

54-
// Calculate the sum of all locked subnet values
55-
let locked_sum = SubnetLocked::<T>::iter().fold(TaoCurrency::ZERO, |acc, (_, locked)| {
56-
acc.saturating_add(locked)
57-
});
58-
// Add weight for reading all subnet locked entries
59-
weight = weight
60-
.saturating_add(T::DbWeight::get().reads(SubnetLocked::<T>::iter().count() as u64));
61-
6254
// Retrieve the total balance sum
6355
let total_balance = <T as Config>::Currency::total_issuance();
6456
// Add weight for reading total issuance
@@ -69,8 +61,7 @@ pub fn migrate_total_issuance<T: Config>(test: bool) -> Weight {
6961
Ok(total_balance_sum) => {
7062
// Compute the total issuance value
7163
let total_issuance_value = stake_sum
72-
.saturating_add(total_balance_sum.into())
73-
.saturating_add(locked_sum.into());
64+
.saturating_add(total_balance_sum.into());
7465

7566
// Update the total issuance in storage
7667
TotalIssuance::<T>::put(total_issuance_value);

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub mod migrate_remove_unused_maps_and_values;
4141
pub mod migrate_remove_zero_total_hotkey_alpha;
4242
pub mod migrate_reset_bonds_moving_average;
4343
pub mod migrate_reset_max_burn;
44+
pub mod migrate_reset_unactive_sn;
4445
pub mod migrate_set_first_emission_block_number;
4546
pub mod migrate_set_min_burn;
4647
pub mod migrate_set_min_difficulty;
@@ -55,7 +56,6 @@ pub mod migrate_subnet_symbols;
5556
pub mod migrate_subnet_volume;
5657
pub mod migrate_to_v1_separate_emission;
5758
pub mod migrate_to_v2_fixed_total_stake;
58-
pub mod migrate_total_issuance;
5959
pub mod migrate_transfer_ownership_to_foundation;
6060
pub mod migrate_upgrade_revealed_commitments;
6161

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,16 @@ fn test_coinbase_tao_issuance_base() {
8989
let subnet_owner_ck = U256::from(1001);
9090
let subnet_owner_hk = U256::from(1002);
9191
let netuid = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
92+
let total_issuance_before = TotalIssuance::<Test>::get();
9293
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from(3141) / I96F32::from(1000));
9394
let tao_in_before = SubnetTAO::<Test>::get(netuid);
9495
let total_stake_before = TotalStake::<Test>::get();
9596
SubtensorModule::run_coinbase(U96F32::from_num(emission));
9697
assert_eq!(SubnetTAO::<Test>::get(netuid), tao_in_before + emission);
97-
assert_eq!(TotalIssuance::<Test>::get(), emission);
98+
assert_eq!(
99+
TotalIssuance::<Test>::get(),
100+
total_issuance_before + emission
101+
);
98102
assert_eq!(TotalStake::<Test>::get(), total_stake_before + emission);
99103
});
100104
}
@@ -3317,3 +3321,86 @@ fn test_mining_emission_distribution_with_root_sell() {
33173321
);
33183322
});
33193323
}
3324+
3325+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_pending_emission_start_call_not_done --exact --show-output --nocapture
3326+
#[test]
3327+
fn test_pending_emission_start_call_not_done() {
3328+
new_test_ext(1).execute_with(|| {
3329+
let validator_coldkey = U256::from(1);
3330+
let validator_hotkey = U256::from(2);
3331+
let subnet_tempo = 10;
3332+
let stake: u64 = 100_000_000_000;
3333+
let root_stake: u64 = 200_000_000_000; // 200 TAO
3334+
3335+
// Create root network
3336+
NetworksAdded::<Test>::insert(NetUid::ROOT, true);
3337+
// enabled root
3338+
SubtokenEnabled::<Test>::insert(NetUid::ROOT, true);
3339+
3340+
// Add network, register hotkeys, and setup network parameters
3341+
let owner_hotkey = U256::from(10);
3342+
let owner_coldkey = U256::from(11);
3343+
let netuid = add_dynamic_network(&owner_hotkey, &owner_coldkey);
3344+
// Remove FirstEmissionBlockNumber
3345+
FirstEmissionBlockNumber::<Test>::remove(netuid);
3346+
Tempo::<Test>::insert(netuid, subnet_tempo);
3347+
3348+
register_ok_neuron(netuid, validator_hotkey, validator_coldkey, 0);
3349+
SubtensorModule::add_balance_to_coldkey_account(
3350+
&validator_coldkey,
3351+
stake + ExistentialDeposit::get(),
3352+
);
3353+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
3354+
step_block(subnet_tempo);
3355+
SubnetOwnerCut::<Test>::set(u16::MAX / 10);
3356+
// There are two validators and three neurons
3357+
MaxAllowedUids::<Test>::set(netuid, 3);
3358+
SubtensorModule::set_max_allowed_validators(netuid, 2);
3359+
3360+
// Add stake to validator so it has root stake
3361+
SubtensorModule::add_balance_to_coldkey_account(&validator_coldkey, root_stake.into());
3362+
// init root
3363+
assert_ok!(SubtensorModule::add_stake(
3364+
RuntimeOrigin::signed(validator_coldkey),
3365+
validator_hotkey,
3366+
NetUid::ROOT,
3367+
root_stake.into()
3368+
));
3369+
// Set tao weight non zero
3370+
SubtensorModule::set_tao_weight(u64::MAX / 10);
3371+
3372+
// Make root sell happen
3373+
// Set moving price > 1.0
3374+
// Set price > 1.0
3375+
pallet_subtensor_swap::AlphaSqrtPrice::<Test>::insert(
3376+
netuid,
3377+
U64F64::saturating_from_num(10.0),
3378+
);
3379+
3380+
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(2));
3381+
3382+
// Make sure we are root selling, so we have root alpha divs.
3383+
let root_sell_flag = SubtensorModule::get_network_root_sell_flag(&[netuid]);
3384+
assert!(root_sell_flag, "Root sell flag should be true");
3385+
3386+
// !!! Check that the subnet FirstEmissionBlockNumber is None -- no entry
3387+
assert!(FirstEmissionBlockNumber::<Test>::get(netuid).is_none());
3388+
3389+
// Run run_coinbase until emissions are accumulated
3390+
step_block(subnet_tempo - 2);
3391+
3392+
// Verify that all pending emissions are zero
3393+
assert_eq!(
3394+
PendingServerEmission::<Test>::get(netuid),
3395+
AlphaCurrency::ZERO
3396+
);
3397+
assert_eq!(
3398+
PendingValidatorEmission::<Test>::get(netuid),
3399+
AlphaCurrency::ZERO
3400+
);
3401+
assert_eq!(
3402+
PendingRootAlphaDivs::<Test>::get(netuid),
3403+
AlphaCurrency::ZERO
3404+
);
3405+
});
3406+
}

0 commit comments

Comments
 (0)