Skip to content

Commit a2d161d

Browse files
Merge branch 'devnet-ready' into change-subnet-owner-extrinsics
2 parents 8ab1673 + dd28ad4 commit a2d161d

File tree

25 files changed

+642
-79
lines changed

25 files changed

+642
-79
lines changed

evm-tests/src/contracts/alpha.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,5 +315,18 @@ export const IAlphaABI = [
315315
],
316316
"stateMutability": "view",
317317
"type": "function"
318+
},
319+
{
320+
"inputs": [],
321+
"name": "getCKBurn",
322+
"outputs": [
323+
{
324+
"internalType": "uint256",
325+
"name": "",
326+
"type": "uint256"
327+
}
328+
],
329+
"stateMutability": "view",
330+
"type": "function"
318331
}
319332
]

evm-tests/test/alpha.precompile.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PublicClient } from "viem";
88
import { PolkadotSigner, TypedApi } from "polkadot-api";
99
import { toViemAddress, convertPublicKeyToSs58 } from "../src/address-utils"
1010
import { IAlphaABI, IALPHA_ADDRESS } from "../src/contracts/alpha"
11+
import { u64 } from "@polkadot-api/substrate-bindings";
1112

1213
describe("Test Alpha Precompile", () => {
1314
// init substrate part
@@ -209,6 +210,24 @@ describe("Test Alpha Precompile", () => {
209210
assert.ok(typeof alphaIssuance === 'bigint', "Alpha issuance should be a bigint");
210211
assert.ok(alphaIssuance >= BigInt(0), "Alpha issuance should be non-negative");
211212
});
213+
214+
it("getCKBurn returns valid CK burn rate", async () => {
215+
const ckBurn = await publicClient.readContract({
216+
abi: IAlphaABI,
217+
address: toViemAddress(IALPHA_ADDRESS),
218+
functionName: "getCKBurn",
219+
args: []
220+
})
221+
222+
const ckBurnOnChain = await api.query.SubtensorModule.CKBurn.getValue()
223+
224+
assert.strictEqual(ckBurn, ckBurnOnChain, "CK burn should match on chain");
225+
assert.ok(ckBurn !== undefined, "CK burn should be defined");
226+
const ckBurnPercentage = BigInt(ckBurn) * BigInt(100) / BigInt(2 ** 64 - 1)
227+
assert.ok(ckBurnPercentage >= BigInt(0), "CK burn percentage should be non-negative");
228+
assert.ok(ckBurnPercentage <= BigInt(100), "CK burn percentage should be less than or equal to 100");
229+
assert.ok(typeof ckBurn === 'bigint', "CK burn should be a bigint");
230+
});
212231
});
213232

214233
describe("Global Functions", () => {

pallets/admin-utils/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,20 @@ pub mod pallet {
16051605
pallet_subtensor::Pallet::<T>::set_owner_immune_neuron_limit(netuid, immune_neurons)?;
16061606
Ok(())
16071607
}
1608+
1609+
/// Sets the childkey burn for a subnet.
1610+
/// It is only callable by the root account.
1611+
/// The extrinsic will call the Subtensor pallet to set the childkey burn.
1612+
#[pallet::call_index(73)]
1613+
#[pallet::weight(Weight::from_parts(15_650_000, 0)
1614+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
1615+
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
1616+
pub fn sudo_set_ck_burn(origin: OriginFor<T>, burn: u64) -> DispatchResult {
1617+
ensure_root(origin)?;
1618+
pallet_subtensor::Pallet::<T>::set_ck_burn(burn);
1619+
log::debug!("CKBurnSet( burn: {burn:?} ) ");
1620+
Ok(())
1621+
}
16081622
}
16091623
}
16101624

