Skip to content

Commit

Permalink
Merge pull request #1205 from opentensor/feat/identity-upgrades
Browse files Browse the repository at this point in the history
Feat/RPC Upgrades
  • Loading branch information
sam0x17 authored Feb 5, 2025
2 parents 8c8bc16 + 6c946db commit ffe3cc0
Show file tree
Hide file tree
Showing 16 changed files with 676 additions and 120 deletions.
6 changes: 3 additions & 3 deletions pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ impl<T: Config> Pallet<T> {
);

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

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

// --- 13. Remove subnet identity if it exists.
if SubnetIdentities::<T>::contains_key(netuid) {
SubnetIdentities::<T>::remove(netuid);
if SubnetIdentitiesV2::<T>::contains_key(netuid) {
SubnetIdentitiesV2::<T>::remove(netuid);
Self::deposit_event(Event::SubnetIdentityRemoved(netuid));
}
}
Expand Down
65 changes: 59 additions & 6 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ pub mod pallet {
pub ip_type: u8,
}

/// Struct for ChainIdentities.
/// Struct for ChainIdentities. (DEPRECATED for V2)
pub type ChainIdentityOf = ChainIdentity;

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

/// Struct for SubnetIdentities.
/// Struct for ChainIdentities.
pub type ChainIdentityOfV2 = ChainIdentityV2;

/// Data structure for Chain Identities.
#[crate::freeze_struct("ad72a270be7b59d7")]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct ChainIdentityV2 {
/// The name of the chain identity
pub name: Vec<u8>,
/// The URL associated with the chain identity
pub url: Vec<u8>,
/// The github repository associated with the identity
pub github_repo: Vec<u8>,
/// The image representation of the chain identity
pub image: Vec<u8>,
/// The Discord information for the chain identity
pub discord: Vec<u8>,
/// A description of the chain identity
pub description: Vec<u8>,
/// Additional information about the chain identity
pub additional: Vec<u8>,
}

/// Struct for SubnetIdentities. (DEPRECATED for V2)
pub type SubnetIdentityOf = SubnetIdentity;
/// Data structure for Subnet Identities
/// Data structure for Subnet Identities. (DEPRECATED for V2)
#[crate::freeze_struct("f448dc3dad763108")]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct SubnetIdentity {
Expand All @@ -223,6 +246,28 @@ pub mod pallet {
/// The subnet's contact
pub subnet_contact: Vec<u8>,
}

