Skip to content

Commit e5ad0a0

Browse files
wip
1 parent 3cb512e commit e5ad0a0

File tree

7 files changed

+184
-2
lines changed

7 files changed

+184
-2
lines changed

pallets/subtensor/rpc/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ pub trait SubtensorCustomApi<BlockHash> {
4141
fn get_neurons(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
4242
#[method(name = "neuronInfo_getNeuron")]
4343
fn get_neuron(&self, netuid: u16, uid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
44-
44+
#[method(name = "neuronInfo_getNeuronCertificate")]
45+
fn get_neuron_certificate(
46+
&self,
47+
netuid: u16,
48+
uid: u16,
49+
at: Option<BlockHash>,
50+
) -> RpcResult<Vec<u8>>;
4551
#[method(name = "subnetInfo_getSubnetInfo")]
4652
fn get_subnet_info(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
4753
#[method(name = "subnetInfo_getSubnetsInfo")]
@@ -212,6 +218,25 @@ where
212218
})
213219
}
214220

221+
fn get_neuron_certificate(
222+
&self,
223+
netuid: u16,
224+
uid: u16,
225+
at: Option<<Block as BlockT>::Hash>,
226+
) -> RpcResult<Vec<u8>> {
227+
let api = self.client.runtime_api();
228+
let at = at.unwrap_or_else(|| self.client.info().best_hash);
229+
230+
api.get_neuron_certificate(at, netuid, uid).map_err(|e| {
231+
CallError::Custom(ErrorObject::owned(
232+
Error::RuntimeError.into(),
233+
"Unable to get neuron certificate.",
234+
Some(e.to_string()),
235+
))
236+
.into()
237+
})
238+
}
239+
215240
fn get_subnet_info(
216241
&self,
217242
netuid: u16,

pallets/subtensor/runtime-api/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sp_api::decl_runtime_apis! {
1414
pub trait NeuronInfoRuntimeApi {
1515
fn get_neurons(netuid: u16) -> Vec<u8>;
1616
fn get_neuron(netuid: u16, uid: u16) -> Vec<u8>;
17+
fn get_neuron_certificate(netuid: u16, uid: u16) -> Vec<u8>;
1718
fn get_neurons_lite(netuid: u16) -> Vec<u8>;
1819
fn get_neuron_lite(netuid: u16, uid: u16) -> Vec<u8>;
1920
}

pallets/subtensor/src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod benchmarks;
3535
// =========================
3636
mod block_step;
3737

38+
mod certificate;
3839
mod epoch;
3940
mod math;
4041
mod registration;
@@ -57,6 +58,7 @@ pub mod migration;
5758
#[frame_support::pallet]
5859
pub mod pallet {
5960

61+
use crate::certificate::Certificate;
6062
use frame_support::{
6163
dispatch::GetDispatchInfo,
6264
inherent::Vec,
@@ -522,6 +524,14 @@ pub mod pallet {
522524
pub placeholder2: u8, // --- Axon proto placeholder 1.
523525
}
524526

527+
// --- Struct for NeuronCertificate.
528+
pub type NeuronCertificateOf = NeuronCertificate;
529+
530+
#[derive(Decode, Encode, Default, TypeInfo, PartialEq, Eq, Clone, Debug)]
531+
pub struct NeuronCertificate {
532+
pub certificate: Certificate,
533+
}
534+
525535
// --- Struct for Prometheus.
526536
pub type PrometheusInfoOf = PrometheusInfo;
527537
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
@@ -560,6 +570,16 @@ pub mod pallet {
560570
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> axon_info
561571
pub(super) type Axons<T: Config> =
562572
StorageDoubleMap<_, Identity, u16, Blake2_128Concat, T::AccountId, AxonInfoOf, OptionQuery>;
573+
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> certificate
574+
pub(super) type NeuronCertificates<T: Config> = StorageDoubleMap<
575+
_,
576+
Identity,
577+
u16,
578+
Blake2_128Concat,
579+
T::AccountId,
580+
NeuronCertificateOf,
581+
OptionQuery,
582+
>;
563583
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> prometheus_info
564584
pub(super) type Prometheus<T: Config> = StorageDoubleMap<
565585
_,
@@ -1167,6 +1187,7 @@ pub mod pallet {
11671187
))
11681188
.saturating_add(migration::migrate_delete_subnet_3::<T>())
11691189
.saturating_add(migration::migrate_delete_subnet_21::<T>());
1190+
// .saturating_add(migration::migrate_handle_neuron_certificate::<T>());
11701191
return weight;
11711192
}
11721193
}
@@ -1445,6 +1466,37 @@ pub mod pallet {
14451466
protocol,
14461467
placeholder1,
14471468
placeholder2,
1469+
None,
1470+
)
1471+
}
1472+
1473+
#[pallet::call_index(100)]
1474+
#[pallet::weight((Weight::from_ref_time(19_000_000)
1475+
.saturating_add(T::DbWeight::get().reads(2))
1476+
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::No))]
1477+
pub fn serve_axon_tls(
1478+
origin: OriginFor<T>,
1479+
netuid: u16,
1480+
version: u32,
1481+
ip: u128,
1482+
port: u16,
1483+
ip_type: u8,
1484+
protocol: u8,
1485+
placeholder1: u8,
1486+
placeholder2: u8,
1487+
certificate: Certificate,
1488+
) -> DispatchResult {
1489+
Self::do_serve_axon(
1490+
origin,
1491+
netuid,
1492+
version,
1493+
ip,
1494+
port,
1495+
ip_type,
1496+
protocol,
1497+
placeholder1,
1498+
placeholder2,
1499+
Some(certificate),
14481500
)
14491501
}
14501502

