Skip to content

Commit

Permalink
Fix test_childkey_set_weights_single_parent
Browse files Browse the repository at this point in the history
  • Loading branch information
keithtensor committed Jan 9, 2025
1 parent 5398a82 commit 8c27fe2
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 83 deletions.
3 changes: 2 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,8 @@ pub mod pallet {
/// Is the caller allowed to set weights
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
// Blacklist weights transactions for low stake peers.
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_stake_threshold()
let (total_stake, _, _) = Self::get_stake_weights_for_hotkey_on_subnet(hotkey, netuid);
total_stake >= Self::get_stake_threshold()
}

/// Helper function to check if register is allowed
Expand Down
27 changes: 26 additions & 1 deletion pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,35 @@ impl<T: Config> Pallet<T> {
TaoWeight::<T>::set(weight);
}

/// Calculates the weighted combination of alpha and global tao for a single hotkey onet a subnet.
///
pub fn get_stake_weights_for_hotkey_on_subnet(
hotkey: &T::AccountId,
netuid: u16,
) -> (I64F64, I64F64, I64F64) {
// Retrieve the global tao weight.
let tao_weight = I64F64::from_num(Self::get_tao_weight());
log::debug!("tao_weight: {:?}", tao_weight);

// Step 1: Get stake of hotkey (neuron)
let alpha_stake = I64F64::from_num(Self::get_inherited_for_hotkey_on_subnet(&hotkey, netuid));
log::trace!("alpha_stake: {:?}", alpha_stake);

// Step 2: Get the global tao stake for the hotkey
let tao_stake = I64F64::from_num(Self::get_inherited_for_hotkey_on_subnet(&hotkey, 0));
log::trace!("tao_stake: {:?}", tao_stake);

// Step 3: Combine alpha and tao stakes
let total_stake = alpha_stake.saturating_add(tao_stake.saturating_mul(tao_weight));
log::trace!("total_stake: {:?}", total_stake);

(total_stake, alpha_stake, tao_stake)
}

