Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Children to use u16s for proprotions #616

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pallets/subtensor/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub trait SubtensorCustomApi<BlockHash> {
&self,
netuid: u16,
child: Vec<u8>,
proportion: u64,
proportion: u16,
at: Option<BlockHash>,
) -> RpcResult<Vec<u8>>;
}
Expand Down Expand Up @@ -252,7 +252,7 @@ where
&self,
netuid: u16,
child: Vec<u8>,
proportion: u64,
proportion: u16,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ sp_api::decl_runtime_apis! {

pub trait ChildrenInfoRuntimeApi {
fn get_children_info(netuid: u16) -> Vec<u8>;
fn get_child_info(netuid: u16, child: Vec<u8>, proportion: u64) -> Vec<u8>;
fn get_child_info(netuid: u16, child: Vec<u8>, proportion: u16) -> Vec<u8>;
}
}
4 changes: 2 additions & 2 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ reveal_weights {
assert_ok!(Subtensor::<T>::do_burned_registration(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey.clone()));

// Set proportion
let proportion: u64 = 500;
let proportion: u16 = 500;

}: set_child_singular(RawOrigin::Signed(coldkey.clone()), hotkey.clone(), child.clone(), netuid, proportion)

Expand Down Expand Up @@ -485,7 +485,7 @@ reveal_weights {
assert_ok!(Subtensor::<T>::do_burned_registration(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey.clone()));

// Set child
let proportion: u64 = 500;
let proportion: u16 = 500;
assert_ok!(Subtensor::<T>::do_set_child_singular(RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone(), child.clone(), netuid, proportion));

}: revoke_child_singular(RawOrigin::Signed(coldkey.clone()), hotkey.clone(), child.clone(), netuid)
Expand Down
31 changes: 13 additions & 18 deletions pallets/subtensor/src/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ impl<T: Config> Pallet<T> {
/// * `netuid` (u16):
/// - The u16 network identifier where the childkey will exist.
///
/// * `proportion` (u64):
/// - Proportion of the hotkey's stake to be given to the child, the value must be u64 normalized.
/// * `proportion` (u16):
/// - Proportion of the hotkey's stake to be given to the child, the value must be u16 normalized.
///
/// # Events:
/// * `ChildAddedSingular`:
Expand Down Expand Up @@ -54,7 +54,7 @@ impl<T: Config> Pallet<T> {
hotkey: T::AccountId,
child: T::AccountId,
netuid: u16,
proportion: u64,
proportion: u16,
) -> DispatchResult {
// --- 1. Check that the caller has signed the transaction. (the coldkey of the pairing)
let coldkey = ensure_signed(origin)?;
Expand Down Expand Up @@ -152,9 +152,9 @@ impl<T: Config> Pallet<T> {
/// * `hotkey` (T::AccountId):
/// The hotkey which will be assigned the children.
///
/// * `children_with_proportions` (Vec<(T::AccountId, u64)>):
/// * `children_with_proportions` (Vec<(T::AccountId, u16)>):
/// A vector of tuples, each containing a child AccountId and its corresponding proportion.
/// The proportion must be a u64 normalized value (0 to u64::MAX).
/// The proportion must be a u16 normalized value (0 to u16::MAX).
///
/// * `netuid` (u16):
/// The u16 network identifier where the childkeys will exist.
Expand All @@ -172,13 +172,11 @@ impl<T: Config> Pallet<T> {
/// Thrown when the coldkey does not own the hotkey.
/// * `InvalidChild`:
/// Thrown when any of the children is the same as the hotkey.
/// * `ProportionSumIncorrect`:
/// Thrown when the sum of proportions does not equal u64::MAX.
///
/// # Detailed Workflow
/// 1. Verify the transaction signature and ownership.
/// 2. Perform various checks (network existence, root network, ownership, valid children).
/// 3. Ensure the sum of proportions equals u64::MAX.
/// 3. Ensure the sum of proportions equals u16::MAX.
/// 4. Remove the hotkey from its old children's parent lists.
/// 5. Create a new list of children with their proportions.
/// 6. Update the ChildKeys storage with the new children.
Expand Down Expand Up @@ -223,8 +221,8 @@ impl<T: Config> Pallet<T> {
}

// --- 6. Ensure the sum of proportions equals u64::MAX (representing 100%)
let (overflowed, total_proportion): (bool, u64) = {
let mut sum: u64 = 0;
let (overflowed, total_proportion): (bool, u16) = {
let mut sum: u16 = 0;
let mut overflowed = false;
for (_, proportion) in children_with_proportions.iter() {
let result = sum.checked_add(*proportion);
Expand All @@ -239,7 +237,7 @@ impl<T: Config> Pallet<T> {
};

ensure!(
!overflowed && total_proportion == u64::MAX,
!overflowed && total_proportion == u16::MAX,
Error::<T>::ProportionSumIncorrect
);

Expand Down Expand Up @@ -283,7 +281,6 @@ impl<T: Config> Pallet<T> {
new_children,
netuid,
));

// --- 12. Return success
Ok(())
}
Expand Down Expand Up @@ -372,11 +369,9 @@ impl<T: Config> Pallet<T> {
netuid
);
Self::deposit_event(Event::RevokeChildSingular(hotkey.clone(), child, netuid));

// Ok and return.
Ok(())
}

/// Revokes multiple children for a given hotkey on a specified network.
///
/// This function allows a coldkey to revoke multiple children for a given hotkey on a specified network.
Expand Down Expand Up @@ -557,13 +552,13 @@ impl<T: Config> Pallet<T> {
/// * `netuid` - The network identifier.
///
/// # Returns
/// * `Vec<(u64, T::AccountId)>` - A vector of tuples containing the proportion and child account ID.
/// * `Vec<(u16, T::AccountId)>` - A vector of tuples containing the proportion and child account ID.
///
/// # Example
/// ```
/// let children = SubtensorModule::get_children(&hotkey, netuid);
*/
pub fn get_children(hotkey: &T::AccountId, netuid: u16) -> Vec<(u64, T::AccountId)> {
pub fn get_children(hotkey: &T::AccountId, netuid: u16) -> Vec<(u16, T::AccountId)> {
ChildKeys::<T>::get(hotkey, netuid)
}

Expand All @@ -574,13 +569,13 @@ impl<T: Config> Pallet<T> {
/// * `netuid` - The network identifier.
///
/// # Returns
/// * `Vec<(u64, T::AccountId)>` - A vector of tuples containing the proportion and parent account ID.
/// * `Vec<(u16, T::AccountId)>` - A vector of tuples containing the proportion and parent account ID.
///
/// # Example
/// ```
/// let parents = SubtensorModule::get_parents(&child, netuid);
*/
pub fn get_parents(child: &T::AccountId, netuid: u16) -> Vec<(u64, T::AccountId)> {
pub fn get_parents(child: &T::AccountId, netuid: u16) -> Vec<(u16, T::AccountId)> {
ParentKeys::<T>::get(child, netuid)
}

Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/children_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct ChildInfo<T: Config> {
/// The AccountId of the child neuron
pub child_ss58: T::AccountId,
/// The proportion of stake allocated to this child
pub proportion: Compact<u64>,
pub proportion: Compact<u16>,
/// The total stake of the child (including its own children and parents)
pub total_stake: Compact<u64>,
/// The emissions per day for this child
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<T: Config> Pallet<T> {
/// # Returns
///
/// * `ChildInfo<T>` - A struct containing detailed information about the child neuron
pub fn get_child_info(netuid: u16, child_bytes: Vec<u8>, proportion: u64) -> ChildInfo<T> {
pub fn get_child_info(netuid: u16, child_bytes: Vec<u8>, proportion: u16) -> ChildInfo<T> {
// Convert Vec<u8> to T::AccountId
let child = T::AccountId::decode(&mut &child_bytes[..])
.expect("Failed to decode child_bytes into AccountId");
Expand Down
16 changes: 8 additions & 8 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub mod pallet {
}
/// Default account linkage
#[pallet::type_value]
pub fn DefaultProportion<T: Config>() -> u64 {
pub fn DefaultProportion<T: Config>() -> u16 {
0
}
/// Default accumulated emission for a hotkey
Expand Down Expand Up @@ -469,7 +469,7 @@ pub mod pallet {
T::AccountId,
Identity,
u16,
Vec<(u64, T::AccountId)>,
Vec<(u16, T::AccountId)>,
ValueQuery,
DefaultAccountLinkage<T>,
>;
Expand All @@ -481,7 +481,7 @@ pub mod pallet {
T::AccountId,
Identity,
u16,
Vec<(u64, T::AccountId)>,
Vec<(u16, T::AccountId)>,
ValueQuery,
DefaultAccountLinkage<T>,
>;
Expand Down Expand Up @@ -2297,7 +2297,7 @@ pub mod pallet {
/// * `netuid` (u16):
/// - The u16 network identifier where the childkey will exist.
///
/// * `proportion` (u64):
/// * `proportion` (u16):
/// - Proportion of the hotkey's stake to be given to the child, the value must be u64 normalized.
///
/// # Events:
Expand Down Expand Up @@ -2333,7 +2333,7 @@ pub mod pallet {
hotkey: T::AccountId,
child: T::AccountId,
netuid: u16,
proportion: u64,
proportion: u16,
) -> DispatchResultWithPostInfo {
Self::do_set_child_singular(origin, hotkey, child, netuid, proportion)?;
Ok(().into())
Expand Down Expand Up @@ -2394,9 +2394,9 @@ pub mod pallet {
/// * `hotkey` (T::AccountId):
/// - The hotkey which will be assigned the children.
///
/// * `children_with_proportions` (Vec<(T::AccountId, u64)>):
/// * `children_with_proportions` (Vec<(T::AccountId, u16)>):
/// - A vector of tuples, each containing a child AccountId and its corresponding proportion.
/// The proportion must be a u64 normalized value.
/// The proportion must be a u16 normalized value.
///
/// * `netuid` (u16):
/// - The u16 network identifier where the childkeys will exist.
Expand Down Expand Up @@ -2431,7 +2431,7 @@ pub mod pallet {
pub fn set_children_multiple(
origin: OriginFor<T>,
hotkey: T::AccountId,
children_with_proportions: Vec<(T::AccountId, u64)>,
children_with_proportions: Vec<(T::AccountId, u16)>,
netuid: u16,
) -> DispatchResultWithPostInfo {
Self::do_set_children_multiple(origin, hotkey, children_with_proportions, netuid)?;
Expand Down
Loading
Loading