Skip to content

Commit

Permalink
add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
open-junius committed Aug 20, 2024
1 parent 9be558e commit 8d9bb2d
Showing 1 changed file with 189 additions and 1 deletion.
190 changes: 189 additions & 1 deletion pallets/subtensor/tests/networks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::mock::*;
use frame_support::assert_ok;
use frame_system::Config;
use pallet_subtensor::{DissolveNetworkScheduleDuration, Event};
use pallet_subtensor::{ColdkeySwapScheduleDuration, DissolveNetworkScheduleDuration, Event};
use sp_core::U256;

mod mock;
Expand Down Expand Up @@ -94,3 +94,191 @@ fn test_schedule_dissolve_network_execution() {
assert!(!SubtensorModule::if_subnet_exist(netuid));
})
}

#[test]
fn test_non_owner_schedule_dissolve_network_execution() {
new_test_ext(1).execute_with(|| {
let block_number: u64 = 0;
let netuid: u16 = 2;
let tempo: u16 = 13;
let hotkey_account_id: U256 = U256::from(1);
let coldkey_account_id = U256::from(0); // Neighbour of the beast, har har
let non_network_owner_account_id = U256::from(2); //
let (nonce, work): (u64, Vec<u8>) = SubtensorModule::create_work_for_block_number(
netuid,
block_number,
129123813,
&hotkey_account_id,
);

//add network
add_network(netuid, tempo, 0);

assert_ok!(SubtensorModule::register(
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
netuid,
block_number,
nonce,
work.clone(),
hotkey_account_id,
coldkey_account_id
));

assert!(SubtensorModule::if_subnet_exist(netuid));

assert_ok!(SubtensorModule::schedule_dissolve_network(
<<Test as Config>::RuntimeOrigin>::signed(non_network_owner_account_id),
netuid
));

let current_block = System::block_number();
let execution_block = current_block + DissolveNetworkScheduleDuration::<Test>::get();

System::assert_last_event(
Event::DissolveNetworkScheduled {
account: non_network_owner_account_id,
netuid,
execution_block,
}
.into(),
);

run_to_block(execution_block);
// network exists since the caller is no the network owner
assert!(SubtensorModule::if_subnet_exist(netuid));
})
}

#[test]
fn test_new_owner_schedule_dissolve_network_execution() {
new_test_ext(1).execute_with(|| {
let block_number: u64 = 0;
let netuid: u16 = 2;
let tempo: u16 = 13;
let hotkey_account_id: U256 = U256::from(1);
let coldkey_account_id = U256::from(0); // Neighbour of the beast, har har
let new_network_owner_account_id = U256::from(2); //
let (nonce, work): (u64, Vec<u8>) = SubtensorModule::create_work_for_block_number(
netuid,
block_number,
129123813,
&hotkey_account_id,
);

//add network
add_network(netuid, tempo, 0);

assert_ok!(SubtensorModule::register(
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
netuid,
block_number,
nonce,
work.clone(),
hotkey_account_id,
coldkey_account_id
));

assert!(SubtensorModule::if_subnet_exist(netuid));

// the account is not network owner when schedule the call
assert_ok!(SubtensorModule::schedule_dissolve_network(
<<Test as Config>::RuntimeOrigin>::signed(new_network_owner_account_id),
netuid
));

let current_block = System::block_number();
let execution_block = current_block + DissolveNetworkScheduleDuration::<Test>::get();

System::assert_last_event(
Event::DissolveNetworkScheduled {
account: new_network_owner_account_id,
netuid,
execution_block,
}
.into(),
);
run_to_block(current_block + 1);
// become network owner after call scheduled
pallet_subtensor::SubnetOwner::<Test>::insert(netuid, new_network_owner_account_id);

run_to_block(execution_block);
// network exists since the caller is no the network owner
assert!(!SubtensorModule::if_subnet_exist(netuid));
})
}

#[test]
fn test_schedule_dissolve_network_execution_with_coldkey_swap() {
new_test_ext(1).execute_with(|| {
let block_number: u64 = 0;
let netuid: u16 = 2;
let tempo: u16 = 13;
let hotkey_account_id: U256 = U256::from(1);
let coldkey_account_id = U256::from(0); // Neighbour of the beast, har har
let new_network_owner_account_id = U256::from(2); //

SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 1000000000000000);

let (nonce, work): (u64, Vec<u8>) = SubtensorModule::create_work_for_block_number(
netuid,
block_number,
129123813,
&hotkey_account_id,
);

//add network
add_network(netuid, tempo, 0);

assert_ok!(SubtensorModule::register(
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
netuid,
block_number,
nonce,
work.clone(),
hotkey_account_id,
coldkey_account_id
));

assert!(SubtensorModule::if_subnet_exist(netuid));

// the account is not network owner when schedule the call
assert_ok!(SubtensorModule::schedule_swap_coldkey(
<<Test as Config>::RuntimeOrigin>::signed(coldkey_account_id),
new_network_owner_account_id
));

let current_block = System::block_number();
let execution_block = current_block + ColdkeySwapScheduleDuration::<Test>::get();

run_to_block(execution_block - 1);

// the account is not network owner when schedule the call
assert_ok!(SubtensorModule::schedule_dissolve_network(
<<Test as Config>::RuntimeOrigin>::signed(new_network_owner_account_id),
netuid
));

System::assert_last_event(
Event::DissolveNetworkScheduled {
account: new_network_owner_account_id,
netuid: netuid,
execution_block: DissolveNetworkScheduleDuration::<Test>::get() + execution_block
- 1,
}
.into(),
);

run_to_block(execution_block);
assert_eq!(
pallet_subtensor::SubnetOwner::<Test>::get(netuid),
new_network_owner_account_id
);

let current_block = System::block_number();
let execution_block = current_block + DissolveNetworkScheduleDuration::<Test>::get();

run_to_block(execution_block);
// network exists since the caller is no the network owner
assert!(!SubtensorModule::if_subnet_exist(netuid));
})
}

0 comments on commit 8d9bb2d

Please sign in to comment.