Skip to content

Commit

Permalink
add neuron certificate handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andreea-popescu-reef committed Apr 3, 2024
1 parent 09e0270 commit b4e04f5
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pallets/subtensor/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ pub trait SubtensorCustomApi<BlockHash> {
fn get_neurons(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "neuronInfo_getNeuron")]
fn get_neuron(&self, netuid: u16, uid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;

#[method(name = "neuronInfo_getNeuronCertificate")]
fn get_neuron_certificate(
&self,
netuid: u16,
uid: u16,
at: Option<BlockHash>,
) -> RpcResult<Vec<u8>>;
#[method(name = "subnetInfo_getSubnetInfo")]
fn get_subnet_info(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "subnetInfo_getSubnetsInfo")]
Expand Down Expand Up @@ -212,6 +218,25 @@ where
})
}

fn get_neuron_certificate(
&self,
netuid: u16,
uid: 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_neuron_certificate(at, netuid, uid).map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to get neuron certificate.",
Some(e.to_string()),
))
.into()
})
}

fn get_subnet_info(
&self,
netuid: u16,
Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sp_api::decl_runtime_apis! {
pub trait NeuronInfoRuntimeApi {
fn get_neurons(netuid: u16) -> Vec<u8>;
fn get_neuron(netuid: u16, uid: u16) -> Vec<u8>;
fn get_neuron_certificate(netuid: u16, uid: u16) -> Vec<u8>;
fn get_neurons_lite(netuid: u16) -> Vec<u8>;
fn get_neuron_lite(netuid: u16, uid: u16) -> Vec<u8>;
}
Expand Down
55 changes: 55 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod benchmarks;
// =========================
mod block_step;

mod certificate;
mod epoch;
mod math;
mod registration;
Expand All @@ -57,6 +58,7 @@ pub mod migration;
#[frame_support::pallet]
pub mod pallet {

use crate::certificate::Certificate;
use frame_support::{
dispatch::GetDispatchInfo,
inherent::Vec,
Expand Down Expand Up @@ -522,6 +524,14 @@ pub mod pallet {
pub placeholder2: u8, // --- Axon proto placeholder 1.
}

// --- Struct for NeuronCertificate.
pub type NeuronCertificateOf = NeuronCertificate;

#[derive(Decode, Encode, Default, TypeInfo, PartialEq, Eq, Clone, Debug)]
pub struct NeuronCertificate {
pub certificate: Certificate,
}

// --- Struct for Prometheus.
pub type PrometheusInfoOf = PrometheusInfo;
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -560,6 +570,16 @@ 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 ) --> certificate
pub(super) type NeuronCertificates<T: Config> = StorageDoubleMap<
_,
Identity,
u16,
Blake2_128Concat,
T::AccountId,
NeuronCertificateOf,
OptionQuery,
>;
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> prometheus_info
pub(super) type Prometheus<T: Config> = StorageDoubleMap<
_,
Expand Down Expand Up @@ -1446,6 +1466,37 @@ pub mod pallet {
protocol,
placeholder1,
placeholder2,
None,
)
}

#[pallet::call_index(100)]
#[pallet::weight((Weight::from_ref_time(19_000_000)
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::No))]
pub fn serve_axon_tls(
origin: OriginFor<T>,
netuid: u16,
version: u32,
ip: u128,
port: u16,
ip_type: u8,
protocol: u8,
placeholder1: u8,
placeholder2: u8,
certificate: Certificate,
) -> DispatchResult {
Self::do_serve_axon(
origin,
netuid,
version,
ip,
port,
ip_type,
protocol,
placeholder1,
placeholder2,
Some(certificate),
)
}

Expand Down Expand Up @@ -1869,6 +1920,10 @@ where
let transaction_fee = 0;
Ok((CallType::Serve, transaction_fee, who.clone()))
}
Some(Call::serve_axon_tls { .. }) => {
let transaction_fee = 0;
Ok((CallType::Serve, transaction_fee, who.clone()))
}
Some(Call::register_network { .. }) => {
let transaction_fee = 0;
Ok((CallType::RegisterNetwork, transaction_fee, who.clone()))
Expand Down
24 changes: 24 additions & 0 deletions pallets/subtensor/src/neuron_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ impl<T: Config> Pallet<T> {
neuron
}

pub fn get_neuron_certificate(netuid: u16, uid: u16) -> Option<NeuronCertificate> {
if !Self::if_subnet_exist(netuid) {
return None;
}

let _hotkey = Self::get_hotkey_for_net_and_uid(netuid, uid);
let hotkey;
if _hotkey.is_err() {
return None;
} else {
// No error, hotkey was registered
hotkey = _hotkey.expect("Hotkey should exist");
}

if Self::has_neuron_certificate(netuid, &hotkey) {
let certificate = NeuronCertificates::<T>::get(netuid, hotkey).unwrap();
log::error!("\n---------> get_neuron_certificate {:?}\n", certificate.clone());
Some(certificate)
} else {
log::error!("\n---------> get_neuron_certificate NONE\n");
None
}
}

fn get_neuron_lite_subnet_exists(netuid: u16, uid: u16) -> Option<NeuronInfoLite<T>> {
let _hotkey = Self::get_hotkey_for_net_and_uid(netuid, uid);
let hotkey;
Expand Down
10 changes: 10 additions & 0 deletions pallets/subtensor/src/serving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ impl<T: Config> Pallet<T> {
Error::<T>::ServingRateLimitExceeded
);

// --- 5. Check certificate
if let Some(certificate) = certificate {
log::error!("\n---------> do_serve_axon saving cert {:?}\n", certificate.clone());
NeuronCertificates::<T>::insert(netuid, hotkey_id.clone(), NeuronCertificate{certificate})
}

// --- 6. We insert the axon meta.
prev_axon.block = Self::get_current_block_as_u64();
prev_axon.version = version;
Expand Down Expand Up @@ -241,6 +247,10 @@ impl<T: Config> Pallet<T> {
return Axons::<T>::contains_key(netuid, hotkey);
}

pub fn has_neuron_certificate(netuid: u16, hotkey: &T::AccountId) -> bool {
return NeuronCertificates::<T>::contains_key(netuid, hotkey);
}

pub fn has_prometheus_info(netuid: u16, hotkey: &T::AccountId) -> bool {
return Prometheus::<T>::contains_key(netuid, hotkey);
}
Expand Down
12 changes: 12 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use codec::Encode;

use frame_support::log;
use pallet_commitments::CanCommit;
use pallet_grandpa::{
fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
Expand Down Expand Up @@ -1328,6 +1329,17 @@ impl_runtime_apis! {
vec![]
}
}

fn get_neuron_certificate(netuid: u16, uid: u16) -> Vec<u8> {
use frame_support::log;
let _result = SubtensorModule::get_neuron_certificate(netuid, uid);
if _result.is_some() {
let result = _result.expect("Could not get Certificate");
result.encode()
} else {
vec![]
}
}
}

impl subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi<Block> for Runtime {
Expand Down

0 comments on commit b4e04f5

Please sign in to comment.