Skip to content

Commit

Permalink
add missing validate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnReedV committed Jul 31, 2024
1 parent 826e55c commit beadf9f
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 5 deletions.
114 changes: 109 additions & 5 deletions pallets/subtensor/tests/root.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#![allow(clippy::indexing_slicing, clippy::unwrap_used)]

use crate::mock::*;
use frame_support::{assert_err, assert_ok};
use frame_system::Config;
use frame_system::{EventRecord, Phase};
use pallet_subtensor::migration;
use pallet_subtensor::Error;
use frame_support::{assert_err, assert_ok, dispatch::DispatchInfo};
use frame_system::{Config, EventRecord, Phase};
use pallet_subtensor::{
migration, BaseDifficulty, ColdkeySwapDestinations, Error, MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP,
};
use sp_core::{Get, H256, U256};
use sp_runtime::{
traits::{DispatchInfoOf, SignedExtension},
transaction_validity::{InvalidTransaction, TransactionValidityError},
};

mod mock;

Expand Down Expand Up @@ -974,3 +978,103 @@ fn test_dissolve_network_does_not_exist_err() {
);
});
}

#[test]
fn test_dissolve_network_validate() {
fn generate_valid_pow(coldkey: &U256, block_number: u64, difficulty: U256) -> (H256, u64) {
let mut nonce: u64 = 0;
loop {
let work = SubtensorModule::create_seal_hash(block_number, nonce, coldkey);
if SubtensorModule::hash_meets_difficulty(&work, difficulty) {
return (work, nonce);
}
nonce += 1;
}
}
// Testing the signed extension validate function
// correctly filters the `dissolve_network` transaction.

new_test_ext(0).execute_with(|| {
let netuid: u16 = 1;
let delegate_coldkey = U256::from(1);
let delegate_hotkey = U256::from(2);
let new_coldkey = U256::from(3);
let current_block = 0u64;

add_network(netuid, 0, 0);
register_ok_neuron(netuid, delegate_hotkey, delegate_coldkey, 0);

// Make delegate a delegate
assert_ok!(SubtensorModule::become_delegate(
RuntimeOrigin::signed(delegate_coldkey),
delegate_hotkey
));

// Add more than 500 TAO of stake to the delegate's hotkey
let stake_amount = 501_000_000_000; // 501 TAO in RAO
let delegator = U256::from(4);
SubtensorModule::add_balance_to_coldkey_account(&delegator, stake_amount);
assert_ok!(SubtensorModule::add_stake(
RuntimeOrigin::signed(delegator),
delegate_hotkey,
stake_amount
));

// Ensure the delegate's coldkey has less than minimum balance
assert!(
SubtensorModule::get_coldkey_balance(&delegate_coldkey)
< MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP,
"Delegate coldkey balance should be less than minimum required"
);

// Ensure the delegate's hotkey has more than 500 TAO delegated
assert!(
SubtensorModule::get_total_delegated_stake(&delegate_coldkey) >= 500_000_000_000,
"Delegate hotkey should have at least 500 TAO delegated"
);

// Generate valid PoW
let (work, nonce) = generate_valid_pow(
&delegate_coldkey,
current_block,
U256::from(4) * U256::from(BaseDifficulty::<Test>::get()),
);

// Schedule coldkey swap
assert_ok!(SubtensorModule::do_schedule_coldkey_swap(
&delegate_coldkey,
&new_coldkey,
work.to_fixed_bytes().to_vec(),
current_block,
nonce,
));

// Verify that the swap was scheduled
assert_eq!(
ColdkeySwapDestinations::<Test>::get(delegate_coldkey),
vec![new_coldkey]
);

// Check if the coldkey is in arbitration
assert!(
SubtensorModule::coldkey_in_arbitration(&delegate_coldkey),
"Delegate coldkey should be in arbitration after swap"
);

let who = delegate_coldkey; // The coldkey signs this transaction
let call = RuntimeCall::SubtensorModule(SubtensorCall::dissolve_network { netuid });

let info: DispatchInfo =
DispatchInfoOf::<<Test as frame_system::Config>::RuntimeCall>::default();

let extension = pallet_subtensor::SubtensorSignedExtension::<Test>::new();

// Submit to the signed extension validate function
let result_in_arbitration = extension.validate(&who, &call.clone(), &info, 10);
// Should fail with InvalidTransaction::Custom(6) because coldkey is in arbitration
assert_err!(
result_in_arbitration,
TransactionValidityError::Invalid(InvalidTransaction::Custom(6))
);
});
}
60 changes: 60 additions & 0 deletions pallets/subtensor/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,66 @@ fn test_reveal_weights_dispatch_info_ok() {
});
}

#[test]
fn test_set_weights_validate() {
// Testing the signed extension validate function
// correctly filters the `set_weights` transaction.

new_test_ext(0).execute_with(|| {
let netuid: u16 = 1;
let coldkey = U256::from(0);
let hotkey: U256 = U256::from(1);
assert_ne!(hotkey, coldkey);

let who = hotkey; // The hotkey signs this transaction

let call = RuntimeCall::SubtensorModule(SubtensorCall::set_weights {
netuid,
dests: vec![1, 1],
weights: vec![1, 1],
version_key: 0,
});

// Create netuid
add_network(netuid, 0, 0);
// Register the hotkey
SubtensorModule::append_neuron(netuid, &hotkey, 0);
Owner::<Test>::insert(hotkey, coldkey);

let min_stake = 500_000_000_000;
// Set the minimum stake
SubtensorModule::set_weights_min_stake(min_stake);

// Verify stake is less than minimum
assert!(SubtensorModule::get_total_stake_for_hotkey(&hotkey) < min_stake);
let info: DispatchInfo =
DispatchInfoOf::<<Test as frame_system::Config>::RuntimeCall>::default();

let extension = pallet_subtensor::SubtensorSignedExtension::<Test>::new();
// Submit to the signed extension validate function
let result_no_stake = extension.validate(&who, &call.clone(), &info, 10);
// Should fail due to insufficient stake
assert_err!(
result_no_stake,
TransactionValidityError::Invalid(InvalidTransaction::Custom(3))
);

// Increase the stake to be equal to the minimum
SubtensorModule::increase_stake_on_hotkey_account(&hotkey, min_stake);

// Verify stake is equal to minimum
assert_eq!(
SubtensorModule::get_total_stake_for_hotkey(&hotkey),
min_stake
);

// Submit to the signed extension validate function
let result_min_stake = extension.validate(&who, &call.clone(), &info, 10);
// Now the call should pass
assert_ok!(result_min_stake);
});
}

#[test]
fn test_reveal_weights_validate() {
// Testing the signed extension validate function
Expand Down

0 comments on commit beadf9f

Please sign in to comment.