Skip to content
1 change: 1 addition & 0 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ where
C::Api: subtensor_custom_rpc_runtime_api::DelegateInfoRuntimeApi<Block>,
C::Api: subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi<Block>,
C::Api: subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi<Block>,
C::Api: subtensor_custom_rpc_runtime_api::ValidatorIPRuntimeApi<Block>,
P: TransactionPool + 'static
{
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
Expand Down
20 changes: 20 additions & 0 deletions pallets/subtensor/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use sp_api::ProvideRuntimeApi;
pub use subtensor_custom_rpc_runtime_api::DelegateInfoRuntimeApi;
pub use subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi;
pub use subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi;
pub use subtensor_custom_rpc_runtime_api::ValidatorIPRuntimeApi;


#[rpc(client, server)]
pub trait SubtensorCustomApi<BlockHash> {
Expand Down Expand Up @@ -45,6 +47,9 @@ pub trait SubtensorCustomApi<BlockHash> {
fn get_subnet_info(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "subnetInfo_getSubnetsInfo")]
fn get_subnets_info(&self, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;

#[method(name = "validatorIP_getAssociatedValidatorIPInfoForSubnet")]
fn get_associated_validator_ip_info_for_subnet(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
}

pub struct SubtensorCustom<C, P> {
Expand Down Expand Up @@ -84,6 +89,7 @@ where
C::Api: DelegateInfoRuntimeApi<Block>,
C::Api: NeuronInfoRuntimeApi<Block>,
C::Api: SubnetInfoRuntimeApi<Block>,
C::Api: ValidatorIPRuntimeApi<Block>,
{
fn get_delegates(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
Expand Down Expand Up @@ -236,4 +242,18 @@ where
.into()
})
}

fn get_associated_validator_ip_info_for_subnet(&self, netuid: u16, at: Option<<Block as BlockT>::Hash>) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

api.get_associated_validator_ip_info_for_subnet(at, netuid).map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to get associated validator ip info for subnet.",
Some(e.to_string()),
))
.into()
})
}
}
4 changes: 4 additions & 0 deletions pallets/subtensor/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ sp_api::decl_runtime_apis! {
fn get_subnets_info() -> Vec<u8>;
}

pub trait ValidatorIPRuntimeApi {
fn get_associated_validator_ip_info_for_subnet(netuid: u16) -> Vec<u8>;
}

pub trait StakeInfoRuntimeApi {
fn get_stake_info_for_coldkey( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
fn get_stake_info_for_coldkeys( coldkey_account_vecs: Vec<Vec<u8>> ) -> Vec<u8>;
Expand Down
32 changes: 31 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use frame_support::{
traits::{tokens::WithdrawReasons, Currency, ExistenceRequirement, IsSubType},
};

use codec::{Decode, Encode};
use codec::{Decode, Encode, Compact};
use frame_support::sp_runtime::transaction_validity::ValidTransaction;
use scale_info::TypeInfo;
use sp_runtime::{
Expand Down Expand Up @@ -64,6 +64,7 @@ pub mod pallet {
pallet_prelude::{DispatchResult, StorageMap, *},
};
use frame_system::pallet_prelude::*;
use codec::Compact;

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
Expand Down Expand Up @@ -166,6 +167,12 @@ pub mod pallet {
type InitialSenateRequiredStakePercentage: Get<u64>;
#[pallet::constant] // Initial adjustment alpha on burn and pow.
type InitialAdjustmentAlpha: Get<u64>;

// =================================
// ===== Bounded Vec Max Sizes =====
// =================================
#[pallet::constant] // Maximum number of associated IPs per validator hotkey.
type AssociatedIPsMaxSize: Get<u32>;
}

pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
Expand Down Expand Up @@ -426,6 +433,16 @@ pub mod pallet {
pub placeholder2: u8, // --- Axon proto placeholder 1.
}

// --- Struct for IP Info.
pub type IPInfoOf = IPInfo;
#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct IPInfo {
pub ip: Compact<u128>, // --- u128 encoded IP address of type v6 or v4.
pub ip_type_and_protocol: Compact<u8>, // Combined IP Type and Protocol. High bits are IP type, low bits are protocol.
// --- IP type, 4 for ipv4 and 6 for ipv6. (4-bits)
// --- Axon protocol. TCP, UDP, other. (4-bits)
}

// --- Struct for Prometheus.
pub type PrometheusInfoOf = PrometheusInfo;
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -464,6 +481,9 @@ pub mod pallet {
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> axon_info
pub(super) type Axons<T: Config> =
StorageDoubleMap<_, Identity, u16, Blake2_128Concat, T::AccountId, AxonInfoOf, OptionQuery>;
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> (block_last_updated, Vec<ip_info>)
pub(super) type AssociatedIPInfo<T: Config> =
StorageDoubleMap<_, Identity, u16, Blake2_128Concat, T::AccountId, (u64, BoundedVec<IPInfoOf, T::AssociatedIPsMaxSize>), OptionQuery>;
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> prometheus_info
pub(super) type Prometheus<T: Config> = StorageDoubleMap<
_,
Expand Down Expand Up @@ -762,6 +782,7 @@ pub mod pallet {
RAORecycledForRegistrationSet(u16, u64), // Event created when setting the RAO recycled for registration.
SenateRequiredStakePercentSet(u64), // Event created when setting the minimum required stake amount for senate registration.
AdjustmentAlphaSet(u16, u64), // Event created when setting the adjustment alpha on a subnet.
IPInfoSet(u16, T::AccountId, Vec<IPInfoOf>), // Event created when setting the IP info for a neuron.
}

// Errors inform users that something went wrong.
Expand Down Expand Up @@ -818,6 +839,7 @@ pub mod pallet {
BelowStakeThreshold, // --- Thrown when a hotkey attempts to join the senate without enough stake
NotDelegate, // --- Thrown when a hotkey attempts to join the senate without being a delegate first
IncorrectNetuidsLength, // --- Thrown when an incorrect amount of Netuids are passed as input
AssociatedIPsMaxSizeExceeded, // --- Thrown when the number of associated IPs exceeds the maximum allowed
}

// ==================
Expand Down Expand Up @@ -1289,6 +1311,14 @@ pub mod pallet {
Self::do_serve_prometheus(origin, netuid, version, ip, port, ip_type)
}

#[pallet::call_index(59)]
#[pallet::weight((Weight::from_ref_time(15_000_000)
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::No))]
pub fn associate_ips(origin: OriginFor<T>, netuid: u16, ip_info_list: Vec<IPInfoOf>) -> DispatchResult {
Self::do_associate_ips(origin, netuid, ip_info_list)
}

// ---- Registers a new neuron to the subnetwork.
//
// # Args:
Expand Down
Loading