/// Calculates the weighted combination of alpha and global tao for hotkeys on a subnet.
///
pub fn get_stake_weights_for_network(netuid: u16) -> (Vec<I64F64>, Vec<I64F64>, Vec<I64F64>) {
// Retrieve the global global weight.
// Retrieve the global tao weight.
let tao_weight: I64F64 = I64F64::from_num(Self::get_tao_weight());
log::debug!("tao_weight: {:?}", tao_weight);

Expand Down
162 changes: 81 additions & 81 deletions pallets/subtensor/src/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2630,105 +2630,105 @@ fn test_set_children_rate_limit_fail_then_succeed() {
#[test]
fn test_childkey_set_weights_single_parent() {
new_test_ext(1).execute_with(|| {
assert!(false);
let netuid: u16 = 1;
add_network(netuid, 1, 0);

// let netuid: u16 = 1;
// add_network(netuid, 1, 0);
// Define hotkeys
let parent: U256 = U256::from(1);
let child: U256 = U256::from(2);
let weight_setter: U256 = U256::from(3);

// // Define hotkeys
// let parent: U256 = U256::from(1);
// let child: U256 = U256::from(2);
// let weight_setter: U256 = U256::from(3);
// Define coldkeys with more readable names
let coldkey_parent: U256 = U256::from(100);
let coldkey_child: U256 = U256::from(101);
let coldkey_weight_setter: U256 = U256::from(102);

// // Define coldkeys with more readable names
// let coldkey_parent: U256 = U256::from(100);
// let coldkey_child: U256 = U256::from(101);
// let coldkey_weight_setter: U256 = U256::from(102);
let stake_to_give_child = 109_999;

// let stake_to_give_child = 109_999;
// Register parent with minimal stake and child with high stake
SubtensorModule::add_balance_to_coldkey_account(&coldkey_parent, 1);
SubtensorModule::add_balance_to_coldkey_account(&coldkey_child, stake_to_give_child + 10);
SubtensorModule::add_balance_to_coldkey_account(&coldkey_weight_setter, 1_000_000);

// // Register parent with minimal stake and child with high stake
// SubtensorModule::add_balance_to_coldkey_account(&coldkey_parent, 1);
// SubtensorModule::add_balance_to_coldkey_account(&coldkey_child, stake_to_give_child + 10);
// SubtensorModule::add_balance_to_coldkey_account(&coldkey_weight_setter, 1_000_000);
// Add neurons for parent, child and weight_setter
register_ok_neuron(netuid, parent, coldkey_parent, 1);
register_ok_neuron(netuid, child, coldkey_child, 1);
register_ok_neuron(netuid, weight_setter, coldkey_weight_setter, 1);

// // Add neurons for parent, child and weight_setter
// register_ok_neuron(netuid, parent, coldkey_parent, 1);
// register_ok_neuron(netuid, child, coldkey_child, 1);
// register_ok_neuron(netuid, weight_setter, coldkey_weight_setter, 1);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&parent,
&coldkey_parent,
netuid,
stake_to_give_child,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&weight_setter,
&coldkey_weight_setter,
netuid,
1_000_000,
);

// SubtensorModule::increase_stake_on_coldkey_hotkey_account(
// &coldkey_parent,
// &parent,
// stake_to_give_child,
// );
// SubtensorModule::increase_stake_on_coldkey_hotkey_account(
// &coldkey_weight_setter,
// &weight_setter,
// 1_000_000,
// );
SubtensorModule::set_weights_set_rate_limit(netuid, 0);

// SubtensorModule::set_weights_set_rate_limit(netuid, 0);
// Set parent-child relationship
mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX, child)]);

// // Set parent-child relationship
// mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX, child)]);
step_block(7200 + 1);
// Set weights on the child using the weight_setter account
let origin = RuntimeOrigin::signed(weight_setter);
let uids: Vec<u16> = vec![1]; // Only set weight for the child (UID 1)
let values: Vec<u16> = vec![u16::MAX]; // Use maximum value for u16
let version_key = SubtensorModule::get_weights_version_key(netuid);
assert_ok!(SubtensorModule::set_weights(
origin,
netuid,
uids.clone(),
values.clone(),
version_key
));

// step_block(7200 + 1);
// // Set weights on the child using the weight_setter account
// let origin = RuntimeOrigin::signed(weight_setter);
// let uids: Vec<u16> = vec![1]; // Only set weight for the child (UID 1)
// let values: Vec<u16> = vec![u16::MAX]; // Use maximum value for u16
// let version_key = SubtensorModule::get_weights_version_key(netuid);
// assert_ok!(SubtensorModule::set_weights(
// origin,
// netuid,
// uids.clone(),
// values.clone(),
// version_key
// ));
// Set the min stake very high
SubtensorModule::set_stake_threshold(stake_to_give_child * 5);

// // Set the min stake very high
// SubtensorModule::set_stake_threshold(stake_to_give_child * 5);
// Check the child has less stake than required
assert!(
SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
< SubtensorModule::get_stake_threshold()
);

// // Check the child has less stake than required
// assert!(
// SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
// < SubtensorModule::get_stake_threshold()
// );
// Check the child cannot set weights
assert_noop!(
SubtensorModule::set_weights(
RuntimeOrigin::signed(child),
netuid,
uids.clone(),
values.clone(),
version_key
),
Error::<Test>::NotEnoughStakeToSetWeights
);

// // Check the child cannot set weights
// assert_noop!(
// SubtensorModule::set_weights(
// RuntimeOrigin::signed(child),
// netuid,
// uids.clone(),
// values.clone(),
// version_key
// ),
// Error::<Test>::NotEnoughStakeToSetWeights
// );
assert!(!SubtensorModule::check_weights_min_stake(&child, netuid));

// assert!(!SubtensorModule::check_weights_min_stake(&child, netuid));
// Set a minimum stake to set weights
SubtensorModule::set_stake_threshold(stake_to_give_child - 5);

// // Set a minimum stake to set weights
// SubtensorModule::set_stake_threshold(stake_to_give_child - 5);
let (total_stakes, _, _) = SubtensorModule::get_stake_weights_for_network(netuid);
let child_stake = total_stakes[child.as_usize()];

// // Check if the stake for the child is above
// assert!(
// SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
// >= SubtensorModule::get_stake_threshold()
// );
// Check if the stake for the child is above
assert!(child_stake >= SubtensorModule::get_stake_threshold());

// // Check the child can set weights
// assert_ok!(SubtensorModule::set_weights(
// RuntimeOrigin::signed(child),
// netuid,
// uids,
// values,
// version_key
// ));
// Check the child can set weights
assert_ok!(SubtensorModule::set_weights(
RuntimeOrigin::signed(child),
netuid,
uids,
values,
version_key
));

// assert!(SubtensorModule::check_weights_min_stake(&child, netuid));
assert!(SubtensorModule::check_weights_min_stake(&child, netuid));
});
}

Expand Down

0 comments on commit 8c27fe2

Please sign in to comment.