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

Schedule Coldkey Swap #620

Merged
merged 57 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
c66aeb9
initial
Jul 5, 2024
4a39895
merge
Jul 5, 2024
bfea68a
push changes
Jul 5, 2024
462ed12
Fix build and straighten up the logic for draining coldkeys
gztensor Jul 5, 2024
7149b5b
Make tests build for draining coldkeys
gztensor Jul 5, 2024
b76408a
merge
Jul 6, 2024
b6bdb30
Merge branch 'arbitrage_coldkeys' of github.com:opentensor/subtensor …
Jul 6, 2024
a499116
Fix coldkey drain
gztensor Jul 6, 2024
3f3201c
Merge branch 'arbitrage_coldkeys' of github.com:opentensor/subtensor …
Jul 6, 2024
c5aef40
check
Jul 6, 2024
8851879
passing here
Jul 6, 2024
e7337af
fxi tests on arbitrated coldkey swap
Jul 6, 2024
cf4c8d5
add more test
Jul 6, 2024
c5b78f8
chore: update comments, lints
Jul 7, 2024
dad3925
fix: tests , feat: benchmarks
Jul 7, 2024
9a3f725
fix: benchmarks
Jul 7, 2024
f32c546
chore: whitelist schedule_arbitrated_coldkey_swap and set_weights, se…
Jul 7, 2024
68df4e1
feat: signed extension arbitration , swap cold key min amount
Jul 7, 2024
507e8cf
chore: lints
Jul 7, 2024
0bcdd26
feat: signed extensions , reduce arbitration time
Jul 7, 2024
cc9c094
feat: pow coldkey swaps ,todo fix tests , dont clear map during abrit…
Jul 7, 2024
60f3713
fix unit tests
open-junius Jul 8, 2024
c210904
fix conflict
open-junius Jul 8, 2024
be043ac
update runtime version
open-junius Jul 8, 2024
25744e4
fix benchmark function
open-junius Jul 8, 2024
8f8b92c
add arithmetic side effects
open-junius Jul 8, 2024
6daad1e
fix clippy in test file
open-junius Jul 8, 2024
fd980b1
chore: add event
Jul 8, 2024
fbe1924
feat: unlimited arb time
Jul 8, 2024
2e2b51d
fix: broken test
Jul 8, 2024
82be091
use on_idle for swap_coldkeys logic
orriin Jul 8, 2024
01c1529
Merge branch 'arbitrage_coldkeys' into arbitrate_coldkeys_on_idle
orriin Jul 8, 2024
247c861
Increase base difficulty to 10M for PoW swaps
gztensor Jul 8, 2024
219413c
Merge pull request #621 from opentensor/arbitrate_coldkeys_on_idle
orriin Jul 8, 2024
c6dab93
add the unit test back
open-junius Jul 8, 2024
e271f37
fix broken tests
Jul 8, 2024
54a1466
chore: fix tests
Jul 8, 2024
7bf6524
chore: fix test_arbitrated_coldkey_swap_multiple_arbitrations
Jul 8, 2024
2891d62
chore: fix tests
Jul 8, 2024
d1ee744
add swap test
Jul 8, 2024
9b4d63f
Merge branch 'arbitrage_coldkeys' of github.com:opentensor/subtensor …
Jul 8, 2024
4a97e79
working
Jul 8, 2024
ba00c23
add stake balance as equal
Jul 8, 2024
41feafc
chore: bump spec version
Jul 8, 2024
b1feb3c
feat: reduce arb period to 18 hours for testnet
Jul 8, 2024
e4c9ef2
feat: allow subnet owners to swap without funds , todo: fix test_comp…
Jul 8, 2024
af555d7
chore: introduce difficulty var to make tests shorter , fix test_comp…
Jul 9, 2024
a14337b
make it easier
open-junius Jul 9, 2024
83819ff
fix fmt
open-junius Jul 9, 2024
4563e78
feat: get total delegated stake
Jul 9, 2024
4eefcf9
fix: delegated stake tests, whitelist serve_axon, lints
Jul 9, 2024
e30c8f4
feat: coldkey swap info
Jul 9, 2024
7aaeb09
feat: move back to on_initialise, lints
Jul 9, 2024
3817b76
chore: bump arb delay back to 3 days
Jul 9, 2024
380aa65
chore: reduce min tao req to 0.1
Jul 9, 2024
fec59cb
feat: whitelist set_commitment
Jul 9, 2024
9de2afb
spec verion , lints
Jul 9, 2024
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
1 change: 1 addition & 0 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ where
C::Api: subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi<Block>,
C::Api: subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi<Block>,
C::Api: subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi<Block>,
C::Api: subtensor_custom_rpc_runtime_api::ColdkeySwapRuntimeApi<Block>,
B: sc_client_api::Backend<Block> + Send + Sync + 'static,
P: TransactionPool + 'static,
{
Expand Down
2 changes: 2 additions & 0 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ parameter_types! {
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
pub const InitialBaseDifficulty: u64 = 10_000; // Base difficulty
}

impl pallet_subtensor::Config for Test {
Expand Down Expand Up @@ -169,6 +170,7 @@ impl pallet_subtensor::Config for Test {
type AlphaHigh = InitialAlphaHigh;
type AlphaLow = InitialAlphaLow;
type LiquidAlphaOn = InitialLiquidAlphaOn;
type InitialBaseDifficulty = InitialBaseDifficulty;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
Expand Down
68 changes: 67 additions & 1 deletion pallets/subtensor/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::sync::Arc;
use sp_api::ProvideRuntimeApi;

pub use subtensor_custom_rpc_runtime_api::{
DelegateInfoRuntimeApi, NeuronInfoRuntimeApi, SubnetInfoRuntimeApi,
ColdkeySwapRuntimeApi, DelegateInfoRuntimeApi, NeuronInfoRuntimeApi, SubnetInfoRuntimeApi,
SubnetRegistrationRuntimeApi,
};

Expand Down Expand Up @@ -51,6 +51,24 @@ pub trait SubtensorCustomApi<BlockHash> {

#[method(name = "subnetInfo_getLockCost")]
fn get_network_lock_cost(&self, at: Option<BlockHash>) -> RpcResult<u64>;
#[method(name = "coldkeySwap_getScheduledColdkeySwap")]
fn get_scheduled_coldkey_swap(
&self,
coldkey_account_vec: Vec<u8>,
at: Option<BlockHash>,
) -> RpcResult<Vec<u8>>;
#[method(name = "coldkeySwap_getRemainingArbitrationPeriod")]
fn get_remaining_arbitration_period(
&self,
coldkey_account_vec: Vec<u8>,
at: Option<BlockHash>,
) -> RpcResult<Vec<u8>>;
#[method(name = "coldkeySwap_getColdkeySwapDestinations")]
fn get_coldkey_swap_destinations(
&self,
coldkey_account_vec: Vec<u8>,
at: Option<BlockHash>,
) -> RpcResult<Vec<u8>>;
}

pub struct SubtensorCustom<C, P> {
Expand Down Expand Up @@ -99,6 +117,7 @@ where
C::Api: NeuronInfoRuntimeApi<Block>,
C::Api: SubnetInfoRuntimeApi<Block>,
C::Api: SubnetRegistrationRuntimeApi<Block>,
C::Api: ColdkeySwapRuntimeApi<Block>,
{
fn get_delegates(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
Expand Down Expand Up @@ -223,4 +242,51 @@ where
Error::RuntimeError(format!("Unable to get subnet lock cost: {:?}", e)).into()
})
}

fn get_scheduled_coldkey_swap(
&self,
coldkey_account_vec: Vec<u8>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

api.get_scheduled_coldkey_swap(at, coldkey_account_vec)
.map_err(|e| {
Error::RuntimeError(format!("Unable to get scheduled coldkey swap: {:?}", e)).into()
})
}

fn get_remaining_arbitration_period(
&self,
coldkey_account_vec: Vec<u8>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

api.get_remaining_arbitration_period(at, coldkey_account_vec)
.map_err(|e| {
Error::RuntimeError(format!(
"Unable to get remaining arbitration period: {:?}",
e
))
.into()
})
}

fn get_coldkey_swap_destinations(
&self,
coldkey_account_vec: Vec<u8>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

api.get_coldkey_swap_destinations(at, coldkey_account_vec)
.map_err(|e| {
Error::RuntimeError(format!("Unable to get coldkey swap destinations: {:?}", e))
.into()
})
}
}
6 changes: 6 additions & 0 deletions pallets/subtensor/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ sp_api::decl_runtime_apis! {
pub trait SubnetRegistrationRuntimeApi {
fn get_network_registration_cost() -> u64;
}

pub trait ColdkeySwapRuntimeApi {
fn get_scheduled_coldkey_swap( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
fn get_remaining_arbitration_period( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
fn get_coldkey_swap_destinations( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
}
}
27 changes: 27 additions & 0 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,31 @@ reveal_weights {
let _ = Subtensor::<T>::commit_weights(<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(hotkey.clone())), netuid, commit_hash);

}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, salt, version_key)

schedule_coldkey_swap {
distributedstatemachine marked this conversation as resolved.
Show resolved Hide resolved
let seed: u32 = 1;
let old_coldkey: T::AccountId = account("OldColdkey", 0, seed);
let new_coldkey: T::AccountId = account("NewColdkey", 0, seed + 1);
let hotkey: T::AccountId = account("Hotkey", 0, seed);

let netuid = 1u16;
let tempo = 1u16;
let block_number: u64 = Subtensor::<T>::get_current_block_as_u64();
let nonce = 0;

// Initialize the network
Subtensor::<T>::init_new_network(netuid, tempo);
Subtensor::<T>::set_network_registration_allowed(netuid, true);

// Add balance to the old coldkey account
let amount_to_be_staked: u64 = 1000000u32.into();
Subtensor::<T>::add_balance_to_coldkey_account(&old_coldkey.clone(), amount_to_be_staked+1000000000);
// Burned register the hotkey with the old coldkey
assert_ok!(Subtensor::<T>::burned_register(
RawOrigin::Signed(old_coldkey.clone()).into(),
netuid,
hotkey.clone()
));

}: schedule_coldkey_swap(RawOrigin::Signed(old_coldkey.clone()), new_coldkey.clone(), vec![], block_number, nonce)
}
73 changes: 57 additions & 16 deletions pallets/subtensor/src/delegate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,19 @@ impl<T: Config> Pallet<T> {
let mut emissions_per_day: U64F64 = U64F64::from_num(0);

for netuid in registrations.iter() {
let _uid = Self::get_uid_for_net_and_hotkey(*netuid, &delegate.clone());
if _uid.is_err() {
continue; // this should never happen
} else {
let uid = _uid.expect("Delegate's UID should be ok");
if let Ok(uid) = Self::get_uid_for_net_and_hotkey(*netuid, &delegate.clone()) {
let validator_permit = Self::get_validator_permit_for_uid(*netuid, uid);
if validator_permit {
validator_permits.push((*netuid).into());
}

let emission: U64F64 = Self::get_emission_for_uid(*netuid, uid).into();
let tempo: U64F64 = Self::get_tempo(*netuid).into();
let epochs_per_day: U64F64 = U64F64::from_num(7200).saturating_div(tempo);
emissions_per_day =
emissions_per_day.saturating_add(emission.saturating_mul(epochs_per_day));
if tempo > U64F64::from_num(0) {
let epochs_per_day: U64F64 = U64F64::from_num(7200).saturating_div(tempo);
emissions_per_day =
emissions_per_day.saturating_add(emission.saturating_mul(epochs_per_day));
}
}
}

Expand All @@ -63,15 +61,15 @@ impl<T: Config> Pallet<T> {

let total_stake: U64F64 = Self::get_total_stake_for_hotkey(&delegate.clone()).into();

let mut return_per_1000: U64F64 = U64F64::from_num(0);

if total_stake > U64F64::from_num(0) {
return_per_1000 = emissions_per_day
let return_per_1000: U64F64 = if total_stake > U64F64::from_num(0) {
emissions_per_day
.saturating_mul(U64F64::from_num(0.82))
.saturating_div(total_stake.saturating_div(U64F64::from_num(1000)));
}
.saturating_div(total_stake.saturating_div(U64F64::from_num(1000)))
} else {
U64F64::from_num(0)
};

return DelegateInfo {
DelegateInfo {
delegate_ss58: delegate.clone(),
take,
nominators,
Expand All @@ -80,7 +78,7 @@ impl<T: Config> Pallet<T> {
validator_permits,
return_per_1000: U64F64::to_num::<u64>(return_per_1000).into(),
total_daily_return: U64F64::to_num::<u64>(emissions_per_day).into(),
};
}
}

pub fn get_delegate(delegate_account_vec: Vec<u8>) -> Option<DelegateInfo<T>> {
Expand Down Expand Up @@ -132,4 +130,47 @@ impl<T: Config> Pallet<T> {

delegates
}

pub fn get_total_delegated_stake(coldkey: &T::AccountId) -> u64 {
let mut total_delegated = 0u64;

// Get all hotkeys associated with this coldkey
let hotkeys = StakingHotkeys::<T>::get(coldkey);

for hotkey in hotkeys {
let owner = Owner::<T>::get(&hotkey);

for (delegator, stake) in Stake::<T>::iter_prefix(&hotkey) {
if delegator != owner {
total_delegated = total_delegated.saturating_add(stake);
}
}
}

log::info!(
"Total delegated stake for coldkey {:?}: {}",
coldkey,
total_delegated
);
total_delegated
}

// Helper function to get total delegated stake for a hotkey
pub fn get_total_hotkey_delegated_stake(hotkey: &T::AccountId) -> u64 {
let mut total_delegated = 0u64;

// Iterate through all delegators for this hotkey
for (delegator, stake) in Stake::<T>::iter_prefix(hotkey) {
if delegator != Self::get_coldkey_for_hotkey(hotkey) {
total_delegated = total_delegated.saturating_add(stake);
}
}

total_delegated
}

// Helper function to get the coldkey associated with a hotkey
pub fn get_coldkey_for_hotkey(hotkey: &T::AccountId) -> T::AccountId {
Owner::<T>::get(hotkey)
}
}
10 changes: 10 additions & 0 deletions pallets/subtensor/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,15 @@ mod errors {
NoBalanceToTransfer,
/// Same coldkey
SameColdkey,
/// The coldkey is in arbitration
ColdkeyIsInArbitration,
/// The new coldkey is already registered for the drain
DuplicateColdkey,
/// Error thrown on a coldkey swap.
ColdkeySwapError,
/// Insufficient Balance to Schedule coldkey swap
InsufficientBalanceToPerformColdkeySwap,
/// The maximum number of coldkey destinations has been reached
MaxColdkeyDestinationsReached,
}
}
14 changes: 14 additions & 0 deletions pallets/subtensor/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,19 @@ mod events {
<T as frame_system::Config>::AccountId,
>>::Balance,
},
/// A coldkey swap has been scheduled
ColdkeySwapScheduled {
/// The account ID of the old coldkey
old_coldkey: T::AccountId,
/// The account ID of the new coldkey
new_coldkey: T::AccountId,
/// The arbitration block for the coldkey swap
arbitration_block: u64,
},
/// The arbitration period has been extended
ArbitrationPeriodExtended {
/// The account ID of the coldkey
coldkey: T::AccountId,
},
}
}
Loading
Loading