Skip to content

Commit

Permalink
feat(pallet): prefer module key
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Mar 24, 2024
1 parent 9cd78ad commit 736f280
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 57 deletions.
12 changes: 1 addition & 11 deletions pallets/subspace/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,9 @@ pub struct ModuleInfo {
pub stats: ModuleStats,
}

// sp_api::decl_runtime_apis! {
// pub trait SubspaceRuntimeApi where
// AccountId: <<Signature as Verify>::Signer as IdentifyAccount>::AccountId
// {
// fn get_module_info() -> Result<ModuleInfo>;
// }
// }

sp_api::decl_runtime_apis! {
pub trait SubspaceRuntimeApi
{
pub trait SubspaceRuntimeApi {
fn get_burn_rate() -> u16;

fn get_module_info(key: AccountId, netuid: u16) -> ModuleInfo;
}
}
6 changes: 3 additions & 3 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub mod pallet {
pub struct ModuleParams<T: Config> {
pub name: Vec<u8>,
pub address: Vec<u8>,
pub delegation_fee: Percent, // delegate_fee
pub delegation_fee: Percent,
pub controller: T::AccountId,
}

Expand Down Expand Up @@ -1202,8 +1202,8 @@ pub mod pallet {
) -> DispatchResult {
let key = ensure_signed(origin.clone())?;
ensure!(Self::is_registered(netuid, &key), Error::<T>::NotRegistered);
let uid: u16 = Self::get_uid_for_key(netuid, &key);
let params = Self::module_params(netuid, uid);

let params = Self::module_params(netuid, &key);

let changeset = ModuleChangeset::update(&params, name, address, delegation_fee);
Self::do_update_module(origin, netuid, changeset)
Expand Down
51 changes: 12 additions & 39 deletions pallets/subspace/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ pub struct ModuleStats<T: Config> {
pub weights: Vec<(u16, u16)>, // Vec of (uid, weight)
}

#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct ModuleInfo<T: Config> {
params: ModuleParams<T>,
stats: ModuleStats<T>,
}

#[derive(Debug)]
pub struct ModuleChangeset {
pub name: Option<Vec<u8>>,
Expand All @@ -32,6 +26,7 @@ pub struct ModuleChangeset {
}

impl ModuleChangeset {
#[must_use]
pub fn new(name: Vec<u8>, address: Vec<u8>) -> Self {
Self {
name: Some(name),
Expand All @@ -40,6 +35,7 @@ impl ModuleChangeset {
}
}

#[must_use]
pub fn update<T: Config>(
params: &ModuleParams<T>,
name: Vec<u8>,
Expand Down Expand Up @@ -110,42 +106,17 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub fn module_params(netuid: u16, uid: u16) -> ModuleParams<T> {
pub fn module_params(netuid: u16, key: &T::AccountId) -> ModuleParams<T> {
let uid = Uids::<T>::try_get(netuid, key).expect("module key does not exist");

ModuleParams {
name: Self::get_module_name(netuid, uid),
address: Self::get_module_address(netuid, uid),
delegation_fee: Self::get_module_delegation_fee(netuid, uid),
controller: Self::get_key_for_uid(netuid, uid),
name: Name::<T>::get(netuid, uid),
address: Address::<T>::get(netuid, uid),
delegation_fee: DelegationFee::<T>::get(netuid, key),
controller: key.clone(),
}
}

pub fn get_module_address(netuid: u16, uid: u16) -> Vec<u8> {
Address::<T>::get(netuid, uid)
}

pub fn set_module_address(netuid: u16, uid: u16, address: Vec<u8>) {
Address::<T>::insert(netuid, uid, address);
}

pub fn get_module_delegation_fee(netuid: u16, uid: u16) -> Percent {
let key = Self::get_key_for_uid(netuid, uid);
let delegation_fee: Percent = DelegationFee::<T>::get(netuid, key);
delegation_fee
}

pub fn set_module_delegation_fee(netuid: u16, uid: u16, delegation_fee: Percent) {
let key = Self::get_key_for_uid(netuid, uid);
DelegationFee::<T>::insert(netuid, key, delegation_fee);
}

pub fn get_module_name(netuid: u16, uid: u16) -> Vec<u8> {
Name::<T>::get(netuid, uid)
}

pub fn set_module_name(netuid: u16, uid: u16, name: Vec<u8>) {
Name::<T>::insert(netuid, uid, name.clone());
}

// Replace the module under this uid.
pub fn remove_module(netuid: u16, uid: u16) {
// 1. Get the old key under this position.
Expand Down Expand Up @@ -269,7 +240,9 @@ impl<T: Config> Pallet<T> {
Ok(uid)
}

pub fn get_module_stats(netuid: u16, uid: u16) -> ModuleStats<T> {
pub fn get_module_stats(netuid: u16, key: &T::AccountId) -> ModuleStats<T> {
let uid = Uids::<T>::try_get(netuid, key).expect("module key does not exist");

let key = Self::get_key_for_uid(netuid, uid);
let emission = Self::get_emission_for_uid(netuid, uid);
let incentive = Self::get_incentive_for_uid(netuid, uid);
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/tests/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ fn validates_module_on_update() {
assert_ok!(update_module(b"test2", b"0.0.0.0:2"));
assert_ok!(update_module(b"test3", b"0.0.0.0:3"));

let params = SubspaceModule::module_params(0, 1);
let params = SubspaceModule::module_params(0, &key_1);
assert_eq!(params.name, b"test3");
assert_eq!(params.address, b"0.0.0.0:3");

Expand Down
5 changes: 2 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,8 @@ impl_runtime_apis! {
}

fn get_module_info(key: AccountId, netuid: u16) -> ModuleInfo {
let uid = SubspaceModule::get_uid_for_key(netuid, &key);
let stats = SubspaceModule::get_module_stats(netuid, uid);
let params = SubspaceModule::module_params(netuid, uid);
let stats = SubspaceModule::get_module_stats(netuid, &key);
let params = SubspaceModule::module_params(netuid, &key);

ModuleInfo {
stats: ModuleStats {
Expand Down

0 comments on commit 736f280

Please sign in to comment.