Skip to content

Commit b36490c

Browse files
authored
Merge pull request #1339 from opentensor/devnet-ready
devnet deploy 2/24/2025 number 2
2 parents c411178 + 220ab34 commit b36490c

File tree

10 files changed

+179
-7
lines changed

10 files changed

+179
-7
lines changed

docs/rust-setup.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Use a terminal shell to execute the following commands:
2424
```bash
2525
sudo apt update
2626
# May prompt for location information
27-
sudo apt install -y git clang curl libssl-dev llvm libudev-dev
27+
sudo apt install -y git clang curl libssl-dev llvm libudev-dev make pkg-config protobuf-compiler
2828
```
2929

3030
### Arch Linux

pallets/admin-utils/src/lib.rs

+1-2
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

+25
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
}
+13
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/decrease_take.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ impl<T: Config> Pallet<T> {
5757
// --- 4. Set the new take value.
5858
Delegates::<T>::insert(hotkey.clone(), take);
5959

60-
// --- 5. Emit the take value.
60+
// --- 5. Set last block for rate limiting
61+
let block: u64 = Self::get_current_block_as_u64();
62+
Self::set_last_tx_block_delegate_take(&hotkey, block);
63+
64+
// --- 6. Emit the take value.
6165
log::debug!(
6266
"TakeDecreased( coldkey:{:?}, hotkey:{:?}, take:{:?} )",
6367
coldkey,

pallets/subtensor/src/staking/increase_take.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ impl<T: Config> Pallet<T> {
6161
let block: u64 = Self::get_current_block_as_u64();
6262
ensure!(
6363
!Self::exceeds_tx_delegate_take_rate_limit(
64-
Self::get_last_tx_block_delegate_take(&coldkey),
64+
Self::get_last_tx_block_delegate_take(&hotkey),
6565
block
6666
),
6767
Error::<T>::DelegateTxRateLimitExceeded
6868
);
6969

7070
// Set last block for rate limiting
71-
Self::set_last_tx_block_delegate_take(&coldkey, block);
71+
Self::set_last_tx_block_delegate_take(&hotkey, block);
7272

7373
// --- 6. Set the new take value.
7474
Delegates::<T>::insert(hotkey.clone(), take);

pallets/subtensor/src/staking/mod.rs

+1
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/staking.rs

+64
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,70 @@ fn test_rate_limits_enforced_on_increase_take() {
16711671
});
16721672
}
16731673

1674+
// Test rate-limiting on an increase take just after a decrease take
1675+
// Prevents a Validator from decreasing take and then increasing it immediately after.
1676+
#[test]
1677+
fn test_rate_limits_enforced_on_decrease_before_increase_take() {
1678+
new_test_ext(1).execute_with(|| {
1679+
// Make account
1680+
let hotkey0 = U256::from(1);
1681+
let coldkey0 = U256::from(3);
1682+
1683+
// Add balance
1684+
SubtensorModule::add_balance_to_coldkey_account(&coldkey0, 100000);
1685+
1686+
// Register the neuron to a new network
1687+
let netuid = 1;
1688+
add_network(netuid, 1, 0);
1689+
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);
1690+
1691+
// Coldkey / hotkey 0 become delegates with 9% take
1692+
Delegates::<Test>::insert(hotkey0, SubtensorModule::get_min_delegate_take() + 1);
1693+
assert_eq!(
1694+
SubtensorModule::get_hotkey_take(&hotkey0),
1695+
SubtensorModule::get_min_delegate_take() + 1
1696+
);
1697+
1698+
// Decrease take
1699+
assert_ok!(SubtensorModule::do_decrease_take(
1700+
RuntimeOrigin::signed(coldkey0),
1701+
hotkey0,
1702+
SubtensorModule::get_min_delegate_take()
1703+
)); // Verify decrease
1704+
assert_eq!(
1705+
SubtensorModule::get_hotkey_take(&hotkey0),
1706+
SubtensorModule::get_min_delegate_take()
1707+
);
1708+
1709+
// Increase take immediately after
1710+
assert_eq!(
1711+
SubtensorModule::do_increase_take(
1712+
RuntimeOrigin::signed(coldkey0),
1713+
hotkey0,
1714+
SubtensorModule::get_min_delegate_take() + 1
1715+
),
1716+
Err(Error::<Test>::DelegateTxRateLimitExceeded.into())
1717+
); // Verify no change
1718+
assert_eq!(
1719+
SubtensorModule::get_hotkey_take(&hotkey0),
1720+
SubtensorModule::get_min_delegate_take()
1721+
);
1722+
1723+
step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16);
1724+
1725+
// Can increase after waiting
1726+
assert_ok!(SubtensorModule::do_increase_take(
1727+
RuntimeOrigin::signed(coldkey0),
1728+
hotkey0,
1729+
SubtensorModule::get_min_delegate_take() + 1
1730+
)); // Verify increase
1731+
assert_eq!(
1732+
SubtensorModule::get_hotkey_take(&hotkey0),
1733+
SubtensorModule::get_min_delegate_take() + 1
1734+
);
1735+
});
1736+
}
1737+
16741738
#[test]
16751739
fn test_get_total_delegated_stake_after_unstaking() {
16761740
new_test_ext(1).execute_with(|| {

pallets/subtensor/src/tests/staking2.rs

+66
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

+1-1
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)