Skip to content

Commit a3ae938

Browse files
authored
Merge pull request #1338 from opentensor/feat/owner-association-ext
add call to associate hk
2 parents 2d70704 + 00c7472 commit a3ae938

File tree

6 files changed

+107
-3
lines changed

6 files changed

+107
-3
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,8 +1383,7 @@ pub mod pallet {
13831383
///
13841384
/// # Arguments
13851385
/// * `origin` - The origin of the call, which must be the root account.
1386-
/// * `precompile_id` - The identifier of the EVM precompile to toggle.
1387-
/// * `enabled` - The new enablement state of the precompile.
1386+
/// * `alpha` - The new moving alpha value for the SubnetMovingAlpha.
13881387
///
13891388
/// # Errors
13901389
/// * `BadOrigin` - If the caller is not the root account.

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,5 +1882,30 @@ mod dispatches {
18821882
allow_partial,
18831883
)
18841884
}
1885+
1886+
/// Attempts to associate a hotkey with a coldkey.
1887+
///
1888+
/// # Arguments
1889+
/// * `origin` - The origin of the transaction, which must be signed by the coldkey that owns the `hotkey`.
1890+
/// * `hotkey` - The hotkey to associate with the coldkey.
1891+
///
1892+
/// # Note
1893+
/// Will charge based on the weight even if the hotkey is already associated with a coldkey.
1894+
#[pallet::call_index(91)]
1895+
#[pallet::weight((
1896+
Weight::from_parts(3_000_000, 0).saturating_add(T::DbWeight::get().reads_writes(3, 3)),
1897+
DispatchClass::Operational,
1898+
Pays::Yes
1899+
))]
1900+
pub fn try_associate_hotkey(
1901+
origin: T::RuntimeOrigin,
1902+
hotkey: T::AccountId,
1903+
) -> DispatchResult {
1904+
let coldkey = ensure_signed(origin)?;
1905+
1906+
let _ = Self::do_try_associate_hotkey(&coldkey, &hotkey);
1907+
1908+
Ok(())
1909+
}
18851910
}
18861911
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use super::*;
2+
3+
impl<T: Config> Pallet<T> {
4+
pub fn do_try_associate_hotkey(
5+
coldkey: &T::AccountId,
6+
hotkey: &T::AccountId,
7+
) -> DispatchResult {
8+
// Ensure the hotkey is not already associated with a coldkey
9+
Self::create_account_if_non_existent(coldkey, hotkey);
10+
11+
Ok(())
12+
}
13+
}

pallets/subtensor/src/staking/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
pub mod account;
23
pub mod add_stake;
34
pub mod decrease_take;
45
pub mod helpers;

pallets/subtensor/src/tests/staking2.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use super::mock::*;
22
use crate::*;
3+
use frame_support::{
4+
assert_ok,
5+
dispatch::{GetDispatchInfo, Pays},
6+
weights::Weight,
7+
};
38
use sp_core::U256;
49
use substrate_fixed::types::I96F32;
510

@@ -557,3 +562,64 @@ fn test_share_based_staking_stake_inject_stake_new() {
557562
});
558563
});
559564
}
565+
566+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::staking2::test_try_associate_hotkey --exact --show-output --nocapture
567+
#[test]
568+
fn test_try_associate_hotkey() {
569+
new_test_ext(1).execute_with(|| {
570+
let hotkey1 = U256::from(1);
571+
let coldkey1 = U256::from(2);
572+
let coldkey2 = U256::from(3);
573+
574+
// Check initial association
575+
assert!(!SubtensorModule::hotkey_account_exists(&hotkey1));
576+
577+
// Associate hotkey1 with coldkey1
578+
assert_ok!(SubtensorModule::try_associate_hotkey(
579+
RuntimeOrigin::signed(coldkey1),
580+
hotkey1
581+
));
582+
583+
// Check that hotkey1 is associated with coldkey1
584+
assert!(SubtensorModule::hotkey_account_exists(&hotkey1));
585+
assert_eq!(
586+
SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey1),
587+
coldkey1
588+
);
589+
assert_ne!(SubtensorModule::get_owned_hotkeys(&coldkey1).len(), 0);
590+
assert!(SubtensorModule::get_owned_hotkeys(&coldkey1).contains(&hotkey1));
591+
592+
// Verify this tx requires a fee
593+
let call =
594+
RuntimeCall::SubtensorModule(crate::Call::try_associate_hotkey { hotkey: hotkey1 });
595+
let dispatch_info = call.get_dispatch_info();
596+
// Verify tx weight > 0
597+
assert!(dispatch_info.weight.all_gte(Weight::from_all(0)));
598+
// Verify pays Yes is set
599+
assert_eq!(dispatch_info.pays_fee, Pays::Yes);
600+
601+
// Check that coldkey2 is not associated with any hotkey
602+
assert!(!SubtensorModule::get_owned_hotkeys(&coldkey2).contains(&hotkey1));
603+
assert_eq!(SubtensorModule::get_owned_hotkeys(&coldkey2).len(), 0);
604+
605+
// Try to associate hotkey1 with coldkey2
606+
// Should have no effect because coldkey1 is already associated with hotkey1
607+
assert_ok!(SubtensorModule::try_associate_hotkey(
608+
RuntimeOrigin::signed(coldkey2),
609+
hotkey1
610+
));
611+
612+
// Check that hotkey1 is still associated with coldkey1
613+
assert!(SubtensorModule::hotkey_account_exists(&hotkey1));
614+
assert_eq!(
615+
SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey1),
616+
coldkey1
617+
);
618+
assert_ne!(SubtensorModule::get_owned_hotkeys(&coldkey1).len(), 0);
619+
assert!(SubtensorModule::get_owned_hotkeys(&coldkey1).contains(&hotkey1));
620+
621+
// Check that coldkey2 is still not associated with any hotkey
622+
assert!(!SubtensorModule::get_owned_hotkeys(&coldkey2).contains(&hotkey1));
623+
assert_eq!(SubtensorModule::get_owned_hotkeys(&coldkey2).len(), 0);
624+
});
625+
}

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
228228
// `spec_version`, and `authoring_version` are the same between Wasm and native.
229229
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
230230
// the compatible custom types.
231-
spec_version: 243,
231+
spec_version: 244,
232232
impl_version: 1,
233233
apis: RUNTIME_API_VERSIONS,
234234
transaction_version: 1,

0 commit comments

Comments
 (0)