Skip to content

Commit

Permalink
fixes, code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andreea-popescu-reef committed Apr 5, 2024
1 parent 472d178 commit 6a5aedc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 70 deletions.
4 changes: 4 additions & 0 deletions pallets/subtensor/src/certificate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use sp_std::vec::Vec;

// TODO Certificate Encryption Pubkey
pub type Certificate = Vec<u8>;
3 changes: 2 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,8 @@ pub mod pallet {
)
}

// ---- Serves or updates axon /promethteus information for the neuron associated with the caller. If the caller is
// ---- Same as `serve_axon` but takes a certificate as an extra optional argument.
// Serves or updates axon /promethteus information for the neuron associated with the caller. If the caller is
// already registered the metadata is updated. If the caller is not registered this call throws NotRegistered.
//
// # Args:
Expand Down
39 changes: 13 additions & 26 deletions pallets/subtensor/src/neuron_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,10 @@ impl<T: Config> Pallet<T> {
}

fn get_neuron_subnet_exists(netuid: u16, uid: u16) -> Option<NeuronInfo<T>> {
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");
}
let hotkey = match Self::get_hotkey_for_net_and_uid(netuid, uid) {
Ok(hotkey) => hotkey, // hotkey was registered
Err(_) => return None,
};

let axon_info = Self::get_axon_info(netuid, &hotkey.clone());

Expand Down Expand Up @@ -175,32 +171,23 @@ impl<T: Config> Pallet<T> {
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");
}
let hotkey = match Self::get_hotkey_for_net_and_uid(netuid, uid) {
Ok(hotkey) => hotkey, // hotkey was registered
Err(_) => return None,
};

if Self::has_neuron_certificate(netuid, &hotkey) {
let certificate = NeuronCertificates::<T>::get(netuid, hotkey).unwrap();
Some(certificate)
NeuronCertificates::<T>::get(netuid, hotkey)
} else {
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;
if _hotkey.is_err() {
return None;
} else {
// No error, hotkey was registered
hotkey = _hotkey.expect("Hotkey should exist");
}
let hotkey = match Self::get_hotkey_for_net_and_uid(netuid, uid) {
Ok(hotkey) => hotkey, // hotkey was registered
Err(_) => return None,
};

let axon_info = Self::get_axon_info(netuid, &hotkey.clone());

Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/tests/serving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn test_serving_ok() {
fn test_serving_tls_ok() {
new_test_ext().execute_with(|| {
let hotkey_account_id = U256::from(1);
let uid: u16 = 0;
let uid: u16 = 0;
let netuid: u16 = 1;
let tempo: u16 = 13;
let version: u32 = 2;
Expand Down
48 changes: 6 additions & 42 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,13 +1283,7 @@ impl_runtime_apis! {
}

fn get_delegate(delegate_account_vec: Vec<u8>) -> Vec<u8> {
let _result = SubtensorModule::get_delegate(delegate_account_vec);
if _result.is_some() {
let result = _result.expect("Could not get DelegateInfo");
result.encode()
} else {
vec![]
}
SubtensorModule::get_delegate(delegate_account_vec).map(|r| r.encode()).unwrap_or(vec![])
}

fn get_delegated(delegatee_account_vec: Vec<u8>) -> Vec<u8> {
Expand All @@ -1305,13 +1299,7 @@ impl_runtime_apis! {
}

fn get_neuron_lite(netuid: u16, uid: u16) -> Vec<u8> {
let _result = SubtensorModule::get_neuron_lite(netuid, uid);
if _result.is_some() {
let result = _result.expect("Could not get NeuronInfoLite");
result.encode()
} else {
vec![]
}
SubtensorModule::get_neuron_lite(netuid, uid).map(|r| r.encode()).unwrap_or(vec![])
}

fn get_neurons(netuid: u16) -> Vec<u8> {
Expand All @@ -1320,35 +1308,17 @@ impl_runtime_apis! {
}

fn get_neuron(netuid: u16, uid: u16) -> Vec<u8> {
let _result = SubtensorModule::get_neuron(netuid, uid);
if _result.is_some() {
let result = _result.expect("Could not get NeuronInfo");
result.encode()
} else {
vec![]
}
SubtensorModule::get_neuron(netuid, uid).map(|r| r.encode()).unwrap_or(vec![])
}

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

impl subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi<Block> for Runtime {
fn get_subnet_info(netuid: u16) -> Vec<u8> {
let _result = SubtensorModule::get_subnet_info(netuid);
if _result.is_some() {
let result = _result.expect("Could not get SubnetInfo");
result.encode()
} else {
vec![]
}
SubtensorModule::get_subnet_info(netuid).map(|r| r.encode()).unwrap_or(vec![])
}

fn get_subnets_info() -> Vec<u8> {
Expand All @@ -1357,13 +1327,7 @@ impl_runtime_apis! {
}

fn get_subnet_hyperparams(netuid: u16) -> Vec<u8> {
let _result = SubtensorModule::get_subnet_hyperparams(netuid);
if _result.is_some() {
let result = _result.expect("Could not get SubnetHyperparams");
result.encode()
} else {
vec![]
}
SubtensorModule::get_subnet_hyperparams(netuid).map(|r| r.encode()).unwrap_or(vec![])
}
}

Expand Down

0 comments on commit 6a5aedc

Please sign in to comment.