diff --git a/pallets/subtensor/src/tests/senate.rs b/pallets/subtensor/src/tests/senate.rs index 93229f9de..dffb86e23 100644 --- a/pallets/subtensor/src/tests/senate.rs +++ b/pallets/subtensor/src/tests/senate.rs @@ -522,80 +522,84 @@ fn test_senate_leave_vote_removal() { #[test] fn test_senate_not_leave_when_stake_removed() { new_test_ext().execute_with(|| { - assert!(false); - - // migrations::migrate_create_root_network::migrate_create_root_network::(); - - // let netuid: u16 = 1; - // let tempo: u16 = 13; - // let hotkey_account_id = U256::from(6); - // let burn_cost = 1000; - // let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har - - // //add network - // SubtensorModule::set_burn(netuid, burn_cost); - // add_network(netuid, tempo, 0); - // // Give it some $$$ in his coldkey balance - // SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); - - // // Subscribe and check extrinsic output - // assert_ok!(SubtensorModule::burned_register( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // netuid, - // hotkey_account_id - // )); - // // Check if balance has decreased to pay for the burn. - // assert_eq!( - // SubtensorModule::get_coldkey_balance(&coldkey_account_id), - // (10000 - burn_cost) - // ); // funds drained on reg. - // // Check if neuron has added to the specified network(netuid) - // assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 1); - // // Check if hotkey is added to the Hotkeys - // assert_eq!( - // SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey_account_id), - // coldkey_account_id - // ); - - // // Lets make this new key a delegate with a 10% take. - // assert_ok!(SubtensorModule::do_become_delegate( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // hotkey_account_id, - // u16::MAX / 10 - // )); - - // let staker_coldkey = U256::from(7); - // let stake_amount: u64 = 100_000; - // SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake_amount); - - // assert_ok!(SubtensorModule::add_stake( - // <::RuntimeOrigin>::signed(staker_coldkey), - // hotkey_account_id, - // stake_amount - // )); - // assert_eq!( - // SubtensorModule::get_stake_for_coldkey_and_hotkey(&staker_coldkey, &hotkey_account_id), - // stake_amount - 1 // Need to account for ED - // ); - // assert_eq!( - // SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - // stake_amount - 1 // Need to account for ED - // ); - - // assert_ok!(SubtensorModule::root_register( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // hotkey_account_id - // )); - // assert!(Senate::is_member(&hotkey_account_id)); - - // step_block(100); - - // assert_ok!(SubtensorModule::remove_stake( - // <::RuntimeOrigin>::signed(staker_coldkey), - // hotkey_account_id, - // stake_amount - 1 - // )); - // assert!(Senate::is_member(&hotkey_account_id)); + migrations::migrate_create_root_network::migrate_create_root_network::(); + + let netuid: u16 = 1; + let tempo: u16 = 13; + let hotkey_account_id = U256::from(6); + let burn_cost = 1000; + let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har + + //add network + SubtensorModule::set_burn(netuid, burn_cost); + add_network(netuid, tempo, 0); + // Give it some $$$ in his coldkey balance + SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); + + // Subscribe and check extrinsic output + assert_ok!(SubtensorModule::burned_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + netuid, + hotkey_account_id + )); + // Check if balance has decreased to pay for the burn. + assert_eq!( + SubtensorModule::get_coldkey_balance(&coldkey_account_id), + (10000 - burn_cost) + ); // funds drained on reg. + // Check if neuron has added to the specified network(netuid) + assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 1); + // Check if hotkey is added to the Hotkeys + assert_eq!( + SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey_account_id), + coldkey_account_id + ); + + // Lets make this new key a delegate with a 10% take. + assert_ok!(SubtensorModule::do_become_delegate( + <::RuntimeOrigin>::signed(coldkey_account_id), + hotkey_account_id, + u16::MAX / 10 + )); + + let staker_coldkey = U256::from(7); + let stake_amount: u64 = 100_000; + SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake_amount); + + assert_ok!(SubtensorModule::add_stake( + <::RuntimeOrigin>::signed(staker_coldkey), + hotkey_account_id, + netuid, + stake_amount + )); + assert_eq!( + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey_account_id, + &staker_coldkey, + netuid + ), + stake_amount - 1 // Need to account for ED + ); + assert_eq!( + SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey_account_id, netuid), + stake_amount - 1 // Need to account for ED + ); + + assert_ok!(SubtensorModule::root_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + hotkey_account_id + )); + assert!(Senate::is_member(&hotkey_account_id)); + + step_block(100); + + assert_ok!(SubtensorModule::remove_stake( + <::RuntimeOrigin>::signed(staker_coldkey), + hotkey_account_id, + netuid, + stake_amount - 1 + )); + assert!(Senate::is_member(&hotkey_account_id)); }); } @@ -677,157 +681,160 @@ fn test_senate_join_current_delegate() { fn test_adjust_senate_events() { // Test the events emitted after adjusting the senate successfully new_test_ext().execute_with(|| { - assert!(false); - - // migrations::migrate_create_root_network::migrate_create_root_network::(); - - // let netuid: u16 = 1; - // let tempo: u16 = 13; - // let hotkey_account_id = U256::from(6); - // let burn_cost = 1000; - // let coldkey_account_id = U256::from(667); - // let root_netuid = SubtensorModule::get_root_netuid(); - - // let max_senate_size: u16 = SenateMaxMembers::get() as u16; - // let stake_threshold: u64 = 100_000; // Give this much to every senator - - // // We will be registering MaxMembers hotkeys and two more to try a replace - // let balance_to_add = 50_000 + (stake_threshold + burn_cost) * (max_senate_size + 2) as u64; - - // let replacement_hotkey_account_id = U256::from(7); // Will be added to the senate to replace hotkey_account_id - - // //add network - // SubtensorModule::set_burn(netuid, burn_cost); - // add_network(netuid, tempo, 0); - // // Give some coldkey balance - // SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, balance_to_add); - - // // Allow all registrations in netuid in same block. Same for root network. - // SubtensorModule::set_max_registrations_per_block(netuid, max_senate_size + 1); - // SubtensorModule::set_target_registrations_per_interval(netuid, max_senate_size + 1); - // SubtensorModule::set_max_registrations_per_block(root_netuid, max_senate_size + 1); - // SubtensorModule::set_target_registrations_per_interval(root_netuid, max_senate_size + 1); - - // // Subscribe and check extrinsic output - // assert_ok!(SubtensorModule::burned_register( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // netuid, - // hotkey_account_id - // )); - // // Check if balance has decreased to pay for the burn. - // assert_eq!( - // SubtensorModule::get_coldkey_balance(&coldkey_account_id), - // (balance_to_add - burn_cost) - // ); // funds drained on reg. - // // Check if neuron has added to the specified network(netuid) - // assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 1); - // // Check if hotkey is added to the Hotkeys - // assert_eq!( - // SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey_account_id), - // coldkey_account_id - // ); - - // // Should *NOT* be a member of the senate - // assert!(!Senate::is_member(&hotkey_account_id)); - - // // root register - // assert_ok!(SubtensorModule::root_register( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // hotkey_account_id - // )); // Has no stake, but is now a senate member - - // // Check if they are a member of the senate - // assert!(Senate::is_member(&hotkey_account_id)); - - // // Register MaxMembers - 1 more hotkeys, add stake and join the senate - // for i in 0..(max_senate_size - 1) { - // let new_hotkey_account_id = U256::from(8 + i); - - // assert_ok!(SubtensorModule::burned_register( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // netuid, - // new_hotkey_account_id - // )); - // // Check if this hotkey is added to the Hotkeys - // assert_eq!( - // SubtensorModule::get_owning_coldkey_for_hotkey(&new_hotkey_account_id), - // coldkey_account_id - // ); - // // Add/delegate enough stake to join the senate - // assert_ok!(SubtensorModule::add_stake( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // new_hotkey_account_id, - // stake_threshold + 1 + i as u64 // Increasing with i to make them ordered - // )); // +1 to be above hotkey_account_id - // // Join senate - // assert_ok!(SubtensorModule::root_register( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // new_hotkey_account_id - // )); - // // Check if they are a member of the senate - // assert!(Senate::is_member(&new_hotkey_account_id)); - // } - - // // Verify we are at max senate size - // assert_eq!(Senate::members().len(), max_senate_size as usize); - - // // Verify the replacement hotkey is not a member of the senate - // assert!(!Senate::is_member(&replacement_hotkey_account_id)); - - // // Register - // assert_ok!(SubtensorModule::burned_register( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // netuid, - // replacement_hotkey_account_id - // )); - - // // Register in root network - // assert_ok!(SubtensorModule::root_register( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // replacement_hotkey_account_id - // )); - - // // Check if they are a member of the senate, should not be, - // // as they have no stake - // assert!(!Senate::is_member(&replacement_hotkey_account_id)); - - // // Add/delegate enough stake to join the senate - // assert_ok!(SubtensorModule::add_stake( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // replacement_hotkey_account_id, - // 1 // Will be more than the last one in the senate by stake (has 0 stake) - // )); - // assert_eq!( - // SubtensorModule::get_stake_for_coldkey_and_hotkey( - // &coldkey_account_id, - // &replacement_hotkey_account_id - // ), - // 1 - // ); - // assert_eq!( - // SubtensorModule::get_total_stake_for_hotkey(&replacement_hotkey_account_id), - // 1 - // ); - - // System::reset_events(); - - // // We can call now to adjust the senate - // assert_ok!(SubtensorModule::adjust_senate( - // <::RuntimeOrigin>::signed(coldkey_account_id), - // replacement_hotkey_account_id - // )); - - // // This should make the hotkey a member of the senate - // assert!(Senate::is_member(&replacement_hotkey_account_id)); - - // // Check the events - // assert!( - // System::events().contains(&record(RuntimeEvent::SubtensorModule( - // SubtensorEvent::SenateAdjusted { - // old_member: None, - // new_member: replacement_hotkey_account_id - // } - // ))) - // ); + migrations::migrate_create_root_network::migrate_create_root_network::(); + + let netuid: u16 = 1; + let tempo: u16 = 13; + let hotkey_account_id = U256::from(6); + let burn_cost = 1000; + let coldkey_account_id = U256::from(667); + let root_netuid = SubtensorModule::get_root_netuid(); + + let max_senate_size: u16 = SenateMaxMembers::get() as u16; + let stake_threshold: u64 = 100_000; // Give this much to every senator + + // We will be registering MaxMembers hotkeys and two more to try a replace + let balance_to_add = 50_000 + (stake_threshold + burn_cost) * (max_senate_size + 2) as u64; + + let replacement_hotkey_account_id = U256::from(7); // Will be added to the senate to replace hotkey_account_id + + //add network + SubtensorModule::set_burn(netuid, burn_cost); + add_network(netuid, tempo, 0); + // Give some coldkey balance + SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, balance_to_add); + + // Allow all registrations in netuid in same block. Same for root network. + SubtensorModule::set_max_registrations_per_block(netuid, max_senate_size + 1); + SubtensorModule::set_target_registrations_per_interval(netuid, max_senate_size + 1); + SubtensorModule::set_max_registrations_per_block(root_netuid, max_senate_size + 1); + SubtensorModule::set_target_registrations_per_interval(root_netuid, max_senate_size + 1); + + // Subscribe and check extrinsic output + assert_ok!(SubtensorModule::burned_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + netuid, + hotkey_account_id + )); + // Check if balance has decreased to pay for the burn. + assert_eq!( + SubtensorModule::get_coldkey_balance(&coldkey_account_id), + (balance_to_add - burn_cost) + ); // funds drained on reg. + // Check if neuron has added to the specified network(netuid) + assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 1); + // Check if hotkey is added to the Hotkeys + assert_eq!( + SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey_account_id), + coldkey_account_id + ); + + // Should *NOT* be a member of the senate + assert!(!Senate::is_member(&hotkey_account_id)); + + // root register + assert_ok!(SubtensorModule::root_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + hotkey_account_id + )); // Has no stake, but is now a senate member + + // Check if they are a member of the senate + assert!(Senate::is_member(&hotkey_account_id)); + + // Register MaxMembers - 1 more hotkeys, add stake and join the senate + for i in 0..(max_senate_size - 1) { + let new_hotkey_account_id = U256::from(8 + i); + + assert_ok!(SubtensorModule::burned_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + netuid, + new_hotkey_account_id + )); + // Check if this hotkey is added to the Hotkeys + assert_eq!( + SubtensorModule::get_owning_coldkey_for_hotkey(&new_hotkey_account_id), + coldkey_account_id + ); + // Add/delegate enough stake to join the senate + assert_ok!(SubtensorModule::add_stake( + <::RuntimeOrigin>::signed(coldkey_account_id), + new_hotkey_account_id, + netuid, + stake_threshold + 1 + i as u64 // Increasing with i to make them ordered + )); // +1 to be above hotkey_account_id + // Join senate + assert_ok!(SubtensorModule::root_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + new_hotkey_account_id + )); + // Check if they are a member of the senate + assert!(Senate::is_member(&new_hotkey_account_id)); + } + + // Verify we are at max senate size + assert_eq!(Senate::members().len(), max_senate_size as usize); + + // Verify the replacement hotkey is not a member of the senate + assert!(!Senate::is_member(&replacement_hotkey_account_id)); + + // Register + assert_ok!(SubtensorModule::burned_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + netuid, + replacement_hotkey_account_id + )); + + // Register in root network + assert_ok!(SubtensorModule::root_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + replacement_hotkey_account_id + )); + + // Check if they are a member of the senate, should not be, + // as they have no stake + assert!(!Senate::is_member(&replacement_hotkey_account_id)); + // Add/delegate enough stake to join the senate + assert_ok!(SubtensorModule::add_stake( + <::RuntimeOrigin>::signed(coldkey_account_id), + replacement_hotkey_account_id, + root_netuid, + 1 // Will be more than the last one in the senate by stake (has 0 stake) + )); + assert_eq!( + SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( + &replacement_hotkey_account_id, + &coldkey_account_id, + root_netuid + ), + 1 + ); + assert_eq!( + SubtensorModule::get_stake_for_hotkey_on_subnet( + &replacement_hotkey_account_id, + root_netuid + ), + 1 + ); + + System::reset_events(); + + // We can call now to adjust the senate + assert_ok!(SubtensorModule::adjust_senate( + <::RuntimeOrigin>::signed(coldkey_account_id), + replacement_hotkey_account_id + )); + + // This should make the hotkey a member of the senate + assert!(Senate::is_member(&replacement_hotkey_account_id)); + + // Check the events + assert!( + System::events().contains(&record(RuntimeEvent::SubtensorModule( + SubtensorEvent::SenateAdjusted { + old_member: None, + new_member: replacement_hotkey_account_id + } + ))) + ); }); }