pallets/subtensor/src/coinbase/root.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,10 @@ impl<T: Config> Pallet<T> {
565565
NetworkLastLockCost::<T>::get()
566566
}
567567
pub fn get_network_last_lock_block() -> u64 {
568-
NetworkLastRegistered::<T>::get()
568+
Self::get_rate_limited_last_block(&RateLimitKey::NetworkLastRegistered)
569569
}
570570
pub fn set_network_last_lock_block(block: u64) {
571-
NetworkLastRegistered::<T>::set(block);
571+
Self::set_rate_limited_last_block(&RateLimitKey::NetworkLastRegistered, block);
572572
}
573573
pub fn set_lock_reduction_interval(interval: u64) {
574574
NetworkLockReductionInterval::<T>::set(interval);
@@ -588,10 +588,13 @@ impl<T: Config> Pallet<T> {
588588
let halved_interval: I64F64 = interval.saturating_mul(halving);
589589
halved_interval.saturating_to_num::<u64>()
590590
}
591-
pub fn get_rate_limited_last_block(rate_limit_key: &RateLimitKey) -> u64 {
591+
pub fn get_rate_limited_last_block(rate_limit_key: &RateLimitKey<T::AccountId>) -> u64 {
592592
LastRateLimitedBlock::<T>::get(rate_limit_key)
593593
}
594-
pub fn set_rate_limited_last_block(rate_limit_key: &RateLimitKey, block: u64) {
595-
LastRateLimitedBlock::<T>::set(rate_limit_key, block);
594+
pub fn set_rate_limited_last_block(rate_limit_key: &RateLimitKey<T::AccountId>, block: u64) {
595+
LastRateLimitedBlock::<T>::insert(rate_limit_key, block);
596+
}
597+
pub fn remove_rate_limited_last_block(rate_limit_key: &RateLimitKey<T::AccountId>) {
598+
LastRateLimitedBlock::<T>::remove(rate_limit_key);
596599
}
597600
}

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -744,18 +744,19 @@ impl<T: Config> Pallet<T> {
744744
// Calculate the hotkey's share of the validator emission based on its childkey take
745745
let validating_emission: U96F32 = U96F32::saturating_from_num(dividends);
746746
let mut remaining_emission: U96F32 = validating_emission;
747-
let childkey_take_proportion: U96F32 =
747+
let burn_take_proportion: U96F32 = Self::get_ck_burn();
748+
let child_take_proportion: U96F32 =
748749
U96F32::saturating_from_num(Self::get_childkey_take(hotkey, netuid))
749750
.safe_div(U96F32::saturating_from_num(u16::MAX));
750-
log::debug!("Childkey take proportion: {childkey_take_proportion:?} for hotkey {hotkey:?}");
751+
log::debug!("Childkey take proportion: {child_take_proportion:?} for hotkey {hotkey:?}");
751752
// NOTE: Only the validation emission should be split amongst parents.
752753

753754
// Grab the owner of the childkey.
754755
let childkey_owner = Self::get_owning_coldkey_for_hotkey(hotkey);
755756

756757
// Initialize variables to track emission distribution
757758
let mut to_parents: u64 = 0;
758-
let mut total_child_emission_take: U96F32 = U96F32::saturating_from_num(0);
759+
let mut total_child_take: U96F32 = U96F32::saturating_from_num(0);
759760

760761
// Initialize variables to calculate total stakes from parents
761762
let mut total_contribution: U96F32 = U96F32::saturating_from_num(0);
@@ -819,23 +820,26 @@ impl<T: Config> Pallet<T> {
819820
remaining_emission = remaining_emission.saturating_sub(parent_emission);
820821

821822
// Get the childkey take for this parent.
822-
let child_emission_take: U96F32 = if parent_owner == childkey_owner {
823-
// The parent is from the same coldkey, so we don't remove any childkey take.
824-
U96F32::saturating_from_num(0)
825-
} else {
826-
childkey_take_proportion
827-
.saturating_mul(U96F32::saturating_from_num(parent_emission))
823+
let mut burn_take: U96F32 = U96F32::saturating_from_num(0);
824+
let mut child_take: U96F32 = U96F32::saturating_from_num(0);
825+
if parent_owner != childkey_owner {
826+
// The parent is from a different coldkey, we burn some proportion
827+
burn_take = burn_take_proportion.saturating_mul(parent_emission);
828+
child_take = child_take_proportion.saturating_mul(parent_emission);
829+
parent_emission = parent_emission.saturating_sub(burn_take);
830+
parent_emission = parent_emission.saturating_sub(child_take);
831+
total_child_take = total_child_take.saturating_add(child_take);
832+
833+
Self::burn_subnet_alpha(
834+
netuid,
835+
AlphaCurrency::from(burn_take.saturating_to_num::<u64>()),
836+
);
828837
};
838+
log::debug!("burn_takee: {burn_take:?} for hotkey {hotkey:?}");
839+
log::debug!("child_take: {child_take:?} for hotkey {hotkey:?}");
840+
log::debug!("parent_emission: {parent_emission:?} for hotkey {hotkey:?}");
841+
log::debug!("total_child_take: {total_child_take:?} for hotkey {hotkey:?}");
829842

830-
// Remove the childkey take from the parent's emission.
831-
parent_emission = parent_emission.saturating_sub(child_emission_take);
832-
833-
// Add the childkey take to the total childkey take tracker.
834-
total_child_emission_take =
835-
total_child_emission_take.saturating_add(child_emission_take);
836-
837-
log::debug!("Child emission take: {child_emission_take:?} for hotkey {hotkey:?}");
838-
log::debug!("Parent emission: {parent_emission:?} for hotkey {hotkey:?}");
839843
log::debug!("remaining emission: {remaining_emission:?}");
840844

841845
// Add the parent's emission to the distribution list
@@ -853,7 +857,7 @@ impl<T: Config> Pallet<T> {
853857
// Calculate the final emission for the hotkey itself.
854858
// This includes the take left from the parents and the self contribution.
855859
let child_emission = remaining_emission
856-
.saturating_add(total_child_emission_take)
860+
.saturating_add(total_child_take)
857861
.saturating_to_num::<u64>()
858862
.into();
859863

pallets/subtensor/src/lib.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern crate alloc;
5454

5555
pub const MAX_CRV3_COMMIT_SIZE_BYTES: u32 = 5000;
5656

57+
#[allow(deprecated)]
5758
#[deny(missing_docs)]
5859
#[import_section(errors::errors)]
5960
#[import_section(events::events)]
@@ -525,11 +526,6 @@ pub mod pallet {
525526
T::InitialNetworkImmunityPeriod::get()
526527
}
527528
#[pallet::type_value]
528-
/// Default value for network last registered.
529-
pub fn DefaultNetworkLastRegistered<T: Config>() -> u64 {
530-
0
531-
}
532-
#[pallet::type_value]
533529
/// Default value for network min allowed UIDs.
534530
pub fn DefaultNetworkMinAllowedUids<T: Config>() -> u16 {
535531
T::InitialNetworkMinAllowedUids::get()
@@ -867,6 +863,12 @@ pub mod pallet {
867863
50400
868864
}
869865

866+
#[pallet::type_value]
867+
/// Default value for ck burn, 18%.
868+
pub fn DefaultCKBurn<T: Config>() -> u64 {
869+
u64::MAX / 100 * 18
870+
}
871+
870872
#[pallet::storage]
871873
pub type MinActivityCutoff<T: Config> =
872874
StorageValue<_, u16, ValueQuery, DefaultMinActivityCutoff<T>>;
@@ -920,6 +922,9 @@ pub mod pallet {
920922
/// --- ITEM --> Global weight
921923
pub type TaoWeight<T> = StorageValue<_, u64, ValueQuery, DefaultTaoWeight<T>>;
922924
#[pallet::storage]
925+
/// --- ITEM --> CK burn
926+
pub type CKBurn<T> = StorageValue<_, u64, ValueQuery, DefaultCKBurn<T>>;
927+
#[pallet::storage]
923928
/// --- ITEM ( default_delegate_take )
924929
pub type MaxDelegateTake<T> = StorageValue<_, u16, ValueQuery, DefaultDelegateTake<T>>;
925930
#[pallet::storage]
@@ -1164,10 +1169,6 @@ pub mod pallet {
11641169
pub type NetworkImmunityPeriod<T> =
11651170
StorageValue<_, u64, ValueQuery, DefaultNetworkImmunityPeriod<T>>;
11661171
#[pallet::storage]
1167-
/// ITEM( network_last_registered_block )
1168-
pub type NetworkLastRegistered<T> =
1169-
StorageValue<_, u64, ValueQuery, DefaultNetworkLastRegistered<T>>;
1170-
#[pallet::storage]
11711172
/// ITEM( min_network_lock_cost )
11721173
pub type NetworkMinLockCost<T> =
11731174
StorageValue<_, TaoCurrency, ValueQuery, DefaultNetworkMinLockCost<T>>;
@@ -1200,7 +1201,7 @@ pub mod pallet {
12001201
#[pallet::storage]
12011202
/// --- MAP ( RateLimitKey ) --> Block number in which the last rate limited operation occured
12021203
pub type LastRateLimitedBlock<T: Config> =
1203-
StorageMap<_, Identity, RateLimitKey, u64, ValueQuery, DefaultZeroU64<T>>;
1204+
StorageMap<_, Identity, RateLimitKey<T::AccountId>, u64, ValueQuery, DefaultZeroU64<T>>;
12041205

12051206
/// ============================
12061207
/// ==== Subnet Locks =====
@@ -1657,14 +1658,17 @@ pub mod pallet {
16571658
u64,
16581659
ValueQuery,
16591660
>;
1661+
#[deprecated]
16601662
#[pallet::storage]
16611663
/// --- MAP ( key ) --> last_block
16621664
pub type LastTxBlock<T: Config> =
16631665
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
1666+
#[deprecated]
16641667
#[pallet::storage]
16651668
/// --- MAP ( key ) --> last_tx_block_childkey_take
16661669
pub type LastTxBlockChildKeyTake<T: Config> =
16671670
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
1671+
#[deprecated]
16681672
#[pallet::storage]
16691673
/// --- MAP ( key ) --> last_tx_block_delegate_take
16701674
pub type LastTxBlockDelegateTake<T: Config> =
@@ -2135,9 +2139,17 @@ impl<T: Config + pallet_balances::Config<Balance = u64>>
21352139
/// Enum that defines types of rate limited operations for
21362140
/// storing last block when this operation occured
21372141
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)]
2138-
pub enum RateLimitKey {
2142+
pub enum RateLimitKey<AccountId> {
21392143
// The setting sn owner hotkey operation is rate limited per netuid
21402144
SetSNOwnerHotkey(NetUid),
2145+
// Subnet registration rate limit
2146+
NetworkLastRegistered,
2147+
// Last tx block limit per account ID
2148+
LastTxBlock(AccountId),
2149+
// Last tx block child key limit per account ID
2150+
LastTxBlockChildKeyTake(AccountId),
2151+
// Last tx block delegate key limit per account ID
2152+
LastTxBlockDelegateTake(AccountId),
21412153
}
21422154

21432155
pub trait ProxyInterface<AccountId> {

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ mod dispatches {
20312031
/// * commit_reveal_version (`u16`):
20322032
/// - The client (bittensor-drand) version
20332033
#[pallet::call_index(113)]
2034-
#[pallet::weight((Weight::from_parts(64_530_000, 0)
2034+
#[pallet::weight((Weight::from_parts(80_690_000, 0)
20352035
.saturating_add(T::DbWeight::get().reads(7_u64))
20362036
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
20372037
pub fn commit_timelocked_weights(

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ mod hooks {
135135
//Migrate CRV3 to TimelockedCommits
136136
.saturating_add(migrations::migrate_crv3_v2_to_timelocked::migrate_crv3_v2_to_timelocked::<T>())
137137
// Migrate to fix root counters
138-
.saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::<T>());
138+
.saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::<T>())
139+
// Migrate last block rate limiting storage items
140+
.saturating_add(migrations::migrate_rate_limiting_last_blocks::migrate_obsolete_rate_limiting_last_blocks_storage::<T>());
139141
weight
140142
}
141143

0 commit comments

Comments
 (0)