Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreea-popescu-reef committed Apr 4, 2024
1 parent 3cb512e commit e5ad0a0
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 2 deletions.
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
56 changes: 56 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 @@ -1167,6 +1187,7 @@ pub mod pallet {
))
.saturating_add(migration::migrate_delete_subnet_3::<T>())
.saturating_add(migration::migrate_delete_subnet_21::<T>());
// .saturating_add(migration::migrate_handle_neuron_certificate::<T>());
return weight;
}
}
Expand Down Expand Up @@ -1445,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 @@ -1868,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
12 changes: 12 additions & 0 deletions pallets/subtensor/src/serving.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::certificate::Certificate;
use frame_support::inherent::Vec;
use frame_support::sp_std::vec;

Expand Down Expand Up @@ -63,6 +64,7 @@ impl<T: Config> Pallet<T> {
protocol: u8,
placeholder1: u8,
placeholder2: u8,
certificate: Option<Certificate>,
) -> dispatch::DispatchResult {
// --- 1. We check the callers (hotkey) signature.
let hotkey_id = ensure_signed(origin)?;
Expand All @@ -88,6 +90,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 +249,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
52 changes: 52 additions & 0 deletions pallets/subtensor/tests/serving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use frame_system::Config;
use pallet_subtensor::Error;
use sp_core::U256;

type Certificate = Vec<u8>;

mod test {
use std::net::{Ipv4Addr, Ipv6Addr};

Expand Down Expand Up @@ -95,6 +97,56 @@ fn test_serving_ok() {
});
}

#[test]
fn test_serving_tls_ok() {
// use crate::certificate::Certificate;
new_test_ext().execute_with(|| {
let hotkey_account_id = U256::from(1);
let netuid: u16 = 1;
let tempo: u16 = 13;
let version: u32 = 2;
let ip: u128 = 1676056785;
let port: u16 = 128;
let ip_type: u8 = 4;
let modality: u16 = 0;
let protocol: u8 = 0;
let placeholder1: u8 = 0;
let placeholder2: u8 = 0;
let certificate: Certificate = "TEST".as_bytes().to_vec();
add_network(netuid, tempo, modality);
register_ok_neuron(netuid, hotkey_account_id, U256::from(66), 0);
assert_ok!(SubtensorModule::serve_axon_tls(
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
netuid,
version,
ip,
port,
ip_type,
protocol,
placeholder1,
placeholder2,
certificate.clone()
));
let stored_certificate = SubtensorModule::get_neuron_certificate(netuid, 1);
assert_eq!(stored_certificate.unwrap().certificate, certificate);
let new_certificate = "NEWTEST".as_bytes().to_vec();
assert_ok!(SubtensorModule::serve_axon_tls(
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
netuid,
version,
ip,
port,
ip_type,
protocol,
placeholder1,
placeholder2,
new_certificate.clone()
));
let stored_certificate = SubtensorModule::get_neuron_certificate(netuid, 1);
assert_eq!(stored_certificate.unwrap().certificate, new_certificate)
});
}

#[test]
fn test_serving_set_metadata_update() {
new_test_ext().execute_with(|| {
Expand Down
14 changes: 13 additions & 1 deletion 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 @@ -121,7 +122,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 144,
spec_version: 146,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
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 e5ad0a0

Please sign in to comment.