Skip to content

Commit c4f92ad

Browse files
committed
fix conflict
2 parents fbefae9 + b97bb55 commit c4f92ad

23 files changed

+1031
-344
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/benchmarks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,11 +1615,11 @@ mod pallet_benchmarks {
16151615
);
16161616

16171617
let pending_root_alpha = 10_000_000u64;
1618-
Subtensor::<T>::drain_pending_emission(
1618+
Subtensor::<T>::distribute_emission(
16191619
netuid,
16201620
AlphaCurrency::ZERO,
16211621
pending_root_alpha.into(),
1622-
pending_root_alpha.into(), // alpha out
1622+
pending_root_alpha.into(),
16231623
AlphaCurrency::ZERO,
16241624
);
16251625

pallets/subtensor/src/coinbase/root.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ impl<T: Config> Pallet<T> {
314314

315315
// --- 15. Mechanism step / emissions bookkeeping.
316316
FirstEmissionBlockNumber::<T>::remove(netuid);
317-
PendingEmission::<T>::remove(netuid);
317+
PendingValidatorEmission::<T>::remove(netuid);
318+
PendingServerEmission::<T>::remove(netuid);
318319
PendingRootAlphaDivs::<T>::remove(netuid);
319320
PendingOwnerCut::<T>::remove(netuid);
320321
BlocksSinceLastStep::<T>::remove(netuid);

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 122 additions & 80 deletions
Large diffs are not rendered by default.

pallets/subtensor/src/coinbase/subnet_emissions.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
use super::*;
2-
use crate::alloc::borrow::ToOwned;
32
use alloc::collections::BTreeMap;
43
use safe_math::FixedExt;
54
use substrate_fixed::transcendental::{exp, ln};
65
use substrate_fixed::types::{I32F32, I64F64, U64F64, U96F32};
76
use subtensor_swap_interface::SwapHandler;
87

98
impl<T: Config> Pallet<T> {
10-
pub fn get_subnet_block_emissions(
11-
subnets: &[NetUid],
12-
block_emission: U96F32,
13-
) -> BTreeMap<NetUid, U96F32> {
9+
pub fn get_subnets_to_emit_to(subnets: &[NetUid]) -> Vec<NetUid> {
1410
// Filter out subnets with no first emission block number.
15-
let subnets_to_emit_to: Vec<NetUid> = subnets
16-
.to_owned()
17-
.clone()
18-
.into_iter()
11+
subnets
12+
.iter()
1913
.filter(|netuid| FirstEmissionBlockNumber::<T>::get(*netuid).is_some())
20-
.collect();
21-
log::debug!("Subnets to emit to: {subnets_to_emit_to:?}");
14+
.copied()
15+
.collect()
16+
}
2217

18+
pub fn get_subnet_block_emissions(
19+
subnets_to_emit_to: &[NetUid],
20+
block_emission: U96F32,
21+
) -> BTreeMap<NetUid, U96F32> {
2322
// Get subnet TAO emissions.
24-
let shares = Self::get_shares(&subnets_to_emit_to);
23+
let shares = Self::get_shares(subnets_to_emit_to);
2524
log::debug!("Subnet emission shares = {shares:?}");
2625

2726
shares

pallets/subtensor/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,9 +1430,13 @@ pub mod pallet {
14301430
pub type NetworkRegisteredAt<T: Config> =
14311431
StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultNetworkRegisteredAt<T>>;
14321432
#[pallet::storage]
1433-
/// --- MAP ( netuid ) --> pending_emission
1434-
pub type PendingEmission<T> =
1435-
StorageMap<_, Identity, NetUid, AlphaCurrency, ValueQuery, DefaultPendingEmission<T>>;
1433+
/// --- MAP ( netuid ) --> pending_server_emission
1434+
pub type PendingServerEmission<T> =
1435+
StorageMap<_, Identity, NetUid, AlphaCurrency, ValueQuery, DefaultZeroAlpha<T>>;
1436+
/// --- MAP ( netuid ) --> pending_validator_emission
1437+
#[pallet::storage]
1438+
pub type PendingValidatorEmission<T> =
1439+
StorageMap<_, Identity, NetUid, AlphaCurrency, ValueQuery, DefaultZeroAlpha<T>>;
14361440
/// --- MAP ( netuid ) --> pending_root_alpha_emission
14371441
#[pallet::storage]
14381442
pub type PendingRootAlphaDivs<T> =

pallets/subtensor/src/macros/hooks.rs

Lines changed: 5 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>())
@@ -161,7 +158,11 @@ mod hooks {
161158
// Remove obsolete map entries
162159
.saturating_add(migrations::migrate_remove_tao_dividends::migrate_remove_tao_dividends::<T>())
163160
// Re-init tao flows
164-
.saturating_add(migrations::migrate_init_tao_flow::migrate_init_tao_flow::<T>());
161+
.saturating_add(migrations::migrate_init_tao_flow::migrate_init_tao_flow::<T>())
162+
// Migrate pending emissions
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>());
165166
weight
166167
}
167168

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use super::*;
2+
use frame_support::{storage_alias, traits::Get, weights::Weight};
3+
use substrate_fixed::types::U96F32;
4+
5+
pub mod deprecated_pending_emission_format {
6+
use super::*;
7+
8+
#[storage_alias]
9+
pub(super) type PendingEmission<T: Config> =
10+
StorageMap<Pallet<T>, Identity, NetUid, AlphaCurrency, ValueQuery>;
11+
}
12+
13+
pub fn migrate_pending_emissions<T: Config>() -> Weight {
14+
let migration_name = b"migrate_pending_emissions".to_vec();
15+
let mut weight: Weight = T::DbWeight::get().reads(1);
16+
17+
// Skip if already executed
18+
if HasMigrationRun::<T>::get(&migration_name) {
19+
log::info!(
20+
target: "runtime",
21+
"Migration '{}' already run - skipping.",
22+
String::from_utf8_lossy(&migration_name)
23+
);
24+
return weight;
25+
}
26+
log::info!(
27+
"Running migration '{}'",
28+
String::from_utf8_lossy(&migration_name)
29+
);
30+
31+
// Pull from PendingEmission and distribute to PendingValidatorEmission and PendingServerEmission
32+
for (netuid, pending_emission) in
33+
deprecated_pending_emission_format::PendingEmission::<T>::iter()
34+
{
35+
// Split up the pending emission into server and validator emission
36+
// Server emission is pending+root_alpha times the 50% miner cut.
37+
let root_alpha: U96F32 =
38+
U96F32::saturating_from_num(PendingRootAlphaDivs::<T>::get(netuid).to_u64());
39+
let server_emission_float: U96F32 = U96F32::saturating_from_num(pending_emission.to_u64())
40+
.saturating_add(root_alpha)
41+
.saturating_div(U96F32::saturating_from_num(2));
42+
let server_emission: AlphaCurrency =
43+
server_emission_float.saturating_to_num::<u64>().into();
44+
let validator_emission = pending_emission.saturating_sub(server_emission);
45+
46+
PendingValidatorEmission::<T>::mutate(netuid, |total| {
47+
*total = total.saturating_add(validator_emission)
48+
});
49+
PendingServerEmission::<T>::mutate(netuid, |total| {
50+
*total = total.saturating_add(server_emission)
51+
});
52+
53+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2));
54+
}
55+
56+
// Kill the map
57+
let removal_result =
58+
deprecated_pending_emission_format::PendingEmission::<T>::clear(u32::MAX, None);
59+
weight = weight.saturating_add(
60+
T::DbWeight::get().reads_writes(removal_result.loops as u64, removal_result.backend as u64),
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+
}
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+
}

0 commit comments

Comments
 (0)