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

Delegate Id Coldkey Swap #706

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
19 changes: 12 additions & 7 deletions pallets/subtensor/src/swap/swap_coldkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,41 @@ impl<T: Config> Pallet<T> {
Error::<T>::ColdKeyAlreadyAssociated
);

// 5. Calculate the swap cost and ensure sufficient balance
// 5. Swap the identity if the old coldkey has one
if let Some(identity) = Identities::<T>::take(&old_coldkey) {
Identities::<T>::insert(new_coldkey, identity);
}

// 6. Calculate the swap cost and ensure sufficient balance
let swap_cost = Self::get_key_swap_cost();
log::debug!("Coldkey swap cost: {:?}", swap_cost);
ensure!(
Self::can_remove_balance_from_coldkey_account(&old_coldkey, swap_cost),
Error::<T>::NotEnoughBalanceToPaySwapColdKey
);

// 6. Remove and burn the swap cost from the old coldkey's account
// 7. Remove and burn the swap cost from the old coldkey's account
let actual_burn_amount =
Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?;
Self::burn_tokens(actual_burn_amount);

// 7. Update the weight for the balance operations
// 8. Update the weight for the balance operations
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));

// 8. Perform the actual coldkey swap
// 9. Perform the actual coldkey swap
let _ = Self::perform_swap_coldkey(&old_coldkey, new_coldkey, &mut weight);

// 9. Update the last transaction block for the new coldkey
// 10. Update the last transaction block for the new coldkey
Self::set_last_tx_block(new_coldkey, Self::get_current_block_as_u64());
weight.saturating_accrue(T::DbWeight::get().writes(1));

// 10. Emit the ColdkeySwapped event
// 11. Emit the ColdkeySwapped event
Self::deposit_event(Event::ColdkeySwapped {
old_coldkey: old_coldkey.clone(),
new_coldkey: new_coldkey.clone(),
});

// 11. Return the result with the updated weight
// 12. Return the result with the updated weight
Ok(Some(weight).into())
}

Expand Down
133 changes: 133 additions & 0 deletions pallets/subtensor/tests/serving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,136 @@ fn test_migrate_set_hotkey_identities() {
);
});
}

#[test]
fn test_coldkey_swap_delegate_identity_updated() {
JohnReedV marked this conversation as resolved.
Show resolved Hide resolved
new_test_ext(1).execute_with(|| {
let old_coldkey = U256::from(1);
let new_coldkey = U256::from(2);

let netuid = 1;
let burn_cost = 10;
let tempo = 1;

SubtensorModule::set_burn(netuid, burn_cost);
add_network(netuid, tempo, 0);

SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, 100_000_000_000);

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

let name: Vec<u8> = b"The Third Coolest Identity".to_vec();
let identity: ChainIdentity = ChainIdentity {
name: name.clone(),
url: vec![],
image: vec![],
discord: vec![],
description: vec![],
additional: vec![],
};

Identities::<Test>::insert(old_coldkey, identity.clone());

assert!(Identities::<Test>::get(old_coldkey).is_some());
assert!(Identities::<Test>::get(new_coldkey).is_none());

assert_ok!(SubtensorModule::do_swap_coldkey(
<<Test as Config>::RuntimeOrigin>::signed(old_coldkey),
&new_coldkey
));

assert!(Identities::<Test>::get(old_coldkey).is_none());
assert!(Identities::<Test>::get(new_coldkey).is_some());
assert_eq!(
Identities::<Test>::get(new_coldkey).expect("Expected an Identity"),
identity
);
});
}

#[test]
fn test_coldkey_swap_no_identity_no_changes() {
new_test_ext(1).execute_with(|| {
let old_coldkey = U256::from(1);
let new_coldkey = U256::from(2);

let netuid = 1;
let burn_cost = 10;
let tempo = 1;

SubtensorModule::set_burn(netuid, burn_cost);
add_network(netuid, tempo, 0);

SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, 100_000_000_000);

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

// Ensure the old coldkey does not have an identity before the swap
assert!(Identities::<Test>::get(old_coldkey).is_none());

// Perform the coldkey swap
assert_ok!(SubtensorModule::do_swap_coldkey(
<<Test as Config>::RuntimeOrigin>::signed(old_coldkey),
&new_coldkey,
));

// Ensure no identities have been changed
assert!(Identities::<Test>::get(old_coldkey).is_none());
assert!(Identities::<Test>::get(new_coldkey).is_none());
});
}

#[test]
fn test_coldkey_swap_no_identity_no_changes_newcoldkey_exists() {
new_test_ext(1).execute_with(|| {
let old_coldkey_2 = U256::from(3);
let new_coldkey_2 = U256::from(4);

let netuid = 1;
let burn_cost = 10;
let tempo = 1;

SubtensorModule::set_burn(netuid, burn_cost);
add_network(netuid, tempo, 0);
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey_2, 100_000_000_000);

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

let name: Vec<u8> = b"The Coolest Identity".to_vec();
let identity: ChainIdentity = ChainIdentity {
name: name.clone(),
url: vec![],
image: vec![],
discord: vec![],
description: vec![],
additional: vec![],
};

Identities::<Test>::insert(new_coldkey_2, identity.clone());
// Ensure the new coldkey does have an identity before the swap
assert!(Identities::<Test>::get(new_coldkey_2).is_some());
assert!(Identities::<Test>::get(old_coldkey_2).is_none());

// Perform the coldkey swap
assert_ok!(SubtensorModule::do_swap_coldkey(
<<Test as Config>::RuntimeOrigin>::signed(old_coldkey_2),
&new_coldkey_2,
));

// Ensure no identities have been changed
assert!(Identities::<Test>::get(old_coldkey_2).is_none());
assert!(Identities::<Test>::get(new_coldkey_2).is_some());
});
}
Loading