@@ -1868,6 +1920,10 @@ where
18681920
let transaction_fee = 0;
18691921
Ok((CallType::Serve, transaction_fee, who.clone()))
18701922
}
1923+
Some(Call::serve_axon_tls { .. }) => {
1924+
let transaction_fee = 0;
1925+
Ok((CallType::Serve, transaction_fee, who.clone()))
1926+
}
18711927
Some(Call::register_network { .. }) => {
18721928
let transaction_fee = 0;
18731929
Ok((CallType::RegisterNetwork, transaction_fee, who.clone()))

pallets/subtensor/src/neuron_info.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,30 @@ impl<T: Config> Pallet<T> {
170170
neuron
171171
}
172172

173+
pub fn get_neuron_certificate(netuid: u16, uid: u16) -> Option<NeuronCertificate> {
174+
if !Self::if_subnet_exist(netuid) {
175+
return None;
176+
}
177+
178+
let _hotkey = Self::get_hotkey_for_net_and_uid(netuid, uid);
179+
let hotkey;
180+
if _hotkey.is_err() {
181+
return None;
182+
} else {
183+
// No error, hotkey was registered
184+
hotkey = _hotkey.expect("Hotkey should exist");
185+
}
186+
187+
if Self::has_neuron_certificate(netuid, &hotkey) {
188+
let certificate = NeuronCertificates::<T>::get(netuid, hotkey).unwrap();
189+
log::error!("\n---------> get_neuron_certificate {:?}\n", certificate.clone());
190+
Some(certificate)
191+
} else {
192+
log::error!("\n---------> get_neuron_certificate NONE\n");
193+
None
194+
}
195+
}
196+
173197
fn get_neuron_lite_subnet_exists(netuid: u16, uid: u16) -> Option<NeuronInfoLite<T>> {
174198
let _hotkey = Self::get_hotkey_for_net_and_uid(netuid, uid);
175199
let hotkey;

pallets/subtensor/src/serving.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use crate::certificate::Certificate;
23
use frame_support::inherent::Vec;
34
use frame_support::sp_std::vec;
45

@@ -63,6 +64,7 @@ impl<T: Config> Pallet<T> {
6364
protocol: u8,
6465
placeholder1: u8,
6566
placeholder2: u8,
67+
certificate: Option<Certificate>,
6668
) -> dispatch::DispatchResult {
6769
// --- 1. We check the callers (hotkey) signature.
6870
let hotkey_id = ensure_signed(origin)?;
@@ -88,6 +90,12 @@ impl<T: Config> Pallet<T> {
8890
Error::<T>::ServingRateLimitExceeded
8991
);
9092

93+
// --- 5. Check certificate
94+
if let Some(certificate) = certificate {
95+
log::error!("\n---------> do_serve_axon saving cert {:?}\n", certificate.clone());
96+
NeuronCertificates::<T>::insert(netuid, hotkey_id.clone(), NeuronCertificate{certificate})
97+
}
98+
9199
// --- 6. We insert the axon meta.
92100
prev_axon.block = Self::get_current_block_as_u64();
93101
prev_axon.version = version;
@@ -241,6 +249,10 @@ impl<T: Config> Pallet<T> {
241249
return Axons::<T>::contains_key(netuid, hotkey);
242250
}
243251

252+
pub fn has_neuron_certificate(netuid: u16, hotkey: &T::AccountId) -> bool {
253+
return NeuronCertificates::<T>::contains_key(netuid, hotkey);
254+
}
255+
244256
pub fn has_prometheus_info(netuid: u16, hotkey: &T::AccountId) -> bool {
245257
return Prometheus::<T>::contains_key(netuid, hotkey);
246258
}

pallets/subtensor/tests/serving.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use frame_system::Config;
88
use pallet_subtensor::Error;
99
use sp_core::U256;
1010

11+
type Certificate = Vec<u8>;
12+
1113
mod test {
1214
use std::net::{Ipv4Addr, Ipv6Addr};
1315

@@ -95,6 +97,56 @@ fn test_serving_ok() {
9597
});
9698
}
9799

100+
#[test]
101+
fn test_serving_tls_ok() {
102+
// use crate::certificate::Certificate;
103+
new_test_ext().execute_with(|| {
104+
let hotkey_account_id = U256::from(1);
105+
let netuid: u16 = 1;
106+
let tempo: u16 = 13;
107+
let version: u32 = 2;
108+
let ip: u128 = 1676056785;
109+
let port: u16 = 128;
110+
let ip_type: u8 = 4;
111+
let modality: u16 = 0;
112+
let protocol: u8 = 0;
113+
let placeholder1: u8 = 0;
114+
let placeholder2: u8 = 0;
115+
let certificate: Certificate = "TEST".as_bytes().to_vec();
116+
add_network(netuid, tempo, modality);
117+
register_ok_neuron(netuid, hotkey_account_id, U256::from(66), 0);
118+
assert_ok!(SubtensorModule::serve_axon_tls(
119+
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
120+
netuid,
121+
version,
122+
ip,
123+
port,
124+
ip_type,
125+
protocol,
126+
placeholder1,
127+
placeholder2,
128+
certificate.clone()
129+
));
130+
let stored_certificate = SubtensorModule::get_neuron_certificate(netuid, 1);
131+
assert_eq!(stored_certificate.unwrap().certificate, certificate);
132+
let new_certificate = "NEWTEST".as_bytes().to_vec();
133+
assert_ok!(SubtensorModule::serve_axon_tls(
134+
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
135+
netuid,
136+
version,
137+
ip,
138+
port,
139+
ip_type,
140+
protocol,
141+
placeholder1,
142+
placeholder2,
143+
new_certificate.clone()
144+
));
145+
let stored_certificate = SubtensorModule::get_neuron_certificate(netuid, 1);
146+
assert_eq!(stored_certificate.unwrap().certificate, new_certificate)
147+
});
148+
}
149+
98150
#[test]
99151
fn test_serving_set_metadata_update() {
100152
new_test_ext().execute_with(|| {

runtime/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
88

99
use codec::Encode;
1010

11+
use frame_support::log;
1112
use pallet_commitments::CanCommit;
1213
use pallet_grandpa::{
1314
fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
@@ -121,7 +122,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
121122
// `spec_version`, and `authoring_version` are the same between Wasm and native.
122123
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
123124
// the compatible custom types.
124-
spec_version: 144,
125+
spec_version: 146,
125126
impl_version: 1,
126127
apis: RUNTIME_API_VERSIONS,
127128
transaction_version: 1,
@@ -1328,6 +1329,17 @@ impl_runtime_apis! {
13281329
vec![]
13291330
}
13301331
}
1332+
1333+
fn get_neuron_certificate(netuid: u16, uid: u16) -> Vec<u8> {
1334+
use frame_support::log;
1335+
let _result = SubtensorModule::get_neuron_certificate(netuid, uid);
1336+
if _result.is_some() {
1337+
let result = _result.expect("Could not get Certificate");
1338+
result.encode()
1339+
} else {
1340+
vec![]
1341+
}
1342+
}
13311343
}
13321344

13331345
impl subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi<Block> for Runtime {

0 commit comments

Comments
 (0)