Skip to content

Commit ffe3cc0

Browse files
authored
Merge pull request #1205 from opentensor/feat/identity-upgrades
Feat/RPC Upgrades
2 parents 8c8bc16 + 6c946db commit ffe3cc0

File tree

16 files changed

+676
-120
lines changed

16 files changed

+676
-120
lines changed

pallets/subtensor/src/coinbase/root.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ impl<T: Config> Pallet<T> {
479479
);
480480

481481
// --- 4. Remove the subnet identity if it exists.
482-
if SubnetIdentities::<T>::take(netuid).is_some() {
482+
if SubnetIdentitiesV2::<T>::take(netuid).is_some() {
483483
Self::deposit_event(Event::SubnetIdentityRemoved(netuid));
484484
}
485485

@@ -590,8 +590,8 @@ impl<T: Config> Pallet<T> {
590590
SubnetOwner::<T>::remove(netuid);
591591

592592
// --- 13. Remove subnet identity if it exists.
593-
if SubnetIdentities::<T>::contains_key(netuid) {
594-
SubnetIdentities::<T>::remove(netuid);
593+
if SubnetIdentitiesV2::<T>::contains_key(netuid) {
594+
SubnetIdentitiesV2::<T>::remove(netuid);
595595
Self::deposit_event(Event::SubnetIdentityRemoved(netuid));
596596
}
597597
}

pallets/subtensor/src/lib.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ pub mod pallet {
189189
pub ip_type: u8,
190190
}
191191

192-
/// Struct for ChainIdentities.
192+
/// Struct for ChainIdentities. (DEPRECATED for V2)
193193
pub type ChainIdentityOf = ChainIdentity;
194194

195-
/// Data structure for Chain Identities.
195+
/// Data structure for Chain Identities. (DEPRECATED for V2)
196196
#[crate::freeze_struct("bbfd00438dbe2b58")]
197197
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
198198
pub struct ChainIdentity {
@@ -210,9 +210,32 @@ pub mod pallet {
210210
pub additional: Vec<u8>,
211211
}
212212

213-
/// Struct for SubnetIdentities.
213+
/// Struct for ChainIdentities.
214+
pub type ChainIdentityOfV2 = ChainIdentityV2;
215+
216+
/// Data structure for Chain Identities.
217+
#[crate::freeze_struct("ad72a270be7b59d7")]
218+
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
219+
pub struct ChainIdentityV2 {
220+
/// The name of the chain identity
221+
pub name: Vec<u8>,
222+
/// The URL associated with the chain identity
223+
pub url: Vec<u8>,
224+
/// The github repository associated with the identity
225+
pub github_repo: Vec<u8>,
226+
/// The image representation of the chain identity
227+
pub image: Vec<u8>,
228+
/// The Discord information for the chain identity
229+
pub discord: Vec<u8>,
230+
/// A description of the chain identity
231+
pub description: Vec<u8>,
232+
/// Additional information about the chain identity
233+
pub additional: Vec<u8>,
234+
}
235+
236+
/// Struct for SubnetIdentities. (DEPRECATED for V2)
214237
pub type SubnetIdentityOf = SubnetIdentity;
215-
/// Data structure for Subnet Identities
238+
/// Data structure for Subnet Identities. (DEPRECATED for V2)
216239
#[crate::freeze_struct("f448dc3dad763108")]
217240
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
218241
pub struct SubnetIdentity {
@@ -223,6 +246,28 @@ pub mod pallet {
223246
/// The subnet's contact
224247
pub subnet_contact: Vec<u8>,
225248
}
249+
250+
/// Struct for SubnetIdentitiesV2.
251+
pub type SubnetIdentityOfV2 = SubnetIdentityV2;
252+
/// Data structure for Subnet Identities
253+
#[crate::freeze_struct("e002be4cd05d7b3e")]
254+
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
255+
pub struct SubnetIdentityV2 {
256+
/// The name of the subnet
257+
pub subnet_name: Vec<u8>,
258+
/// The github repository associated with the subnet
259+
pub github_repo: Vec<u8>,
260+
/// The subnet's contact
261+
pub subnet_contact: Vec<u8>,
262+
/// The subnet's website
263+
pub subnet_url: Vec<u8>,
264+
/// The subnet's discord
265+
pub discord: Vec<u8>,
266+
/// The subnet's description
267+
pub description: Vec<u8>,
268+
/// Additional information about the subnet
269+
pub additional: Vec<u8>,
270+
}
226271
/// ============================
227272
/// ==== Staking + Accounts ====
228273
/// ============================
@@ -1426,14 +1471,22 @@ pub mod pallet {
14261471
PrometheusInfoOf,
14271472
OptionQuery,
14281473
>;
1429-
#[pallet::storage] // --- MAP ( coldkey ) --> identity
1474+
#[pallet::storage] // --- MAP ( coldkey ) --> identity. (DEPRECATED for V2)
14301475
pub type Identities<T: Config> =
14311476
StorageMap<_, Blake2_128Concat, T::AccountId, ChainIdentityOf, OptionQuery>;
14321477

1433-
#[pallet::storage] // --- MAP ( netuid ) --> identity
1478+
#[pallet::storage] // --- MAP ( coldkey ) --> identity
1479+
pub type IdentitiesV2<T: Config> =
1480+
StorageMap<_, Blake2_128Concat, T::AccountId, ChainIdentityOfV2, OptionQuery>;
1481+
1482+
#[pallet::storage] // --- MAP ( netuid ) --> identity. (DEPRECATED for V2)
14341483
pub type SubnetIdentities<T: Config> =
14351484
StorageMap<_, Blake2_128Concat, u16, SubnetIdentityOf, OptionQuery>;
14361485

1486+
#[pallet::storage] // --- MAP ( netuid ) --> identityV2
1487+
pub type SubnetIdentitiesV2<T: Config> =
1488+
StorageMap<_, Blake2_128Concat, u16, SubnetIdentityOfV2, OptionQuery>;
1489+
14371490
/// =================================
14381491
/// ==== Axon / Promo Endpoints =====
14391492
/// =================================

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,12 +1453,22 @@ mod dispatches {
14531453
origin: OriginFor<T>,
14541454
name: Vec<u8>,
14551455
url: Vec<u8>,
1456+
github_repo: Vec<u8>,
14561457
image: Vec<u8>,
14571458
discord: Vec<u8>,
14581459
description: Vec<u8>,
14591460
additional: Vec<u8>,
14601461
) -> DispatchResult {
1461-
Self::do_set_identity(origin, name, url, image, discord, description, additional)
1462+
Self::do_set_identity(
1463+
origin,
1464+
name,
1465+
url,
1466+
github_repo,
1467+
image,
1468+
discord,
1469+
description,
1470+
additional,
1471+
)
14621472
}
14631473

14641474
/// ---- Set the identity information for a subnet.
@@ -1487,8 +1497,22 @@ mod dispatches {
14871497
subnet_name: Vec<u8>,
14881498
github_repo: Vec<u8>,
14891499
subnet_contact: Vec<u8>,
1500+
subnet_url: Vec<u8>,
1501+
discord: Vec<u8>,
1502+
description: Vec<u8>,
1503+
additional: Vec<u8>,
14901504
) -> DispatchResult {
1491-
Self::do_set_subnet_identity(origin, netuid, subnet_name, github_repo, subnet_contact)
1505+
Self::do_set_subnet_identity(
1506+
origin,
1507+
netuid,
1508+
subnet_name,
1509+
github_repo,
1510+
subnet_contact,
1511+
subnet_url,
1512+
discord,
1513+
description,
1514+
additional,
1515+
)
14921516
}
14931517

14941518
/// User register a new subnetwork
@@ -1499,7 +1523,7 @@ mod dispatches {
14991523
pub fn register_network_with_identity(
15001524
origin: OriginFor<T>,
15011525
hotkey: T::AccountId,
1502-
identity: Option<SubnetIdentityOf>,
1526+
identity: Option<SubnetIdentityOfV2>,
15031527
) -> DispatchResult {
15041528
Self::do_register_network(origin, &hotkey, 1, identity)
15051529
}

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ mod hooks {
7777
.saturating_add(migrations::migrate_rao::migrate_rao::<T>())
7878
// Fix the IsNetworkMember map to be consistent with other storage maps
7979
.saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::<T>())
80-
.saturating_add(migrations::migrate_subnet_volume::migrate_subnet_volume::<T>());
80+
.saturating_add(migrations::migrate_subnet_volume::migrate_subnet_volume::<T>())
81+
// Upgrade identities to V2
82+
.saturating_add(migrations::migrate_identities_v2::migrate_identities_to_v2::<T>());
8183
weight
8284
}
8385

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use super::*;
2+
use frame_support::weights::Weight;
3+
use log;
4+
use scale_info::prelude::{string::String, vec::Vec};
5+
6+
pub fn migrate_identities_to_v2<T: Config>() -> Weight {
7+
use frame_support::traits::Get;
8+
let migration_name = b"migrate_identities_to_v2".to_vec();
9+
10+
// Start counting weight
11+
let mut weight = T::DbWeight::get().reads(1);
12+
13+
// Check if we already ran this migration
14+
if HasMigrationRun::<T>::get(&migration_name) {
15+
log::info!(
16+
target: "runtime",
17+
"Migration '{:?}' has already run. Skipping.",
18+
String::from_utf8_lossy(&migration_name)
19+
);
20+
return weight;
21+
}
22+
23+
log::info!(
24+
target: "runtime",
25+
"Running migration '{}'",
26+
String::from_utf8_lossy(&migration_name)
27+
);
28+
29+
// -----------------------------
30+
// 1) Migrate Chain Identities
31+
// -----------------------------
32+
let old_identities = Identities::<T>::iter().collect::<Vec<_>>();
33+
for (account_id, old_identity) in old_identities.clone() {
34+
let new_identity = ChainIdentityV2 {
35+
name: old_identity.name,
36+
url: old_identity.url,
37+
github_repo: Vec::new(),
38+
image: old_identity.image,
39+
discord: old_identity.discord,
40+
description: old_identity.description,
41+
additional: old_identity.additional,
42+
};
43+
44+
// Insert into the new storage map
45+
IdentitiesV2::<T>::insert(&account_id, &new_identity);
46+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
47+
48+
Identities::<T>::remove(&account_id);
49+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
50+
}
51+
52+
weight = weight.saturating_add(T::DbWeight::get().reads(old_identities.len() as u64));
53+
54+
// -----------------------------
55+
// 2) Migrate Subnet Identities
56+
// -----------------------------
57+
let old_subnet_identities = SubnetIdentities::<T>::iter().collect::<Vec<_>>();
58+
for (netuid, old_subnet_identity) in old_subnet_identities.clone() {
59+
let new_subnet_identity = SubnetIdentityV2 {
60+
subnet_name: old_subnet_identity.subnet_name,
61+
github_repo: old_subnet_identity.github_repo,
62+
subnet_contact: old_subnet_identity.subnet_contact,
63+
subnet_url: Vec::new(),
64+
discord: Vec::new(),
65+
description: Vec::new(),
66+
additional: Vec::new(),
67+
};
68+
69+
// Insert into the new storage map
70+
SubnetIdentitiesV2::<T>::insert(netuid, &new_subnet_identity);
71+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
72+
73+
SubnetIdentities::<T>::remove(netuid);
74+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
75+
}
76+
weight = weight.saturating_add(T::DbWeight::get().reads(old_subnet_identities.len() as u64));
77+
78+
// -----------------------------
79+
// Mark the migration as done
80+
// -----------------------------
81+
HasMigrationRun::<T>::insert(&migration_name, true);
82+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
83+
84+
log::info!(
85+
target: "runtime",
86+
"Migration '{}' completed successfully.",
87+
String::from_utf8_lossy(&migration_name)
88+
);
89+
90+
weight
91+
}

pallets/subtensor/src/migrations/migrate_rao.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,20 @@ pub fn migrate_rao<T: Config>() -> Weight {
107107
let _neuron_uid: u16 = Pallet::<T>::register_neuron(*netuid, &owner_coldkey);
108108
}
109109
// Register the neuron immediately.
110-
if !Identities::<T>::contains_key(owner_coldkey.clone()) {
110+
if !IdentitiesV2::<T>::contains_key(owner_coldkey.clone()) {
111111
// Set the identitiy for the Owner coldkey if non existent.
112-
let identity = ChainIdentityOf {
112+
let identity = ChainIdentityOfV2 {
113113
name: format!("Owner{}", netuid).as_bytes().to_vec(),
114114
url: Vec::new(),
115115
image: Vec::new(),
116+
github_repo: Vec::new(),
116117
discord: Vec::new(),
117118
description: Vec::new(),
118119
additional: Vec::new(),
119120
};
120121
// Validate the created identity and set it.
121122
if Pallet::<T>::is_valid_identity(&identity) {
122-
Identities::<T>::insert(owner_coldkey.clone(), identity.clone());
123+
IdentitiesV2::<T>::insert(owner_coldkey.clone(), identity.clone());
123124
}
124125
}
125126
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod migrate_delete_subnet_21;
66
pub mod migrate_delete_subnet_3;
77
pub mod migrate_fix_is_network_member;
88
pub mod migrate_fix_total_coldkey_stake;
9+
pub mod migrate_identities_v2;
910
pub mod migrate_init_total_issuance;
1011
pub mod migrate_populate_owned_hotkeys;
1112
pub mod migrate_populate_staking_hotkeys;

pallets/subtensor/src/rpc_info/dynamic_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use codec::Compact;
44
use frame_support::pallet_prelude::{Decode, Encode};
55
use subtensor_macros::freeze_struct;
66

7-
#[freeze_struct("70be0b07db585696")]
7+
#[freeze_struct("f728ab9f6ffbf7f2")]
88
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
99
pub struct DynamicInfo<AccountId: TypeInfo + Encode + Decode> {
1010
netuid: Compact<u16>,
@@ -26,7 +26,7 @@ pub struct DynamicInfo<AccountId: TypeInfo + Encode + Decode> {
2626
pending_root_emission: Compact<u64>,
2727
subnet_volume: Compact<u128>,
2828
network_registered_at: Compact<u64>,
29-
subnet_identity: Option<SubnetIdentity>,
29+
subnet_identity: Option<SubnetIdentityV2>,
3030
}
3131

3232
impl<T: Config> Pallet<T> {
@@ -63,7 +63,7 @@ impl<T: Config> Pallet<T> {
6363
pending_root_emission: PendingRootDivs::<T>::get(netuid).into(),
6464
subnet_volume: SubnetVolume::<T>::get(netuid).into(),
6565
network_registered_at: NetworkRegisteredAt::<T>::get(netuid).into(),
66-
subnet_identity: SubnetIdentities::<T>::get(netuid),
66+
subnet_identity: SubnetIdentitiesV2::<T>::get(netuid),
6767
})
6868
}
6969
pub fn get_all_dynamic_info() -> Vec<Option<DynamicInfo<T::AccountId>>> {

0 commit comments

Comments
 (0)