/// Struct for SubnetIdentitiesV2.
pub type SubnetIdentityOfV2 = SubnetIdentityV2;
/// Data structure for Subnet Identities
#[crate::freeze_struct("e002be4cd05d7b3e")]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct SubnetIdentityV2 {
/// The name of the subnet
pub subnet_name: Vec<u8>,
/// The github repository associated with the subnet
pub github_repo: Vec<u8>,
/// The subnet's contact
pub subnet_contact: Vec<u8>,
/// The subnet's website
pub subnet_url: Vec<u8>,
/// The subnet's discord
pub discord: Vec<u8>,
/// The subnet's description
pub description: Vec<u8>,
/// Additional information about the subnet
pub additional: Vec<u8>,
}
/// ============================
/// ==== Staking + Accounts ====
/// ============================
Expand Down Expand Up @@ -1426,14 +1471,22 @@ pub mod pallet {
PrometheusInfoOf,
OptionQuery,
>;
#[pallet::storage] // --- MAP ( coldkey ) --> identity
#[pallet::storage] // --- MAP ( coldkey ) --> identity. (DEPRECATED for V2)
pub type Identities<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, ChainIdentityOf, OptionQuery>;

#[pallet::storage] // --- MAP ( netuid ) --> identity
#[pallet::storage] // --- MAP ( coldkey ) --> identity
pub type IdentitiesV2<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, ChainIdentityOfV2, OptionQuery>;

#[pallet::storage] // --- MAP ( netuid ) --> identity. (DEPRECATED for V2)
pub type SubnetIdentities<T: Config> =
StorageMap<_, Blake2_128Concat, u16, SubnetIdentityOf, OptionQuery>;

#[pallet::storage] // --- MAP ( netuid ) --> identityV2
pub type SubnetIdentitiesV2<T: Config> =
StorageMap<_, Blake2_128Concat, u16, SubnetIdentityOfV2, OptionQuery>;

/// =================================
/// ==== Axon / Promo Endpoints =====
/// =================================
Expand Down
30 changes: 27 additions & 3 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,12 +1453,22 @@ mod dispatches {
origin: OriginFor<T>,
name: Vec<u8>,
url: Vec<u8>,
github_repo: Vec<u8>,
image: Vec<u8>,
discord: Vec<u8>,
description: Vec<u8>,
additional: Vec<u8>,
) -> DispatchResult {
Self::do_set_identity(origin, name, url, image, discord, description, additional)
Self::do_set_identity(
origin,
name,
url,
github_repo,
image,
discord,
description,
additional,
)
}

/// ---- Set the identity information for a subnet.
Expand Down Expand Up @@ -1487,8 +1497,22 @@ mod dispatches {
subnet_name: Vec<u8>,
github_repo: Vec<u8>,
subnet_contact: Vec<u8>,
subnet_url: Vec<u8>,
discord: Vec<u8>,
description: Vec<u8>,
additional: Vec<u8>,
) -> DispatchResult {
Self::do_set_subnet_identity(origin, netuid, subnet_name, github_repo, subnet_contact)
Self::do_set_subnet_identity(
origin,
netuid,
subnet_name,
github_repo,
subnet_contact,
subnet_url,
discord,
description,
additional,
)
}

/// User register a new subnetwork
Expand All @@ -1499,7 +1523,7 @@ mod dispatches {
pub fn register_network_with_identity(
origin: OriginFor<T>,
hotkey: T::AccountId,
identity: Option<SubnetIdentityOf>,
identity: Option<SubnetIdentityOfV2>,
) -> DispatchResult {
Self::do_register_network(origin, &hotkey, 1, identity)
}
Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ mod hooks {
.saturating_add(migrations::migrate_rao::migrate_rao::<T>())
// Fix the IsNetworkMember map to be consistent with other storage maps
.saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::<T>())
.saturating_add(migrations::migrate_subnet_volume::migrate_subnet_volume::<T>());
.saturating_add(migrations::migrate_subnet_volume::migrate_subnet_volume::<T>())
// Upgrade identities to V2
.saturating_add(migrations::migrate_identities_v2::migrate_identities_to_v2::<T>());
weight
}

Expand Down
91 changes: 91 additions & 0 deletions pallets/subtensor/src/migrations/migrate_identities_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use super::*;
use frame_support::weights::Weight;
use log;
use scale_info::prelude::{string::String, vec::Vec};

pub fn migrate_identities_to_v2<T: Config>() -> Weight {
use frame_support::traits::Get;
let migration_name = b"migrate_identities_to_v2".to_vec();

// Start counting weight
let mut weight = T::DbWeight::get().reads(1);

// Check if we already ran this migration
if HasMigrationRun::<T>::get(&migration_name) {
log::info!(
target: "runtime",
"Migration '{:?}' has already run. Skipping.",
String::from_utf8_lossy(&migration_name)
);
return weight;
}

log::info!(
target: "runtime",
"Running migration '{}'",
String::from_utf8_lossy(&migration_name)
);

// -----------------------------
// 1) Migrate Chain Identities
// -----------------------------
let old_identities = Identities::<T>::iter().collect::<Vec<_>>();
for (account_id, old_identity) in old_identities.clone() {
let new_identity = ChainIdentityV2 {
name: old_identity.name,
url: old_identity.url,
github_repo: Vec::new(),
image: old_identity.image,
discord: old_identity.discord,
description: old_identity.description,
additional: old_identity.additional,
};

// Insert into the new storage map
IdentitiesV2::<T>::insert(&account_id, &new_identity);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

Identities::<T>::remove(&account_id);
weight = weight.saturating_add(T::DbWeight::get().writes(1));
}

weight = weight.saturating_add(T::DbWeight::get().reads(old_identities.len() as u64));

// -----------------------------
// 2) Migrate Subnet Identities
// -----------------------------
let old_subnet_identities = SubnetIdentities::<T>::iter().collect::<Vec<_>>();
for (netuid, old_subnet_identity) in old_subnet_identities.clone() {
let new_subnet_identity = SubnetIdentityV2 {
subnet_name: old_subnet_identity.subnet_name,
github_repo: old_subnet_identity.github_repo,
subnet_contact: old_subnet_identity.subnet_contact,
subnet_url: Vec::new(),
discord: Vec::new(),
description: Vec::new(),
additional: Vec::new(),
};

// Insert into the new storage map
SubnetIdentitiesV2::<T>::insert(netuid, &new_subnet_identity);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

SubnetIdentities::<T>::remove(netuid);
weight = weight.saturating_add(T::DbWeight::get().writes(1));
}
weight = weight.saturating_add(T::DbWeight::get().reads(old_subnet_identities.len() as u64));

// -----------------------------
// Mark the migration as done
// -----------------------------
HasMigrationRun::<T>::insert(&migration_name, true);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

log::info!(
target: "runtime",
"Migration '{}' completed successfully.",
String::from_utf8_lossy(&migration_name)
);

weight
}
7 changes: 4 additions & 3 deletions pallets/subtensor/src/migrations/migrate_rao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,20 @@ pub fn migrate_rao<T: Config>() -> Weight {
let _neuron_uid: u16 = Pallet::<T>::register_neuron(*netuid, &owner_coldkey);
}
// Register the neuron immediately.
if !Identities::<T>::contains_key(owner_coldkey.clone()) {
if !IdentitiesV2::<T>::contains_key(owner_coldkey.clone()) {
// Set the identitiy for the Owner coldkey if non existent.
let identity = ChainIdentityOf {
let identity = ChainIdentityOfV2 {
name: format!("Owner{}", netuid).as_bytes().to_vec(),
url: Vec::new(),
image: Vec::new(),
github_repo: Vec::new(),
discord: Vec::new(),
description: Vec::new(),
additional: Vec::new(),
};
// Validate the created identity and set it.
if Pallet::<T>::is_valid_identity(&identity) {
Identities::<T>::insert(owner_coldkey.clone(), identity.clone());
IdentitiesV2::<T>::insert(owner_coldkey.clone(), identity.clone());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod migrate_delete_subnet_21;
pub mod migrate_delete_subnet_3;
pub mod migrate_fix_is_network_member;
pub mod migrate_fix_total_coldkey_stake;
pub mod migrate_identities_v2;
pub mod migrate_init_total_issuance;
pub mod migrate_populate_owned_hotkeys;
pub mod migrate_populate_staking_hotkeys;
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/src/rpc_info/dynamic_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use codec::Compact;
use frame_support::pallet_prelude::{Decode, Encode};
use subtensor_macros::freeze_struct;

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

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<T: Config> Pallet<T> {
pending_root_emission: PendingRootDivs::<T>::get(netuid).into(),
subnet_volume: SubnetVolume::<T>::get(netuid).into(),
network_registered_at: NetworkRegisteredAt::<T>::get(netuid).into(),
subnet_identity: SubnetIdentities::<T>::get(netuid),
subnet_identity: SubnetIdentitiesV2::<T>::get(netuid),
})
}
pub fn get_all_dynamic_info() -> Vec<Option<DynamicInfo<T::AccountId>>> {
Expand Down
Loading

0 comments on commit ffe3cc0

Please sign in to comment.