diff --git a/Cargo.lock b/Cargo.lock index 3e93e6db44..255a8b7a4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8222,6 +8222,7 @@ dependencies = [ "num-traits", "pallet-commitments", "pallet-drand", + "pallet-rate-limiting", "pallet-shield", "pallet-subtensor", "pallet-subtensor-swap-rpc", @@ -8342,6 +8343,8 @@ dependencies = [ "pallet-nomination-pools-runtime-api", "pallet-offences", "pallet-preimage", + "pallet-rate-limiting", + "pallet-rate-limiting-runtime-api", "pallet-registry", "pallet-safe-mode", "pallet-scheduler", @@ -8365,6 +8368,7 @@ dependencies = [ "precompile-utils", "rand_chacha 0.3.1", "scale-info", + "serde", "serde_json", "sha2 0.10.9", "smallvec", @@ -8771,6 +8775,7 @@ dependencies = [ "pallet-subtensor", "pallet-subtensor-swap", "parity-scale-codec", + "rate-limiting-interface", "scale-info", "sp-consensus-aura", "sp-consensus-grandpa", @@ -10303,6 +10308,49 @@ dependencies = [ "sp-runtime", ] +[[package]] +name = "pallet-rate-limiting" +version = "0.1.0" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "parity-scale-codec", + "rate-limiting-interface", + "scale-info", + "serde", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "subtensor-runtime-common", +] + +[[package]] +name = "pallet-rate-limiting-rpc" +version = "0.1.0" +dependencies = [ + "jsonrpsee", + "pallet-rate-limiting-runtime-api", + "sp-api", + "sp-blockchain", + "sp-runtime", + "subtensor-runtime-common", +] + +[[package]] +name = "pallet-rate-limiting-runtime-api" +version = "0.1.0" +dependencies = [ + "pallet-rate-limiting", + "parity-scale-codec", + "scale-info", + "serde", + "sp-api", + "sp-std", + "subtensor-runtime-common", +] + [[package]] name = "pallet-recovery" version = "41.0.0" @@ -10780,6 +10828,7 @@ dependencies = [ "pallet-crowdloan", "pallet-drand", "pallet-preimage", + "pallet-rate-limiting", "pallet-scheduler", "pallet-subtensor-proxy", "pallet-subtensor-swap", @@ -10789,6 +10838,7 @@ dependencies = [ "polkadot-runtime-common", "rand 0.8.5", "rand_chacha 0.3.1", + "rate-limiting-interface", "safe-math", "scale-info", "serde", @@ -13681,6 +13731,17 @@ dependencies = [ "rand_core 0.9.3", ] +[[package]] +name = "rate-limiting-interface" +version = "0.1.0" +dependencies = [ + "frame-support", + "parity-scale-codec", + "scale-info", + "serde", + "sp-std", +] + [[package]] name = "raw-cpuid" version = "11.6.0" @@ -18033,6 +18094,7 @@ dependencies = [ "pallet-subtensor-utility", "pallet-timestamp", "parity-scale-codec", + "rate-limiting-interface", "scale-info", "sp-core", "sp-io", @@ -18105,6 +18167,7 @@ dependencies = [ "pallet-evm-precompile-modexp", "pallet-evm-precompile-sha3fips", "pallet-evm-precompile-simple", + "pallet-rate-limiting", "pallet-subtensor", "pallet-subtensor-proxy", "pallet-subtensor-swap", @@ -18177,6 +18240,7 @@ dependencies = [ "pallet-subtensor-swap", "pallet-transaction-payment", "parity-scale-codec", + "rate-limiting-interface", "scale-info", "smallvec", "sp-consensus-aura", diff --git a/Cargo.toml b/Cargo.toml index 4a51bda878..cee570bc92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,8 @@ members = [ "common", "node", "pallets/*", + "pallets/rate-limiting/runtime-api", + "pallets/rate-limiting/rpc", "precompiles", "primitives/*", "runtime", @@ -59,6 +61,9 @@ pallet-subtensor = { path = "pallets/subtensor", default-features = false } pallet-subtensor-swap = { path = "pallets/swap", default-features = false } pallet-subtensor-swap-runtime-api = { path = "pallets/swap/runtime-api", default-features = false } pallet-subtensor-swap-rpc = { path = "pallets/swap/rpc", default-features = false } +pallet-rate-limiting = { path = "pallets/rate-limiting", default-features = false } +pallet-rate-limiting-runtime-api = { path = "pallets/rate-limiting/runtime-api", default-features = false } +pallet-rate-limiting-rpc = { path = "pallets/rate-limiting/rpc", default-features = false } procedural-fork = { path = "support/procedural-fork", default-features = false } safe-math = { path = "primitives/safe-math", default-features = false } share-pool = { path = "primitives/share-pool", default-features = false } @@ -70,6 +75,7 @@ subtensor-runtime-common = { default-features = false, path = "common" } subtensor-swap-interface = { default-features = false, path = "pallets/swap-interface" } subtensor-transaction-fee = { default-features = false, path = "pallets/transaction-fee" } subtensor-chain-extensions = { default-features = false, path = "chain-extensions" } +rate-limiting-interface = { default-features = false, path = "pallets/rate-limiting-interface" } ed25519-dalek = { version = "2.1.0", default-features = false } async-trait = "0.1" diff --git a/chain-extensions/Cargo.toml b/chain-extensions/Cargo.toml index 9727439e7a..61ec5b12e6 100644 --- a/chain-extensions/Cargo.toml +++ b/chain-extensions/Cargo.toml @@ -36,6 +36,9 @@ subtensor-swap-interface.workspace = true num_enum.workspace = true substrate-fixed.workspace = true +[dev-dependencies] +rate-limiting-interface.workspace = true + [lints] workspace = true diff --git a/chain-extensions/src/mock.rs b/chain-extensions/src/mock.rs index 660aa0c77c..7e1eb85b2d 100644 --- a/chain-extensions/src/mock.rs +++ b/chain-extensions/src/mock.rs @@ -18,6 +18,7 @@ use pallet_contracts::HoldReason as ContractsHoldReason; use pallet_subtensor::*; use pallet_subtensor_proxy as pallet_proxy; use pallet_subtensor_utility as pallet_utility; +use rate_limiting_interface::{RateLimitingInterface, TryIntoRateLimitTarget}; use sp_core::{ConstU64, H256, U256, offchain::KeyTypeId}; use sp_runtime::Perbill; use sp_runtime::{ @@ -25,7 +26,9 @@ use sp_runtime::{ traits::{BlakeTwo256, Convert, IdentityLookup}, }; use sp_std::{cell::RefCell, cmp::Ordering, sync::OnceLock}; -use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; +use subtensor_runtime_common::{ + AlphaCurrency, NetUid, TaoCurrency, rate_limiting::RateLimitUsageKey, +}; type Block = frame_system::mocking::MockBlock; @@ -291,9 +294,6 @@ parameter_types! { pub const InitialMinChildKeyTake: u16 = 0; // 0 %; pub const InitialMaxChildKeyTake: u16 = 11_796; // 18 %; pub const InitialWeightsVersionKey: u16 = 0; - pub const InitialServingRateLimit: u64 = 0; // No limit. - pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing - pub const InitialTxDelegateTakeRateLimit: u64 = 1; // 1 block take rate limit for testing pub const InitialTxChildKeyTakeRateLimit: u64 = 1; // 1 block take rate limit for testing pub const InitialBurn: u64 = 0; pub const InitialMinBurn: u64 = 500_000; @@ -319,7 +319,6 @@ parameter_types! { pub const InitialNetworkMinLockCost: u64 = 100_000_000_000; pub const InitialSubnetOwnerCut: u16 = 0; // 0%. 100% of rewards go to validators + miners. pub const InitialNetworkLockReductionInterval: u64 = 2; // 2 blocks. - pub const InitialNetworkRateLimit: u64 = 0; pub const InitialKeySwapCost: u64 = 1_000_000_000; 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 @@ -377,9 +376,6 @@ impl pallet_subtensor::Config for Test { type InitialWeightsVersionKey = InitialWeightsVersionKey; type InitialMaxDifficulty = InitialMaxDifficulty; type InitialMinDifficulty = InitialMinDifficulty; - type InitialServingRateLimit = InitialServingRateLimit; - type InitialTxRateLimit = InitialTxRateLimit; - type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit; type InitialBurn = InitialBurn; type InitialMaxBurn = InitialMaxBurn; type InitialMinBurn = InitialMinBurn; @@ -390,7 +386,6 @@ impl pallet_subtensor::Config for Test { type InitialNetworkMinLockCost = InitialNetworkMinLockCost; type InitialSubnetOwnerCut = InitialSubnetOwnerCut; type InitialNetworkLockReductionInterval = InitialNetworkLockReductionInterval; - type InitialNetworkRateLimit = InitialNetworkRateLimit; type KeySwapCost = InitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; @@ -411,6 +406,7 @@ impl pallet_subtensor::Config for Test { type GetCommitments = (); type MaxImmuneUidsPercentage = MaxImmuneUidsPercentage; type CommitmentsInterface = CommitmentsI; + type RateLimiting = NoRateLimiting; type EvmKeyAssociateRateLimit = EvmKeyAssociateRateLimit; } @@ -449,6 +445,42 @@ impl CommitmentsInterface for CommitmentsI { fn purge_netuid(_netuid: NetUid) {} } +pub struct NoRateLimiting; + +impl RateLimitingInterface for NoRateLimiting { + type GroupId = subtensor_runtime_common::rate_limiting::GroupId; + type CallMetadata = RuntimeCall; + type Limit = BlockNumber; + type Scope = subtensor_runtime_common::NetUid; + type UsageKey = RateLimitUsageKey; + + fn rate_limit(_target: TargetArg, _scope: Option) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + None + } + + fn last_seen( + _target: TargetArg, + _usage_key: Option, + ) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + None + } + + fn set_last_seen( + _target: TargetArg, + _usage_key: Option, + _block: Option, + ) where + TargetArg: TryIntoRateLimitTarget, + { + } +} + parameter_types! { pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * BlockWeights::get().max_block; @@ -687,11 +719,6 @@ pub fn add_dynamic_network(hotkey: &U256, coldkey: &U256) -> NetUid { netuid } -#[allow(dead_code)] -pub(crate) fn remove_stake_rate_limit_for_tests(hotkey: &U256, coldkey: &U256, netuid: NetUid) { - StakingOperationRateLimiter::::remove((hotkey, coldkey, netuid)); -} - #[allow(dead_code)] pub(crate) fn setup_reserves(netuid: NetUid, tao: TaoCurrency, alpha: AlphaCurrency) { SubnetTAO::::set(netuid, tao); diff --git a/chain-extensions/src/tests.rs b/chain-extensions/src/tests.rs index bd6f46c8ab..41c81e8664 100644 --- a/chain-extensions/src/tests.rs +++ b/chain-extensions/src/tests.rs @@ -100,8 +100,6 @@ fn remove_stake_full_limit_success_with_limit_price() { stake_amount_raw.into(), )); - mock::remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); - let expected_weight = Weight::from_parts(395_300_000, 0) .saturating_add(::DbWeight::get().reads(28)) .saturating_add(::DbWeight::get().writes(14)); @@ -166,8 +164,6 @@ fn swap_stake_limit_with_tight_price_returns_slippage_error() { stake_alpha, ); - mock::remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid_a); - let alpha_origin_before = pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, &coldkey, netuid_a, @@ -239,8 +235,6 @@ fn remove_stake_limit_success_respects_price_limit() { stake_amount_raw.into(), )); - mock::remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); - let alpha_before = pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, &coldkey, netuid, @@ -390,8 +384,6 @@ fn swap_stake_success_moves_between_subnets() { stake_amount_raw.into(), )); - mock::remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid_a); - let alpha_origin_before = pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, &coldkey, netuid_a, @@ -467,8 +459,6 @@ fn transfer_stake_success_moves_between_coldkeys() { stake_amount_raw.into(), )); - mock::remove_stake_rate_limit_for_tests(&hotkey, &origin_coldkey, netuid); - let alpha_before = pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, @@ -551,8 +541,6 @@ fn move_stake_success_moves_alpha_between_hotkeys() { stake_amount_raw.into(), )); - mock::remove_stake_rate_limit_for_tests(&origin_hotkey, &coldkey, netuid); - let alpha_before = pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( &origin_hotkey, @@ -631,8 +619,6 @@ fn unstake_all_alpha_success_moves_stake_to_root() { stake_amount_raw.into(), )); - mock::remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); - let expected_weight = Weight::from_parts(358_500_000, 0) .saturating_add(::DbWeight::get().reads(36)) .saturating_add(::DbWeight::get().writes(21)); @@ -940,8 +926,6 @@ fn unstake_all_success_unstakes_balance() { stake_amount_raw.into(), )); - mock::remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); - let expected_weight = Weight::from_parts(28_830_000, 0) .saturating_add(::DbWeight::get().reads(6)) .saturating_add(::DbWeight::get().writes(0)); diff --git a/common/src/lib.rs b/common/src/lib.rs index 658f8b2e01..eac4d49db5 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -17,6 +17,7 @@ use subtensor_macros::freeze_struct; pub use currency::*; mod currency; +pub mod rate_limiting; /// Balance of an account. pub type Balance = u64; diff --git a/common/src/rate_limiting.rs b/common/src/rate_limiting.rs new file mode 100644 index 0000000000..c8c563878d --- /dev/null +++ b/common/src/rate_limiting.rs @@ -0,0 +1,110 @@ +//! Shared rate-limiting types. +//! +//! Note: `pallet-rate-limiting` supports multiple independent instances, and is intended to be used +//! as “one instance per pallet” with pallet-specific scope/usage-key types and resolvers. +//! +//! The scope/usage-key types in this module are centralized today due to the current state of +//! `pallet-subtensor` (a large, centralized pallet) and its coupling with `pallet-admin-utils`, +//! which share a single `pallet-rate-limiting` instance and resolver implementation in the runtime. +//! +//! For new pallets, it is strongly recommended to: +//! - define their own `LimitScope` and `UsageKey` types (do not extend `RateLimitUsageKey` here), +//! - provide pallet-local scope/usage resolvers, +//! - and use a dedicated `pallet-rate-limiting` instance. +//! +//! Long-term, we should move away from these shared types by refactoring `pallet-subtensor` into +//! smaller pallets with dedicated `pallet-rate-limiting` instances. + +use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen}; +use frame_support::pallet_prelude::Parameter; +use scale_info::TypeInfo; +use serde::{Deserialize, Serialize}; + +use crate::{MechId, NetUid}; + +/// Identifier type for rate-limiting groups. +pub type GroupId = u32; + +/// Group id for serving-related calls. +pub const GROUP_SERVE: GroupId = 0; +/// Group id for delegate-take related calls. +pub const GROUP_DELEGATE_TAKE: GroupId = 1; +/// Group id for subnet weight-setting calls. +pub const GROUP_WEIGHTS_SET: GroupId = 2; +/// Group id for network registration calls. +pub const GROUP_REGISTER_NETWORK: GroupId = 3; +/// Group id for owner hyperparameter calls. +pub const GROUP_OWNER_HPARAMS: GroupId = 4; +/// Group id for staking operations. +pub const GROUP_STAKING_OPS: GroupId = 5; +/// Group id for key swap calls. +pub const GROUP_SWAP_KEYS: GroupId = 6; + +/// Usage-key type currently shared by the centralized `pallet-subtensor` rate-limiting instance. +/// +/// Do not add new variants for new pallets. Prefer defining pallet-specific types and using a +/// dedicated `pallet-rate-limiting` instance per pallet. +#[derive( + Serialize, + Deserialize, + Encode, + Decode, + DecodeWithMemTracking, + Clone, + PartialEq, + Eq, + PartialOrd, + Ord, + Debug, + TypeInfo, + MaxEncodedLen, +)] +#[scale_info(skip_type_params(AccountId))] +pub enum RateLimitUsageKey { + Account(AccountId), + Subnet(NetUid), + AccountSubnet { + account: AccountId, + netuid: NetUid, + }, + ColdkeyHotkeySubnet { + coldkey: AccountId, + hotkey: AccountId, + netuid: NetUid, + }, + SubnetNeuron { + netuid: NetUid, + uid: u16, + }, + SubnetMechanismNeuron { + netuid: NetUid, + mecid: MechId, + uid: u16, + }, + AccountSubnetServing { + account: AccountId, + netuid: NetUid, + endpoint: ServingEndpoint, + }, +} + +#[derive( + Serialize, + Deserialize, + Encode, + Decode, + DecodeWithMemTracking, + Clone, + Copy, + PartialEq, + Eq, + PartialOrd, + Ord, + Debug, + TypeInfo, + MaxEncodedLen, +)] +pub enum ServingEndpoint { + Axon, + Prometheus, +} diff --git a/contract-tests/README.md b/contract-tests/README.md index 78294603d3..90068e5553 100644 --- a/contract-tests/README.md +++ b/contract-tests/README.md @@ -36,6 +36,9 @@ npx papi add devnet -w ws://localhost:9944 If the runtime is upgrade, need to get the metadata again. ```bash +cd contract-tests/bittensor +cargo contract build --release +cd .. sh get-metadata.sh ``` diff --git a/contract-tests/get-metadata.sh b/contract-tests/get-metadata.sh index 64d76bff29..bd24051922 100755 --- a/contract-tests/get-metadata.sh +++ b/contract-tests/get-metadata.sh @@ -1,3 +1,8 @@ +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +cd "$SCRIPT_DIR" + rm -rf .papi npx papi add devnet -w ws://localhost:9944 -npx papi ink add ./bittensor/target/ink/bittensor.json \ No newline at end of file +npx papi ink add ./bittensor/target/ink/bittensor.json +# Yarn copies file: dependencies into node_modules, so reinstall to pick up new .papi/descriptors. +yarn install --check-files diff --git a/contract-tests/src/subtensor.ts b/contract-tests/src/subtensor.ts index fab9e8cc10..fc4c5b5045 100644 --- a/contract-tests/src/subtensor.ts +++ b/contract-tests/src/subtensor.ts @@ -7,14 +7,27 @@ import { convertH160ToSS58, convertPublicKeyToSs58, ethAddressToH160 } from './a import { tao } from './balance-math' import internal from "stream"; +const rateLimitTargetGroup = (groupId: number) => Enum("Group", groupId); +const rateLimitKindExact = (limit: bigint | number) => + Enum("Exact", typeof limit === "bigint" ? Number(limit) : limit); + // create a new subnet and return netuid export async function addNewSubnetwork(api: TypedApi, hotkey: KeyPair, coldkey: KeyPair) { const alice = getAliceSigner() const totalNetworks = await api.query.SubtensorModule.TotalNetworks.getValue() - const rateLimit = await api.query.SubtensorModule.NetworkRateLimit.getValue() + const registerNetworkGroupId = 3; // GROUP_REGISTER_NETWORK constant + const target = rateLimitTargetGroup(registerNetworkGroupId); + const limits = await api.query.RateLimiting.Limits.getValue(target as any) as any; + assert.ok(limits?.type === "Global"); + assert.ok(limits.value?.type === "Exact"); + const rateLimit = BigInt(limits.value.value); if (rateLimit !== BigInt(0)) { - const internalCall = api.tx.AdminUtils.sudo_set_network_rate_limit({ rate_limit: BigInt(0) }) + const internalCall = api.tx.RateLimiting.set_rate_limit({ + target: target as any, + scope: undefined, + limit: rateLimitKindExact(0), + }) const tx = api.tx.Sudo.sudo({ call: internalCall.decodedCall }) await waitForTransactionWithRetry(api, tx, alice) } @@ -66,17 +79,32 @@ export async function setCommitRevealWeightsEnabled(api: TypedApi } export async function setWeightsSetRateLimit(api: TypedApi, netuid: number, rateLimit: bigint) { - const value = await api.query.SubtensorModule.WeightsSetRateLimit.getValue(netuid) - if (value === rateLimit) { + const weightsSetGroupId = 2; // GROUP_WEIGHTS_SET constant + const target = rateLimitTargetGroup(weightsSetGroupId); + const limits = await api.query.RateLimiting.Limits.getValue(target as any) as any; + assert.ok(limits?.type === "Scoped"); + const entries = Array.from(limits.value as any); + const entry = entries.find((item: any) => Number(item[0]) === netuid); + const currentLimit = entry ? BigInt(entry[1].value) : BigInt(0); + if (currentLimit === rateLimit) { return; } - const alice = getAliceSigner() - const internalCall = api.tx.AdminUtils.sudo_set_weights_set_rate_limit({ netuid: netuid, weights_set_rate_limit: rateLimit }) + const internalCall = api.tx.RateLimiting.set_rate_limit({ + target: target as any, + scope: netuid, + limit: rateLimitKindExact(rateLimit), + }) const tx = api.tx.Sudo.sudo({ call: internalCall.decodedCall }) await waitForTransactionWithRetry(api, tx, alice) - assert.equal(rateLimit, await api.query.SubtensorModule.WeightsSetRateLimit.getValue(netuid)) + const updated = await api.query.RateLimiting.Limits.getValue(target as any) as any; + assert.ok(updated?.type === "Scoped"); + const updatedEntry = Array.from(updated.value as any).find( + (item: any) => Number(item[0]) === netuid, + ); + assert.ok(updatedEntry); + assert.equal(rateLimit, BigInt(updatedEntry[1].value)) } // tempo is u16 in rust, but we just number in js. so value should be less than u16::Max @@ -167,16 +195,24 @@ export async function sendProxyCall(api: TypedApi, calldata: TxCa export async function setTxRateLimit(api: TypedApi, txRateLimit: bigint) { - const value = await api.query.SubtensorModule.TxRateLimit.getValue() - if (value === txRateLimit) { + const swapKeysGroupId = 6; // GROUP_SWAP_KEYS constant + const target = rateLimitTargetGroup(swapKeysGroupId); + const limits = await api.query.RateLimiting.Limits.getValue(target as any) as any; + assert.ok(limits?.type === "Global"); + assert.ok(limits.value?.type === "Exact"); + const currentLimit = BigInt(limits.value.value); + if (currentLimit === txRateLimit) { return; } const alice = getAliceSigner() - const internalCall = api.tx.AdminUtils.sudo_set_tx_rate_limit({ tx_rate_limit: txRateLimit }) + const internalCall = api.tx.RateLimiting.set_rate_limit({ + target: target as any, + scope: undefined, + limit: rateLimitKindExact(txRateLimit), + }) const tx = api.tx.Sudo.sudo({ call: internalCall.decodedCall }) - await waitForTransactionWithRetry(api, tx, alice) } @@ -398,4 +434,4 @@ export async function sendWasmContractExtrinsic(api: TypedApi, co storage_deposit_limit: BigInt(1000000000) }) await waitForTransactionWithRetry(api, tx, signer) -} \ No newline at end of file +} diff --git a/contract-tests/test/subnet.precompile.hyperparameter.test.ts b/contract-tests/test/subnet.precompile.hyperparameter.test.ts index 8598b45a81..369e9a919b 100644 --- a/contract-tests/test/subnet.precompile.hyperparameter.test.ts +++ b/contract-tests/test/subnet.precompile.hyperparameter.test.ts @@ -2,7 +2,7 @@ import * as assert from "assert"; import { getAliceSigner, getDevnetApi, getRandomSubstrateKeypair, waitForTransactionWithRetry } from "../src/substrate" import { devnet } from "@polkadot-api/descriptors" -import { Binary, TypedApi, getTypedCodecs } from "polkadot-api"; +import { Binary, Enum, TypedApi, getTypedCodecs } from "polkadot-api"; import { convertH160ToSS58, convertPublicKeyToSs58 } from "../src/address-utils" import { generateRandomEthersWallet } from "../src/utils"; import { ISubnetABI, ISUBNET_ADDRESS } from "../src/contracts/subnet" @@ -16,6 +16,7 @@ describe("Test the Subnet precompile contract", () => { const hotkey1 = getRandomSubstrateKeypair(); const hotkey2 = getRandomSubstrateKeypair(); + const hotkey3 = getRandomSubstrateKeypair(); let api: TypedApi before(async () => { @@ -24,9 +25,15 @@ describe("Test the Subnet precompile contract", () => { await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey1.publicKey)) await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey2.publicKey)) + await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey3.publicKey)) await forceSetBalanceToEthAddress(api, wallet.address) await disableAdminFreezeWindowAndOwnerHyperparamRateLimit(api) + + // Ensure the EVM wallet owns a subnet so owner-only calls pass when tests run in isolation. + const contract = new ethers.Contract(ISUBNET_ADDRESS, ISubnetABI, wallet); + const tx = await contract.registerNetwork(hotkey3.publicKey); + await tx.wait(); }) it("Can register network without identity info", async () => { @@ -91,8 +98,14 @@ describe("Test the Subnet precompile contract", () => { const tx = await contract.setServingRateLimit(netuid, newValue); await tx.wait(); - let onchainValue = await api.query.SubtensorModule.ServingRateLimit.getValue(netuid) - + const limits = await api.query.RateLimiting.Limits.getValue(Enum("Group", 0) as any) as any; + assert.ok(limits?.type === "Scoped"); + const entry = Array.from(limits.value as any).find( + (item: any) => Number(item[0]) === netuid, + ); + assert.ok(entry); + assert.ok(entry[1]?.type === "Exact"); + const onchainValue = Number(entry[1].value); let valueFromContract = Number( await contract.getServingRateLimit(netuid) diff --git a/contract-tests/test/wasm.contract.test.ts b/contract-tests/test/wasm.contract.test.ts index 26d5c87924..c42dbf5555 100644 --- a/contract-tests/test/wasm.contract.test.ts +++ b/contract-tests/test/wasm.contract.test.ts @@ -6,12 +6,23 @@ import { contracts } from "../.papi/descriptors"; import { getInkClient, InkClient, } from "@polkadot-api/ink-contracts" import { forceSetBalanceToSs58Address, startCall, burnedRegister } from "../src/subtensor"; import fs from "fs" +import path from "path"; import { convertPublicKeyToSs58 } from "../src/address-utils"; import { addNewSubnetwork, sendWasmContractExtrinsic } from "../src/subtensor"; import { tao } from "../src/balance-math"; -const bittensorWasmPath = "./bittensor/target/ink/bittensor.wasm" -const bittensorBytecode = fs.readFileSync(bittensorWasmPath) +const bittensorWasmPath = path.resolve(__dirname, "../bittensor/target/ink/bittensor.wasm") +const loadBittensorBytecode = () => { + if (!fs.existsSync(bittensorWasmPath)) { + throw new Error( + `Missing Ink wasm at ${bittensorWasmPath}. Run ` + + "`cd contract-tests/bittensor && cargo contract build --release` to generate it." + ) + } + + return fs.readFileSync(bittensorWasmPath) +} +let bittensorBytecode: Buffer; describe("Test wasm contract", () => { @@ -60,6 +71,7 @@ describe("Test wasm contract", () => { before(async () => { + bittensorBytecode = loadBittensorBytecode() // init variables got from await and async api = await getDevnetApi() @@ -584,4 +596,4 @@ describe("Test wasm contract", () => { assert.ok(result !== undefined) }) -}); \ No newline at end of file +}); diff --git a/node/Cargo.toml b/node/Cargo.toml index 1d2351c265..06b96c0eba 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -70,6 +70,7 @@ frame-system.workspace = true pallet-transaction-payment.workspace = true pallet-commitments.workspace = true pallet-drand.workspace = true +pallet-rate-limiting.workspace = true sp-crypto-ec-utils = { workspace = true, default-features = true, features = [ "bls12-381", ] } @@ -173,6 +174,7 @@ runtime-benchmarks = [ "polkadot-sdk/runtime-benchmarks", "pallet-subtensor/runtime-benchmarks", "pallet-shield/runtime-benchmarks", + "pallet-rate-limiting/runtime-benchmarks", ] pow-faucet = [] diff --git a/node/src/benchmarking.rs b/node/src/benchmarking.rs index 5430a75d9e..2f401e3e95 100644 --- a/node/src/benchmarking.rs +++ b/node/src/benchmarking.rs @@ -143,8 +143,12 @@ pub fn create_benchmark_extrinsic( pallet_subtensor::transaction_extension::SubtensorTransactionExtension::< runtime::Runtime, >::new(), - pallet_drand::drand_priority::DrandPriority::::new(), - frame_metadata_hash_extension::CheckMetadataHash::::new(true), + // Keep the same order while staying under the 12-item tuple limit. + ( + pallet_drand::drand_priority::DrandPriority::::new(), + frame_metadata_hash_extension::CheckMetadataHash::::new(true), + ), + pallet_rate_limiting::RateLimitTransactionExtension::::new(), ); let raw_payload = runtime::SignedPayload::from_raw( @@ -161,8 +165,8 @@ pub fn create_benchmark_extrinsic( (), (), (), + ((), None), (), - None, ), ); let signature = raw_payload.using_encoded(|e| sender.sign(e)); diff --git a/node/src/chain_spec/devnet.rs b/node/src/chain_spec/devnet.rs index cb3bc66924..7770065abb 100644 --- a/node/src/chain_spec/devnet.rs +++ b/node/src/chain_spec/devnet.rs @@ -2,6 +2,14 @@ #![allow(clippy::unwrap_used)] use super::*; +use node_subtensor_runtime::rate_limiting::legacy::defaults as rate_limit_defaults; +use subtensor_runtime_common::{ + NetUid, + rate_limiting::{ + GROUP_DELEGATE_TAKE, GROUP_REGISTER_NETWORK, GROUP_SERVE, GROUP_STAKING_OPS, + GROUP_SWAP_KEYS, GROUP_WEIGHTS_SET, + }, +}; pub fn devnet_config() -> Result { let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?; @@ -89,5 +97,31 @@ fn devnet_genesis( "sudo": { "key": Some(root_key), }, + "rateLimiting": { + "defaultLimit": 0, + "limits": vec![ + (serde_json::json!({ "Group": GROUP_SERVE }), Some(NetUid::ROOT), serde_json::json!({ "Exact": rate_limit_defaults::serving_rate_limit() })), + (serde_json::json!({ "Group": GROUP_SERVE }), Some(NetUid::from(1u16)), serde_json::json!({ "Exact": rate_limit_defaults::serving_rate_limit() })), + (serde_json::json!({ "Group": GROUP_REGISTER_NETWORK }), Option::::None, serde_json::json!({ "Exact": rate_limit_defaults::network_rate_limit() })), + (serde_json::json!({ "Group": GROUP_DELEGATE_TAKE }), Option::::None, serde_json::json!({ "Exact": rate_limit_defaults::tx_delegate_take_rate_limit() })), + (serde_json::json!({ "Group": GROUP_STAKING_OPS }), Option::::None, serde_json::json!({ "Exact": 1 })), + // Legacy TxRateLimit blocks when delta <= limit; rate-limiting blocks at delta < span. + // Add one block to preserve legacy swap-keys behavior when legacy rate-limiting is removed. + (serde_json::json!({ "Group": GROUP_SWAP_KEYS }), Option::::None, serde_json::json!({ "Exact": rate_limit_defaults::tx_rate_limit().saturating_add(1) })), + (serde_json::json!({ "Group": GROUP_WEIGHTS_SET }), Some(NetUid::ROOT), serde_json::json!({ "Exact": rate_limit_defaults::weights_set_rate_limit() })), + (serde_json::json!({ "Group": GROUP_WEIGHTS_SET }), Some(NetUid::from(1u16)), serde_json::json!({ "Exact": rate_limit_defaults::weights_set_rate_limit() })), + ], + "groups": vec![ + (GROUP_SERVE, b"serving".to_vec(), "ConfigAndUsage"), + (GROUP_REGISTER_NETWORK, b"register-network".to_vec(), "ConfigAndUsage"), + (GROUP_DELEGATE_TAKE, b"delegate-take".to_vec(), "ConfigAndUsage"), + (GROUP_STAKING_OPS, b"staking-ops".to_vec(), "ConfigAndUsage"), + (GROUP_SWAP_KEYS, b"swap-keys".to_vec(), "ConfigAndUsage"), + (GROUP_WEIGHTS_SET, b"weights".to_vec(), "ConfigAndUsage"), + ], + "limitSettingRules": vec![ + (serde_json::json!({ "Group": GROUP_SERVE }), "RootOrSubnetOwnerAdminWindow"), + ], + }, }) } diff --git a/node/src/chain_spec/localnet.rs b/node/src/chain_spec/localnet.rs index 02ea8896b5..efd8c8de5f 100644 --- a/node/src/chain_spec/localnet.rs +++ b/node/src/chain_spec/localnet.rs @@ -2,6 +2,14 @@ #![allow(clippy::unwrap_used)] use super::*; +use node_subtensor_runtime::rate_limiting::legacy::defaults as rate_limit_defaults; +use subtensor_runtime_common::{ + NetUid, + rate_limiting::{ + GROUP_DELEGATE_TAKE, GROUP_REGISTER_NETWORK, GROUP_SERVE, GROUP_STAKING_OPS, + GROUP_SWAP_KEYS, GROUP_WEIGHTS_SET, + }, +}; pub fn localnet_config(single_authority: bool) -> Result { let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?; @@ -124,5 +132,31 @@ fn localnet_genesis( "evmChainId": { "chainId": 42, }, + "rateLimiting": { + "defaultLimit": 0, + "limits": vec![ + (serde_json::json!({ "Group": GROUP_SERVE }), Some(NetUid::ROOT), serde_json::json!({ "Exact": rate_limit_defaults::serving_rate_limit() })), + (serde_json::json!({ "Group": GROUP_SERVE }), Some(NetUid::from(1u16)), serde_json::json!({ "Exact": rate_limit_defaults::serving_rate_limit() })), + (serde_json::json!({ "Group": GROUP_REGISTER_NETWORK }), Option::::None, serde_json::json!({ "Exact": rate_limit_defaults::network_rate_limit() })), + (serde_json::json!({ "Group": GROUP_DELEGATE_TAKE }), Option::::None, serde_json::json!({ "Exact": rate_limit_defaults::tx_delegate_take_rate_limit() })), + (serde_json::json!({ "Group": GROUP_STAKING_OPS }), Option::::None, serde_json::json!({ "Exact": 1 })), + // Legacy TxRateLimit blocks when delta <= limit; rate-limiting blocks at delta < span. + // Add one block to preserve legacy swap-keys behavior when legacy rate-limiting is removed. + (serde_json::json!({ "Group": GROUP_SWAP_KEYS }), Option::::None, serde_json::json!({ "Exact": rate_limit_defaults::tx_rate_limit().saturating_add(1) })), + (serde_json::json!({ "Group": GROUP_WEIGHTS_SET }), Some(NetUid::ROOT), serde_json::json!({ "Exact": rate_limit_defaults::weights_set_rate_limit() })), + (serde_json::json!({ "Group": GROUP_WEIGHTS_SET }), Some(NetUid::from(1u16)), serde_json::json!({ "Exact": rate_limit_defaults::weights_set_rate_limit() })), + ], + "groups": vec![ + (GROUP_SERVE, b"serving".to_vec(), "ConfigAndUsage"), + (GROUP_REGISTER_NETWORK, b"register-network".to_vec(), "ConfigAndUsage"), + (GROUP_DELEGATE_TAKE, b"delegate-take".to_vec(), "ConfigAndUsage"), + (GROUP_STAKING_OPS, b"staking-ops".to_vec(), "ConfigAndUsage"), + (GROUP_SWAP_KEYS, b"swap-keys".to_vec(), "ConfigAndUsage"), + (GROUP_WEIGHTS_SET, b"weights".to_vec(), "ConfigAndUsage"), + ], + "limitSettingRules": vec![ + (serde_json::json!({ "Group": GROUP_SERVE }), "RootOrSubnetOwnerAdminWindow"), + ], + }, }) } diff --git a/node/src/mev_shield/author.rs b/node/src/mev_shield/author.rs index 8ac57b1d52..bcc70b21b0 100644 --- a/node/src/mev_shield/author.rs +++ b/node/src/mev_shield/author.rs @@ -394,8 +394,12 @@ where pallet_subtensor::transaction_extension::SubtensorTransactionExtension::< runtime::Runtime, >::new(), - pallet_drand::drand_priority::DrandPriority::::new(), - frame_metadata_hash_extension::CheckMetadataHash::::new(false), + // Keep the same order while staying under the 12-item tuple limit. + ( + pallet_drand::drand_priority::DrandPriority::::new(), + frame_metadata_hash_extension::CheckMetadataHash::::new(false), + ), + pallet_rate_limiting::RateLimitTransactionExtension::::new(), ); // 3) Manually construct the `Implicit` tuple that the runtime will also derive. @@ -429,8 +433,12 @@ where (), // ChargeTransactionPaymentWrapper::Implicit = () (), // SudoTransactionExtension::Implicit = () (), // SubtensorTransactionExtension::Implicit = () - (), // DrandPriority::Implicit = () - None, // CheckMetadataHash::Implicit = Option<[u8; 32]> + // Match the nested tuple shape used by TransactionExtensions. + ( + (), // DrandPriority::Implicit = () + None, // CheckMetadataHash::Implicit = Option<[u8; 32]> + ), + (), // RateLimitTransactionExtension::Implicit = () ); // 4) Build the exact signable payload from call + extra + implicit. diff --git a/pallets/admin-utils/Cargo.toml b/pallets/admin-utils/Cargo.toml index 61cdba4cbf..aa6d7e593c 100644 --- a/pallets/admin-utils/Cargo.toml +++ b/pallets/admin-utils/Cargo.toml @@ -38,6 +38,7 @@ sp-core.workspace = true sp-io.workspace = true sp-tracing.workspace = true sp-consensus-aura.workspace = true +rate-limiting-interface.workspace = true pallet-balances = { workspace = true, features = ["std"] } pallet-scheduler.workspace = true pallet-grandpa.workspace = true @@ -75,6 +76,7 @@ std = [ "substrate-fixed/std", "subtensor-swap-interface/std", "subtensor-runtime-common/std", + "rate-limiting-interface/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", diff --git a/pallets/admin-utils/src/benchmarking.rs b/pallets/admin-utils/src/benchmarking.rs index 7b8124144d..105a8b8ddb 100644 --- a/pallets/admin-utils/src/benchmarking.rs +++ b/pallets/admin-utils/src/benchmarking.rs @@ -64,11 +64,19 @@ mod benchmarks { } #[benchmark] + #[allow(deprecated)] fn sudo_set_serving_rate_limit() { // disable admin freeze window pallet_subtensor::Pallet::::set_admin_freeze_window(0); - #[extrinsic_call] - _(RawOrigin::Root, 1u16.into()/*netuid*/, 100u64/*serving_rate_limit*/)/*sudo_set_serving_rate_limit*/; + #[block] + { + #[allow(deprecated)] + let _ = AdminUtils::::sudo_set_serving_rate_limit( + RawOrigin::Root.into(), + 1u16.into(), /*netuid*/ + 100u64, /*serving_rate_limit*/ + ); + } } #[benchmark] diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 2a33262073..654d88b438 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -117,6 +117,8 @@ pub mod pallet { MaxAllowedUidsGreaterThanDefaultMaxAllowedUids, /// Bad parameter value InvalidValue, + /// The called extrinsic has been deprecated. + Deprecated, } /// Enum for specifying the type of precompile operation. #[derive( @@ -207,6 +209,9 @@ pub mod pallet { /// The extrinsic sets the transaction rate limit for the network. /// It is only callable by the root account. /// The extrinsic will call the Subtensor pallet to set the transaction rate limit. + /// + /// Deprecated: swap-keys rate limits are now configured via `pallet-rate-limiting` on the + /// swap-keys group target (`GROUP_SWAP_KEYS`). #[pallet::call_index(2)] #[pallet::weight( (Weight::from_parts(5_400_000, 0) @@ -214,39 +219,33 @@ pub mod pallet { DispatchClass::Operational, Pays::Yes) )] - pub fn sudo_set_tx_rate_limit(origin: OriginFor, tx_rate_limit: u64) -> DispatchResult { - ensure_root(origin)?; - pallet_subtensor::Pallet::::set_tx_rate_limit(tx_rate_limit); - log::debug!("TxRateLimitSet( tx_rate_limit: {tx_rate_limit:?} ) "); - Ok(()) + #[deprecated( + note = "deprecated: configure via pallet-rate-limiting::set_rate_limit(target=Group(GROUP_SWAP_KEYS), ...)" + )] + pub fn sudo_set_tx_rate_limit( + _origin: OriginFor, + _tx_rate_limit: u64, + ) -> DispatchResult { + Err(Error::::Deprecated.into()) } /// The extrinsic sets the serving rate limit for a subnet. - /// It is only callable by the root account or subnet owner. - /// The extrinsic will call the Subtensor pallet to set the serving rate limit. + /// + /// Deprecated: serving rate limits are now configured via `pallet-rate-limiting` on the + /// serving group target (`GROUP_SERVE`) with `scope = Some(netuid)`. #[pallet::call_index(3)] #[pallet::weight(Weight::from_parts(22_980_000, 0) .saturating_add(::DbWeight::get().reads(2_u64)) .saturating_add(::DbWeight::get().writes(1_u64)))] + #[deprecated( + note = "deprecated: configure via pallet-rate-limiting::set_rate_limit(target=Group(GROUP_SERVE), scope=Some(netuid), ...)" + )] pub fn sudo_set_serving_rate_limit( - origin: OriginFor, - netuid: NetUid, - serving_rate_limit: u64, + _origin: OriginFor, + _netuid: NetUid, + _serving_rate_limit: u64, ) -> DispatchResult { - let maybe_owner = pallet_subtensor::Pallet::::ensure_sn_owner_or_root_with_limits( - origin, - netuid, - &[Hyperparameter::ServingRateLimit.into()], - )?; - pallet_subtensor::Pallet::::ensure_admin_window_open(netuid)?; - pallet_subtensor::Pallet::::set_serving_rate_limit(netuid, serving_rate_limit); - log::debug!("ServingRateLimitSet( serving_rate_limit: {serving_rate_limit:?} ) "); - pallet_subtensor::Pallet::::record_owner_rl( - maybe_owner, - netuid, - &[Hyperparameter::ServingRateLimit.into()], - ); - Ok(()) + Err(Error::::Deprecated.into()) } /// The extrinsic sets the minimum difficulty for a subnet. @@ -350,29 +349,22 @@ pub mod pallet { /// The extrinsic sets the weights set rate limit for a subnet. /// It is only callable by the root account. /// The extrinsic will call the Subtensor pallet to set the weights set rate limit. + /// + /// Deprecated: weights set rate limit is now configured via `pallet-rate-limiting` on the + /// weights set group target (`GROUP_WEIGHTS_SET`) with `scope = Some(netuid)`. #[pallet::call_index(7)] #[pallet::weight(Weight::from_parts(15_060_000, 0) .saturating_add(::DbWeight::get().reads(1_u64)) .saturating_add(::DbWeight::get().writes(1_u64)))] + #[deprecated( + note = "deprecated: configure via pallet-rate-limiting::set_rate_limit(target=Group(GROUP_WEIGHTS_SET), scope=Some(netuid), ...)" + )] pub fn sudo_set_weights_set_rate_limit( - origin: OriginFor, - netuid: NetUid, - weights_set_rate_limit: u64, + _origin: OriginFor, + _netuid: NetUid, + _weights_set_rate_limit: u64, ) -> DispatchResult { - ensure_root(origin)?; - - ensure!( - pallet_subtensor::Pallet::::if_subnet_exist(netuid), - Error::::SubnetDoesNotExist - ); - pallet_subtensor::Pallet::::set_weights_set_rate_limit( - netuid, - weights_set_rate_limit, - ); - log::debug!( - "WeightsSetRateLimitSet( netuid: {netuid:?} weights_set_rate_limit: {weights_set_rate_limit:?} ) " - ); - Ok(()) + Err(Error::::Deprecated.into()) } /// The extrinsic sets the adjustment interval for a subnet. @@ -993,8 +985,9 @@ pub mod pallet { } /// The extrinsic sets the network rate limit for the network. - /// It is only callable by the root account. - /// The extrinsic will call the Subtensor pallet to set the network rate limit. + /// + /// Deprecated: network rate limits are now configured via `pallet-rate-limiting` on the + /// register-network group target (`GROUP_REGISTER_NETWORK`) with `scope = None`. #[pallet::call_index(29)] #[pallet::weight(( Weight::from_parts(14_000_000, 0) @@ -1002,14 +995,14 @@ pub mod pallet { DispatchClass::Operational, Pays::Yes ))] + #[deprecated( + note = "deprecated: configure via pallet-rate-limiting::set_rate_limit(target=Group(GROUP_REGISTER_NETWORK), scope=None, ...)" + )] pub fn sudo_set_network_rate_limit( - origin: OriginFor, - rate_limit: u64, + _origin: OriginFor, + _rate_limit: u64, ) -> DispatchResult { - ensure_root(origin)?; - pallet_subtensor::Pallet::::set_network_rate_limit(rate_limit); - log::debug!("NetworkRateLimit( rate_limit: {rate_limit:?} ) "); - Ok(()) + Err(Error::::Deprecated.into()) } /// The extrinsic sets the tempo for a subnet. @@ -1211,7 +1204,11 @@ pub mod pallet { /// The extrinsic sets the rate limit for delegate take transactions. /// It is only callable by the root account. - /// The extrinsic will call the Subtensor pallet to set the rate limit for delegate take transactions. + /// The extrinsic will call the Subtensor pallet to set the rate limit for delegate take + /// transactions. + /// + /// Deprecated: delegate take rate limit is now configured via `pallet-rate-limiting` on the + /// delegate take group target (`GROUP_DELEGATE_TAKE`). #[pallet::call_index(45)] #[pallet::weight(( Weight::from_parts(5_019_000, 0) @@ -1220,16 +1217,14 @@ pub mod pallet { DispatchClass::Operational, Pays::Yes ))] + #[deprecated( + note = "deprecated: configure via pallet-rate-limiting::set_rate_limit(target=Group(GROUP_DELEGATE_TAKE), ...)" + )] pub fn sudo_set_tx_delegate_take_rate_limit( - origin: OriginFor, - tx_rate_limit: u64, + _origin: OriginFor, + _tx_rate_limit: u64, ) -> DispatchResult { - ensure_root(origin)?; - pallet_subtensor::Pallet::::set_tx_delegate_take_rate_limit(tx_rate_limit); - log::debug!( - "TxRateLimitDelegateTakeSet( tx_delegate_take_rate_limit: {tx_rate_limit:?} ) " - ); - Ok(()) + Err(Error::::Deprecated.into()) } /// The extrinsic sets the minimum delegate take. diff --git a/pallets/admin-utils/src/tests/mock.rs b/pallets/admin-utils/src/tests/mock.rs index 0117dff889..2a10dcbfb8 100644 --- a/pallets/admin-utils/src/tests/mock.rs +++ b/pallets/admin-utils/src/tests/mock.rs @@ -8,6 +8,7 @@ use frame_support::{ }; use frame_system::{self as system, offchain::CreateTransactionBase}; use frame_system::{EnsureRoot, limits}; +use rate_limiting_interface::{RateLimitingInterface, TryIntoRateLimitTarget}; use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_consensus_grandpa::AuthorityList as GrandpaAuthorityList; use sp_core::U256; @@ -19,7 +20,7 @@ use sp_runtime::{ }; use sp_std::cmp::Ordering; use sp_weights::Weight; -use subtensor_runtime_common::{NetUid, TaoCurrency}; +use subtensor_runtime_common::{NetUid, TaoCurrency, rate_limiting::RateLimitUsageKey}; type Block = frame_system::mocking::MockBlock; // Configure a mock runtime to test the pallet. @@ -103,9 +104,6 @@ parameter_types! { pub const InitialMinChildKeyTake: u16 = 0; // Allow 0 % pub const InitialMaxChildKeyTake: u16 = 11_796; // 18 %; pub const InitialWeightsVersionKey: u16 = 0; - pub const InitialServingRateLimit: u64 = 0; // No limit. - pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing - pub const InitialTxDelegateTakeRateLimit: u64 = 0; // Disable rate limit for testing pub const InitialTxChildKeyTakeRateLimit: u64 = 0; // Disable rate limit for testing pub const InitialBurn: u64 = 0; pub const InitialMinBurn: u64 = 500_000; @@ -132,7 +130,6 @@ parameter_types! { pub const InitialSubnetOwnerCut: u16 = 0; // 0%. 100% of rewards go to validators + miners. pub const InitialNetworkLockReductionInterval: u64 = 2; // 2 blocks. // pub const InitialSubnetLimit: u16 = 10; // (DEPRECATED) - pub const InitialNetworkRateLimit: u64 = 0; pub const InitialKeySwapCost: u64 = 1_000_000_000; 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 @@ -189,9 +186,6 @@ impl pallet_subtensor::Config for Test { type InitialWeightsVersionKey = InitialWeightsVersionKey; type InitialMaxDifficulty = InitialMaxDifficulty; type InitialMinDifficulty = InitialMinDifficulty; - type InitialServingRateLimit = InitialServingRateLimit; - type InitialTxRateLimit = InitialTxRateLimit; - type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit; type InitialTxChildKeyTakeRateLimit = InitialTxChildKeyTakeRateLimit; type InitialBurn = InitialBurn; type InitialMaxBurn = InitialMaxBurn; @@ -203,7 +197,6 @@ impl pallet_subtensor::Config for Test { type InitialNetworkMinLockCost = InitialNetworkMinLockCost; type InitialSubnetOwnerCut = InitialSubnetOwnerCut; type InitialNetworkLockReductionInterval = InitialNetworkLockReductionInterval; - type InitialNetworkRateLimit = InitialNetworkRateLimit; type KeySwapCost = InitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; @@ -224,6 +217,7 @@ impl pallet_subtensor::Config for Test { type GetCommitments = (); type MaxImmuneUidsPercentage = MaxImmuneUidsPercentage; type CommitmentsInterface = CommitmentsI; + type RateLimiting = NoRateLimiting; type EvmKeyAssociateRateLimit = EvmKeyAssociateRateLimit; } @@ -356,6 +350,42 @@ impl pallet_subtensor::CommitmentsInterface for CommitmentsI { fn purge_netuid(_netuid: NetUid) {} } +pub struct NoRateLimiting; + +impl RateLimitingInterface for NoRateLimiting { + type GroupId = subtensor_runtime_common::rate_limiting::GroupId; + type CallMetadata = RuntimeCall; + type Limit = u64; + type Scope = subtensor_runtime_common::NetUid; + type UsageKey = RateLimitUsageKey; + + fn rate_limit(_target: TargetArg, _scope: Option) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + None + } + + fn last_seen( + _target: TargetArg, + _usage_key: Option, + ) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + None + } + + fn set_last_seen( + _target: TargetArg, + _usage_key: Option, + _block: Option, + ) where + TargetArg: TryIntoRateLimitTarget, + { + } +} + pub struct GrandpaInterfaceImpl; impl crate::GrandpaInterface for GrandpaInterfaceImpl { fn schedule_change( diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 93d56ec343..eaf1dd3dca 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -44,26 +44,30 @@ fn test_sudo_set_default_take() { } #[test] +#[allow(deprecated)] fn test_sudo_set_serving_rate_limit() { new_test_ext().execute_with(|| { let netuid = NetUid::from(3); let to_be_set: u64 = 10; let init_value: u64 = SubtensorModule::get_serving_rate_limit(netuid); - assert_eq!( + assert_noop!( AdminUtils::sudo_set_serving_rate_limit( <::RuntimeOrigin>::signed(U256::from(1)), netuid, to_be_set ), - Err(DispatchError::BadOrigin) + Error::::Deprecated + ); + assert_eq!(SubtensorModule::get_serving_rate_limit(netuid), init_value); + assert_noop!( + AdminUtils::sudo_set_serving_rate_limit( + <::RuntimeOrigin>::root(), + netuid, + to_be_set + ), + Error::::Deprecated ); assert_eq!(SubtensorModule::get_serving_rate_limit(netuid), init_value); - assert_ok!(AdminUtils::sudo_set_serving_rate_limit( - <::RuntimeOrigin>::root(), - netuid, - to_be_set - )); - assert_eq!(SubtensorModule::get_serving_rate_limit(netuid), to_be_set); }); } @@ -265,45 +269,6 @@ fn test_sudo_set_weights_version_key_rate_limit_root() { }); } -#[test] -fn test_sudo_set_weights_set_rate_limit() { - new_test_ext().execute_with(|| { - let netuid = NetUid::from(1); - let to_be_set: u64 = 10; - add_network(netuid, 10); - let init_value: u64 = SubtensorModule::get_weights_set_rate_limit(netuid); - assert_eq!( - AdminUtils::sudo_set_weights_set_rate_limit( - <::RuntimeOrigin>::signed(U256::from(1)), - netuid, - to_be_set - ), - Err(DispatchError::BadOrigin) - ); - assert_eq!( - AdminUtils::sudo_set_weights_set_rate_limit( - <::RuntimeOrigin>::root(), - netuid.next(), - to_be_set - ), - Err(Error::::SubnetDoesNotExist.into()) - ); - assert_eq!( - SubtensorModule::get_weights_set_rate_limit(netuid), - init_value - ); - assert_ok!(AdminUtils::sudo_set_weights_set_rate_limit( - <::RuntimeOrigin>::root(), - netuid, - to_be_set - )); - assert_eq!( - SubtensorModule::get_weights_set_rate_limit(netuid), - to_be_set - ); - }); -} - #[test] fn test_sudo_set_adjustment_interval() { new_test_ext().execute_with(|| { @@ -1075,33 +1040,6 @@ mod sudo_set_nominator_min_required_stake { } } -#[test] -fn test_sudo_set_tx_delegate_take_rate_limit() { - new_test_ext().execute_with(|| { - let to_be_set: u64 = 10; - let init_value: u64 = SubtensorModule::get_tx_delegate_take_rate_limit(); - assert_eq!( - AdminUtils::sudo_set_tx_delegate_take_rate_limit( - <::RuntimeOrigin>::signed(U256::from(1)), - to_be_set - ), - Err(DispatchError::BadOrigin) - ); - assert_eq!( - SubtensorModule::get_tx_delegate_take_rate_limit(), - init_value - ); - assert_ok!(AdminUtils::sudo_set_tx_delegate_take_rate_limit( - <::RuntimeOrigin>::root(), - to_be_set - )); - assert_eq!( - SubtensorModule::get_tx_delegate_take_rate_limit(), - to_be_set - ); - }); -} - #[test] fn test_sudo_set_min_delegate_take() { new_test_ext().execute_with(|| { diff --git a/pallets/rate-limiting-interface/Cargo.toml b/pallets/rate-limiting-interface/Cargo.toml new file mode 100644 index 0000000000..8f352d0c58 --- /dev/null +++ b/pallets/rate-limiting-interface/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "rate-limiting-interface" +version = "0.1.0" +edition.workspace = true + +[lints] +workspace = true + +[dependencies] +codec = { workspace = true, features = ["derive"], default-features = false } +frame-support = { workspace = true, default-features = false } +scale-info = { workspace = true, features = ["derive"], default-features = false } +serde = { workspace = true, features = ["derive"], default-features = false } +sp-std = { workspace = true, default-features = false } + +[features] +default = ["std"] +std = [ + "codec/std", + "frame-support/std", + "scale-info/std", + "serde/std", + "sp-std/std", +] diff --git a/pallets/rate-limiting-interface/README.md b/pallets/rate-limiting-interface/README.md new file mode 100644 index 0000000000..73011099c1 --- /dev/null +++ b/pallets/rate-limiting-interface/README.md @@ -0,0 +1,3 @@ +# `rate-limiting-interface` + +Small, `no_std`-friendly interface crate that defines [`RateLimitingInterface`](src/lib.rs). diff --git a/pallets/rate-limiting-interface/src/lib.rs b/pallets/rate-limiting-interface/src/lib.rs new file mode 100644 index 0000000000..ebed2883aa --- /dev/null +++ b/pallets/rate-limiting-interface/src/lib.rs @@ -0,0 +1,277 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +//! Interface for querying rate limits and last-seen usage, with optional write access. + +use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen}; +use frame_support::traits::GetCallMetadata; +use scale_info::TypeInfo; +use serde::{Deserialize, Serialize}; +use sp_std::vec::Vec; + +/// Interface for rate-limiting configuration and usage tracking. +pub trait RateLimitingInterface { + /// Group id type used by rate-limiting targets. + type GroupId; + /// Call type used for name/index resolution. + type CallMetadata: GetCallMetadata; + /// Numeric type used for returned values (commonly a block number / block span type). + type Limit; + /// Optional configuration scope (for example per-network `netuid`). + type Scope; + /// Optional usage key used to refine "last seen" tracking. + type UsageKey; + + /// Returns the configured limit for `target` and optional `scope`. + fn rate_limit(target: TargetArg, scope: Option) -> Option + where + TargetArg: TryIntoRateLimitTarget; + + /// Returns when `target` was last observed for the optional `usage_key`. + fn last_seen( + target: TargetArg, + usage_key: Option, + ) -> Option + where + TargetArg: TryIntoRateLimitTarget; + + /// Sets the last-seen block for `target` and optional `usage_key`. + /// + /// Passing `None` clears the value. + fn set_last_seen( + target: TargetArg, + usage_key: Option, + block: Option, + ) where + TargetArg: TryIntoRateLimitTarget; +} + +/// Target identifier for rate limit and usage configuration. +#[derive( + Serialize, + Deserialize, + Clone, + Copy, + PartialEq, + Eq, + PartialOrd, + Ord, + Encode, + Decode, + DecodeWithMemTracking, + TypeInfo, + MaxEncodedLen, + Debug, +)] +pub enum RateLimitTarget { + /// Per-transaction configuration keyed by pallet/extrinsic indices. + Transaction(TransactionIdentifier), + /// Shared configuration for a named group. + Group(GroupId), +} + +impl RateLimitTarget { + /// Returns the transaction identifier when the target represents a single extrinsic. + pub fn as_transaction(&self) -> Option<&TransactionIdentifier> { + match self { + RateLimitTarget::Transaction(identifier) => Some(identifier), + RateLimitTarget::Group(_) => None, + } + } + + /// Returns the group identifier when the target represents a group configuration. + pub fn as_group(&self) -> Option<&GroupId> { + match self { + RateLimitTarget::Transaction(_) => None, + RateLimitTarget::Group(id) => Some(id), + } + } +} + +impl From for RateLimitTarget { + fn from(identifier: TransactionIdentifier) -> Self { + Self::Transaction(identifier) + } +} + +/// Identifies a runtime call by pallet and extrinsic indices. +#[derive( + Serialize, + Deserialize, + Clone, + Copy, + PartialEq, + Eq, + PartialOrd, + Ord, + Encode, + Decode, + DecodeWithMemTracking, + TypeInfo, + MaxEncodedLen, + Debug, +)] +pub struct TransactionIdentifier { + /// Pallet variant index. + pub pallet_index: u8, + /// Call variant index within the pallet. + pub extrinsic_index: u8, +} + +impl TransactionIdentifier { + /// Builds a new identifier from pallet/extrinsic indices. + pub const fn new(pallet_index: u8, extrinsic_index: u8) -> Self { + Self { + pallet_index, + extrinsic_index, + } + } + + /// Attempts to build an identifier from a SCALE-encoded call by reading the first two bytes. + pub fn from_call(call: &Call) -> Option { + call.using_encoded(|encoded| { + let pallet_index = *encoded.get(0)?; + let extrinsic_index = *encoded.get(1)?; + Some(Self::new(pallet_index, extrinsic_index)) + }) + } + + /// Resolves pallet/extrinsic names for this identifier using call metadata. + pub fn names(&self) -> Option<(&'static str, &'static str)> { + let modules = Call::get_module_names(); + let pallet_name = *modules.get(self.pallet_index as usize)?; + let call_names = Call::get_call_names(pallet_name); + let extrinsic_name = *call_names.get(self.extrinsic_index as usize)?; + Some((pallet_name, extrinsic_name)) + } + + /// Resolves a pallet/extrinsic name pair into a transaction identifier. + pub fn for_call_names( + pallet_name: &str, + extrinsic_name: &str, + ) -> Option { + let modules = Call::get_module_names(); + let pallet_pos = modules.iter().position(|name| *name == pallet_name)?; + let call_names = Call::get_call_names(pallet_name); + let extrinsic_pos = call_names.iter().position(|name| *name == extrinsic_name)?; + let pallet_index = u8::try_from(pallet_pos).ok()?; + let extrinsic_index = u8::try_from(extrinsic_pos).ok()?; + Some(Self::new(pallet_index, extrinsic_index)) + } +} + +/// Conversion into a concrete [`RateLimitTarget`]. +pub trait TryIntoRateLimitTarget { + type Error; + + fn try_into_rate_limit_target( + self, + ) -> Result, Self::Error>; +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RateLimitTargetConversionError { + InvalidUtf8, + UnknownCall, +} + +impl TryIntoRateLimitTarget for RateLimitTarget { + type Error = core::convert::Infallible; + + fn try_into_rate_limit_target( + self, + ) -> Result, Self::Error> { + Ok(self) + } +} + +impl TryIntoRateLimitTarget for GroupId { + type Error = core::convert::Infallible; + + fn try_into_rate_limit_target( + self, + ) -> Result, Self::Error> { + Ok(RateLimitTarget::Group(self)) + } +} + +impl TryIntoRateLimitTarget for (Vec, Vec) { + type Error = RateLimitTargetConversionError; + + fn try_into_rate_limit_target( + self, + ) -> Result, Self::Error> { + let (pallet, extrinsic) = self; + let pallet_name = sp_std::str::from_utf8(&pallet) + .map_err(|_| RateLimitTargetConversionError::InvalidUtf8)?; + let extrinsic_name = sp_std::str::from_utf8(&extrinsic) + .map_err(|_| RateLimitTargetConversionError::InvalidUtf8)?; + + let identifier = TransactionIdentifier::for_call_names::(pallet_name, extrinsic_name) + .ok_or(RateLimitTargetConversionError::UnknownCall)?; + + Ok(RateLimitTarget::Transaction(identifier)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use codec::Encode; + use frame_support::traits::CallMetadata; + + #[derive(Clone, Copy, Debug, Encode)] + struct DummyCall(u8, u8); + + impl GetCallMetadata for DummyCall { + fn get_module_names() -> &'static [&'static str] { + &["P0", "P1"] + } + + fn get_call_names(module: &str) -> &'static [&'static str] { + match module { + "P0" => &["C0"], + "P1" => &["C0", "C1", "C2", "C3", "C4"], + _ => &[], + } + } + + fn get_call_metadata(&self) -> CallMetadata { + CallMetadata { + function_name: "unused", + pallet_name: "unused", + } + } + } + + #[test] + fn transaction_identifier_from_call_reads_first_two_bytes() { + let id = TransactionIdentifier::from_call(&DummyCall(1, 4)).expect("identifier"); + assert_eq!(id, TransactionIdentifier::new(1, 4)); + } + + #[test] + fn transaction_identifier_names_resolves_metadata() { + let id = TransactionIdentifier::new(1, 4); + assert_eq!(id.names::(), Some(("P1", "C4"))); + } + + #[test] + fn transaction_identifier_for_call_names_resolves_indices() { + let id = TransactionIdentifier::for_call_names::("P1", "C4").expect("id"); + assert_eq!(id, TransactionIdentifier::new(1, 4)); + } + + #[test] + fn rate_limit_target_accessors_work() { + let tx = RateLimitTarget::::Transaction(TransactionIdentifier::new(1, 4)); + assert!(tx.as_group().is_none()); + assert_eq!( + tx.as_transaction().copied(), + Some(TransactionIdentifier::new(1, 4)) + ); + + let group = RateLimitTarget::::Group(7); + assert!(group.as_transaction().is_none()); + assert_eq!(group.as_group().copied(), Some(7)); + } +} diff --git a/pallets/rate-limiting/Cargo.toml b/pallets/rate-limiting/Cargo.toml new file mode 100644 index 0000000000..00bae918dd --- /dev/null +++ b/pallets/rate-limiting/Cargo.toml @@ -0,0 +1,49 @@ +[package] +name = "pallet-rate-limiting" +version = "0.1.0" +edition.workspace = true + +[lints] +workspace = true + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { workspace = true, features = ["derive"] } +frame-benchmarking = { workspace = true, optional = true } +frame-support.workspace = true +frame-system.workspace = true +scale-info = { workspace = true, features = ["derive"] } +serde = { workspace = true, features = ["derive"] } +sp-std.workspace = true +sp-runtime.workspace = true +rate-limiting-interface.workspace = true +subtensor-runtime-common.workspace = true + +[dev-dependencies] +sp-core.workspace = true +sp-io.workspace = true +sp-runtime.workspace = true + +[features] +default = ["std"] +std = [ + "codec/std", + "frame-benchmarking?/std", + "frame-support/std", + "frame-system/std", + "rate-limiting-interface/std", + "scale-info/std", + "serde/std", + "sp-std/std", + "sp-runtime/std", + "subtensor-runtime-common/std", +] +runtime-benchmarks = [ + "frame-benchmarking", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", +] diff --git a/pallets/rate-limiting/rpc/Cargo.toml b/pallets/rate-limiting/rpc/Cargo.toml new file mode 100644 index 0000000000..d5bf689e8b --- /dev/null +++ b/pallets/rate-limiting/rpc/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "pallet-rate-limiting-rpc" +version = "0.1.0" +description = "RPC interface for the rate limiting pallet" +edition.workspace = true + +[dependencies] +jsonrpsee = { workspace = true, features = ["client-core", "server", "macros"] } +sp-api.workspace = true +sp-blockchain.workspace = true +sp-runtime.workspace = true +pallet-rate-limiting-runtime-api.workspace = true +subtensor-runtime-common = { workspace = true, default-features = false } + +[features] +default = ["std"] +std = [ + "sp-api/std", + "sp-runtime/std", + "pallet-rate-limiting-runtime-api/std", + "subtensor-runtime-common/std", +] diff --git a/pallets/rate-limiting/rpc/src/lib.rs b/pallets/rate-limiting/rpc/src/lib.rs new file mode 100644 index 0000000000..ca7452a7a0 --- /dev/null +++ b/pallets/rate-limiting/rpc/src/lib.rs @@ -0,0 +1,82 @@ +//! RPC interface for the rate limiting pallet. + +use jsonrpsee::{ + core::RpcResult, + proc_macros::rpc, + types::{ErrorObjectOwned, error::ErrorObject}, +}; +use sp_api::ProvideRuntimeApi; +use sp_blockchain::HeaderBackend; +use sp_runtime::traits::Block as BlockT; +use std::sync::Arc; + +pub use pallet_rate_limiting_runtime_api::{RateLimitRpcResponse, RateLimitingRuntimeApi}; + +#[rpc(client, server)] +pub trait RateLimitingRpcApi { + #[method(name = "rateLimiting_getRateLimit")] + fn get_rate_limit( + &self, + pallet: Vec, + extrinsic: Vec, + at: Option, + ) -> RpcResult>; +} + +/// Error type of this RPC api. +pub enum Error { + /// The call to runtime failed. + RuntimeError(String), +} + +impl From for ErrorObjectOwned { + fn from(e: Error) -> Self { + match e { + Error::RuntimeError(e) => ErrorObject::owned(1, e, None::<()>), + } + } +} + +impl From for i32 { + fn from(e: Error) -> i32 { + match e { + Error::RuntimeError(_) => 1, + } + } +} + +/// RPC implementation for the rate limiting pallet. +pub struct RateLimiting { + client: Arc, + _marker: std::marker::PhantomData, +} + +impl RateLimiting { + /// Creates a new instance of the rate limiting RPC helper. + pub fn new(client: Arc) -> Self { + Self { + client, + _marker: Default::default(), + } + } +} + +impl RateLimitingRpcApiServer<::Hash> for RateLimiting +where + Block: BlockT, + C: ProvideRuntimeApi + HeaderBackend + Send + Sync + 'static, + C::Api: RateLimitingRuntimeApi, +{ + fn get_rate_limit( + &self, + pallet: Vec, + extrinsic: Vec, + at: Option<::Hash>, + ) -> RpcResult> { + let api = self.client.runtime_api(); + let at = at.unwrap_or_else(|| self.client.info().best_hash); + + api.get_rate_limit(at, pallet, extrinsic) + .map_err(|e| Error::RuntimeError(format!("Unable to fetch rate limit: {e:?}")).into()) + } +} diff --git a/pallets/rate-limiting/runtime-api/Cargo.toml b/pallets/rate-limiting/runtime-api/Cargo.toml new file mode 100644 index 0000000000..2847d865dd --- /dev/null +++ b/pallets/rate-limiting/runtime-api/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "pallet-rate-limiting-runtime-api" +version = "0.1.0" +description = "Runtime API for the rate limiting pallet" +edition.workspace = true + +[dependencies] +codec = { workspace = true, features = ["derive"] } +scale-info = { workspace = true, features = ["derive"] } +sp-api.workspace = true +sp-std.workspace = true +pallet-rate-limiting.workspace = true +subtensor-runtime-common = { workspace = true, default-features = false } +serde = { workspace = true, features = ["derive"], optional = true } + +[features] +default = ["std"] +std = [ + "codec/std", + "scale-info/std", + "sp-api/std", + "sp-std/std", + "pallet-rate-limiting/std", + "subtensor-runtime-common/std", + "serde", +] diff --git a/pallets/rate-limiting/runtime-api/src/lib.rs b/pallets/rate-limiting/runtime-api/src/lib.rs new file mode 100644 index 0000000000..98b55e9a26 --- /dev/null +++ b/pallets/rate-limiting/runtime-api/src/lib.rs @@ -0,0 +1,25 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +use codec::{Decode, Encode}; +use pallet_rate_limiting::RateLimitKind; +use scale_info::TypeInfo; +use sp_std::vec::Vec; +use subtensor_runtime_common::BlockNumber; + +#[cfg(feature = "std")] +use serde::{Deserialize, Serialize}; + +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)] +pub struct RateLimitRpcResponse { + pub global: Option>, + pub contextual: Vec<(Vec, RateLimitKind)>, + pub default_limit: BlockNumber, + pub resolved: Option, +} + +sp_api::decl_runtime_apis! { + pub trait RateLimitingRuntimeApi { + fn get_rate_limit(pallet: Vec, extrinsic: Vec) -> Option; + } +} diff --git a/pallets/rate-limiting/src/benchmarking.rs b/pallets/rate-limiting/src/benchmarking.rs new file mode 100644 index 0000000000..4b1b2ed385 --- /dev/null +++ b/pallets/rate-limiting/src/benchmarking.rs @@ -0,0 +1,186 @@ +//! Benchmarking setup for pallet-rate-limiting +#![cfg(feature = "runtime-benchmarks")] +#![allow(clippy::arithmetic_side_effects)] + +use codec::Decode; +use frame_benchmarking::v2::*; +use frame_system::{RawOrigin, pallet_prelude::BlockNumberFor}; +use sp_runtime::traits::{One, Saturating}; +use sp_std::boxed::Box; + +use super::*; +use crate::CallReadOnly; + +pub trait BenchmarkHelper { + fn sample_call() -> Call; +} + +impl BenchmarkHelper for () +where + Call: Decode, +{ + fn sample_call() -> Call { + Decode::decode(&mut &[][..]).expect("Provide a call via BenchmarkHelper::sample_call") + } +} + +fn sample_call() -> Box<::RuntimeCall> +where + T::BenchmarkHelper: BenchmarkHelper<::RuntimeCall>, +{ + Box::new(T::BenchmarkHelper::sample_call()) +} + +fn seed_group(name: &[u8], sharing: GroupSharing) -> ::GroupId { + Pallet::::create_group(RawOrigin::Root.into(), name.to_vec(), sharing) + .expect("group created"); + Pallet::::next_group_id().saturating_sub(::GroupId::one()) +} + +fn register_call_with_group( + group: Option<::GroupId>, +) -> TransactionIdentifier { + let call = sample_call::(); + let identifier = TransactionIdentifier::from_call(call.as_ref()).expect("id"); + Pallet::::register_call(RawOrigin::Root.into(), call, group).expect("registered"); + identifier +} + +#[benchmarks] +mod benchmarks { + use super::*; + use sp_std::vec::Vec; + + #[benchmark] + fn register_call() { + let call = sample_call::(); + let identifier = TransactionIdentifier::from_call(call.as_ref()).expect("id"); + let target = RateLimitTarget::Transaction(identifier); + + #[extrinsic_call] + _(RawOrigin::Root, call, None); + + assert!(Limits::::contains_key(target)); + } + + #[benchmark] + fn set_rate_limit() { + let call = sample_call::(); + let identifier = TransactionIdentifier::from_call(call.as_ref()).expect("id"); + let target = RateLimitTarget::Transaction(identifier); + Limits::::insert(target, RateLimit::global(RateLimitKind::Default)); + + let limit = RateLimitKind::>::Exact(BlockNumberFor::::from(10u32)); + + #[extrinsic_call] + _(RawOrigin::Root, target, None, limit); + + let stored = Limits::::get(target).expect("limit stored"); + assert!( + matches!(stored, RateLimit::Global(RateLimitKind::Exact(span)) if span == BlockNumberFor::::from(10u32)) + ); + } + + #[benchmark] + fn assign_call_to_group() { + let group = seed_group::(b"grp", GroupSharing::UsageOnly); + let identifier = register_call_with_group::(None); + + #[extrinsic_call] + _(RawOrigin::Root, identifier, group, false); + + assert_eq!(CallGroups::::get(identifier), Some(group)); + assert_eq!(CallReadOnly::::get(identifier), Some(false)); + assert!(GroupMembers::::get(group).contains(&identifier)); + } + + #[benchmark] + fn remove_call_from_group() { + let group = seed_group::(b"team", GroupSharing::ConfigOnly); + let identifier = register_call_with_group::(Some(group)); + + #[extrinsic_call] + _(RawOrigin::Root, identifier); + + assert!(CallGroups::::get(identifier).is_none()); + assert!(!GroupMembers::::get(group).contains(&identifier)); + } + + #[benchmark] + fn create_group() { + let name = b"bench".to_vec(); + let sharing = GroupSharing::ConfigAndUsage; + + #[extrinsic_call] + _(RawOrigin::Root, name.clone(), sharing); + + let group = Pallet::::next_group_id().saturating_sub(::GroupId::one()); + let details = Groups::::get(group).expect("group stored"); + let stored: Vec = details.name.into(); + assert_eq!(stored, name); + assert_eq!(details.sharing, sharing); + } + + #[benchmark] + fn update_group() { + let group = seed_group::(b"old", GroupSharing::UsageOnly); + let new_name = b"new".to_vec(); + let new_sharing = GroupSharing::ConfigAndUsage; + + #[extrinsic_call] + _( + RawOrigin::Root, + group, + Some(new_name.clone()), + Some(new_sharing), + ); + + let details = Groups::::get(group).expect("group exists"); + let stored: Vec = details.name.into(); + assert_eq!(stored, new_name); + assert_eq!(details.sharing, new_sharing); + } + + #[benchmark] + fn delete_group() { + let group = seed_group::(b"delete", GroupSharing::UsageOnly); + + #[extrinsic_call] + _(RawOrigin::Root, group); + + assert!(Groups::::get(group).is_none()); + } + + #[benchmark] + fn deregister_call() { + let group = seed_group::(b"dreg", GroupSharing::ConfigAndUsage); + let identifier = register_call_with_group::(Some(group)); + let target = RateLimitTarget::Transaction(identifier); + let usage_target = Pallet::::usage_target(&identifier).expect("usage target"); + LastSeen::::insert( + usage_target, + None::, + BlockNumberFor::::from(1u32), + ); + + #[extrinsic_call] + _(RawOrigin::Root, identifier, None, true); + + assert!(Limits::::get(target).is_none()); + assert!(LastSeen::::get(usage_target, None::).is_none()); + assert!(CallGroups::::get(identifier).is_none()); + assert!(!GroupMembers::::get(group).contains(&identifier)); + } + + #[benchmark] + fn set_default_rate_limit() { + let block_span = BlockNumberFor::::from(10u32); + + #[extrinsic_call] + _(RawOrigin::Root, block_span); + + assert_eq!(DefaultLimit::::get(), block_span); + } + + impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test); +} diff --git a/pallets/rate-limiting/src/lib.rs b/pallets/rate-limiting/src/lib.rs new file mode 100644 index 0000000000..aa7e1765b2 --- /dev/null +++ b/pallets/rate-limiting/src/lib.rs @@ -0,0 +1,1456 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +//! Rate limiting for runtime calls with optional contextual restrictions. +//! +//! # Overview +//! +//! `pallet-rate-limiting` lets a runtime restrict how frequently particular calls can execute. +//! Limits are stored on-chain, keyed by explicit [`RateLimitTarget`] values. A target is either a +//! single [`TransactionIdentifier`] (the pallet/extrinsic indices) or a named *group* managed by +//! the admin APIs. Groups provide a way to give multiple calls the same configuration and/or usage +//! tracking without duplicating storage. Each target entry stores either a global span or a set of +//! scoped spans resolved at runtime. The pallet exposes a handful of extrinsics, restricted by +//! [`Config::AdminOrigin`], to manage this data: +//! +//! - [`register_call`](pallet::Pallet::register_call): register a call for rate limiting, seed its +//! initial configuration using [`Config::LimitScopeResolver`], and optionally place it into a +//! group. +//! - [`set_rate_limit`](pallet::Pallet::set_rate_limit): assign or override the limit at a specific +//! target/scope by supplying a [`RateLimitKind`] span. +//! - [`assign_call_to_group`](pallet::Pallet::assign_call_to_group) and +//! [`remove_call_from_group`](pallet::Pallet::remove_call_from_group): manage group membership +//! for registered calls. +//! - [`set_call_read_only`](pallet::Pallet::set_call_read_only): for grouped calls, choose whether +//! successful dispatches should update the shared usage row (`false` by default). +//! - [`deregister_call`](pallet::Pallet::deregister_call): remove scoped configuration or wipe the +//! registration entirely. +//! - [`set_default_rate_limit`](pallet::Pallet::set_default_rate_limit): set the global default +//! block span used by `RateLimitKind::Default` entries. +//! +//! The pallet also tracks the last block in which a target was observed, per optional *usage key*. +//! A usage key may refine tracking beyond the limit scope (for example combining a `netuid` with a +//! hyperparameter), so the two concepts are explicitly separated in the configuration. When the +//! admin puts several calls into a group and marks usage as shared, each dispatch still runs the +//! resolver: the group only chooses the storage target, while the resolver output (or `None`) picks +//! the row under that target. Calls that resolve to the same usage key update the same timestamp; +//! calls that resolve to different keys keep isolated timers even when they share a group. The same +//! rule applies to limit scopes—grouping funnels configuration into the same target, but the scope +//! resolver decides whether that entry is global or per-context. +//! +//! Each storage map is namespaced by pallet instance; runtimes can deploy multiple independent +//! instances to manage distinct rate-limiting scopes (in the global sense). +//! +//! # Transaction extension +//! +//! Enforcement happens via [`RateLimitTransactionExtension`], which implements +//! `sp_runtime::traits::TransactionExtension`. The extension consults `Limits`, fetches the current +//! block, and decides whether the call is eligible. If successful, it returns metadata that causes +//! [`LastSeen`](pallet::LastSeen) to update after dispatch. A rejected call yields +//! `InvalidTransaction::Custom(1)`. +//! +//! To enable the extension, add it to your runtime's transaction extension tuple. For example: +//! +//! ```ignore +//! pub type TransactionExtensions = ( +//! // ... other extensions ... +//! pallet_rate_limiting::RateLimitTransactionExtension, +//! ); +//! ``` +//! +//! # Context resolvers +//! +//! The pallet relies on two resolvers: +//! +//! - [`Config::LimitScopeResolver`], which determines how limits are stored (for example by +//! returning a `netuid`). The resolver can also signal that a call should bypass rate limiting or +//! adjust the effective span at validation time. When it returns `None`, the configuration is +//! stored as a global fallback. +//! - [`Config::UsageResolver`], which decides how executions are tracked in +//! [`LastSeen`](pallet::LastSeen). This can refine the limit scope (for example by returning a +//! tuple of `(netuid, hyperparameter)`). +//! +//! Each resolver receives the origin and call and may return `Some(identifier)` when scoping is +//! required, or `None` to use the global entry. Extrinsics such as +//! [`set_rate_limit`](pallet::Pallet::set_rate_limit) automatically consult these resolvers. When a +//! call belongs to a group the pallet still runs the resolver—instead of indexing storage at the +//! transaction-level target, it indexes at the group target. Resolving to different contexts keeps +//! independent limit/usage rows even though the calls share a group; resolving to the same context +//! causes them to share enforcement state. +//! +//! ```ignore +//! pub struct WeightsContextResolver; +//! +//! // Limits are scoped per netuid. +//! pub struct ScopeResolver; +//! impl pallet_rate_limiting::RateLimitScopeResolver< +//! RuntimeOrigin, +//! RuntimeCall, +//! NetUid, +//! BlockNumber, +//! > for ScopeResolver { +//! fn context(origin: &RuntimeOrigin, call: &RuntimeCall) -> Option> { +//! match call { +//! RuntimeCall::Subtensor(pallet_subtensor::Call::set_weights { netuid, .. }) => { +//! Some(vec![*netuid]) +//! } +//! _ => None, +//! } +//! } +//! +//! fn should_bypass(origin: &RuntimeOrigin, _call: &RuntimeCall) -> BypassDecision { +//! if matches!(origin, RuntimeOrigin::Root) { +//! BypassDecision::bypass_and_skip() +//! } else { +//! BypassDecision::enforce_and_record() +//! } +//! } +//! +//! fn adjust_span(_origin: &RuntimeOrigin, _call: &RuntimeCall, span: BlockNumber) -> BlockNumber { +//! span +//! } +//! } +//! +//! // Usage tracking distinguishes hyperparameter + netuid. +//! pub struct UsageResolver; +//! impl pallet_rate_limiting::RateLimitUsageResolver< +//! RuntimeOrigin, +//! RuntimeCall, +//! (NetUid, HyperParam), +//! > for UsageResolver { +//! fn context(_origin: &RuntimeOrigin, call: &RuntimeCall) -> Option<(NetUid, HyperParam)> { +//! match call { +//! RuntimeCall::Subtensor(pallet_subtensor::Call::set_hyperparam { +//! netuid, +//! hyper, +//! .. +//! }) => Some((*netuid, *hyper)), +//! _ => None, +//! } +//! } +//! } +//! +//! impl pallet_rate_limiting::Config for Runtime { +//! type RuntimeCall = RuntimeCall; +//! type LimitScope = NetUid; +//! type LimitScopeResolver = ScopeResolver; +//! type UsageKey = (NetUid, HyperParam); +//! type UsageResolver = UsageResolver; +//! type AdminOrigin = frame_system::EnsureRoot; +//! } +//! ``` + +#[cfg(feature = "runtime-benchmarks")] +pub use benchmarking::BenchmarkHelper; +pub use pallet::*; +pub use rate_limiting_interface::{RateLimitTarget, TransactionIdentifier}; +pub use rate_limiting_interface::{RateLimitingInterface, TryIntoRateLimitTarget}; +pub use tx_extension::RateLimitTransactionExtension; +pub use types::{ + BypassDecision, EnsureLimitSettingRule, GroupSharing, RateLimit, RateLimitGroup, RateLimitKind, + RateLimitScopeResolver, RateLimitUsageResolver, +}; + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; +mod tx_extension; +mod types; + +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod tests; + +#[frame_support::pallet] +pub mod pallet { + use codec::Codec; + use frame_support::{ + BoundedBTreeSet, BoundedVec, + pallet_prelude::*, + traits::{BuildGenesisConfig, EnsureOrigin, GetCallMetadata}, + }; + use frame_system::pallet_prelude::*; + use sp_runtime::traits::{ + AtLeast32BitUnsigned, DispatchOriginOf, Dispatchable, Member, One, Saturating, Zero, + }; + use sp_std::{ + boxed::Box, collections::btree_map::BTreeMap, convert::TryFrom, marker::PhantomData, vec, + vec::Vec, + }; + + #[cfg(feature = "runtime-benchmarks")] + use crate::benchmarking::BenchmarkHelper as BenchmarkHelperTrait; + use crate::types::{ + EnsureLimitSettingRule, GroupSharing, RateLimit, RateLimitGroup, RateLimitKind, + RateLimitScopeResolver, RateLimitTarget, RateLimitUsageResolver, TransactionIdentifier, + }; + + type GroupNameOf = BoundedVec>::MaxGroupNameLength>; + type GroupMembersOf = + BoundedBTreeSet>::MaxGroupMembers>; + type GroupDetailsOf = RateLimitGroup<>::GroupId, GroupNameOf>; + + /// Configuration trait for the rate limiting pallet. + #[pallet::config] + pub trait Config: frame_system::Config + where + BlockNumberFor: MaybeSerializeDeserialize, + <>::RuntimeCall as Dispatchable>::RuntimeOrigin: + From<::RuntimeOrigin>, + { + /// The overarching runtime call type. + type RuntimeCall: Parameter + + Codec + + GetCallMetadata + + Dispatchable + + IsType<::RuntimeCall>; + + /// Origin permitted to configure rate limits. + type AdminOrigin: EnsureOrigin>; + + /// Rule type that decides which origins may call [`Pallet::set_rate_limit`]. + type LimitSettingRule: Parameter + Member + MaxEncodedLen + MaybeSerializeDeserialize; + + /// Default rule applied when a target does not have an explicit entry in + /// [`LimitSettingRules`]. + type DefaultLimitSettingRule: Get; + + /// Origin checker invoked when setting a rate limit, parameterized by the stored rule. + type LimitSettingOrigin: EnsureLimitSettingRule, Self::LimitSettingRule, Self::LimitScope>; + + /// Scope identifier used to namespace stored rate limits. + type LimitScope: Parameter + Clone + PartialEq + Eq + Ord + MaybeSerializeDeserialize; + + /// Resolves the scope for the given runtime call when configuring limits. + type LimitScopeResolver: RateLimitScopeResolver< + DispatchOriginOf<>::RuntimeCall>, + >::RuntimeCall, + Self::LimitScope, + BlockNumberFor, + >; + + /// Usage key tracked in [`LastSeen`] for rate-limited calls. + type UsageKey: Parameter + Clone + PartialEq + Eq + Ord + MaybeSerializeDeserialize; + + /// Resolves the usage key for the given runtime call when enforcing limits. + type UsageResolver: RateLimitUsageResolver< + DispatchOriginOf<>::RuntimeCall>, + >::RuntimeCall, + Self::UsageKey, + >; + + /// Identifier assigned to managed groups. + type GroupId: Parameter + + Member + + Copy + + MaybeSerializeDeserialize + + MaxEncodedLen + + AtLeast32BitUnsigned + + Default; + + /// Maximum number of extrinsics that may belong to a single group. + #[pallet::constant] + type MaxGroupMembers: Get; + + /// Maximum length (in bytes) of a group name. + #[pallet::constant] + type MaxGroupNameLength: Get; + + /// Helper used to construct runtime calls for benchmarking. + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper: BenchmarkHelperTrait<>::RuntimeCall>; + } + + /// Storage mapping from rate limit target to its configured rate limit. + #[pallet::storage] + #[pallet::getter(fn limits)] + pub type Limits, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + RateLimitTarget<>::GroupId>, + RateLimit<>::LimitScope, BlockNumberFor>, + OptionQuery, + >; + + #[pallet::type_value] + pub fn DefaultLimitSettingRuleFor, I: 'static>() -> T::LimitSettingRule { + T::DefaultLimitSettingRule::get() + } + + /// Stores the rule used to authorize [`Pallet::set_rate_limit`] per call/group target. + #[pallet::storage] + #[pallet::getter(fn limit_setting_rule)] + pub type LimitSettingRules, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + RateLimitTarget<>::GroupId>, + >::LimitSettingRule, + ValueQuery, + DefaultLimitSettingRuleFor, + >; + + /// Tracks when a rate-limited target was last observed per usage key. + #[pallet::storage] + pub type LastSeen, I: 'static = ()> = StorageDoubleMap< + _, + Blake2_128Concat, + RateLimitTarget<>::GroupId>, + Blake2_128Concat, + Option<>::UsageKey>, + BlockNumberFor, + OptionQuery, + >; + + /// Default block span applied when an extrinsic uses the default rate limit. + #[pallet::storage] + #[pallet::getter(fn default_limit)] + pub type DefaultLimit, I: 'static = ()> = + StorageValue<_, BlockNumberFor, ValueQuery>; + + /// Maps a transaction identifier to its assigned group. + #[pallet::storage] + #[pallet::getter(fn call_group)] + pub type CallGroups, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + TransactionIdentifier, + >::GroupId, + OptionQuery, + >; + + /// Tracks whether a grouped call should skip writing usage metadata on success. + #[pallet::storage] + #[pallet::getter(fn call_read_only)] + pub type CallReadOnly, I: 'static = ()> = + StorageMap<_, Blake2_128Concat, TransactionIdentifier, bool, OptionQuery>; + + /// Metadata for each configured group. + #[pallet::storage] + #[pallet::getter(fn groups)] + pub type Groups, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + >::GroupId, + GroupDetailsOf, + OptionQuery, + >; + + /// Tracks membership for each group. + #[pallet::storage] + #[pallet::getter(fn group_members)] + pub type GroupMembers, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + >::GroupId, + GroupMembersOf, + ValueQuery, + >; + + /// Enforces unique group names. + #[pallet::storage] + #[pallet::getter(fn group_id_by_name)] + pub type GroupNameIndex, I: 'static = ()> = + StorageMap<_, Blake2_128Concat, GroupNameOf, >::GroupId, OptionQuery>; + + /// Identifier used for the next group creation. + #[pallet::storage] + #[pallet::getter(fn next_group_id)] + pub type NextGroupId, I: 'static = ()> = + StorageValue<_, >::GroupId, ValueQuery>; + + /// Events emitted by the rate limiting pallet. + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event, I: 'static = ()> { + /// A call was registered for rate limiting. + CallRegistered { + /// Identifier of the registered transaction. + transaction: TransactionIdentifier, + /// Scope seeded during registration (if any). + scope: Option>::LimitScope>>, + /// Optional group assignment applied at registration time. + group: Option<>::GroupId>, + /// Pallet name associated with the transaction. + pallet: Vec, + /// Extrinsic name associated with the transaction. + extrinsic: Vec, + }, + /// A rate limit was set or updated for the specified target. + RateLimitSet { + /// Target whose configuration changed. + target: RateLimitTarget<>::GroupId>, + /// Identifier of the transaction when the target represents a call. + transaction: Option, + /// Limit scope to which the configuration applies, if any. + scope: Option<>::LimitScope>, + /// The rate limit policy applied to the target. + limit: RateLimitKind>, + /// Pallet name associated with the transaction, when available. + pallet: Option>, + /// Extrinsic name associated with the transaction, when available. + extrinsic: Option>, + }, + /// The rule that authorizes [`Pallet::set_rate_limit`] was updated for a target. + LimitSettingRuleUpdated { + /// Target whose limit-setting rule changed. + target: RateLimitTarget<>::GroupId>, + /// Updated rule. + rule: >::LimitSettingRule, + }, + /// A rate-limited call was deregistered or had a scoped entry cleared. + CallDeregistered { + /// Target whose configuration changed. + target: RateLimitTarget<>::GroupId>, + /// Identifier of the transaction when the target represents a call. + transaction: Option, + /// Limit scope from which the configuration was cleared, if any. + scope: Option<>::LimitScope>, + /// Pallet name associated with the transaction, when available. + pallet: Option>, + /// Extrinsic name associated with the transaction, when available. + extrinsic: Option>, + }, + /// The default rate limit was set or updated. + DefaultRateLimitSet { + /// The new default limit expressed in blocks. + block_span: BlockNumberFor, + }, + /// A group was created. + GroupCreated { + /// Identifier of the new group. + group: >::GroupId, + /// Human readable group name. + name: Vec, + /// Sharing policy configured for the group. + sharing: GroupSharing, + }, + /// A group's metadata or policy changed. + GroupUpdated { + /// Identifier of the group. + group: >::GroupId, + /// Human readable name. + name: Vec, + /// Updated sharing configuration. + sharing: GroupSharing, + }, + /// A group was deleted. + GroupDeleted { + /// Identifier of the removed group. + group: >::GroupId, + }, + /// A transaction was assigned to or removed from a group. + CallGroupUpdated { + /// Identifier of the transaction. + transaction: TransactionIdentifier, + /// Updated group assignment (None when cleared). + group: Option<>::GroupId>, + }, + /// A grouped call toggled whether it writes usage after enforcement. + CallReadOnlyUpdated { + /// Identifier of the transaction. + transaction: TransactionIdentifier, + /// Group to which the call belongs. + group: >::GroupId, + /// Current read-only flag. + read_only: bool, + }, + } + + /// Errors that can occur while configuring rate limits. + #[pallet::error] + pub enum Error { + /// Failed to extract the pallet and extrinsic indices from the call. + InvalidRuntimeCall, + /// Attempted to remove a limit that is not present. + MissingRateLimit, + /// Group metadata was not found. + UnknownGroup, + /// Attempted to create or rename a group to an existing name. + DuplicateGroupName, + /// Group name exceeds the configured maximum length. + GroupNameTooLong, + /// Operation requires the group to have no members. + GroupHasMembers, + /// Adding a member would exceed the configured limit. + GroupMemberLimitExceeded, + /// Call already belongs to the requested group. + CallAlreadyInGroup, + /// Call is not assigned to a group. + CallNotInGroup, + /// Operation requires the call to be registered first. + CallNotRegistered, + /// Attempted to register a call that already exists. + CallAlreadyRegistered, + /// Rate limit for this call must be configured via its group target. + MustTargetGroup, + /// Resolver failed to supply a required context value. + MissingScope, + /// Group cannot be removed because configuration or usage entries remain. + GroupInUse, + } + + #[pallet::genesis_config] + pub struct GenesisConfig, I: 'static = ()> { + pub default_limit: BlockNumberFor, + pub limits: Vec<( + RateLimitTarget<>::GroupId>, + Option<>::LimitScope>, + RateLimitKind>, + )>, + pub groups: Vec<(>::GroupId, Vec, GroupSharing)>, + pub limit_setting_rules: Vec<( + RateLimitTarget<>::GroupId>, + >::LimitSettingRule, + )>, + } + + impl, I: 'static> Default for GenesisConfig { + fn default() -> Self { + Self { + default_limit: Zero::zero(), + limits: Vec::new(), + groups: Vec::new(), + limit_setting_rules: Vec::new(), + } + } + } + + #[pallet::genesis_build] + impl, I: 'static> BuildGenesisConfig for GenesisConfig { + fn build(&self) { + DefaultLimit::::put(self.default_limit); + + // Seed groups first so limit targets can reference them. + let mut max_group: >::GroupId = Zero::zero(); + for (group_id, name, sharing) in &self.groups { + let bounded = GroupNameOf::::try_from(name.clone()) + .expect("Genesis group name exceeds MaxGroupNameLength"); + + assert!( + !Groups::::contains_key(group_id), + "Duplicate group id in genesis config" + ); + assert!( + !GroupNameIndex::::contains_key(&bounded), + "Duplicate group name in genesis config" + ); + + Groups::::insert( + group_id, + RateLimitGroup { + id: *group_id, + name: bounded.clone(), + sharing: *sharing, + }, + ); + GroupNameIndex::::insert(&bounded, *group_id); + GroupMembers::::insert(*group_id, GroupMembersOf::::new()); + if *group_id > max_group { + max_group = *group_id; + } + } + let next = max_group.saturating_add(One::one()); + NextGroupId::::put(next); + + for (identifier, scope, kind) in &self.limits { + if let RateLimitTarget::Group(group) = identifier { + assert!( + Groups::::contains_key(group), + "Genesis limit references unknown group" + ); + } + let target = *identifier; + Limits::::mutate(target, |entry| match scope { + None => { + *entry = Some(RateLimit::global(*kind)); + } + Some(sc) => { + if let Some(config) = entry { + config.upsert_scope(sc.clone(), *kind); + } else { + *entry = Some(RateLimit::scoped_single(sc.clone(), *kind)); + } + } + }); + } + + for (target, rule) in &self.limit_setting_rules { + LimitSettingRules::::insert(target, rule.clone()); + } + } + } + + #[pallet::pallet] + #[pallet::without_storage_info] + pub struct Pallet(PhantomData<(T, I)>); + + impl, I: 'static> Pallet { + /// Returns `true` when the given transaction identifier passes its configured rate limit + /// within the provided usage scope. + pub fn is_within_limit( + origin: &DispatchOriginOf<>::RuntimeCall>, + call: &>::RuntimeCall, + identifier: &TransactionIdentifier, + scopes: &Option>::LimitScope>>, + usage_key: &Option<>::UsageKey>, + ) -> Result { + let bypass = >::LimitScopeResolver::should_bypass(origin, call); + if bypass.bypass_enforcement { + return Ok(true); + } + + let target = Self::config_target(identifier)?; + Self::ensure_scope_available(&target, scopes)?; + + let usage_target = Self::usage_target(identifier)?; + let scope_list: Vec>::LimitScope>> = match scopes { + None => vec![None], + Some(resolved) if resolved.is_empty() => vec![None], + Some(resolved) => resolved.iter().cloned().map(Some).collect(), + }; + + for scope in scope_list { + let Some(block_span) = Self::effective_span(origin, call, &target, &scope) else { + continue; + }; + if !Self::within_span(&usage_target, usage_key, block_span) { + return Ok(false); + } + } + + Ok(true) + } + + /// Resolves the configured span for the provided target/scope, applying the pallet default + /// when the stored value uses [`RateLimitKind::Default`]. + pub fn resolved_limit( + target: &RateLimitTarget<>::GroupId>, + scope: &Option<>::LimitScope>, + ) -> Option> { + let config = Limits::::get(target)?; + let kind = config.kind_for(scope.as_ref())?; + Some(match *kind { + RateLimitKind::Default => DefaultLimit::::get(), + RateLimitKind::Exact(block_span) => block_span, + }) + } + + /// Resolves the span for a target/scope and applies the configured span adjustment (e.g., + /// tempo scaling) using the pallet's scope resolver. + pub fn effective_span( + origin: &DispatchOriginOf<>::RuntimeCall>, + call: &>::RuntimeCall, + target: &RateLimitTarget<>::GroupId>, + scope: &Option<>::LimitScope>, + ) -> Option> { + let span = Self::resolved_limit(target, scope)?; + Some(>::LimitScopeResolver::adjust_span( + origin, call, span, + )) + } + + pub(crate) fn within_span( + target: &RateLimitTarget<>::GroupId>, + usage_key: &Option<>::UsageKey>, + block_span: BlockNumberFor, + ) -> bool { + if block_span.is_zero() { + return true; + } + + if let Some(last) = LastSeen::::get(target, usage_key) { + let current = frame_system::Pallet::::block_number(); + let delta = current.saturating_sub(last); + if delta < block_span { + return false; + } + } + + true + } + + pub(crate) fn should_record_usage( + identifier: &TransactionIdentifier, + usage_target: &RateLimitTarget<>::GroupId>, + ) -> bool { + match usage_target { + RateLimitTarget::Group(_) => { + !CallReadOnly::::get(identifier).unwrap_or(false) + } + RateLimitTarget::Transaction(_) => true, + } + } + + /// Inserts or updates the cached usage timestamp for a rate-limited call. + /// + /// This is primarily intended for migrations that need to hydrate the new tracking storage + /// from legacy pallets. + pub fn record_last_seen( + target: RateLimitTarget<>::GroupId>, + usage_key: Option<>::UsageKey>, + block_number: BlockNumberFor, + ) { + LastSeen::::insert(target, usage_key, block_number); + } + + /// Migrates a stored rate limit configuration from one scope to another. + /// + /// Returns `true` when an entry was moved. Passing identical `from`/`to` scopes simply + /// checks that a configuration exists. + pub fn migrate_limit_scope( + target: RateLimitTarget<>::GroupId>, + from: Option<>::LimitScope>, + to: Option<>::LimitScope>, + ) -> bool { + if from == to { + return Limits::::contains_key(target); + } + + let mut migrated = false; + Limits::::mutate(target, |maybe_config| { + if let Some(config) = maybe_config { + match (from.as_ref(), to.as_ref()) { + (None, Some(target)) => { + if let RateLimit::Global(kind) = config { + *config = RateLimit::scoped_single(target.clone(), *kind); + migrated = true; + } + } + (Some(source), Some(target)) => { + if let RateLimit::Scoped(map) = config { + if let Some(kind) = map.remove(source) { + map.insert(target.clone(), kind); + migrated = true; + } + } + } + (Some(source), None) => { + if let RateLimit::Scoped(map) = config { + if map.len() == 1 && map.contains_key(source) { + if let Some(kind) = map.remove(source) { + *config = RateLimit::global(kind); + migrated = true; + } + } + } + } + _ => {} + } + } + }); + + migrated + } + + /// Migrates the cached usage information for a rate-limited call to a new key. + /// + /// Returns `true` when an entry was moved. Passing identical keys simply checks that an + /// entry exists. + pub fn migrate_usage_key( + target: RateLimitTarget<>::GroupId>, + from: Option<>::UsageKey>, + to: Option<>::UsageKey>, + ) -> bool { + if from == to { + return LastSeen::::contains_key(target, to); + } + + let Some(block) = LastSeen::::take(target, from) else { + return false; + }; + + LastSeen::::insert(target, to, block); + true + } + + /// Returns the configured limit for the specified pallet/extrinsic names, if any. + pub fn limit_for_call_names( + pallet_name: &str, + extrinsic_name: &str, + scope: Option<>::LimitScope>, + ) -> Option>> { + let identifier = Self::identifier_for_call_names(pallet_name, extrinsic_name)?; + let target = Self::config_target(&identifier).ok()?; + Limits::::get(target).and_then(|config| config.kind_for(scope.as_ref()).copied()) + } + + /// Returns the resolved block span for the specified pallet/extrinsic names, if any. + pub fn resolved_limit_for_call_names( + pallet_name: &str, + extrinsic_name: &str, + scope: Option<>::LimitScope>, + ) -> Option> { + let identifier = Self::identifier_for_call_names(pallet_name, extrinsic_name)?; + let target = Self::config_target(&identifier).ok()?; + Self::resolved_limit(&target, &scope) + } + + /// Looks up the transaction identifier for a pallet/extrinsic name pair. + pub fn identifier_for_call_names( + pallet_name: &str, + extrinsic_name: &str, + ) -> Option { + let modules = >::RuntimeCall::get_module_names(); + let pallet_pos = modules.iter().position(|name| *name == pallet_name)?; + let call_names = >::RuntimeCall::get_call_names(pallet_name); + let extrinsic_pos = call_names.iter().position(|name| *name == extrinsic_name)?; + let pallet_index = u8::try_from(pallet_pos).ok()?; + let extrinsic_index = u8::try_from(extrinsic_pos).ok()?; + Some(TransactionIdentifier::new(pallet_index, extrinsic_index)) + } + + fn ensure_call_registered(identifier: &TransactionIdentifier) -> DispatchResult { + let target = RateLimitTarget::Transaction(*identifier); + ensure!( + Limits::::contains_key(target), + Error::::CallNotRegistered + ); + Ok(()) + } + + fn ensure_call_unregistered(identifier: &TransactionIdentifier) -> DispatchResult { + let target = RateLimitTarget::Transaction(*identifier); + ensure!( + !Limits::::contains_key(target), + Error::::CallAlreadyRegistered + ); + Ok(()) + } + + /// Returns true when the call has been registered (either directly or via a group). + pub fn is_registered(identifier: &TransactionIdentifier) -> bool { + let tx_target = RateLimitTarget::Transaction(*identifier); + Limits::::contains_key(tx_target) || CallGroups::::contains_key(identifier) + } + + fn call_metadata( + identifier: &TransactionIdentifier, + ) -> Result<(Vec, Vec), DispatchError> { + let (pallet_name, extrinsic_name) = identifier + .names::<>::RuntimeCall>() + .ok_or(Error::::InvalidRuntimeCall)?; + Ok(( + Vec::from(pallet_name.as_bytes()), + Vec::from(extrinsic_name.as_bytes()), + )) + } + + /// Returns the storage target used to store configuration for the provided identifier, + /// respecting any configured group assignment. + pub fn config_target( + identifier: &TransactionIdentifier, + ) -> Result>::GroupId>, DispatchError> { + Self::target_for(identifier, GroupSharing::config_uses_group) + } + + pub(crate) fn usage_target( + identifier: &TransactionIdentifier, + ) -> Result>::GroupId>, DispatchError> { + Self::target_for(identifier, GroupSharing::usage_uses_group) + } + + fn target_for( + identifier: &TransactionIdentifier, + predicate: impl Fn(GroupSharing) -> bool, + ) -> Result>::GroupId>, DispatchError> { + let group = Self::group_assignment(identifier)?; + Ok(Self::target_from_details( + identifier, + group.as_ref(), + predicate, + )) + } + + fn group_assignment( + identifier: &TransactionIdentifier, + ) -> Result>, DispatchError> { + let Some(group) = CallGroups::::get(identifier) else { + return Ok(None); + }; + let details = Self::ensure_group_details(group)?; + Ok(Some(details)) + } + + fn target_from_details( + identifier: &TransactionIdentifier, + details: Option<&GroupDetailsOf>, + predicate: impl Fn(GroupSharing) -> bool, + ) -> RateLimitTarget<>::GroupId> { + if let Some(details) = details { + if predicate(details.sharing) { + return RateLimitTarget::Group(details.id); + } + } + RateLimitTarget::Transaction(*identifier) + } + + fn ensure_group_details( + group: >::GroupId, + ) -> Result, DispatchError> { + Groups::::get(group).ok_or(Error::::UnknownGroup.into()) + } + + fn ensure_scope_available( + target: &RateLimitTarget<>::GroupId>, + scopes: &Option>::LimitScope>>, + ) -> Result<(), DispatchError> { + let has_scope = scopes.as_ref().map_or(false, |scopes| !scopes.is_empty()); + if has_scope { + return Ok(()); + } + + if let Some(RateLimit::Scoped(map)) = Limits::::get(target) { + if !map.is_empty() { + return Err(Error::::MissingScope.into()); + } + } + + Ok(()) + } + + fn bounded_group_name(name: Vec) -> Result, DispatchError> { + GroupNameOf::::try_from(name).map_err(|_| Error::::GroupNameTooLong.into()) + } + + fn ensure_group_name_available( + name: &GroupNameOf, + current: Option<>::GroupId>, + ) -> DispatchResult { + if let Some(existing) = GroupNameIndex::::get(name) { + ensure!(Some(existing) == current, Error::::DuplicateGroupName); + } + Ok(()) + } + + fn ensure_group_deletable(group: >::GroupId) -> DispatchResult { + ensure!( + GroupMembers::::get(group).is_empty(), + Error::::GroupHasMembers + ); + let target = RateLimitTarget::Group(group); + ensure!( + !Limits::::contains_key(target), + Error::::GroupInUse + ); + ensure!( + LastSeen::::iter_prefix(target).next().is_none(), + Error::::GroupInUse + ); + Ok(()) + } + + fn insert_call_into_group( + identifier: &TransactionIdentifier, + group: >::GroupId, + ) -> DispatchResult { + GroupMembers::::try_mutate(group, |members| -> DispatchResult { + match members.try_insert(*identifier) { + Ok(true) => Ok(()), + Ok(false) => Err(Error::::CallAlreadyInGroup.into()), + Err(_) => Err(Error::::GroupMemberLimitExceeded.into()), + } + })?; + Ok(()) + } + + fn detach_call_from_group( + identifier: &TransactionIdentifier, + group: >::GroupId, + ) -> bool { + GroupMembers::::mutate(group, |members| members.remove(identifier)) + } + } + + #[pallet::call] + impl, I: 'static> Pallet { + /// Registers a call for rate limiting and seeds its initial configuration. + #[pallet::call_index(0)] + #[pallet::weight(T::DbWeight::get().reads_writes(3, 3))] + pub fn register_call( + origin: OriginFor, + call: Box<>::RuntimeCall>, + group: Option<>::GroupId>, + ) -> DispatchResult { + let resolver_origin: DispatchOriginOf<>::RuntimeCall> = + Into::>::RuntimeCall>>::into(origin.clone()); + let scopes = + >::LimitScopeResolver::context(&resolver_origin, call.as_ref()); + + T::AdminOrigin::ensure_origin(origin)?; + + let identifier = TransactionIdentifier::from_call(call.as_ref()) + .ok_or(Error::::InvalidRuntimeCall)?; + Self::ensure_call_unregistered(&identifier)?; + + let target = RateLimitTarget::Transaction(identifier); + + let scopes = scopes.and_then(|scopes| { + if scopes.is_empty() { + None + } else { + Some(scopes) + } + }); + if let Some(ref resolved) = scopes { + let mut map = BTreeMap::new(); + for scope in resolved { + map.insert(scope.clone(), RateLimitKind::Default); + } + Limits::::insert(target, RateLimit::Scoped(map)); + } else { + Limits::::insert(target, RateLimit::global(RateLimitKind::Default)); + } + + let mut assigned_group = None; + if let Some(group_id) = group { + Self::ensure_group_details(group_id)?; + Self::insert_call_into_group(&identifier, group_id)?; + CallGroups::::insert(&identifier, group_id); + CallReadOnly::::insert(&identifier, false); + assigned_group = Some(group_id); + } + + let (pallet, extrinsic) = Self::call_metadata(&identifier)?; + Self::deposit_event(Event::CallRegistered { + transaction: identifier, + scope: scopes, + group: assigned_group, + pallet: pallet, + extrinsic: extrinsic, + }); + + if let Some(group_id) = assigned_group { + Self::deposit_event(Event::CallGroupUpdated { + transaction: identifier, + group: Some(group_id), + }); + Self::deposit_event(Event::CallReadOnlyUpdated { + transaction: identifier, + group: group_id, + read_only: false, + }); + } + + Ok(()) + } + + /// Configures a rate limit for either a transaction or group target. + #[pallet::call_index(1)] + #[pallet::weight(T::DbWeight::get().reads_writes(2, 2))] + pub fn set_rate_limit( + origin: OriginFor, + target: RateLimitTarget<>::GroupId>, + scope: Option<>::LimitScope>, + limit: RateLimitKind>, + ) -> DispatchResult { + let rule = LimitSettingRules::::get(&target); + T::LimitSettingOrigin::ensure_origin(origin, &rule, &scope)?; + + let (transaction, pallet, extrinsic) = match target { + RateLimitTarget::Transaction(identifier) => { + Self::ensure_call_registered(&identifier)?; + if let Some(group) = CallGroups::::get(&identifier) { + let details = Self::ensure_group_details(group)?; + ensure!( + !details.sharing.config_uses_group(), + Error::::MustTargetGroup + ); + } + let (pallet, extrinsic) = Self::call_metadata(&identifier)?; + (Some(identifier), Some(pallet), Some(extrinsic)) + } + RateLimitTarget::Group(group) => { + Self::ensure_group_details(group)?; + (None, None, None) + } + }; + + if let Some(ref scoped) = scope { + Limits::::mutate(target, |slot| match slot { + Some(config) => config.upsert_scope(scoped.clone(), limit), + None => *slot = Some(RateLimit::scoped_single(scoped.clone(), limit)), + }); + } else { + Limits::::insert(target, RateLimit::global(limit)); + } + + Self::deposit_event(Event::RateLimitSet { + target, + transaction, + scope, + limit, + pallet, + extrinsic, + }); + Ok(()) + } + + /// Sets the rule used to authorize [`Pallet::set_rate_limit`] for the provided target. + #[pallet::call_index(10)] + #[pallet::weight(T::DbWeight::get().reads_writes(2, 1))] + pub fn set_limit_setting_rule( + origin: OriginFor, + target: RateLimitTarget<>::GroupId>, + rule: >::LimitSettingRule, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + match target { + RateLimitTarget::Transaction(identifier) => { + Self::ensure_call_registered(&identifier)?; + } + RateLimitTarget::Group(group) => { + Self::ensure_group_details(group)?; + } + } + + LimitSettingRules::::insert(target, rule.clone()); + Self::deposit_event(Event::LimitSettingRuleUpdated { target, rule }); + + Ok(()) + } + + /// Assigns a registered call to the specified group and optionally marks it as read-only + /// for usage tracking. + #[pallet::call_index(2)] + #[pallet::weight(T::DbWeight::get().reads_writes(3, 3))] + pub fn assign_call_to_group( + origin: OriginFor, + transaction: TransactionIdentifier, + group: >::GroupId, + read_only: bool, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + Self::ensure_call_registered(&transaction)?; + Self::ensure_group_details(group)?; + + let current = CallGroups::::get(&transaction); + ensure!(current.is_none(), Error::::CallAlreadyInGroup); + Self::insert_call_into_group(&transaction, group)?; + CallGroups::::insert(&transaction, group); + CallReadOnly::::insert(&transaction, read_only); + + Self::deposit_event(Event::CallGroupUpdated { + transaction, + group: Some(group), + }); + Self::deposit_event(Event::CallReadOnlyUpdated { + transaction, + group, + read_only, + }); + + Ok(()) + } + + /// Removes a registered call from its current group assignment. + #[pallet::call_index(3)] + #[pallet::weight(T::DbWeight::get().reads_writes(2, 2))] + pub fn remove_call_from_group( + origin: OriginFor, + transaction: TransactionIdentifier, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + Self::ensure_call_registered(&transaction)?; + let Some(group) = CallGroups::::take(&transaction) else { + return Err(Error::::CallNotInGroup.into()); + }; + CallReadOnly::::remove(&transaction); + Self::detach_call_from_group(&transaction, group); + + Self::deposit_event(Event::CallGroupUpdated { + transaction, + group: None, + }); + + Ok(()) + } + + /// Sets the default rate limit that applies when an extrinsic uses [`RateLimitKind::Default`]. + #[pallet::call_index(4)] + #[pallet::weight(T::DbWeight::get().writes(1))] + pub fn set_default_rate_limit( + origin: OriginFor, + block_span: BlockNumberFor, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + DefaultLimit::::put(block_span); + Self::deposit_event(Event::DefaultRateLimitSet { block_span }); + Ok(()) + } + + /// Creates a new rate-limiting group with the provided name and sharing configuration. + #[pallet::call_index(5)] + #[pallet::weight(T::DbWeight::get().reads_writes(1, 3))] + pub fn create_group( + origin: OriginFor, + name: Vec, + sharing: GroupSharing, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + let bounded = Self::bounded_group_name(name)?; + Self::ensure_group_name_available(&bounded, None)?; + + let group = NextGroupId::::mutate(|current| { + let next = current.saturating_add(One::one()); + sp_std::mem::replace(current, next) + }); + + Groups::::insert( + group, + RateLimitGroup { + id: group, + name: bounded.clone(), + sharing, + }, + ); + GroupNameIndex::::insert(&bounded, group); + GroupMembers::::insert(group, GroupMembersOf::::new()); + + let name_bytes: Vec = bounded.into(); + Self::deposit_event(Event::GroupCreated { + group, + name: name_bytes, + sharing, + }); + Ok(()) + } + + /// Updates the metadata or sharing configuration of an existing group. + #[pallet::call_index(6)] + #[pallet::weight(T::DbWeight::get().reads_writes(3, 3))] + pub fn update_group( + origin: OriginFor, + group: >::GroupId, + name: Option>, + sharing: Option, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + Groups::::try_mutate(group, |maybe_details| -> DispatchResult { + let details = maybe_details.as_mut().ok_or(Error::::UnknownGroup)?; + + if let Some(new_name) = name { + let bounded = Self::bounded_group_name(new_name)?; + Self::ensure_group_name_available(&bounded, Some(group))?; + GroupNameIndex::::remove(&details.name); + GroupNameIndex::::insert(&bounded, group); + details.name = bounded; + } + + if let Some(new_sharing) = sharing { + details.sharing = new_sharing; + } + + Ok(()) + })?; + + let updated = Self::ensure_group_details(group)?; + let name_bytes: Vec = updated.name.clone().into(); + Self::deposit_event(Event::GroupUpdated { + group, + name: name_bytes, + sharing: updated.sharing, + }); + + Ok(()) + } + + /// Deletes an existing group. The group must be empty and unused. + #[pallet::call_index(7)] + #[pallet::weight(T::DbWeight::get().reads_writes(3, 3))] + pub fn delete_group( + origin: OriginFor, + group: >::GroupId, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + Self::ensure_group_deletable(group)?; + + let details = Groups::::take(group).ok_or(Error::::UnknownGroup)?; + GroupNameIndex::::remove(&details.name); + GroupMembers::::remove(group); + + Self::deposit_event(Event::GroupDeleted { group }); + + Ok(()) + } + + /// Deregisters a call or removes a scoped entry from its configuration. + #[pallet::call_index(8)] + #[pallet::weight(T::DbWeight::get().reads_writes(4, 4))] + pub fn deregister_call( + origin: OriginFor, + transaction: TransactionIdentifier, + scope: Option<>::LimitScope>, + clear_usage: bool, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + Self::ensure_call_registered(&transaction)?; + let target = Self::config_target(&transaction)?; + let tx_target = RateLimitTarget::Transaction(transaction); + let usage_target = Self::usage_target(&transaction)?; + + match &scope { + Some(sc) => { + let mut removed = false; + Limits::::mutate_exists(target, |maybe_config| { + if let Some(RateLimit::Scoped(map)) = maybe_config { + if map.remove(sc).is_some() { + removed = true; + if map.is_empty() { + *maybe_config = None; + } + } + } + }); + ensure!(removed, Error::::MissingRateLimit); + + if let Some(group) = CallGroups::::take(&transaction) { + CallReadOnly::::remove(&transaction); + Self::detach_call_from_group(&transaction, group); + Self::deposit_event(Event::CallGroupUpdated { + transaction, + group: None, + }); + } + } + None => { + Limits::::remove(target); + if target != tx_target { + Limits::::remove(tx_target); + } + + if let Some(group) = CallGroups::::take(&transaction) { + CallReadOnly::::remove(&transaction); + Self::detach_call_from_group(&transaction, group); + Self::deposit_event(Event::CallGroupUpdated { + transaction, + group: None, + }); + } + } + } + + if clear_usage { + let _ = LastSeen::::clear_prefix(&usage_target, u32::MAX, None); + } + + let (pallet, extrinsic) = Self::call_metadata(&transaction)?; + Self::deposit_event(Event::CallDeregistered { + target, + transaction: Some(transaction), + scope, + pallet: Some(pallet), + extrinsic: Some(extrinsic), + }); + + Ok(()) + } + + /// Updates whether a grouped call should skip writing usage metadata after enforcement. + /// + /// The call must already be assigned to a group. + #[pallet::call_index(9)] + #[pallet::weight(T::DbWeight::get().reads_writes(2, 1))] + pub fn set_call_read_only( + origin: OriginFor, + transaction: TransactionIdentifier, + read_only: bool, + ) -> DispatchResult { + T::AdminOrigin::ensure_origin(origin)?; + + Self::ensure_call_registered(&transaction)?; + let group = + CallGroups::::get(&transaction).ok_or(Error::::CallNotInGroup)?; + CallReadOnly::::insert(&transaction, read_only); + + Self::deposit_event(Event::CallReadOnlyUpdated { + transaction, + group, + read_only, + }); + + Ok(()) + } + } +} + +impl, I: 'static> RateLimitingInterface for pallet::Pallet { + type GroupId = >::GroupId; + type CallMetadata = >::RuntimeCall; + type Limit = frame_system::pallet_prelude::BlockNumberFor; + type Scope = >::LimitScope; + type UsageKey = >::UsageKey; + + fn rate_limit(target: TargetArg, scope: Option) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + let raw_target = target + .try_into_rate_limit_target::() + .ok()?; + let config_target = match raw_target { + // A transaction identifier may be assigned to a group; resolve the effective storage + // target. + RateLimitTarget::Transaction(identifier) => Self::config_target(&identifier).ok()?, + _ => raw_target, + }; + Self::resolved_limit(&config_target, &scope) + } + + fn last_seen( + target: TargetArg, + usage_key: Option, + ) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + let raw_target = target + .try_into_rate_limit_target::() + .ok()?; + let usage_target = match raw_target { + // A transaction identifier may be assigned to a group; resolve the effective storage + // target. + RateLimitTarget::Transaction(identifier) => Self::usage_target(&identifier).ok()?, + _ => raw_target, + }; + pallet::LastSeen::::get(usage_target, usage_key) + } + + fn set_last_seen( + target: TargetArg, + usage_key: Option, + block: Option, + ) where + TargetArg: TryIntoRateLimitTarget, + { + let Some(raw_target) = target + .try_into_rate_limit_target::() + .ok() + else { + return; + }; + + let usage_target = match raw_target { + RateLimitTarget::Transaction(identifier) => { + if let Ok(resolved) = Self::usage_target(&identifier) { + resolved + } else { + return; + } + } + _ => raw_target, + }; + + match block { + Some(block) => pallet::LastSeen::::insert(usage_target, usage_key, block), + None => pallet::LastSeen::::remove(usage_target, usage_key), + } + } +} diff --git a/pallets/rate-limiting/src/mock.rs b/pallets/rate-limiting/src/mock.rs new file mode 100644 index 0000000000..a29c719ce4 --- /dev/null +++ b/pallets/rate-limiting/src/mock.rs @@ -0,0 +1,237 @@ +#![allow(dead_code)] + +use core::convert::TryInto; + +use frame_support::{ + derive_impl, + dispatch::DispatchResult, + sp_runtime::{ + BuildStorage, + traits::{BlakeTwo256, IdentityLookup}, + }, + traits::{ConstU16, ConstU32, ConstU64, EnsureOrigin, Everything}, +}; +use frame_system::{EnsureRoot, ensure_signed}; +use serde::{Deserialize, Serialize}; +use sp_core::H256; +use sp_io::TestExternalities; +use sp_std::vec::Vec; + +use crate as pallet_rate_limiting; +use crate::{RateLimitKind, TransactionIdentifier}; + +pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +pub type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test { + System: frame_system, + RateLimiting: pallet_rate_limiting, + } +); + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Test { + type BaseCallFilter = Everything; + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Nonce = u64; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = ConstU64<250>; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = ConstU16<42>; + type OnSetCode = (); + type MaxConsumers = ConstU32<16>; + type Block = Block; +} + +pub type LimitScope = u16; +pub type UsageKey = u16; +pub type GroupId = u32; + +#[derive( + codec::Encode, + codec::Decode, + codec::DecodeWithMemTracking, + Serialize, + Deserialize, + Clone, + Copy, + PartialEq, + Eq, + scale_info::TypeInfo, + codec::MaxEncodedLen, + Debug, +)] +pub enum LimitSettingRule { + RootOnly, + AnySigned, +} + +frame_support::parameter_types! { + pub const DefaultLimitSettingRule: LimitSettingRule = LimitSettingRule::RootOnly; +} + +pub struct LimitSettingOrigin; + +impl pallet_rate_limiting::EnsureLimitSettingRule + for LimitSettingOrigin +{ + fn ensure_origin( + origin: RuntimeOrigin, + rule: &LimitSettingRule, + _scope: &Option, + ) -> DispatchResult { + match rule { + LimitSettingRule::RootOnly => EnsureRoot::::ensure_origin(origin) + .map(|_| ()) + .map_err(Into::into), + LimitSettingRule::AnySigned => { + let _ = ensure_signed(origin)?; + Ok(()) + } + } + } +} + +pub struct TestScopeResolver; +pub struct TestUsageResolver; + +impl pallet_rate_limiting::RateLimitScopeResolver + for TestScopeResolver +{ + fn context(_origin: &RuntimeOrigin, call: &RuntimeCall) -> Option> { + match call { + RuntimeCall::RateLimiting(RateLimitingCall::set_rate_limit { limit, .. }) => { + let RateLimitKind::Exact(span) = limit else { + return Some(vec![1]); + }; + let scope = (*span).try_into().ok()?; + // Multi-scope path used by tests: Exact(42/43) returns two scopes. + if *span == 42 || *span == 43 { + Some(vec![scope, scope.saturating_add(1)]) + } else { + Some(vec![scope]) + } + } + RuntimeCall::RateLimiting(RateLimitingCall::set_default_rate_limit { block_span }) => { + let scope = (*block_span).try_into().ok()?; + Some(vec![scope]) + } + RuntimeCall::RateLimiting(_) => Some(vec![1]), + _ => None, + } + } + + fn should_bypass( + _origin: &RuntimeOrigin, + call: &RuntimeCall, + ) -> pallet_rate_limiting::types::BypassDecision { + match call { + RuntimeCall::RateLimiting(RateLimitingCall::remove_call_from_group { .. }) => { + pallet_rate_limiting::types::BypassDecision::bypass_and_skip() + } + RuntimeCall::RateLimiting(RateLimitingCall::set_default_rate_limit { .. }) => { + pallet_rate_limiting::types::BypassDecision::bypass_and_record() + } + _ => pallet_rate_limiting::types::BypassDecision::enforce_and_record(), + } + } + + fn adjust_span(_origin: &RuntimeOrigin, call: &RuntimeCall, span: u64) -> u64 { + if matches!( + call, + RuntimeCall::RateLimiting(RateLimitingCall::deregister_call { .. }) + ) { + span.saturating_mul(2) + } else { + span + } + } +} + +impl pallet_rate_limiting::RateLimitUsageResolver + for TestUsageResolver +{ + fn context(_origin: &RuntimeOrigin, call: &RuntimeCall) -> Option> { + match call { + RuntimeCall::RateLimiting(RateLimitingCall::set_rate_limit { limit, .. }) => { + let RateLimitKind::Exact(span) = limit else { + return Some(vec![1]); + }; + let key = (*span).try_into().ok()?; + // Multi-usage path used by tests: Exact(42) returns two usage keys. + if *span == 42 { + Some(vec![key, key.saturating_add(1)]) + } else { + Some(vec![key]) + } + } + RuntimeCall::RateLimiting(RateLimitingCall::set_default_rate_limit { block_span }) => { + let key = (*block_span).try_into().ok()?; + Some(vec![key]) + } + RuntimeCall::RateLimiting(_) => Some(vec![1]), + _ => None, + } + } +} + +impl pallet_rate_limiting::Config for Test { + type RuntimeCall = RuntimeCall; + type LimitScope = LimitScope; + type LimitScopeResolver = TestScopeResolver; + type UsageKey = UsageKey; + type UsageResolver = TestUsageResolver; + type AdminOrigin = EnsureRoot; + type LimitSettingRule = LimitSettingRule; + type DefaultLimitSettingRule = DefaultLimitSettingRule; + type LimitSettingOrigin = LimitSettingOrigin; + type GroupId = GroupId; + type MaxGroupMembers = ConstU32<32>; + type MaxGroupNameLength = ConstU32<64>; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = BenchHelper; +} + +#[cfg(feature = "runtime-benchmarks")] +pub struct BenchHelper; + +#[cfg(feature = "runtime-benchmarks")] +impl crate::BenchmarkHelper for BenchHelper { + fn sample_call() -> RuntimeCall { + RuntimeCall::System(frame_system::Call::remark { remark: Vec::new() }) + } +} + +pub type RateLimitingCall = crate::Call; + +pub fn new_test_ext() -> TestExternalities { + let storage = frame_system::GenesisConfig::::default() + .build_storage() + .expect("genesis build succeeds"); + + let mut ext = TestExternalities::new(storage); + ext.execute_with(|| System::set_block_number(1)); + ext +} + +pub(crate) fn identifier_for(call: &RuntimeCall) -> TransactionIdentifier { + TransactionIdentifier::from_call(call).expect("identifier for call") +} + +pub(crate) fn pop_last_event() -> RuntimeEvent { + System::events().pop().expect("event expected").event +} diff --git a/pallets/rate-limiting/src/tests.rs b/pallets/rate-limiting/src/tests.rs new file mode 100644 index 0000000000..874cc68241 --- /dev/null +++ b/pallets/rate-limiting/src/tests.rs @@ -0,0 +1,794 @@ +use frame_support::{assert_noop, assert_ok}; +use sp_std::vec::Vec; + +use crate::{ + CallGroups, CallReadOnly, Config, GroupMembers, GroupSharing, LastSeen, LimitSettingRules, + Limits, RateLimit, RateLimitKind, RateLimitTarget, TransactionIdentifier, mock::*, + pallet::Error, +}; +use frame_support::traits::Get; + +fn target(identifier: TransactionIdentifier) -> RateLimitTarget { + RateLimitTarget::Transaction(identifier) +} + +fn remark_call() -> RuntimeCall { + RuntimeCall::System(frame_system::Call::::remark { remark: Vec::new() }) +} + +fn scoped_call() -> RuntimeCall { + RuntimeCall::RateLimiting(RateLimitingCall::set_rate_limit { + target: RateLimitTarget::Transaction(TransactionIdentifier::new(0, 0)), + scope: Some(1), + limit: RateLimitKind::Default, + }) +} + +fn register(call: RuntimeCall, group: Option) -> TransactionIdentifier { + let identifier = identifier_for(&call); + assert_ok!(RateLimiting::register_call( + RuntimeOrigin::root(), + Box::new(call), + group + )); + identifier +} + +fn create_group(name: &[u8], sharing: GroupSharing) -> GroupId { + assert_ok!(RateLimiting::create_group( + RuntimeOrigin::root(), + name.to_vec(), + sharing, + )); + RateLimiting::next_group_id().saturating_sub(1) +} + +fn last_event() -> RuntimeEvent { + pop_last_event() +} + +#[test] +fn set_rate_limit_respects_limit_setting_rule() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + let tx_target = target(identifier); + + // Default rule is root-only. + assert_noop!( + RateLimiting::set_rate_limit( + RuntimeOrigin::signed(1), + tx_target, + None, + RateLimitKind::Exact(1), + ), + sp_runtime::DispatchError::BadOrigin + ); + + // Root updates the limit-setting rule for this transaction target. + assert_ok!(RateLimiting::set_limit_setting_rule( + RuntimeOrigin::root(), + tx_target, + LimitSettingRule::AnySigned, + )); + + assert_eq!( + LimitSettingRules::::get(tx_target), + LimitSettingRule::AnySigned + ); + + // Now any signed origin may set the limit for this target. + assert_ok!(RateLimiting::set_rate_limit( + RuntimeOrigin::signed(1), + tx_target, + None, + RateLimitKind::Exact(7), + )); + }); +} + +#[test] +fn register_call_seeds_global_limit() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + let tx_target = target(identifier); + let stored = Limits::::get(tx_target).expect("limit"); + assert!(matches!(stored, RateLimit::Global(RateLimitKind::Default))); + + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::CallRegistered { transaction, .. }) + if transaction == identifier + )); + }); +} + +#[test] +fn register_call_seeds_scoped_limit() { + new_test_ext().execute_with(|| { + let identifier = register(scoped_call(), None); + let tx_target = target(identifier); + let stored = Limits::::get(tx_target).expect("limit"); + match stored { + RateLimit::Scoped(map) => { + assert_eq!(map.get(&1u16), Some(&RateLimitKind::Default)); + } + _ => panic!("expected scoped entry"), + } + + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::CallRegistered { transaction, scope, .. }) + if transaction == identifier && scope == Some(vec![1u16]) + )); + }); +} + +#[test] +fn register_call_seeds_multi_scoped_limit() { + new_test_ext().execute_with(|| { + let call = RuntimeCall::RateLimiting(RateLimitingCall::set_rate_limit { + target: RateLimitTarget::Transaction(TransactionIdentifier::new(0, 0)), + scope: None, + limit: RateLimitKind::Exact(42), + }); + let identifier = register(call, None); + let tx_target = target(identifier); + let stored = Limits::::get(tx_target).expect("limit"); + match stored { + RateLimit::Scoped(map) => { + assert_eq!(map.get(&42u16), Some(&RateLimitKind::Default)); + assert_eq!(map.get(&43u16), Some(&RateLimitKind::Default)); + } + _ => panic!("expected scoped entry"), + } + + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::CallRegistered { transaction, scope, .. }) + if transaction == identifier && scope == Some(vec![42u16, 43u16]) + )); + }); +} + +#[test] +fn set_rate_limit_updates_transaction_target() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + let tx_target = target(identifier); + let limit = RateLimitKind::Exact(9); + assert_ok!(RateLimiting::set_rate_limit( + RuntimeOrigin::root(), + tx_target, + None, + limit, + )); + let stored = Limits::::get(tx_target).expect("limit"); + assert!(matches!(stored, RateLimit::Global(RateLimitKind::Exact(9)))); + + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::RateLimitSet { + target: RateLimitTarget::Transaction(t), + limit: RateLimitKind::Exact(9), + .. + }) if t == identifier + )); + }); +} + +#[test] +fn set_rate_limit_requires_registration_and_group_targeting() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + let target = target(identifier); + + // Unregistered call. + let unknown = TransactionIdentifier::new(99, 0); + assert_noop!( + RateLimiting::set_rate_limit( + RuntimeOrigin::root(), + RateLimitTarget::Transaction(unknown), + None, + RateLimitKind::Exact(1), + ), + Error::::CallNotRegistered + ); + + // Group requires targeting the group. + let group = create_group(b"cfg", GroupSharing::ConfigAndUsage); + assert_ok!(RateLimiting::assign_call_to_group( + RuntimeOrigin::root(), + identifier, + group, + false, + )); + assert_noop!( + RateLimiting::set_rate_limit( + RuntimeOrigin::root(), + target, + None, + RateLimitKind::Exact(2), + ), + Error::::MustTargetGroup + ); + }); +} + +#[test] +fn set_rate_limit_respects_group_config_sharing() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + let group = create_group(b"test", GroupSharing::ConfigAndUsage); + // Consume group creation event to keep ordering predictable. + let created = last_event(); + assert!(matches!( + created, + RuntimeEvent::RateLimiting(crate::Event::GroupCreated { group: g, .. }) if g == group + )); + assert_ok!(RateLimiting::assign_call_to_group( + RuntimeOrigin::root(), + identifier, + group, + false, + )); + let events: Vec<_> = System::events() + .into_iter() + .map(|e| e.event) + .filter(|evt| matches!(evt, RuntimeEvent::RateLimiting(_))) + .collect(); + assert!(events.iter().any(|evt| { + matches!( + evt, + RuntimeEvent::RateLimiting(crate::Event::CallReadOnlyUpdated { + transaction, + group: g, + read_only: false, + }) if *transaction == identifier && *g == group + ) + })); + assert!(events.iter().any(|evt| { + matches!( + evt, + RuntimeEvent::RateLimiting(crate::Event::CallGroupUpdated { + transaction, + group: Some(g), + }) if *transaction == identifier && *g == group + ) + })); + assert_noop!( + RateLimiting::set_rate_limit( + RuntimeOrigin::root(), + RateLimitTarget::Transaction(identifier), + None, + RateLimitKind::Exact(5), + ), + Error::::MustTargetGroup + ); + }); +} + +#[test] +fn assign_and_remove_group_membership() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + let group = create_group(b"team", GroupSharing::UsageOnly); + assert_ok!(RateLimiting::assign_call_to_group( + RuntimeOrigin::root(), + identifier, + group, + false, + )); + assert_eq!(CallGroups::::get(identifier), Some(group)); + assert_eq!(CallReadOnly::::get(identifier), Some(false)); + assert!(GroupMembers::::get(group).contains(&identifier)); + assert_ok!(RateLimiting::remove_call_from_group( + RuntimeOrigin::root(), + identifier, + )); + assert!(CallGroups::::get(identifier).is_none()); + + // Last event should signal removal. + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::CallGroupUpdated { transaction, group: None }) + if transaction == identifier + )); + }); +} + +#[test] +fn set_rate_limit_on_group_updates_storage() { + new_test_ext().execute_with(|| { + let group = create_group(b"grp", GroupSharing::ConfigOnly); + let target = RateLimitTarget::Group(group); + assert_ok!(RateLimiting::set_rate_limit( + RuntimeOrigin::root(), + target, + None, + RateLimitKind::Exact(3), + )); + assert!(matches!( + Limits::::get(target), + Some(RateLimit::Global(RateLimitKind::Exact(3))) + )); + + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::RateLimitSet { + target: RateLimitTarget::Group(g), + limit: RateLimitKind::Exact(3), + .. + }) if g == group + )); + }); +} + +#[test] +fn create_and_delete_group_emit_events() { + new_test_ext().execute_with(|| { + assert_ok!(RateLimiting::create_group( + RuntimeOrigin::root(), + b"ev".to_vec(), + GroupSharing::UsageOnly, + )); + let group = RateLimiting::next_group_id().saturating_sub(1); + let created = last_event(); + assert!(matches!( + created, + RuntimeEvent::RateLimiting(crate::Event::GroupCreated { group: g, .. }) if g == group + )); + + assert_ok!(RateLimiting::delete_group(RuntimeOrigin::root(), group)); + let deleted = last_event(); + assert!(matches!( + deleted, + RuntimeEvent::RateLimiting(crate::Event::GroupDeleted { group: g }) if g == group + )); + }); +} + +#[test] +fn deregister_call_scope_removes_entry() { + new_test_ext().execute_with(|| { + let identifier = register(scoped_call(), None); + let tx_target = target(identifier); + assert_ok!(RateLimiting::set_rate_limit( + RuntimeOrigin::root(), + tx_target, + Some(2u16), + RateLimitKind::Exact(4), + )); + LastSeen::::insert(tx_target, Some(9u16), 10); + assert_ok!(RateLimiting::deregister_call( + RuntimeOrigin::root(), + identifier, + Some(2u16), + false, + )); + match Limits::::get(tx_target) { + Some(RateLimit::Scoped(map)) => { + assert!(map.contains_key(&1u16)); + assert!(!map.contains_key(&2u16)); + } + other => panic!("unexpected config: {:?}", other), + } + // usage remains intact when clear_usage is false + assert_eq!(LastSeen::::get(tx_target, Some(9u16)), Some(10)); + + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::CallDeregistered { + target, + transaction: Some(t), + scope: Some(sc), + .. + }) if target == tx_target && t == identifier && sc == 2u16 + )); + + // No group assigned in this test. + assert!(CallGroups::::get(identifier).is_none()); + }); +} + +#[test] +fn register_call_rejects_duplicates_and_unknown_group() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + // Duplicate should fail. + assert_noop!( + RateLimiting::register_call(RuntimeOrigin::root(), Box::new(remark_call()), None), + Error::::CallAlreadyRegistered + ); + + // Unknown group should fail. + assert_noop!( + RateLimiting::register_call(RuntimeOrigin::root(), Box::new(scoped_call()), Some(99)), + Error::::UnknownGroup + ); + + assert!(Limits::::contains_key(target(identifier))); + }); +} + +#[test] +fn group_name_limits_and_uniqueness_enforced() { + new_test_ext().execute_with(|| { + // Overlong name. + let max_name = <::MaxGroupNameLength as Get>::get() as usize; + let long_name = vec![0u8; max_name + 1]; + assert_noop!( + RateLimiting::create_group(RuntimeOrigin::root(), long_name, GroupSharing::UsageOnly), + Error::::GroupNameTooLong + ); + + // Duplicate names rejected on create and update. + let first = create_group(b"alpha", GroupSharing::UsageOnly); + let second = create_group(b"beta", GroupSharing::UsageOnly); + + assert_noop!( + RateLimiting::create_group( + RuntimeOrigin::root(), + b"alpha".to_vec(), + GroupSharing::UsageOnly + ), + Error::::DuplicateGroupName + ); + + assert_noop!( + RateLimiting::update_group( + RuntimeOrigin::root(), + second, + Some(b"alpha".to_vec()), + None + ), + Error::::DuplicateGroupName + ); + + // Unknown group update. + assert_noop!( + RateLimiting::update_group(RuntimeOrigin::root(), 99, None, None), + Error::::UnknownGroup + ); + + assert_eq!( + RateLimiting::groups(first).unwrap().name.into_inner(), + b"alpha".to_vec() + ); + + // Updating first group emits event. + assert_ok!(RateLimiting::update_group( + RuntimeOrigin::root(), + first, + Some(b"gamma".to_vec()), + None, + )); + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::GroupUpdated { group, .. }) if group == first + )); + }); +} + +#[test] +fn group_member_limit_and_removal_errors() { + new_test_ext().execute_with(|| { + let group = create_group(b"cap", GroupSharing::UsageOnly); + + let max_members = <::MaxGroupMembers as Get>::get(); + GroupMembers::::mutate(group, |members| { + for i in 0..max_members { + let _ = members.try_insert(TransactionIdentifier::new(0, (i + 1) as u8)); + } + }); + + // Next insert should fail. + let extra = register(remark_call(), None); + assert_noop!( + RateLimiting::assign_call_to_group(RuntimeOrigin::root(), extra, group, false), + Error::::GroupMemberLimitExceeded + ); + + // Removing a call not in a group errors. + assert_noop!( + RateLimiting::remove_call_from_group(RuntimeOrigin::root(), extra), + Error::::CallNotInGroup + ); + }); +} + +#[test] +fn set_call_read_only_requires_group() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + assert_noop!( + RateLimiting::set_call_read_only(RuntimeOrigin::root(), identifier, true), + Error::::CallNotInGroup + ); + }); +} + +#[test] +fn set_call_read_only_updates_assignment_and_emits_event() { + new_test_ext().execute_with(|| { + let group = create_group(b"ro", GroupSharing::UsageOnly); + let identifier = register(remark_call(), None); + assert_ok!(RateLimiting::assign_call_to_group( + RuntimeOrigin::root(), + identifier, + group, + false, + )); + + assert_ok!(RateLimiting::set_call_read_only( + RuntimeOrigin::root(), + identifier, + true + )); + + assert_eq!(CallGroups::::get(identifier), Some(group)); + assert_eq!(CallReadOnly::::get(identifier), Some(true)); + + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::CallReadOnlyUpdated { + transaction, + group: g, + read_only: true, + }) if transaction == identifier && g == group + )); + }); +} + +#[test] +fn cannot_delete_group_in_use_or_unknown() { + new_test_ext().execute_with(|| { + let group = create_group(b"busy", GroupSharing::ConfigOnly); + let identifier = register(remark_call(), Some(group)); + let target = RateLimitTarget::Group(group); + Limits::::insert(target, RateLimit::global(RateLimitKind::Exact(1))); + LastSeen::::insert(target, None::, 10); + + // Remove member so only config/usage keep the group in-use. + assert_ok!(RateLimiting::remove_call_from_group( + RuntimeOrigin::root(), + identifier + )); + + // Cannot delete when in use. + assert_noop!( + RateLimiting::delete_group(RuntimeOrigin::root(), group), + Error::::GroupInUse + ); + + // Clear state then delete. + Limits::::remove(target); + let _ = LastSeen::::clear_prefix(&target, u32::MAX, None); + assert_ok!(RateLimiting::delete_group(RuntimeOrigin::root(), group)); + + // Unknown group. + assert_noop!( + RateLimiting::delete_group(RuntimeOrigin::root(), 999), + Error::::UnknownGroup + ); + }); +} + +#[test] +fn deregister_call_clears_registration() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + let tx_target = target(identifier); + LastSeen::::insert(tx_target, None::, 5); + assert_ok!(RateLimiting::deregister_call( + RuntimeOrigin::root(), + identifier, + None, + true, + )); + assert!(Limits::::get(tx_target).is_none()); + assert!(LastSeen::::get(tx_target, None::).is_none()); + assert!(CallGroups::::get(identifier).is_none()); + + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::CallDeregistered { + target, + transaction: Some(t), + scope: None, + .. + }) if target == tx_target && t == identifier + )); + }); +} + +#[test] +fn deregister_errors_for_unknown_or_missing_scope() { + new_test_ext().execute_with(|| { + let unknown = TransactionIdentifier::new(10, 1); + assert_noop!( + RateLimiting::deregister_call(RuntimeOrigin::root(), unknown, None, true), + Error::::CallNotRegistered + ); + + let identifier = register(scoped_call(), None); + let tx_target = target(identifier); + // Removing a non-existent scoped entry fails. + assert_noop!( + RateLimiting::deregister_call(RuntimeOrigin::root(), identifier, Some(99u16), false), + Error::::MissingRateLimit + ); + + // Removing the last scoped entry clears Limits and LastSeen. + LastSeen::::insert(tx_target, Some(1u16), 5); + assert_ok!(RateLimiting::deregister_call( + RuntimeOrigin::root(), + identifier, + Some(1u16), + true, + )); + assert!(Limits::::get(tx_target).is_none()); + assert!(LastSeen::::get(tx_target, Some(1u16)).is_none()); + }); +} + +#[test] +fn is_within_limit_detects_rate_limited_scope() { + new_test_ext().execute_with(|| { + let call = scoped_call(); + let identifier = identifier_for(&call); + let tx_target = target(identifier); + Limits::::insert( + tx_target, + RateLimit::scoped_single(1u16, RateLimitKind::Exact(3)), + ); + LastSeen::::insert(tx_target, Some(1u16), 9); + System::set_block_number(11); + let result = RateLimiting::is_within_limit( + &RuntimeOrigin::signed(1), + &call, + &identifier, + &Some(vec![1u16]), + &Some(1u16), + ) + .expect("ok"); + assert!(!result); + }); +} + +#[test] +fn migrate_usage_key_tracks_scope() { + new_test_ext().execute_with(|| { + let call = scoped_call(); + let identifier = identifier_for(&call); + let tx_target = target(identifier); + LastSeen::::insert(tx_target, Some(6u16), 10); + assert!(RateLimiting::migrate_usage_key( + tx_target, + Some(6u16), + Some(7u16) + )); + assert_eq!(LastSeen::::get(tx_target, Some(7u16)), Some(10)); + }); +} + +#[test] +fn migrate_limit_scope_covers_transitions() { + new_test_ext().execute_with(|| { + let identifier = register(remark_call(), None); + let tx_target = target(identifier); + + // global -> scoped + assert!(RateLimiting::migrate_limit_scope( + tx_target, + None, + Some(42u16) + )); + match Limits::::get(tx_target) { + Some(RateLimit::Scoped(map)) => { + assert_eq!(map.get(&42u16), Some(&RateLimitKind::Default)) + } + other => panic!("unexpected config: {:?}", other), + } + + // scoped -> scoped + assert!(RateLimiting::migrate_limit_scope( + tx_target, + Some(42u16), + Some(43u16) + )); + match Limits::::get(tx_target) { + Some(RateLimit::Scoped(map)) => { + assert_eq!(map.get(&43u16), Some(&RateLimitKind::Default)) + } + other => panic!("unexpected config: {:?}", other), + } + + // scoped -> global (only entry) + assert!(RateLimiting::migrate_limit_scope( + tx_target, + Some(43u16), + None + )); + assert!(matches!( + Limits::::get(tx_target), + Some(RateLimit::Global(RateLimitKind::Default)) + )); + + // no-op when scopes identical + assert!(RateLimiting::migrate_limit_scope(tx_target, None, None)); + }); +} + +#[test] +fn set_default_limit_updates_span_and_resolves_in_enforcement() { + new_test_ext().execute_with(|| { + assert_eq!(RateLimiting::default_limit(), 0); + assert_ok!(RateLimiting::set_default_rate_limit( + RuntimeOrigin::root(), + 5 + )); + let event = last_event(); + assert!(matches!( + event, + RuntimeEvent::RateLimiting(crate::Event::DefaultRateLimitSet { block_span: 5 }) + )); + assert_eq!(RateLimiting::default_limit(), 5); + + let call = remark_call(); + let identifier = register(call.clone(), None); + let tx_target = target(identifier); + + System::set_block_number(10); + // No last-seen yet, first call passes. + assert!( + RateLimiting::is_within_limit( + &RuntimeOrigin::signed(1), + &call, + &identifier, + &None, + &None, + ) + .unwrap() + ); + + LastSeen::::insert(tx_target, None::, 12); + System::set_block_number(15); + // Span 5 should block when delta < 5. + assert!( + !RateLimiting::is_within_limit( + &RuntimeOrigin::signed(1), + &call, + &identifier, + &None, + &None, + ) + .unwrap() + ); + }); +} + +#[test] +fn limit_for_call_names_prefers_scoped_value() { + new_test_ext().execute_with(|| { + let call = scoped_call(); + let identifier = identifier_for(&call); + Limits::::insert( + target(identifier), + RateLimit::scoped_single(9u16, RateLimitKind::Exact(8)), + ); + let fetched = + RateLimiting::limit_for_call_names("RateLimiting", "set_rate_limit", Some(9u16)) + .expect("limit"); + assert_eq!(fetched, RateLimitKind::Exact(8)); + }); +} diff --git a/pallets/rate-limiting/src/tx_extension.rs b/pallets/rate-limiting/src/tx_extension.rs new file mode 100644 index 0000000000..5136659a83 --- /dev/null +++ b/pallets/rate-limiting/src/tx_extension.rs @@ -0,0 +1,728 @@ +use codec::{Decode, DecodeWithMemTracking, Encode}; +use frame_support::{ + dispatch::{DispatchInfo, DispatchResult, PostDispatchInfo}, + pallet_prelude::Weight, + sp_runtime::{ + traits::{ + DispatchInfoOf, DispatchOriginOf, Dispatchable, Implication, TransactionExtension, + ValidateResult, Zero, + }, + transaction_validity::{ + InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction, + }, + }, +}; +use scale_info::TypeInfo; +use sp_std::{marker::PhantomData, result::Result, vec, vec::Vec}; + +use crate::{ + Config, LastSeen, Pallet, + types::{ + RateLimitScopeResolver, RateLimitTarget, RateLimitUsageResolver, TransactionIdentifier, + }, +}; + +/// Identifier returned in the transaction metadata for the rate limiting extension. +const IDENTIFIER: &str = "RateLimitTransactionExtension"; + +/// Custom error code used to signal a rate limit violation. +const RATE_LIMIT_DENIED: u8 = 1; + +/// Transaction extension that enforces pallet rate limiting rules. +#[derive(Default, Encode, Decode, DecodeWithMemTracking, TypeInfo)] +pub struct RateLimitTransactionExtension(PhantomData<(T, I)>) +where + T: Config + Send + Sync + TypeInfo, + I: 'static + TypeInfo; + +impl RateLimitTransactionExtension +where + T: Config + Send + Sync + TypeInfo, + I: 'static + TypeInfo, +{ + pub fn new() -> Self { + Self(PhantomData) + } +} + +impl Clone for RateLimitTransactionExtension +where + T: Config + Send + Sync + TypeInfo, + I: 'static + TypeInfo, +{ + fn clone(&self) -> Self { + Self(PhantomData) + } +} + +impl PartialEq for RateLimitTransactionExtension +where + T: Config + Send + Sync + TypeInfo, + I: 'static + TypeInfo, +{ + fn eq(&self, _other: &Self) -> bool { + true + } +} + +impl Eq for RateLimitTransactionExtension +where + T: Config + Send + Sync + TypeInfo, + I: 'static + TypeInfo, +{ +} + +impl core::fmt::Debug for RateLimitTransactionExtension +where + T: Config + Send + Sync + TypeInfo, + I: 'static + TypeInfo, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.write_str(IDENTIFIER) + } +} + +impl TransactionExtension<>::RuntimeCall> + for RateLimitTransactionExtension +where + T: Config + Send + Sync + TypeInfo, + I: 'static + TypeInfo + Send + Sync, + >::RuntimeCall: Dispatchable, +{ + const IDENTIFIER: &'static str = IDENTIFIER; + + type Implicit = (); + type Val = Option<( + RateLimitTarget<>::GroupId>, + Option>::UsageKey>>, + bool, + )>; + type Pre = Option<( + RateLimitTarget<>::GroupId>, + Option>::UsageKey>>, + bool, + )>; + + fn weight(&self, _call: &>::RuntimeCall) -> Weight { + Weight::zero() + } + + fn validate( + &self, + origin: DispatchOriginOf<>::RuntimeCall>, + call: &>::RuntimeCall, + _info: &DispatchInfoOf<>::RuntimeCall>, + _len: usize, + _self_implicit: Self::Implicit, + _inherited_implication: &impl Implication, + _source: TransactionSource, + ) -> ValidateResult>::RuntimeCall> { + let Some(identifier) = TransactionIdentifier::from_call(call) else { + return Err(TransactionValidityError::Invalid(InvalidTransaction::Call)); + }; + + if !Pallet::::is_registered(&identifier) { + return Ok((ValidTransaction::default(), None, origin)); + } + + let scopes = >::LimitScopeResolver::context(&origin, call); + let usage = >::UsageResolver::context(&origin, call); + + let config_target = Pallet::::config_target(&identifier) + .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let usage_target = Pallet::::usage_target(&identifier) + .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?; + let bypass = >::LimitScopeResolver::should_bypass(&origin, call); + let should_record = + bypass.record_usage && Pallet::::should_record_usage(&identifier, &usage_target); + + if bypass.bypass_enforcement { + return Ok(( + ValidTransaction::default(), + should_record.then_some((usage_target, usage, true)), + origin, + )); + } + + let usage_keys: Vec>::UsageKey>> = match usage.clone() { + None => vec![None], + Some(keys) => keys.into_iter().map(Some).collect(), + }; + + let scope_list: Vec>::LimitScope>> = match scopes { + None => vec![None], + Some(resolved) if resolved.is_empty() => vec![None], + Some(resolved) => resolved.into_iter().map(Some).collect(), + }; + + let mut enforced = false; + for scope in scope_list { + let Some(block_span) = + Pallet::::effective_span(&origin, call, &config_target, &scope) + else { + continue; + }; + if block_span.is_zero() { + continue; + } + enforced = true; + let within_limit = usage_keys + .iter() + .all(|key| Pallet::::within_span(&usage_target, key, block_span)); + if !within_limit { + return Err(TransactionValidityError::Invalid( + InvalidTransaction::Custom(RATE_LIMIT_DENIED), + )); + } + } + + if !enforced { + return Ok((ValidTransaction::default(), None, origin)); + } + + Ok(( + ValidTransaction::default(), + Some((usage_target, usage, should_record)), + origin, + )) + } + + fn prepare( + self, + val: Self::Val, + _origin: &DispatchOriginOf<>::RuntimeCall>, + _call: &>::RuntimeCall, + _info: &DispatchInfoOf<>::RuntimeCall>, + _len: usize, + ) -> Result { + Ok(val) + } + + fn post_dispatch( + pre: Self::Pre, + _info: &DispatchInfoOf<>::RuntimeCall>, + _post_info: &mut PostDispatchInfo, + _len: usize, + result: &DispatchResult, + ) -> Result<(), TransactionValidityError> { + if result.is_ok() { + if let Some((target, usage, should_record)) = pre { + if !should_record { + return Ok(()); + } + let block_number = frame_system::Pallet::::block_number(); + match usage { + None => LastSeen::::insert( + target, + None::<>::UsageKey>, + block_number, + ), + Some(keys) => { + for key in keys { + LastSeen::::insert(target, Some(key), block_number); + } + } + } + } + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use codec::Encode; + use frame_support::{ + assert_ok, + dispatch::{GetDispatchInfo, PostDispatchInfo}, + }; + use sp_runtime::{ + traits::{TransactionExtension, TxBaseImplication}, + transaction_validity::{InvalidTransaction, TransactionSource, TransactionValidityError}, + }; + + use crate::{ + GroupSharing, LastSeen, Limits, + types::{RateLimit, RateLimitKind}, + }; + + use super::*; + use crate::mock::*; + use sp_std::collections::btree_map::BTreeMap; + + fn remark_call() -> RuntimeCall { + RuntimeCall::System(frame_system::Call::::remark { remark: Vec::new() }) + } + + fn bypass_call() -> RuntimeCall { + RuntimeCall::RateLimiting(RateLimitingCall::remove_call_from_group { + transaction: TransactionIdentifier::new(0, 0), + }) + } + + fn adjustable_call() -> RuntimeCall { + RuntimeCall::RateLimiting(RateLimitingCall::deregister_call { + transaction: TransactionIdentifier::new(0, 0), + scope: None, + clear_usage: false, + }) + } + + fn multi_scope_call(block_span: u64) -> RuntimeCall { + RuntimeCall::RateLimiting(RateLimitingCall::set_rate_limit { + target: RateLimitTarget::Transaction(TransactionIdentifier::new(0, 0)), + scope: None, + limit: RateLimitKind::Exact(block_span), + }) + } + + fn new_tx_extension() -> RateLimitTransactionExtension { + RateLimitTransactionExtension(Default::default()) + } + + fn target_for_call(call: &RuntimeCall) -> RateLimitTarget { + RateLimitTarget::Transaction(identifier_for(call)) + } + + fn validate_with_tx_extension( + extension: &RateLimitTransactionExtension, + call: &RuntimeCall, + ) -> Result< + ( + sp_runtime::transaction_validity::ValidTransaction, + Option<(RateLimitTarget, Option>, bool)>, + RuntimeOrigin, + ), + TransactionValidityError, + > { + let info = call.get_dispatch_info(); + let len = call.encode().len(); + extension.validate( + RuntimeOrigin::signed(42), + call, + &info, + len, + (), + &TxBaseImplication(()), + TransactionSource::External, + ) + } + + #[test] + fn tx_extension_allows_calls_without_limit() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = remark_call(); + + let (_valid, val, _origin) = + validate_with_tx_extension(&extension, &call).expect("valid"); + assert!(val.is_none()); + + let info = call.get_dispatch_info(); + let len = call.encode().len(); + let origin_for_prepare = RuntimeOrigin::signed(42); + let pre = extension + .clone() + .prepare(val.clone(), &origin_for_prepare, &call, &info, len) + .expect("prepare succeeds"); + + let mut post = PostDispatchInfo::default(); + RateLimitTransactionExtension::::post_dispatch( + pre, + &info, + &mut post, + len, + &Ok(()), + ) + .expect("post_dispatch succeeds"); + + let target = target_for_call(&call); + assert_eq!(LastSeen::::get(target, None::), None); + }); + } + + #[test] + fn tx_extension_honors_bypass_signal() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = bypass_call(); + + let (valid, val, _) = + validate_with_tx_extension(&extension, &call).expect("bypass should succeed"); + assert_eq!(valid.priority, 0); + assert!(val.is_none()); + + let identifier = identifier_for(&call); + let target = RateLimitTarget::Transaction(identifier); + Limits::::insert(target, RateLimit::global(RateLimitKind::Exact(3))); + LastSeen::::insert(target, None::, 1); + + let (_valid, post_val, _) = + validate_with_tx_extension(&extension, &call).expect("still bypassed"); + assert!(post_val.is_none()); + }); + } + + #[test] + fn tx_extension_applies_adjusted_span() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = adjustable_call(); + let identifier = identifier_for(&call); + let target = RateLimitTarget::Transaction(identifier); + Limits::::insert(target, RateLimit::global(RateLimitKind::Exact(4))); + LastSeen::::insert(target, Some(1u16), 10); + + System::set_block_number(14); + + // Stored span (4) would allow the call, but adjusted span (8) should block it. + let err = validate_with_tx_extension(&extension, &call) + .expect_err("adjusted span should apply"); + match err { + TransactionValidityError::Invalid(InvalidTransaction::Custom(code)) => { + assert_eq!(code, RATE_LIMIT_DENIED); + } + other => panic!("unexpected error: {:?}", other), + } + }); + } + + #[test] + fn tx_extension_rejects_when_any_scope_fails() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = multi_scope_call(43); + let identifier = identifier_for(&call); + let target = RateLimitTarget::Transaction(identifier); + + assert_ok!(RateLimiting::register_call( + RuntimeOrigin::root(), + Box::new(call.clone()), + None, + )); + + let mut scopes = BTreeMap::new(); + scopes.insert(43u16, RateLimitKind::Exact(5)); + scopes.insert(44u16, RateLimitKind::Exact(3)); + Limits::::insert(target, RateLimit::Scoped(scopes)); + LastSeen::::insert(target, Some(43u16), 10); + + System::set_block_number(14); + + let err = + validate_with_tx_extension(&extension, &call).expect_err("one scope should block"); + match err { + TransactionValidityError::Invalid(InvalidTransaction::Custom(code)) => { + assert_eq!(code, RATE_LIMIT_DENIED); + } + other => panic!("unexpected error: {:?}", other), + } + }); + } + + #[test] + fn tx_extension_rejects_when_any_usage_key_fails() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = multi_scope_call(42); + let identifier = identifier_for(&call); + let target = RateLimitTarget::Transaction(identifier); + + assert_ok!(RateLimiting::register_call( + RuntimeOrigin::root(), + Box::new(call.clone()), + None, + )); + + let mut scopes = BTreeMap::new(); + scopes.insert(42u16, RateLimitKind::Exact(5)); + scopes.insert(43u16, RateLimitKind::Exact(5)); + Limits::::insert(target, RateLimit::Scoped(scopes)); + LastSeen::::insert(target, Some(42u16), 8); + LastSeen::::insert(target, Some(43u16), 12); + + System::set_block_number(14); + + let err = validate_with_tx_extension(&extension, &call) + .expect_err("one usage key should block"); + match err { + TransactionValidityError::Invalid(InvalidTransaction::Custom(code)) => { + assert_eq!(code, RATE_LIMIT_DENIED); + } + other => panic!("unexpected error: {:?}", other), + } + }); + } + + #[test] + fn tx_extension_records_usage_on_bypass() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = RuntimeCall::RateLimiting(RateLimitingCall::set_default_rate_limit { + block_span: 2, + }); + let identifier = identifier_for(&call); + let target = RateLimitTarget::Transaction(identifier); + + assert_ok!(RateLimiting::register_call( + RuntimeOrigin::root(), + Box::new(call.clone()), + None, + )); + + System::set_block_number(5); + + let (_valid, val, origin) = + validate_with_tx_extension(&extension, &call).expect("bypass should succeed"); + assert!(val.is_some(), "bypass decision should still record usage"); + + let info = call.get_dispatch_info(); + let len = call.encode().len(); + let pre = extension + .clone() + .prepare(val.clone(), &origin, &call, &info, len) + .expect("prepare succeeds"); + + let mut post = PostDispatchInfo::default(); + RateLimitTransactionExtension::::post_dispatch( + pre, + &info, + &mut post, + len, + &Ok(()), + ) + .expect("post_dispatch succeeds"); + + assert_eq!( + LastSeen::::get(target, Some(2u16)), + Some(5u64.into()) + ); + }); + } + + #[test] + fn tx_extension_records_last_seen_for_successful_call() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = remark_call(); + let identifier = identifier_for(&call); + let target = RateLimitTarget::Transaction(identifier); + Limits::::insert(target, RateLimit::global(RateLimitKind::Exact(5))); + + System::set_block_number(10); + + let (_valid, val, _) = validate_with_tx_extension(&extension, &call).expect("valid"); + assert!(val.is_some()); + + let info = call.get_dispatch_info(); + let len = call.encode().len(); + let origin_for_prepare = RuntimeOrigin::signed(42); + let pre = extension + .clone() + .prepare(val.clone(), &origin_for_prepare, &call, &info, len) + .expect("prepare succeeds"); + + let mut post = PostDispatchInfo::default(); + RateLimitTransactionExtension::::post_dispatch( + pre, + &info, + &mut post, + len, + &Ok(()), + ) + .expect("post_dispatch succeeds"); + + assert_eq!( + LastSeen::::get(target, None::), + Some(10) + ); + }); + } + + #[test] + fn tx_extension_rejects_when_call_occurs_too_soon() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = remark_call(); + let identifier = identifier_for(&call); + let target = RateLimitTarget::Transaction(identifier); + Limits::::insert(target, RateLimit::global(RateLimitKind::Exact(5))); + LastSeen::::insert(target, None::, 20); + + System::set_block_number(22); + + let err = + validate_with_tx_extension(&extension, &call).expect_err("should be rate limited"); + match err { + TransactionValidityError::Invalid(InvalidTransaction::Custom(code)) => { + assert_eq!(code, 1); + } + other => panic!("unexpected error: {:?}", other), + } + }); + } + + #[test] + fn tx_extension_skips_last_seen_when_span_zero() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + let call = remark_call(); + let identifier = identifier_for(&call); + let target = RateLimitTarget::Transaction(identifier); + Limits::::insert(target, RateLimit::global(RateLimitKind::Exact(0))); + + System::set_block_number(30); + + let (_valid, val, _) = validate_with_tx_extension(&extension, &call).expect("valid"); + assert!(val.is_none()); + + let info = call.get_dispatch_info(); + let len = call.encode().len(); + let origin_for_prepare = RuntimeOrigin::signed(42); + let pre = extension + .clone() + .prepare(val.clone(), &origin_for_prepare, &call, &info, len) + .expect("prepare succeeds"); + + let mut post = PostDispatchInfo::default(); + RateLimitTransactionExtension::::post_dispatch( + pre, + &info, + &mut post, + len, + &Ok(()), + ) + .expect("post_dispatch succeeds"); + + assert_eq!(LastSeen::::get(target, None::), None); + }); + } + + #[test] + fn tx_extension_skips_write_for_read_only_group_member() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + assert_ok!(RateLimiting::create_group( + RuntimeOrigin::root(), + b"use-ro".to_vec(), + GroupSharing::UsageOnly, + )); + let group = RateLimiting::next_group_id().saturating_sub(1); + + let call = remark_call(); + let identifier = identifier_for(&call); + assert_ok!(RateLimiting::register_call( + RuntimeOrigin::root(), + Box::new(call.clone()), + Some(group), + )); + assert_ok!(RateLimiting::set_call_read_only( + RuntimeOrigin::root(), + identifier, + true + )); + + let tx_target = RateLimitTarget::Transaction(identifier); + let usage_target = RateLimitTarget::Group(group); + Limits::::insert(tx_target, RateLimit::global(RateLimitKind::Exact(2))); + LastSeen::::insert(usage_target, Some(1u16), 2); + + System::set_block_number(5); + + let (_valid, val, _) = validate_with_tx_extension(&extension, &call).expect("valid"); + let info = call.get_dispatch_info(); + let len = call.encode().len(); + let origin_for_prepare = RuntimeOrigin::signed(42); + let pre = extension + .clone() + .prepare(val.clone(), &origin_for_prepare, &call, &info, len) + .expect("prepare succeeds"); + + let mut post = PostDispatchInfo::default(); + RateLimitTransactionExtension::::post_dispatch( + pre, + &info, + &mut post, + len, + &Ok(()), + ) + .expect("post_dispatch succeeds"); + + // Usage key should remain untouched because the call is read-only. + assert_eq!(LastSeen::::get(usage_target, Some(1u16)), Some(2)); + }); + } + + #[test] + fn tx_extension_respects_usage_group_sharing() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + assert_ok!(RateLimiting::create_group( + RuntimeOrigin::root(), + b"use".to_vec(), + GroupSharing::UsageOnly, + )); + let group = RateLimiting::next_group_id().saturating_sub(1); + + let call = remark_call(); + let identifier = identifier_for(&call); + assert_ok!(RateLimiting::register_call( + RuntimeOrigin::root(), + Box::new(call.clone()), + Some(group), + )); + + let tx_target = RateLimitTarget::Transaction(identifier); + let usage_target = RateLimitTarget::Group(group); + Limits::::insert(tx_target, RateLimit::global(RateLimitKind::Exact(5))); + LastSeen::::insert(usage_target, None::, 10); + System::set_block_number(12); + + let err = validate_with_tx_extension(&extension, &call) + .expect_err("usage grouping should rate limit"); + match err { + TransactionValidityError::Invalid(InvalidTransaction::Custom(code)) => { + assert_eq!(code, RATE_LIMIT_DENIED); + } + other => panic!("unexpected error: {:?}", other), + } + }); + } + + #[test] + fn tx_extension_respects_config_group_sharing() { + new_test_ext().execute_with(|| { + let extension = new_tx_extension(); + assert_ok!(RateLimiting::create_group( + RuntimeOrigin::root(), + b"cfg".to_vec(), + GroupSharing::ConfigOnly, + )); + let group = RateLimiting::next_group_id().saturating_sub(1); + + let call = remark_call(); + let identifier = identifier_for(&call); + assert_ok!(RateLimiting::register_call( + RuntimeOrigin::root(), + Box::new(call.clone()), + Some(group), + )); + + let tx_target = RateLimitTarget::Transaction(identifier); + let group_target = RateLimitTarget::Group(group); + Limits::::remove(tx_target); + Limits::::insert(group_target, RateLimit::global(RateLimitKind::Exact(5))); + LastSeen::::insert(tx_target, None::, 10); + System::set_block_number(12); + + let err = validate_with_tx_extension(&extension, &call) + .expect_err("config grouping should rate limit"); + match err { + TransactionValidityError::Invalid(InvalidTransaction::Custom(code)) => { + assert_eq!(code, RATE_LIMIT_DENIED); + } + other => panic!("unexpected error: {:?}", other), + } + }); + } +} diff --git a/pallets/rate-limiting/src/types.rs b/pallets/rate-limiting/src/types.rs new file mode 100644 index 0000000000..f128433473 --- /dev/null +++ b/pallets/rate-limiting/src/types.rs @@ -0,0 +1,236 @@ +use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen}; +use frame_support::dispatch::DispatchResult; +pub use rate_limiting_interface::{RateLimitTarget, TransactionIdentifier}; +use scale_info::TypeInfo; +use serde::{Deserialize, Serialize}; +use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; + +/// Resolves the optional identifier within which a rate limit applies and can optionally adjust +/// enforcement behaviour. +pub trait RateLimitScopeResolver { + /// Returns `Some(scopes)` when the limit should be applied per-scope, or `None` for global + /// limits. + fn context(origin: &Origin, call: &Call) -> Option>; + + /// Returns how the call should interact with enforcement and usage tracking. + fn should_bypass(_origin: &Origin, _call: &Call) -> BypassDecision { + BypassDecision::enforce_and_record() + } + + /// Optionally adjusts the effective span used during enforcement. Defaults to the original + /// `span`. + fn adjust_span(_origin: &Origin, _call: &Call, span: Span) -> Span { + span + } +} + +/// Controls whether enforcement should run and whether usage should be recorded for a call. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct BypassDecision { + pub bypass_enforcement: bool, + pub record_usage: bool, +} + +impl BypassDecision { + pub const fn new(bypass_enforcement: bool, record_usage: bool) -> Self { + Self { + bypass_enforcement, + record_usage, + } + } + + pub const fn enforce_and_record() -> Self { + Self::new(false, true) + } + + pub const fn bypass_and_record() -> Self { + Self::new(true, true) + } + + pub const fn bypass_and_skip() -> Self { + Self::new(true, false) + } +} + +/// Resolves the optional usage tracking key applied when enforcing limits. +pub trait RateLimitUsageResolver { + /// Returns `Some(keys)` to track usage per key, or `None` for global usage tracking. + /// + /// When multiple keys are returned, the rate limit is enforced against each key and all are + /// recorded on success. + fn context(origin: &Origin, call: &Call) -> Option>; +} + +/// Origin check performed when configuring a rate limit. +/// +/// `pallet-rate-limiting` supports configuring a distinct "who may set limits" rule per call/group +/// target. This trait is invoked by [`pallet::Pallet::set_rate_limit`] after loading the rule from +/// storage, allowing runtimes to implement arbitrary permissioning logic. +/// +/// Note: the hook receives the provided `scope` (if any). Some policies (for example "subnet owner") +/// require a scope value (such as `netuid`) in order to validate the caller. +pub trait EnsureLimitSettingRule { + fn ensure_origin(origin: Origin, rule: &Rule, scope: &Option) -> DispatchResult; +} + +/// Sharing mode configured for a group. +#[derive( + Serialize, + Deserialize, + Clone, + Copy, + PartialEq, + Eq, + Encode, + Decode, + DecodeWithMemTracking, + TypeInfo, + MaxEncodedLen, + Debug, +)] +pub enum GroupSharing { + /// Limits remain per transaction; usage is shared by the group. + UsageOnly, + /// Limits are shared by the group; usage remains per transaction. + ConfigOnly, + /// Both limits and usage are shared by the group. + ConfigAndUsage, +} + +impl GroupSharing { + /// Returns `true` when configuration for this group should use the group target key. + pub fn config_uses_group(self) -> bool { + matches!( + self, + GroupSharing::ConfigOnly | GroupSharing::ConfigAndUsage + ) + } + + /// Returns `true` when usage tracking for this group should use the group target key. + pub fn usage_uses_group(self) -> bool { + matches!(self, GroupSharing::UsageOnly | GroupSharing::ConfigAndUsage) + } +} + +/// Metadata describing a configured group. +#[derive( + Serialize, + Deserialize, + Clone, + PartialEq, + Eq, + Encode, + Decode, + DecodeWithMemTracking, + TypeInfo, + MaxEncodedLen, + Debug, +)] +pub struct RateLimitGroup { + /// Stable identifier assigned to the group. + pub id: GroupId, + /// Human readable group name. + pub name: Name, + /// Sharing configuration enforced for the group. + pub sharing: GroupSharing, +} + +/// Policy describing the block span enforced by a rate limit. +#[derive( + Serialize, + Deserialize, + Clone, + Copy, + PartialEq, + Eq, + Encode, + Decode, + DecodeWithMemTracking, + TypeInfo, + MaxEncodedLen, + Debug, +)] +pub enum RateLimitKind { + /// Use the pallet-level default rate limit. + Default, + /// Apply an exact rate limit measured in blocks. + Exact(BlockNumber), +} + +/// Stored rate limit configuration for a transaction identifier. +/// +/// The configuration is mutually exclusive: either the call is globally limited or it stores a set +/// of per-scope spans. +#[derive( + Serialize, + Deserialize, + Clone, + PartialEq, + Eq, + Encode, + Decode, + DecodeWithMemTracking, + TypeInfo, + Debug, +)] +#[serde( + bound = "Scope: Ord + serde::Serialize + serde::de::DeserializeOwned, BlockNumber: serde::Serialize + serde::de::DeserializeOwned" +)] +pub enum RateLimit { + /// Global span applied to every invocation. + Global(RateLimitKind), + /// Per-scope spans keyed by `Scope`. + Scoped(BTreeMap>), +} + +impl RateLimit +where + Scope: Ord, +{ + /// Convenience helper to build a global configuration. + pub fn global(kind: RateLimitKind) -> Self { + Self::Global(kind) + } + + /// Convenience helper to build a scoped configuration containing a single entry. + pub fn scoped_single(scope: Scope, kind: RateLimitKind) -> Self { + let mut map = BTreeMap::new(); + map.insert(scope, kind); + Self::Scoped(map) + } + + /// Returns the span configured for the provided scope, if any. + pub fn kind_for(&self, scope: Option<&Scope>) -> Option<&RateLimitKind> { + match self { + RateLimit::Global(kind) => Some(kind), + RateLimit::Scoped(map) => scope.and_then(|key| map.get(key)), + } + } + + /// Inserts or updates a scoped entry, converting from a global configuration if needed. + pub fn upsert_scope(&mut self, scope: Scope, kind: RateLimitKind) { + match self { + RateLimit::Global(_) => { + let mut map = BTreeMap::new(); + map.insert(scope, kind); + *self = RateLimit::Scoped(map); + } + RateLimit::Scoped(map) => { + map.insert(scope, kind); + } + } + } + + /// Removes a scoped entry, returning whether one existed. + pub fn remove_scope(&mut self, scope: &Scope) -> bool { + match self { + RateLimit::Global(_) => false, + RateLimit::Scoped(map) => map.remove(scope).is_some(), + } + } + + /// Returns true when the scoped configuration contains no entries. + pub fn is_scoped_empty(&self) -> bool { + matches!(self, RateLimit::Scoped(map) if map.is_empty()) + } +} diff --git a/pallets/subtensor/Cargo.toml b/pallets/subtensor/Cargo.toml index 2e35a89d19..cd9410de8b 100644 --- a/pallets/subtensor/Cargo.toml +++ b/pallets/subtensor/Cargo.toml @@ -55,6 +55,8 @@ sha2.workspace = true rand_chacha.workspace = true pallet-crowdloan.workspace = true pallet-subtensor-proxy.workspace = true +pallet-rate-limiting.workspace = true +rate-limiting-interface.workspace = true [dev-dependencies] pallet-balances = { workspace = true, features = ["std"] } @@ -114,6 +116,8 @@ std = [ "pallet-crowdloan/std", "pallet-drand/std", "pallet-subtensor-proxy/std", + "pallet-rate-limiting/std", + "rate-limiting-interface/std", "pallet-subtensor-swap/std", "subtensor-swap-interface/std", "pallet-subtensor-utility/std", diff --git a/pallets/subtensor/src/benchmarks.rs b/pallets/subtensor/src/benchmarks.rs index f61c35aede..7eaabff73f 100644 --- a/pallets/subtensor/src/benchmarks.rs +++ b/pallets/subtensor/src/benchmarks.rs @@ -157,7 +157,6 @@ mod pallet_benchmarks { netuid, caller.clone() )); - Subtensor::::set_serving_rate_limit(netuid, 0); #[extrinsic_call] _( @@ -195,7 +194,6 @@ mod pallet_benchmarks { netuid, caller.clone() )); - Subtensor::::set_serving_rate_limit(netuid, 0); #[extrinsic_call] _( @@ -259,7 +257,6 @@ mod pallet_benchmarks { let coldkey: T::AccountId = account("Test", 0, seed); let hotkey: T::AccountId = account("TestHotkey", 0, seed); - Subtensor::::set_network_rate_limit(1); let amount: u64 = 100_000_000_000_000u64.saturating_mul(2); Subtensor::::add_balance_to_coldkey_account(&coldkey, amount); @@ -479,7 +476,6 @@ mod pallet_benchmarks { Subtensor::::set_network_registration_allowed(netuid, true); Subtensor::::set_network_pow_registration_allowed(netuid, true); Subtensor::::set_commit_reveal_weights_enabled(netuid, true); - Subtensor::::set_weights_set_rate_limit(netuid, 0); let block_number: u64 = Subtensor::::get_current_block_as_u64(); let (nonce, work) = @@ -740,9 +736,6 @@ mod pallet_benchmarks { Subtensor::::create_account_if_non_existent(&coldkey, &destination); - // Remove stake limit for benchmark - StakingOperationRateLimiter::::remove((origin.clone(), coldkey.clone(), netuid)); - #[extrinsic_call] _( RawOrigin::Signed(coldkey.clone()), @@ -801,9 +794,6 @@ mod pallet_benchmarks { let amount_unstaked = AlphaCurrency::from(30_000_000_000); - // Remove stake limit for benchmark - StakingOperationRateLimiter::::remove((hotkey.clone(), coldkey.clone(), netuid)); - #[extrinsic_call] _( RawOrigin::Signed(coldkey.clone()), @@ -864,9 +854,6 @@ mod pallet_benchmarks { allow )); - // Remove stake limit for benchmark - StakingOperationRateLimiter::::remove((hot.clone(), coldkey.clone(), netuid1)); - #[extrinsic_call] _( RawOrigin::Signed(coldkey.clone()), @@ -918,9 +905,6 @@ mod pallet_benchmarks { Subtensor::::create_account_if_non_existent(&dest, &hot); - // Remove stake limit for benchmark - StakingOperationRateLimiter::::remove((hot.clone(), coldkey.clone(), netuid)); - #[extrinsic_call] _( RawOrigin::Signed(coldkey.clone()), @@ -973,9 +957,6 @@ mod pallet_benchmarks { let alpha_to_swap = Subtensor::::get_stake_for_hotkey_and_coldkey_on_subnet(&hot, &coldkey, netuid1); - // Remove stake limit for benchmark - StakingOperationRateLimiter::::remove((hot.clone(), coldkey.clone(), netuid1)); - #[extrinsic_call] _( RawOrigin::Signed(coldkey.clone()), @@ -1089,7 +1070,6 @@ mod pallet_benchmarks { let identity: Option = None; Subtensor::::set_network_registration_allowed(1.into(), true); - Subtensor::::set_network_rate_limit(1); let amount: u64 = 9_999_999_999_999; Subtensor::::add_balance_to_coldkey_account(&coldkey, amount); @@ -1286,9 +1266,6 @@ mod pallet_benchmarks { staked_amt.into() )); - // Remove stake limit for benchmark - StakingOperationRateLimiter::::remove((hotkey.clone(), coldkey.clone(), netuid)); - #[extrinsic_call] _(RawOrigin::Signed(coldkey), hotkey); } @@ -1338,8 +1315,6 @@ mod pallet_benchmarks { u64_staked_amt.into() )); - StakingOperationRateLimiter::::remove((hotkey.clone(), coldkey.clone(), netuid)); - #[extrinsic_call] _( RawOrigin::Signed(coldkey.clone()), diff --git a/pallets/subtensor/src/coinbase/root.rs b/pallets/subtensor/src/coinbase/root.rs index 83567b6f57..a891ace15c 100644 --- a/pallets/subtensor/src/coinbase/root.rs +++ b/pallets/subtensor/src/coinbase/root.rs @@ -17,9 +17,13 @@ use super::*; use crate::CommitmentsInterface; +use rate_limiting_interface::RateLimitingInterface; use safe_math::*; +use sp_runtime::SaturatedConversion; use substrate_fixed::types::{I64F64, U96F32}; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, NetUidStorageIndex, TaoCurrency}; +use subtensor_runtime_common::{ + AlphaCurrency, Currency, NetUid, NetUidStorageIndex, TaoCurrency, rate_limiting, +}; use subtensor_swap_interface::SwapHandler; impl Pallet { @@ -325,7 +329,6 @@ impl Pallet { LastAdjustmentBlock::::remove(netuid); // --- 16. Serving / rho / curves, and other per-net controls. - ServingRateLimit::::remove(netuid); Rho::::remove(netuid); AlphaSigmoidSteepness::::remove(netuid); @@ -334,7 +337,6 @@ impl Pallet { BondsMovingAverage::::remove(netuid); BondsPenalty::::remove(netuid); BondsResetOn::::remove(netuid); - WeightsSetRateLimit::::remove(netuid); ValidatorPruneLen::::remove(netuid); ScalingLawPower::::remove(netuid); TargetRegistrationsPerInterval::::remove(netuid); @@ -458,20 +460,6 @@ impl Pallet { TransactionKeyLastBlock::::remove((hot, netuid, name)); } } - // StakingOperationRateLimiter NMAP: (hot, cold, netuid) → bool - { - let to_rm: sp_std::vec::Vec<(T::AccountId, T::AccountId)> = - StakingOperationRateLimiter::::iter() - .filter_map( - |((hot, cold, n), _)| { - if n == netuid { Some((hot, cold)) } else { None } - }, - ) - .collect(); - for (hot, cold) in to_rm { - StakingOperationRateLimiter::::remove((hot, cold, netuid)); - } - } // --- 22. Subnet leasing: remove mapping and any lease-scoped state linked to this netuid. if let Some(lease_id) = SubnetUidToLeaseId::::take(netuid) { @@ -508,7 +496,10 @@ impl Pallet { pub fn get_network_lock_cost() -> TaoCurrency { let last_lock = Self::get_network_last_lock(); let min_lock = Self::get_network_min_lock(); - let last_lock_block = Self::get_network_last_lock_block(); + let last_lock_block: u64 = + T::RateLimiting::last_seen(rate_limiting::GROUP_REGISTER_NETWORK, None) + .unwrap_or_default() + .saturated_into(); let current_block = Self::get_current_block_as_u64(); let lock_reduction_interval = Self::get_lock_reduction_interval(); let mult: TaoCurrency = if last_lock_block == 0 { 1 } else { 2 }.into(); @@ -559,12 +550,6 @@ impl Pallet { pub fn get_network_last_lock() -> TaoCurrency { NetworkLastLockCost::::get() } - pub fn get_network_last_lock_block() -> u64 { - Self::get_rate_limited_last_block(&RateLimitKey::NetworkLastRegistered) - } - pub fn set_network_last_lock_block(block: u64) { - Self::set_rate_limited_last_block(&RateLimitKey::NetworkLastRegistered, block); - } pub fn set_lock_reduction_interval(interval: u64) { NetworkLockReductionInterval::::set(interval); Self::deposit_event(Event::NetworkLockCostReductionIntervalSet(interval)); diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 6ae43ac384..504aa3bafd 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -646,15 +646,6 @@ pub mod pallet { RecycleOrBurnEnum::Burn // default to burn } - /// Default value for network rate limit. - #[pallet::type_value] - pub fn DefaultNetworkRateLimit() -> u64 { - if cfg!(feature = "pow-faucet") { - return 0; - } - T::InitialNetworkRateLimit::get() - } - /// Default value for network rate limit. #[pallet::type_value] pub fn DefaultNetworkRegistrationStartBlock() -> u64 { @@ -706,12 +697,6 @@ pub mod pallet { T::InitialTempo::get() } - /// Default value for weights set rate limit. - #[pallet::type_value] - pub fn DefaultWeightsSetRateLimit() -> u64 { - 100 - } - /// Default block number at registration. #[pallet::type_value] pub fn DefaultBlockAtRegistration() -> u64 { @@ -880,21 +865,6 @@ pub mod pallet { T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()) .expect("trailing zeroes always produce a valid account ID; qed") } - // pub fn DefaultHotkeyEmissionTempo() -> u64 { - // T::InitialHotkeyEmissionTempo::get() - // } (DEPRECATED) - - /// Default value for rate limiting - #[pallet::type_value] - pub fn DefaultTxRateLimit() -> u64 { - T::InitialTxRateLimit::get() - } - - /// Default value for delegate take rate limiting - #[pallet::type_value] - pub fn DefaultTxDelegateTakeRateLimit() -> u64 { - T::InitialTxDelegateTakeRateLimit::get() - } /// Default value for chidlkey take rate limiting #[pallet::type_value] @@ -908,12 +878,6 @@ pub mod pallet { 0 } - /// Default value for serving rate limit. - #[pallet::type_value] - pub fn DefaultServingRateLimit() -> u64 { - T::InitialServingRateLimit::get() - } - /// Default value for weight commit/reveal enabled. #[pallet::type_value] pub fn DefaultCommitRevealWeightsEnabled() -> bool { @@ -1241,7 +1205,7 @@ pub mod pallet { /// ================== /// ==== Coinbase ==== /// ================== - /// --- ITEM ( global_block_emission ) + /// --- ITEM ( global_block_emission ) #[pallet::storage] pub type BlockEmission = StorageValue<_, u64, ValueQuery, DefaultBlockEmission>; @@ -1281,7 +1245,7 @@ pub mod pallet { #[pallet::storage] pub type TotalStake = StorageValue<_, TaoCurrency, ValueQuery, DefaultZeroTao>; - /// --- ITEM ( moving_alpha ) -- subnet moving alpha. + /// --- ITEM ( moving_alpha ) -- subnet moving alpha. #[pallet::storage] pub type SubnetMovingAlpha = StorageValue<_, I96F32, ValueQuery, DefaultMovingAlpha>; @@ -1537,10 +1501,6 @@ pub mod pallet { #[pallet::storage] pub type SubnetOwnerCut = StorageValue<_, u16, ValueQuery, DefaultSubnetOwnerCut>; - /// ITEM( network_rate_limit ) - #[pallet::storage] - pub type NetworkRateLimit = StorageValue<_, u64, ValueQuery, DefaultNetworkRateLimit>; - /// --- ITEM( nominator_min_required_stake ) --- Factor of DefaultMinStake in per-mill format. #[pallet::storage] pub type NominatorMinRequiredStake = StorageValue<_, u64, ValueQuery, DefaultZeroU64>; @@ -1678,11 +1638,6 @@ pub mod pallet { pub type RecycleOrBurn = StorageMap<_, Identity, NetUid, RecycleOrBurnEnum, ValueQuery, DefaultRecycleOrBurn>; - /// --- MAP ( netuid ) --> serving_rate_limit - #[pallet::storage] - pub type ServingRateLimit = - StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultServingRateLimit>; - /// --- MAP ( netuid ) --> Rho #[pallet::storage] pub type Rho = StorageMap<_, Identity, NetUid, u16, ValueQuery, DefaultRho>; @@ -1776,11 +1731,6 @@ pub mod pallet { pub type BondsResetOn = StorageMap<_, Identity, NetUid, bool, ValueQuery, DefaultBondsResetOn>; - /// --- MAP ( netuid ) --> weights_set_rate_limit - #[pallet::storage] - pub type WeightsSetRateLimit = - StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultWeightsSetRateLimit>; - /// --- MAP ( netuid ) --> validator_prune_len #[pallet::storage] pub type ValidatorPruneLen = @@ -1860,15 +1810,6 @@ pub mod pallet { DefaultRAORecycledForRegistration, >; - /// --- ITEM ( tx_rate_limit ) - #[pallet::storage] - pub type TxRateLimit = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit>; - - /// --- ITEM ( tx_delegate_take_rate_limit ) - #[pallet::storage] - pub type TxDelegateTakeRateLimit = - StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit>; - /// --- ITEM ( tx_childkey_take_rate_limit ) #[pallet::storage] pub type TxChildkeyTakeRateLimit = @@ -1982,7 +1923,8 @@ pub mod pallet { #[pallet::storage] pub type Emission = StorageMap<_, Identity, NetUid, Vec, ValueQuery>; - /// --- MAP ( netuid ) --> last_update + /// Last updated weights per neuron (used for activity/outdated masking in epochs). + /// This is not rate-limiting state; rate-limiting uses `pallet-rate-limiting` last-seen. #[pallet::storage] pub type LastUpdate = StorageMap<_, Identity, NetUidStorageIndex, Vec, ValueQuery, EmptyU64Vec>; @@ -2208,20 +2150,6 @@ pub mod pallet { OptionQuery, >; - /// DMAP ( hot, cold, netuid ) --> rate limits for staking operations - /// Value contains just a marker: we use this map as a set. - #[pallet::storage] - pub type StakingOperationRateLimiter = StorageNMap< - _, - ( - NMapKey, // hot - NMapKey, // cold - NMapKey, // subnet - ), - bool, - ValueQuery, - >; - #[pallet::storage] // --- MAP(netuid ) --> Root claim threshold pub type RootClaimableThreshold = StorageMap<_, Blake2_128Concat, NetUid, I96F32, ValueQuery, DefaultMinRootClaimAmount>; @@ -2453,7 +2381,6 @@ pub enum CustomTransactionError { TransferDisallowed, HotKeyNotRegisteredInNetwork, InvalidIpAddress, - ServingRateLimitExceeded, InvalidPort, BadRequest, ZeroMaxAmount, @@ -2480,7 +2407,6 @@ impl From for u8 { CustomTransactionError::TransferDisallowed => 9, CustomTransactionError::HotKeyNotRegisteredInNetwork => 10, CustomTransactionError::InvalidIpAddress => 11, - CustomTransactionError::ServingRateLimitExceeded => 12, CustomTransactionError::InvalidPort => 13, CustomTransactionError::BadRequest => 255, CustomTransactionError::ZeroMaxAmount => 14, diff --git a/pallets/subtensor/src/macros/config.rs b/pallets/subtensor/src/macros/config.rs index 2124ec5f3f..93be12b361 100644 --- a/pallets/subtensor/src/macros/config.rs +++ b/pallets/subtensor/src/macros/config.rs @@ -8,6 +8,7 @@ mod config { use crate::{CommitmentsInterface, GetAlphaForTao, GetTaoForAlpha}; use pallet_commitments::GetCommitments; + use rate_limiting_interface::RateLimitingInterface; use subtensor_swap_interface::{SwapEngine, SwapHandler}; /// Configure the pallet by specifying the parameters and types on which it depends. @@ -56,6 +57,17 @@ mod config { /// Interface to clean commitments on network dissolution. type CommitmentsInterface: CommitmentsInterface; + /// Read-only interface for querying rate limiting configuration and usage. + type RateLimiting: RateLimitingInterface< + GroupId = subtensor_runtime_common::rate_limiting::GroupId, + CallMetadata = ::RuntimeCall, + Limit = BlockNumberFor, + Scope = subtensor_runtime_common::NetUid, + UsageKey = subtensor_runtime_common::rate_limiting::RateLimitUsageKey< + Self::AccountId, + >, + >; + /// Rate limit for associating an EVM key. type EvmKeyAssociateRateLimit: Get; @@ -171,15 +183,6 @@ mod config { /// Initial weights version key. #[pallet::constant] type InitialWeightsVersionKey: Get; - /// Initial serving rate limit. - #[pallet::constant] - type InitialServingRateLimit: Get; - /// Initial transaction rate limit. - #[pallet::constant] - type InitialTxRateLimit: Get; - /// Initial delegate take transaction rate limit. - #[pallet::constant] - type InitialTxDelegateTakeRateLimit: Get; /// Initial childkey take transaction rate limit. #[pallet::constant] type InitialTxChildKeyTakeRateLimit: Get; @@ -198,9 +201,6 @@ mod config { /// Initial lock reduction interval. #[pallet::constant] type InitialNetworkLockReductionInterval: Get; - /// Initial network creation rate limit - #[pallet::constant] - type InitialNetworkRateLimit: Get; /// Cost of swapping a hotkey. #[pallet::constant] type KeySwapCost: Get; diff --git a/pallets/subtensor/src/macros/dispatches.rs b/pallets/subtensor/src/macros/dispatches.rs index 2a362783ef..ed2b87476e 100644 --- a/pallets/subtensor/src/macros/dispatches.rs +++ b/pallets/subtensor/src/macros/dispatches.rs @@ -814,10 +814,6 @@ mod dispatches { /// /// * 'InvalidIpAddress': /// - The numerically encoded ip address does not resolve to a proper ip. - /// - /// * 'ServingRateLimitExceeded': - /// - Attempting to set prometheus information withing the rate limit min. - /// #[pallet::call_index(4)] #[pallet::weight((Weight::from_parts(33_010_000, 0) .saturating_add(T::DbWeight::get().reads(4)) @@ -898,10 +894,6 @@ mod dispatches { /// /// * 'InvalidIpAddress': /// - The numerically encoded ip address does not resolve to a proper ip. - /// - /// * 'ServingRateLimitExceeded': - /// - Attempting to set prometheus information withing the rate limit min. - /// #[pallet::call_index(40)] #[pallet::weight((Weight::from_parts(32_510_000, 0) .saturating_add(T::DbWeight::get().reads(4)) diff --git a/pallets/subtensor/src/macros/errors.rs b/pallets/subtensor/src/macros/errors.rs index 6c3d7a35df..db4eb48a5d 100644 --- a/pallets/subtensor/src/macros/errors.rs +++ b/pallets/subtensor/src/macros/errors.rs @@ -79,16 +79,13 @@ mod errors { SettingWeightsTooFast, /// A validator is attempting to set weights from a validator with incorrect weight version. IncorrectWeightVersionKey, + /// DEPRECATED /// An axon or prometheus serving exceeded the rate limit for a registered neuron. ServingRateLimitExceeded, /// The caller is attempting to set weights with more UIDs than allowed. UidsLengthExceedUidsInSubNet, // 32 /// A transactor exceeded the rate limit for add network transaction. NetworkTxRateLimitExceeded, - /// A transactor exceeded the rate limit for delegate transaction. - DelegateTxRateLimitExceeded, - /// A transactor exceeded the rate limit for setting or swapping hotkey. - HotKeySetTxRateLimitExceeded, /// A transactor exceeded the rate limit for staking. StakingRateLimitExceeded, /// Registration is disabled. @@ -176,8 +173,6 @@ mod errors { RevealTooEarly, /// Attempted to batch reveal weights with mismatched vector input lenghts. InputLengthsUnequal, - /// A transactor exceeded the rate limit for setting weights. - CommittingWeightsTooFast, /// Stake amount is too low. AmountTooLow, /// Not enough liquidity. @@ -212,8 +207,6 @@ mod errors { SameNetuid, /// The caller does not have enough balance for the operation. InsufficientBalance, - /// Too frequent staking operations - StakingOperationRateLimitExceeded, /// Invalid lease beneficiary to register the leased network. InvalidLeaseBeneficiary, /// Lease cannot end in the past. diff --git a/pallets/subtensor/src/macros/events.rs b/pallets/subtensor/src/macros/events.rs index c86cc1a1e5..1f184dbc47 100644 --- a/pallets/subtensor/src/macros/events.rs +++ b/pallets/subtensor/src/macros/events.rs @@ -75,8 +75,6 @@ mod events { ValidatorPruneLenSet(NetUid, u64), /// the scaling law power has been set for a subnet. ScalingLawPowerSet(NetUid, u16), - /// weights set rate limit has been set for a subnet. - WeightsSetRateLimitSet(NetUid, u64), /// immunity period is set for a subnet. ImmunityPeriodSet(NetUid, u16), /// bonds moving average is set for a subnet. @@ -101,18 +99,12 @@ mod events { MinDifficultySet(NetUid, u64), /// setting max difficulty on a network. MaxDifficultySet(NetUid, u64), - /// setting the prometheus serving rate limit. - ServingRateLimitSet(NetUid, u64), /// setting burn on a network. BurnSet(NetUid, TaoCurrency), /// setting max burn on a network. MaxBurnSet(NetUid, TaoCurrency), /// setting min burn on a network. MinBurnSet(NetUid, TaoCurrency), - /// setting the transaction rate limit. - TxRateLimitSet(u64), - /// setting the delegate take transaction rate limit. - TxDelegateTakeRateLimitSet(u64), /// setting the childkey take transaction rate limit. TxChildKeyTakeRateLimitSet(u64), /// setting the admin freeze window length (last N blocks of tempo) @@ -143,8 +135,6 @@ mod events { Faucet(T::AccountId, u64), /// the subnet owner cut is set. SubnetOwnerCutSet(u16), - /// the network creation rate limit is set. - NetworkRateLimitSet(u64), /// the network immunity period is set. NetworkImmunityPeriodSet(u64), /// the start call delay is set. diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index ed57d52c8b..8ad22b0423 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -38,17 +38,6 @@ mod hooks { } } - // ---- Called on the finalization of this pallet. The code weight must be taken into account prior to the execution of this macro. - // - // # Args: - // * 'n': (BlockNumberFor): - // - The number of the block we are finalizing. - fn on_finalize(_block_number: BlockNumberFor) { - for _ in StakingOperationRateLimiter::::drain() { - // Clear all entries each block - } - } - fn on_runtime_upgrade() -> frame_support::weights::Weight { // --- Migrate storage let mut weight = frame_support::weights::Weight::from_parts(0, 0); @@ -127,8 +116,6 @@ mod hooks { .saturating_add(migrations::migrate_crv3_v2_to_timelocked::migrate_crv3_v2_to_timelocked::()) // Migrate to fix root counters .saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::()) - // Migrate last block rate limiting storage items - .saturating_add(migrations::migrate_rate_limiting_last_blocks::migrate_obsolete_rate_limiting_last_blocks_storage::()) // Re-encode rate limit keys after introducing OwnerHyperparamUpdate variant .saturating_add(migrations::migrate_rate_limit_keys::migrate_rate_limit_keys::()) // Migrate remove network modality @@ -137,12 +124,8 @@ mod hooks { .saturating_add(migrations::migrate_network_immunity_period::migrate_network_immunity_period::()) // Migrate Subnet Limit .saturating_add(migrations::migrate_subnet_limit_to_default::migrate_subnet_limit_to_default::()) - // Migrate Lock Reduction Interval - .saturating_add(migrations::migrate_network_lock_reduction_interval::migrate_network_lock_reduction_interval::()) // Migrate subnet locked balances .saturating_add(migrations::migrate_subnet_locked::migrate_restore_subnet_locked::()) - // Migrate subnet burn cost to 2500 - .saturating_add(migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::()) // Cleanup child/parent keys .saturating_add(migrations::migrate_fix_childkeys::migrate_fix_childkeys::()) // Migrate AutoStakeDestinationColdkeys diff --git a/pallets/subtensor/src/migrations/migrate_create_root_network.rs b/pallets/subtensor/src/migrations/migrate_create_root_network.rs index 6cca34f815..599f7feb0e 100644 --- a/pallets/subtensor/src/migrations/migrate_create_root_network.rs +++ b/pallets/subtensor/src/migrations/migrate_create_root_network.rs @@ -73,9 +73,6 @@ pub fn migrate_create_root_network() -> Weight { // Set target registrations for validators as 1 per block TargetRegistrationsPerInterval::::insert(NetUid::ROOT, 1); - // TODO: Consider if WeightsSetRateLimit should be set - // WeightsSetRateLimit::::insert(NetUid::ROOT, 7200); - // Accrue weight for database writes weight.saturating_accrue(T::DbWeight::get().writes(7)); diff --git a/pallets/subtensor/src/migrations/migrate_network_lock_cost_2500.rs b/pallets/subtensor/src/migrations/migrate_network_lock_cost_2500.rs deleted file mode 100644 index e12356f6ba..0000000000 --- a/pallets/subtensor/src/migrations/migrate_network_lock_cost_2500.rs +++ /dev/null @@ -1,48 +0,0 @@ -use super::*; -use frame_support::{traits::Get, weights::Weight}; -use log; -use scale_info::prelude::string::String; - -pub fn migrate_network_lock_cost_2500() -> Weight { - const RAO_PER_TAO: u64 = 1_000_000_000; - const TARGET_COST_TAO: u64 = 2_500; - const NEW_LAST_LOCK_RAO: u64 = (TARGET_COST_TAO / 2) * RAO_PER_TAO; // 1,250 TAO - - let migration_name = b"migrate_network_lock_cost_2500".to_vec(); - let mut weight = T::DbWeight::get().reads(1); - - // Skip if already executed - if HasMigrationRun::::get(&migration_name) { - log::info!( - target: "runtime", - "Migration '{}' already run - skipping.", - String::from_utf8_lossy(&migration_name) - ); - return weight; - } - - // Use the current block; ensure it's non-zero so mult == 2 in get_network_lock_cost() - let current_block = Pallet::::get_current_block_as_u64(); - let block_to_set = if current_block == 0 { 1 } else { current_block }; - - // Set last_lock so that price = 2 * last_lock = 2,500 TAO at this block - Pallet::::set_network_last_lock(TaoCurrency::from(NEW_LAST_LOCK_RAO)); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - // Start decay from "now" (no backdated decay) - Pallet::::set_network_last_lock_block(block_to_set); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - // Mark migration done - HasMigrationRun::::insert(&migration_name, true); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - log::info!( - target: "runtime", - "Migration '{}' completed. lock_cost set to 2,500 TAO at block {}.", - String::from_utf8_lossy(&migration_name), - block_to_set - ); - - weight -} diff --git a/pallets/subtensor/src/migrations/migrate_network_lock_reduction_interval.rs b/pallets/subtensor/src/migrations/migrate_network_lock_reduction_interval.rs deleted file mode 100644 index 99bb5b6e97..0000000000 --- a/pallets/subtensor/src/migrations/migrate_network_lock_reduction_interval.rs +++ /dev/null @@ -1,55 +0,0 @@ -use super::*; -use frame_support::{traits::Get, weights::Weight}; -use log; -use scale_info::prelude::string::String; - -pub fn migrate_network_lock_reduction_interval() -> Weight { - const FOUR_DAYS: u64 = 28_800; - const EIGHT_DAYS: u64 = 57_600; - const ONE_WEEK_BLOCKS: u64 = 50_400; - - let migration_name = b"migrate_network_lock_reduction_interval".to_vec(); - let mut weight = T::DbWeight::get().reads(1); - - // Skip if already executed - if HasMigrationRun::::get(&migration_name) { - log::info!( - target: "runtime", - "Migration '{}' already run - skipping.", - String::from_utf8_lossy(&migration_name) - ); - return weight; - } - - let current_block = Pallet::::get_current_block_as_u64(); - - // ── 1) Set new values ───────────────────────────────────────────────── - NetworkLockReductionInterval::::put(EIGHT_DAYS); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - NetworkRateLimit::::put(FOUR_DAYS); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - Pallet::::set_network_last_lock(TaoCurrency::from(1_000_000_000_000)); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - // Hold price at 2000 TAO until day 7, then begin linear decay - Pallet::::set_network_last_lock_block(current_block.saturating_add(ONE_WEEK_BLOCKS)); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - // Allow registrations starting at day 7 - NetworkRegistrationStartBlock::::put(current_block.saturating_add(ONE_WEEK_BLOCKS)); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - // ── 2) Mark migration done ─────────────────────────────────────────── - HasMigrationRun::::insert(&migration_name, true); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - log::info!( - target: "runtime", - "Migration '{}' completed.", - String::from_utf8_lossy(&migration_name), - ); - - weight -} diff --git a/pallets/subtensor/src/migrations/migrate_rate_limiting_last_blocks.rs b/pallets/subtensor/src/migrations/migrate_rate_limiting_last_blocks.rs deleted file mode 100644 index 99ce1e3077..0000000000 --- a/pallets/subtensor/src/migrations/migrate_rate_limiting_last_blocks.rs +++ /dev/null @@ -1,161 +0,0 @@ -use crate::Vec; -use crate::{Config, HasMigrationRun, Pallet}; -use alloc::string::String; -use codec::Decode; -use frame_support::traits::Get; -use frame_support::weights::Weight; -use sp_io::hashing::twox_128; -use sp_io::storage::{clear, get}; - -pub fn migrate_obsolete_rate_limiting_last_blocks_storage() -> Weight { - migrate_network_last_registered::() - .saturating_add(migrate_last_tx_block::()) - .saturating_add(migrate_last_tx_block_childkey_take::()) - .saturating_add(migrate_last_tx_block_delegate_take::()) -} - -pub fn migrate_network_last_registered() -> Weight { - let migration_name = b"migrate_network_last_registered".to_vec(); - let pallet_name = "SubtensorModule"; - let storage_name = "NetworkLastRegistered"; - - migrate_value::(migration_name, pallet_name, storage_name, |limit| { - Pallet::::set_network_last_lock_block(limit); - }) -} - -#[allow(deprecated)] -pub fn migrate_last_tx_block() -> Weight { - let migration_name = b"migrate_last_tx_block".to_vec(); - - migrate_last_block_map::( - migration_name, - || crate::LastTxBlock::::drain().collect::>(), - |account, block| { - Pallet::::set_last_tx_block(&account, block); - }, - ) -} - -#[allow(deprecated)] -pub fn migrate_last_tx_block_childkey_take() -> Weight { - let migration_name = b"migrate_last_tx_block_childkey_take".to_vec(); - - migrate_last_block_map::( - migration_name, - || crate::LastTxBlockChildKeyTake::::drain().collect::>(), - |account, block| { - Pallet::::set_last_tx_block_childkey(&account, block); - }, - ) -} - -#[allow(deprecated)] -pub fn migrate_last_tx_block_delegate_take() -> Weight { - let migration_name = b"migrate_last_tx_block_delegate_take".to_vec(); - - migrate_last_block_map::( - migration_name, - || crate::LastTxBlockDelegateTake::::drain().collect::>(), - |account, block| { - Pallet::::set_last_tx_block_delegate_take(&account, block); - }, - ) -} - -fn migrate_value( - migration_name: Vec, - pallet_name: &str, - storage_name: &str, - set_value: SetValueFunction, -) -> Weight -where - T: Config, - SetValueFunction: Fn(u64 /*limit in blocks*/), -{ - // Initialize the weight with one read operation. - let mut weight = T::DbWeight::get().reads(1); - - // Check if the migration has already run - if HasMigrationRun::::get(&migration_name) { - log::info!("Migration '{migration_name:?}' has already run. Skipping.",); - return weight; - } - log::info!( - "Running migration '{}'", - String::from_utf8_lossy(&migration_name) - ); - - let pallet_name_hash = twox_128(pallet_name.as_bytes()); - let storage_name_hash = twox_128(storage_name.as_bytes()); - let full_key = [pallet_name_hash, storage_name_hash].concat(); - - if let Some(value_bytes) = get(&full_key) { - if let Ok(rate_limit) = Decode::decode(&mut &value_bytes[..]) { - set_value(rate_limit); - } - - clear(&full_key); - } - - weight = weight.saturating_add(T::DbWeight::get().writes(2)); - weight = weight.saturating_add(T::DbWeight::get().reads(1)); - - // Mark the migration as completed - HasMigrationRun::::insert(&migration_name, true); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - log::info!( - "Migration '{:?}' completed.", - String::from_utf8_lossy(&migration_name) - ); - - // Return the migration weight. - weight -} - -fn migrate_last_block_map( - migration_name: Vec, - get_values: GetValuesFunction, - set_value: SetValueFunction, -) -> Weight -where - T: Config, - GetValuesFunction: Fn() -> Vec<(T::AccountId, u64)>, // (account, limit in blocks) - SetValueFunction: Fn(T::AccountId, u64), -{ - // Initialize the weight with one read operation. - let mut weight = T::DbWeight::get().reads(1); - - // Check if the migration has already run - if HasMigrationRun::::get(&migration_name) { - log::info!("Migration '{migration_name:?}' has already run. Skipping.",); - return weight; - } - log::info!( - "Running migration '{}'", - String::from_utf8_lossy(&migration_name) - ); - - let key_values = get_values(); - weight = weight.saturating_add(T::DbWeight::get().reads(key_values.len() as u64)); - - for (account, block) in key_values.into_iter() { - set_value(account, block); - - weight = weight.saturating_add(T::DbWeight::get().writes(2)); - weight = weight.saturating_add(T::DbWeight::get().reads(1)); - } - - // Mark the migration as completed - HasMigrationRun::::insert(&migration_name, true); - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - - log::info!( - "Migration '{:?}' completed.", - String::from_utf8_lossy(&migration_name) - ); - - // Return the migration weight. - weight -} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index a03da9289e..e8395d5bd4 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -24,14 +24,11 @@ pub mod migrate_init_tao_flow; pub mod migrate_init_total_issuance; pub mod migrate_kappa_map_to_default; pub mod migrate_network_immunity_period; -pub mod migrate_network_lock_cost_2500; -pub mod migrate_network_lock_reduction_interval; pub mod migrate_orphaned_storage_items; pub mod migrate_pending_emissions; pub mod migrate_populate_owned_hotkeys; pub mod migrate_rao; pub mod migrate_rate_limit_keys; -pub mod migrate_rate_limiting_last_blocks; pub mod migrate_remove_commitments_rate_limit; pub mod migrate_remove_network_modality; pub mod migrate_remove_old_identity_maps; diff --git a/pallets/subtensor/src/staking/add_stake.rs b/pallets/subtensor/src/staking/add_stake.rs index ea33912bf1..d3470cc572 100644 --- a/pallets/subtensor/src/staking/add_stake.rs +++ b/pallets/subtensor/src/staking/add_stake.rs @@ -75,7 +75,6 @@ impl Pallet { netuid, tao_staked.saturating_to_num::().into(), T::SwapInterface::max_price(), - true, false, )?; @@ -165,15 +164,7 @@ impl Pallet { // 6. Swap the stake into alpha on the subnet and increase counters. // Emit the staking event. - Self::stake_into_subnet( - &hotkey, - &coldkey, - netuid, - tao_staked, - limit_price, - true, - false, - )?; + Self::stake_into_subnet(&hotkey, &coldkey, netuid, tao_staked, limit_price, false)?; // Ok and return. Ok(()) diff --git a/pallets/subtensor/src/staking/decrease_take.rs b/pallets/subtensor/src/staking/decrease_take.rs index 43bc633431..61c1131696 100644 --- a/pallets/subtensor/src/staking/decrease_take.rs +++ b/pallets/subtensor/src/staking/decrease_take.rs @@ -52,10 +52,6 @@ impl Pallet { // --- 4. Set the new take value. Delegates::::insert(hotkey.clone(), take); - // --- 5. Set last block for rate limiting - let block: u64 = Self::get_current_block_as_u64(); - Self::set_last_tx_block_delegate_take(&hotkey, block); - // --- 6. Emit the take value. log::debug!("TakeDecreased( coldkey:{coldkey:?}, hotkey:{hotkey:?}, take:{take:?} )"); Self::deposit_event(Event::TakeDecreased(coldkey, hotkey, take)); diff --git a/pallets/subtensor/src/staking/increase_take.rs b/pallets/subtensor/src/staking/increase_take.rs index 3a101c7e0f..4fbd446c89 100644 --- a/pallets/subtensor/src/staking/increase_take.rs +++ b/pallets/subtensor/src/staking/increase_take.rs @@ -52,19 +52,6 @@ impl Pallet { let max_take = MaxDelegateTake::::get(); ensure!(take <= max_take, Error::::DelegateTakeTooHigh); - // --- 5. Enforce the rate limit (independently on do_add_stake rate limits) - let block: u64 = Self::get_current_block_as_u64(); - ensure!( - !Self::exceeds_tx_delegate_take_rate_limit( - Self::get_last_tx_block_delegate_take(&hotkey), - block - ), - Error::::DelegateTxRateLimitExceeded - ); - - // Set last block for rate limiting - Self::set_last_tx_block_delegate_take(&hotkey, block); - // --- 6. Set the new take value. Delegates::::insert(hotkey.clone(), take); diff --git a/pallets/subtensor/src/staking/move_stake.rs b/pallets/subtensor/src/staking/move_stake.rs index a1d9b46d5b..5dd167c5d4 100644 --- a/pallets/subtensor/src/staking/move_stake.rs +++ b/pallets/subtensor/src/staking/move_stake.rs @@ -50,7 +50,6 @@ impl Pallet { None, None, false, - true, )?; // Log the event. @@ -141,7 +140,6 @@ impl Pallet { None, None, true, - false, )?; // 9. Emit an event for logging/monitoring. @@ -206,7 +204,6 @@ impl Pallet { None, None, false, - true, )?; // Emit an event for logging. @@ -274,7 +271,6 @@ impl Pallet { Some(limit_price), Some(allow_partial), false, - true, )?; // Emit an event for logging. @@ -306,7 +302,6 @@ impl Pallet { maybe_limit_price: Option, maybe_allow_partial: Option, check_transfer_toggle: bool, - set_limit: bool, ) -> Result { // Cap the alpha_amount at available Alpha because user might be paying transaxtion fees // in Alpha and their total is already reduced by now. @@ -379,7 +374,6 @@ impl Pallet { destination_netuid, tao_unstaked, T::SwapInterface::max_price(), - set_limit, drop_fee_destination, )?; } diff --git a/pallets/subtensor/src/staking/remove_stake.rs b/pallets/subtensor/src/staking/remove_stake.rs index 74a6bf34a6..77eaf19f6f 100644 --- a/pallets/subtensor/src/staking/remove_stake.rs +++ b/pallets/subtensor/src/staking/remove_stake.rs @@ -1,8 +1,14 @@ -use super::*; +use rate_limiting_interface::RateLimitingInterface; +use sp_runtime::Saturating; use substrate_fixed::types::U96F32; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; +use subtensor_runtime_common::{ + AlphaCurrency, Currency, NetUid, TaoCurrency, + rate_limiting::{self, RateLimitUsageKey}, +}; use subtensor_swap_interface::{Order, SwapHandler}; +use super::*; + impl Pallet { /// ---- The implementation for the extrinsic remove_stake: Removes stake from a hotkey account and adds it onto a coldkey. /// @@ -226,6 +232,7 @@ impl Pallet { // 3. Get all netuids. let netuids = Self::get_all_subnet_netuids(); log::debug!("All subnet netuids: {netuids:?}"); + let staking_ops_span = T::RateLimiting::rate_limit(rate_limiting::GROUP_STAKING_OPS, None); // 4. Iterate through all subnets and remove stake. let mut total_tao_unstaked = TaoCurrency::ZERO; @@ -235,6 +242,25 @@ impl Pallet { } // If not Root network. if !netuid.is_root() { + // Manually filter out rate-limited subnets. + if let Some(span) = staking_ops_span { + if !span.is_zero() { + let usage_key = RateLimitUsageKey::ColdkeyHotkeySubnet { + coldkey: coldkey.clone(), + hotkey: hotkey.clone(), + netuid, + }; + if let Some(last_seen) = T::RateLimiting::last_seen( + rate_limiting::GROUP_STAKING_OPS, + Some(usage_key), + ) { + let current = >::block_number(); + if current.saturating_sub(last_seen) < span { + continue; + } + } + } + } // Ensure that the hotkey has enough stake to withdraw. let alpha_unstaked = Self::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); @@ -280,7 +306,6 @@ impl Pallet { NetUid::ROOT, total_tao_unstaked, T::SwapInterface::max_price(), - false, // no limit for Root subnet false, )?; diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 1aeeacc33c..a8cacca701 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -763,7 +763,6 @@ impl Pallet { netuid: NetUid, tao: TaoCurrency, price_limit: TaoCurrency, - set_limit: bool, drop_fees: bool, ) -> Result { // Swap the tao to alpha. @@ -808,10 +807,6 @@ impl Pallet { LastColdkeyHotkeyStakeBlock::::insert(coldkey, hotkey, Self::get_current_block_as_u64()); - if set_limit { - Self::set_stake_operation_limit(hotkey, coldkey, netuid.into()); - } - // If this is a root-stake if netuid == NetUid::ROOT { // Adjust root claimed for this hotkey and coldkey. @@ -1033,8 +1028,6 @@ impl Pallet { // Ensure that the subnet exists. ensure!(Self::if_subnet_exist(netuid), Error::::SubnetNotExists); - Self::ensure_stake_operation_limit_not_exceeded(hotkey, coldkey, netuid.into())?; - // Ensure that the subnet is enabled. // Self::ensure_subtoken_enabled(netuid)?; @@ -1129,12 +1122,6 @@ impl Pallet { ensure!(origin_netuid != destination_netuid, Error::::SameNetuid); } - Self::ensure_stake_operation_limit_not_exceeded( - origin_hotkey, - origin_coldkey, - origin_netuid.into(), - )?; - // Ensure that both subnets exist. ensure!( Self::if_subnet_exist(origin_netuid), @@ -1257,27 +1244,6 @@ impl Pallet { SubnetAlphaIn::::set(netuid, subnet_alpha.saturating_sub(carry_over)); } } - - pub fn set_stake_operation_limit( - hotkey: &T::AccountId, - coldkey: &T::AccountId, - netuid: NetUid, - ) { - StakingOperationRateLimiter::::insert((hotkey, coldkey, netuid), true); - } - - pub fn ensure_stake_operation_limit_not_exceeded( - hotkey: &T::AccountId, - coldkey: &T::AccountId, - netuid: NetUid, - ) -> Result<(), Error> { - ensure!( - !StakingOperationRateLimiter::::contains_key((hotkey, coldkey, netuid)), - Error::::StakingOperationRateLimitExceeded - ); - - Ok(()) - } } /////////////////////////////////////////// diff --git a/pallets/subtensor/src/subnets/serving.rs b/pallets/subtensor/src/subnets/serving.rs index d11eb479d3..0791484d88 100644 --- a/pallets/subtensor/src/subnets/serving.rs +++ b/pallets/subtensor/src/subnets/serving.rs @@ -51,10 +51,6 @@ impl Pallet { /// /// * 'InvalidIpAddress': /// - The numerically encoded ip address does not resolve to a proper ip. - /// - /// * 'ServingRateLimitExceeded': - /// - Attempting to set prometheus information withing the rate limit min. - /// pub fn do_serve_axon( origin: T::RuntimeOrigin, netuid: NetUid, @@ -155,10 +151,6 @@ impl Pallet { /// /// * 'InvalidIpAddress': /// - The numerically encoded ip address does not resolve to a proper ip. - /// - /// * 'ServingRateLimitExceeded': - /// - Attempting to set prometheus information withing the rate limit min. - /// pub fn do_serve_prometheus( origin: T::RuntimeOrigin, netuid: NetUid, @@ -185,11 +177,6 @@ impl Pallet { // We get the previous axon info assoicated with this ( netuid, uid ) let mut prev_prometheus = Self::get_prometheus_info(netuid, &hotkey_id); - let current_block: u64 = Self::get_current_block_as_u64(); - ensure!( - Self::prometheus_passes_rate_limit(netuid, &prev_prometheus, current_block), - Error::::ServingRateLimitExceeded - ); // We insert the prometheus meta. prev_prometheus.block = Self::get_current_block_as_u64(); @@ -220,26 +207,6 @@ impl Pallet { --==[[ Helper functions ]]==-- *********************************/ - pub fn axon_passes_rate_limit( - netuid: NetUid, - prev_axon_info: &AxonInfoOf, - current_block: u64, - ) -> bool { - let rate_limit: u64 = Self::get_serving_rate_limit(netuid); - let last_serve = prev_axon_info.block; - rate_limit == 0 || last_serve == 0 || current_block.saturating_sub(last_serve) >= rate_limit - } - - pub fn prometheus_passes_rate_limit( - netuid: NetUid, - prev_prometheus_info: &PrometheusInfoOf, - current_block: u64, - ) -> bool { - let rate_limit: u64 = Self::get_serving_rate_limit(netuid); - let last_serve = prev_prometheus_info.block; - rate_limit == 0 || last_serve == 0 || current_block.saturating_sub(last_serve) >= rate_limit - } - pub fn get_axon_info(netuid: NetUid, hotkey: &T::AccountId) -> AxonInfoOf { if let Some(axons) = Axons::::get(netuid, hotkey) { axons @@ -345,11 +312,6 @@ impl Pallet { // Get the previous axon information. let mut prev_axon = Self::get_axon_info(netuid, hotkey_id); - let current_block: u64 = Self::get_current_block_as_u64(); - ensure!( - Self::axon_passes_rate_limit(netuid, &prev_axon, current_block), - Error::::ServingRateLimitExceeded - ); // Validate axon data with delegate func prev_axon.block = Self::get_current_block_as_u64(); diff --git a/pallets/subtensor/src/subnets/subnet.rs b/pallets/subtensor/src/subnets/subnet.rs index ecb5ce0452..975a5f4b93 100644 --- a/pallets/subtensor/src/subnets/subnet.rs +++ b/pallets/subtensor/src/subnets/subnet.rs @@ -60,13 +60,6 @@ impl Pallet { } } - /// Sets the network rate limit and emit the `NetworkRateLimitSet` event - /// - pub fn set_network_rate_limit(limit: u64) { - NetworkRateLimit::::set(limit); - Self::deposit_event(Event::NetworkRateLimitSet(limit)); - } - /// Checks if registrations are allowed for a given subnet. /// /// This function retrieves the subnet hyperparameters for the specified subnet and checks the @@ -133,12 +126,6 @@ impl Pallet { Error::::SubNetRegistrationDisabled ); - // --- 4. Rate limit for network registrations. - ensure!( - TransactionType::RegisterNetwork.passes_rate_limit::(&coldkey), - Error::::NetworkTxRateLimitExceeded - ); - // --- 5. Check if we need to prune a subnet (if at SubnetLimit). // But do not prune yet; we only do it after all checks pass. let subnet_limit = Self::get_max_subnets(); @@ -170,7 +157,6 @@ impl Pallet { // --- 8. Set the lock amount for use to determine pricing. Self::set_network_last_lock(actual_tao_lock_amount); - Self::set_network_last_lock_block(current_block); // --- 9. If we identified a subnet to prune, do it now. if let Some(prune_netuid) = recycle_netuid { diff --git a/pallets/subtensor/src/subnets/uids.rs b/pallets/subtensor/src/subnets/uids.rs index 0a09017e64..dad434a718 100644 --- a/pallets/subtensor/src/subnets/uids.rs +++ b/pallets/subtensor/src/subnets/uids.rs @@ -1,9 +1,13 @@ use super::*; use frame_support::storage::IterableStorageDoubleMap; -use sp_runtime::Percent; +use rate_limiting_interface::RateLimitingInterface; +use sp_runtime::{Percent, SaturatedConversion}; use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet}; use sp_std::{cmp, vec}; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{ + MechId, NetUid, + rate_limiting::{self, RateLimitUsageKey}, +}; impl Pallet { /// Returns the number of filled slots on a network. @@ -112,9 +116,16 @@ impl Pallet { Emission::::mutate(netuid, |v| v.push(0.into())); Consensus::::mutate(netuid, |v| v.push(0)); for mecid in 0..MechanismCountCurrent::::get(netuid).into() { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into()); + let mecid: MechId = mecid.into(); + let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); Incentive::::mutate(netuid_index, |v| v.push(0)); Self::set_last_update_for_uid(netuid_index, next_uid, block_number); + let usage = Self::weights_rl_usage_key(netuid, mecid, next_uid); + T::RateLimiting::set_last_seen( + rate_limiting::GROUP_WEIGHTS_SET, + Some(usage), + Some(block_number.saturated_into()), + ); } Dividends::::mutate(netuid, |v| v.push(0)); ValidatorTrust::::mutate(netuid, |v| v.push(0)); @@ -265,19 +276,46 @@ impl Pallet { // Update incentives/lastupdates for mechanisms for mecid in 0..mechanisms_count { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into()); + let mecid: MechId = mecid.into(); + let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); let incentive = Incentive::::get(netuid_index); let lastupdate = LastUpdate::::get(netuid_index); let mut trimmed_incentive = Vec::with_capacity(trimmed_uids.len()); let mut trimmed_lastupdate = Vec::with_capacity(trimmed_uids.len()); + let mut trimmed_last_seen = Vec::with_capacity(trimmed_uids.len()); for uid in &trimmed_uids { trimmed_incentive.push(incentive.get(*uid).cloned().unwrap_or_default()); trimmed_lastupdate.push(lastupdate.get(*uid).cloned().unwrap_or_default()); + let usage = Self::weights_rl_usage_key(netuid, mecid, *uid as u16); + trimmed_last_seen.push(T::RateLimiting::last_seen( + rate_limiting::GROUP_WEIGHTS_SET, + Some(usage), + )); } Incentive::::insert(netuid_index, trimmed_incentive); LastUpdate::::insert(netuid_index, trimmed_lastupdate); + + for uid in 0..current_n { + let usage = Self::weights_rl_usage_key(netuid, mecid, uid); + T::RateLimiting::set_last_seen( + rate_limiting::GROUP_WEIGHTS_SET, + Some(usage), + None, + ); + } + for (new_uid, last_seen) in trimmed_last_seen.into_iter().enumerate() { + let Some(block) = last_seen else { + continue; + }; + let usage = Self::weights_rl_usage_key(netuid, mecid, new_uid as u16); + T::RateLimiting::set_last_seen( + rate_limiting::GROUP_WEIGHTS_SET, + Some(usage), + Some(block), + ); + } } // Create mapping from old uid to new compressed uid @@ -439,4 +477,16 @@ impl Pallet { pub fn is_hotkey_registered_on_specific_network(hotkey: &T::AccountId, netuid: NetUid) -> bool { IsNetworkMember::::contains_key(hotkey, netuid) } + + fn weights_rl_usage_key( + netuid: NetUid, + mecid: MechId, + uid: u16, + ) -> RateLimitUsageKey { + if mecid == MechId::MAIN { + RateLimitUsageKey::SubnetNeuron { netuid, uid } + } else { + RateLimitUsageKey::SubnetMechanismNeuron { netuid, mecid, uid } + } + } } diff --git a/pallets/subtensor/src/subnets/weights.rs b/pallets/subtensor/src/subnets/weights.rs index 56acbef9c7..3e8a909da8 100644 --- a/pallets/subtensor/src/subnets/weights.rs +++ b/pallets/subtensor/src/subnets/weights.rs @@ -32,9 +32,6 @@ impl Pallet { /// * `HotKeyNotRegisteredInSubNet`: /// - Raised if the hotkey is not registered on the specified network. /// - /// * `CommittingWeightsTooFast`: - /// - Raised if the hotkey's commit rate exceeds the permitted limit. - /// /// * `TooManyUnrevealedCommits`: /// - Raised if the hotkey has reached the maximum number of unrevealed commits. /// @@ -87,14 +84,8 @@ impl Pallet { Error::::HotKeyNotRegisteredInSubNet ); - // 4. Check that the commit rate does not exceed the allowed frequency. let commit_block = Self::get_current_block_as_u64(); let neuron_uid = Self::get_uid_for_net_and_hotkey(netuid, &who)?; - ensure!( - // Rate limiting should happen per sub-subnet, so use netuid_index here - Self::check_rate_limit(netuid_index, neuron_uid, commit_block), - Error::::CommittingWeightsTooFast - ); // 5. Calculate the reveal blocks based on network tempo and reveal period. let (first_reveal_block, last_reveal_block) = Self::get_reveal_blocks(netuid, commit_block); @@ -216,43 +207,41 @@ impl Pallet { /// ---- Commits a timelocked, encrypted weight payload (Commit-Reveal v3). /// /// # Args - /// * `origin` (`::RuntimeOrigin`): + /// * `origin` (`::RuntimeOrigin`): /// The signed origin of the committing hotkey. - /// * `netuid` (`NetUid` = `u16`): + /// * `netuid` (`NetUid` = `u16`): /// Unique identifier for the subnet on which the commit is made. - /// * `commit` (`BoundedVec>`): - /// The encrypted weight payload, produced as follows: - /// 1. Build a [`WeightsPayload`] structure. - /// 2. SCALE-encode it (`parity_scale_codec::Encode`). - /// 3. Encrypt it following the steps - /// [here](https://github.com/ideal-lab5/tle/blob/f8e6019f0fb02c380ebfa6b30efb61786dede07b/timelock/src/tlock.rs#L283-L336) to - /// produce a [`TLECiphertext`]. + /// * `commit` (`BoundedVec>`): + /// The encrypted weight payload, produced as follows: + /// 1. Build a [`WeightsPayload`] structure. + /// 2. SCALE-encode it (`parity_scale_codec::Encode`). + /// 3. Encrypt it following the steps + /// [here](https://github.com/ideal-lab5/tle/blob/f8e6019f0fb02c380ebfa6b30efb61786dede07b/timelock/src/tlock.rs#L283-L336) to + /// produce a [`TLECiphertext`]. /// 4. Compress & serialise. - /// * `reveal_round` (`u64`): - /// DRAND round whose output becomes known during epoch `n + 1`; the payload + /// * `reveal_round` (`u64`): + /// DRAND round whose output becomes known during epoch `n + 1`; the payload /// must be revealed in that epoch. - /// * `commit_reveal_version` (`u16`): - /// Version tag that **must** match [`get_commit_reveal_weights_version`] for + /// * `commit_reveal_version` (`u16`): + /// Version tag that **must** match [`get_commit_reveal_weights_version`] for /// the call to succeed. Used to gate runtime upgrades. /// /// # Behaviour - /// 1. Verifies the caller’s signature and registration on `netuid`. + /// 1. Verifies the caller’s signature and registration on `netuid`. /// 2. Ensures commit-reveal is enabled **and** the supplied - /// `commit_reveal_version` is current. - /// 3. Enforces per-neuron rate-limiting via [`Pallet::check_rate_limit`]. - /// 4. Rejects the call when the hotkey already has ≥ 10 unrevealed commits in - /// the current epoch. - /// 5. Appends `(hotkey, commit_block, commit, reveal_round)` to - /// `TimelockedWeightCommits[netuid][epoch]`. - /// 6. Emits `TimelockedWeightsCommitted` with the Blake2 hash of `commit`. - /// 7. Updates `LastUpdateForUid` so subsequent rate-limit checks include this + /// `commit_reveal_version` is current. + /// 3. Rejects the call when the hotkey already has ≥ 10 unrevealed commits in + /// the current epoch. + /// 4. Appends `(hotkey, commit_block, commit, reveal_round)` to + /// `TimelockedWeightCommits[netuid][epoch]`. + /// 5. Emits `TimelockedWeightsCommitted` with the Blake2 hash of `commit`. + /// 6. Updates `LastUpdateForUid` so subsequent rate-limit checks include this /// commit. /// /// # Raises - /// * `CommitRevealDisabled` – Commit-reveal is disabled on `netuid`. - /// * `IncorrectCommitRevealVersion` – Provided version ≠ runtime version. - /// * `HotKeyNotRegisteredInSubNet` – Caller’s hotkey is not registered. - /// * `CommittingWeightsTooFast` – Caller exceeds commit-rate limit. + /// * `CommitRevealDisabled` – Commit-reveal is disabled on `netuid`. + /// * `IncorrectCommitRevealVersion` – Provided version ≠ runtime version. + /// * `HotKeyNotRegisteredInSubNet` – Caller’s hotkey is not registered. /// * `TooManyUnrevealedCommits` – Caller already has 10 unrevealed commits. /// /// # Events @@ -329,13 +318,8 @@ impl Pallet { Error::::HotKeyNotRegisteredInSubNet ); - // 5. Check that the commit rate does not exceed the allowed frequency. let commit_block = Self::get_current_block_as_u64(); let neuron_uid = Self::get_uid_for_net_and_hotkey(netuid, &who)?; - ensure!( - Self::check_rate_limit(netuid_index, neuron_uid, commit_block), - Error::::CommittingWeightsTooFast - ); // 6. Retrieve or initialize the VecDeque of commits for the hotkey. let cur_block = Self::get_current_block_as_u64(); @@ -798,16 +782,9 @@ impl Pallet { Error::::IncorrectWeightVersionKey ); - // --- 9. Ensure the uid is not setting weights faster than the weights_set_rate_limit. let neuron_uid = Self::get_uid_for_net_and_hotkey(netuid, &hotkey)?; - let current_block: u64 = Self::get_current_block_as_u64(); - if !Self::get_commit_reveal_weights_enabled(netuid) { - ensure!( - // Rate limit should apply per sub-subnet, so use netuid_index here - Self::check_rate_limit(netuid_index, neuron_uid, current_block), - Error::::SettingWeightsTooFast - ); - } + let current_block = Self::get_current_block_as_u64(); + // --- 10. Check that the neuron uid is an allowed validator permitted to set non-self weights. ensure!( Self::check_validator_permit(netuid, neuron_uid, &uids, &values), @@ -1101,30 +1078,6 @@ impl Pallet { network_version_key == 0 || version_key >= network_version_key } - /// Checks if the neuron has set weights within the weights_set_rate_limit. - /// - pub fn check_rate_limit( - netuid_index: NetUidStorageIndex, - neuron_uid: u16, - current_block: u64, - ) -> bool { - let maybe_netuid_and_subid = Self::get_netuid_and_subid(netuid_index); - if let Ok((netuid, _)) = maybe_netuid_and_subid - && Self::is_uid_exist_on_network(netuid, neuron_uid) - { - // --- 1. Ensure that the diff between current and last_set weights is greater than limit. - let last_set_weights: u64 = Self::get_last_update_for_uid(netuid_index, neuron_uid); - if last_set_weights == 0 { - return true; - } // (Storage default) Never set weights. - return current_block.saturating_sub(last_set_weights) - >= Self::get_weights_set_rate_limit(netuid); - } - - // --- 3. Non registered peers cant pass. Neither can non-existing mecid - false - } - /// Checks for any invalid uids on this network. pub fn contains_invalid_uids(netuid: NetUid, uids: &[u16]) -> bool { for uid in uids { diff --git a/pallets/subtensor/src/swap/swap_coldkey.rs b/pallets/subtensor/src/swap/swap_coldkey.rs index c81138b58c..06c4b56941 100644 --- a/pallets/subtensor/src/swap/swap_coldkey.rs +++ b/pallets/subtensor/src/swap/swap_coldkey.rs @@ -73,10 +73,6 @@ impl Pallet { // 9. Perform the actual coldkey swap let _ = Self::perform_swap_coldkey(old_coldkey, new_coldkey, &mut weight); - // 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)); - // 11. Remove the coldkey swap scheduled record ColdkeySwapScheduled::::remove(old_coldkey); diff --git a/pallets/subtensor/src/swap/swap_hotkey.rs b/pallets/subtensor/src/swap/swap_hotkey.rs index 4fdf87fb7b..a81ff53319 100644 --- a/pallets/subtensor/src/swap/swap_hotkey.rs +++ b/pallets/subtensor/src/swap/swap_hotkey.rs @@ -1,8 +1,12 @@ use super::*; use frame_support::weights::Weight; +use rate_limiting_interface::RateLimitingInterface; use sp_core::Get; use substrate_fixed::types::U64F64; -use subtensor_runtime_common::{Currency, MechId, NetUid}; +use subtensor_runtime_common::{ + Currency, MechId, NetUid, + rate_limiting::{self, RateLimitUsageKey}, +}; impl Pallet { /// Swaps the hotkey of a coldkey account. @@ -21,7 +25,6 @@ impl Pallet { /// # Errors /// /// * `NonAssociatedColdKey` - If the coldkey does not own the old hotkey. - /// * `HotKeySetTxRateLimitExceeded` - If the transaction rate limit is exceeded. /// * `NewHotKeyIsSameWithOld` - If the new hotkey is the same as the old hotkey. /// * `HotKeyAlreadyRegisteredInSubNet` - If the new hotkey is already registered in the subnet. /// * `NotEnoughBalanceToPaySwapHotKey` - If there is not enough balance to pay for the swap. @@ -46,31 +49,22 @@ impl Pallet { // 4. Ensure the new hotkey is different from the old one ensure!(old_hotkey != new_hotkey, Error::::NewHotKeyIsSameWithOld); - // 5. Get the current block number - let block: u64 = Self::get_current_block_as_u64(); - - // 6. Ensure the transaction rate limit is not exceeded - ensure!( - !Self::exceeds_tx_rate_limit(Self::get_last_tx_block(&coldkey), block), - Error::::HotKeySetTxRateLimitExceeded - ); - - weight.saturating_accrue(T::DbWeight::get().reads(2)); - // 7. Ensure the new hotkey is not already registered on any network ensure!( !Self::is_hotkey_registered_on_any_network(new_hotkey), Error::::HotKeyAlreadyRegisteredInSubNet ); - // 8. Swap LastTxBlock - let last_tx_block: u64 = Self::get_last_tx_block(old_hotkey); - Self::set_last_tx_block(new_hotkey, last_tx_block); - weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); - - // 9. Swap LastTxBlockDelegateTake - let last_tx_block_delegate_take: u64 = Self::get_last_tx_block_delegate_take(old_hotkey); - Self::set_last_tx_block_delegate_take(new_hotkey, last_tx_block_delegate_take); + // 8. Swap last-seen + let last_tx_block = T::RateLimiting::last_seen( + rate_limiting::GROUP_SWAP_KEYS, + Some(RateLimitUsageKey::Account(old_hotkey.clone())), + ); + T::RateLimiting::set_last_seen( + rate_limiting::GROUP_SWAP_KEYS, + Some(RateLimitUsageKey::Account(new_hotkey.clone())), + last_tx_block, + ); weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); // 10. Swap LastTxBlockChildKeyTake @@ -107,10 +101,6 @@ impl Pallet { // 19. Perform the hotkey swap Self::perform_hotkey_swap_on_all_subnets(old_hotkey, new_hotkey, &coldkey, &mut weight)?; - // 20. Update the last transaction block for the coldkey - Self::set_last_tx_block(&coldkey, block); - weight.saturating_accrue(T::DbWeight::get().writes(1)); - // 21. Emit an event for the hotkey swap Self::deposit_event(Event::HotkeySwapped { coldkey, @@ -193,8 +183,12 @@ impl Pallet { // 6. Swap LastTxBlock // LastTxBlock( hotkey ) --> u64 -- the last transaction block for the hotkey. - Self::remove_last_tx_block(old_hotkey); - weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); + T::RateLimiting::set_last_seen( + rate_limiting::GROUP_SWAP_KEYS, + Some(RateLimitUsageKey::Account(old_hotkey.clone())), + None, + ); + weight.saturating_accrue(T::DbWeight::get().writes(1)); // 7. Swap LastTxBlockDelegateTake // LastTxBlockDelegateTake( hotkey ) --> u64 -- the last transaction block for the hotkey delegate take. @@ -249,6 +243,9 @@ impl Pallet { let hotkey_swap_interval = T::HotkeySwapOnSubnetInterval::get(); let last_hotkey_swap_block = LastHotkeySwapOnNetuid::::get(netuid, coldkey); + // NOTE: This subnet interval gate is legacy swap-keys rate-limiting group behavior and + // remains in pallet-subtensor; it is not migrated into pallet-rate-limiting because that + // system supports only a single span per target. ensure!( last_hotkey_swap_block.saturating_add(hotkey_swap_interval) < block, Error::::HotKeySwapOnSubnetIntervalNotPassed @@ -302,7 +299,6 @@ impl Pallet { Self::perform_hotkey_swap_on_one_subnet(old_hotkey, new_hotkey, &mut weight, netuid)?; // 10. Update the last transaction block for the coldkey - Self::set_last_tx_block(coldkey, block); LastHotkeySwapOnNetuid::::insert(netuid, coldkey, block); weight.saturating_accrue(T::DbWeight::get().writes(2)); diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index e8be57f021..78202bd64b 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -2271,7 +2271,7 @@ fn test_do_remove_stake_clears_pending_childkeys() { assert!(pending_before.1 > 0); // Remove stake - remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); + assert_ok!(SubtensorModule::do_remove_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -2664,8 +2664,6 @@ fn test_childkey_set_weights_single_parent() { 1_000_000.into(), ); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // Set parent-child relationship mock_set_children_no_epochs(netuid, &parent, &[(u64::MAX, child)]); @@ -2760,8 +2758,6 @@ fn test_set_weights_no_parent() { stake_to_give_child.into(), ); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // Has stake and no parent step_block(7200 + 1); @@ -2863,7 +2859,6 @@ fn test_childkey_take_drain() { &nominator, stake + ExistentialDeposit::get(), ); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_max_allowed_validators(netuid, 2); step_block(subnet_tempo); SubnetOwnerCut::::set(0); @@ -3659,9 +3654,6 @@ fn test_dynamic_parent_child_relationships() { let values: Vec = vec![65535, 65535, 65535]; // Set equal weights for all hotkeys let version_key = SubtensorModule::get_weights_version_key(netuid); - // Ensure we can set weights without rate limiting - SubtensorModule::set_weights_set_rate_limit(netuid, 0); - assert_ok!(SubtensorModule::set_weights( origin, netuid, diff --git a/pallets/subtensor/src/tests/coinbase.rs b/pallets/subtensor/src/tests/coinbase.rs index a79f4b713a..de42cf6b3b 100644 --- a/pallets/subtensor/src/tests/coinbase.rs +++ b/pallets/subtensor/src/tests/coinbase.rs @@ -2468,8 +2468,6 @@ fn test_distribute_emission_zero_emission() { let init_stake: u64 = 100_000_000_000_000; let tempo = 2; SubtensorModule::set_tempo(netuid, tempo); - // Set weight-set limit to 0. - SubtensorModule::set_weights_set_rate_limit(netuid, 0); register_ok_neuron(netuid, hotkey, coldkey, 0); register_ok_neuron(netuid, miner_hk, miner_ck, 0); @@ -2556,8 +2554,6 @@ fn test_run_coinbase_not_started() { let init_stake: u64 = 100_000_000_000_000; let tempo = 2; SubtensorModule::set_tempo(netuid, tempo); - // Set weight-set limit to 0. - SubtensorModule::set_weights_set_rate_limit(netuid, 0); let reserve = init_stake * 1000; mock::setup_reserves(netuid, reserve.into(), reserve.into()); @@ -2650,8 +2646,6 @@ fn test_run_coinbase_not_started_start_after() { let init_stake: u64 = 100_000_000_000_000; let tempo = 2; SubtensorModule::set_tempo(netuid, tempo); - // Set weight-set limit to 0. - SubtensorModule::set_weights_set_rate_limit(netuid, 0); register_ok_neuron(netuid, hotkey, coldkey, 0); register_ok_neuron(netuid, miner_hk, miner_ck, 0); @@ -3018,7 +3012,6 @@ fn test_mining_emission_distribution_with_no_root_sell() { &miner_coldkey, stake + ExistentialDeposit::get(), ); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); step_block(subnet_tempo); SubnetOwnerCut::::set(u16::MAX / 10); // There are two validators and three neurons @@ -3213,7 +3206,6 @@ fn test_mining_emission_distribution_with_root_sell() { &miner_coldkey, stake + ExistentialDeposit::get(), ); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); step_block(subnet_tempo); SubnetOwnerCut::::set(u16::MAX / 10); // There are two validators and three neurons @@ -3865,7 +3857,6 @@ fn test_pending_emission_start_call_not_done() { &validator_coldkey, stake + ExistentialDeposit::get(), ); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); step_block(subnet_tempo); SubnetOwnerCut::::set(u16::MAX / 10); // There are two validators and three neurons diff --git a/pallets/subtensor/src/tests/epoch.rs b/pallets/subtensor/src/tests/epoch.rs index 32f754f78d..c0be00b0fd 100644 --- a/pallets/subtensor/src/tests/epoch.rs +++ b/pallets/subtensor/src/tests/epoch.rs @@ -568,7 +568,6 @@ fn test_1_graph() { stake_amount + ExistentialDeposit::get(), ); register_ok_neuron(netuid, hotkey, coldkey, 1); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey), @@ -1017,7 +1016,6 @@ fn test_bonds() { assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), n); SubtensorModule::set_max_registrations_per_block( netuid, n ); SubtensorModule::set_target_registrations_per_interval(netuid, n); - SubtensorModule::set_weights_set_rate_limit( netuid, 0 ); SubtensorModule::set_min_allowed_weights( netuid, 1 ); SubtensorModule::set_bonds_penalty(netuid, u16::MAX); @@ -1576,7 +1574,6 @@ fn test_outdated_weights() { let stake: u64 = 1; add_network_disable_commit_reveal(netuid, tempo, 0); SubtensorModule::set_max_allowed_uids(netuid, n); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_max_registrations_per_block(netuid, n); SubtensorModule::set_target_registrations_per_interval(netuid, n); SubtensorModule::set_min_allowed_weights(netuid, 0); @@ -1765,7 +1762,6 @@ fn test_zero_weights() { let stake: u64 = 1; add_network_disable_commit_reveal(netuid, tempo, 0); SubtensorModule::set_max_allowed_uids(netuid, n); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_max_registrations_per_block(netuid, n); SubtensorModule::set_target_registrations_per_interval(netuid, n); SubtensorModule::set_min_allowed_weights(netuid, 0); @@ -1967,7 +1963,6 @@ fn test_deregistered_miner_bonds() { let stake: u64 = 1; add_network_disable_commit_reveal(netuid, high_tempo, 0); SubtensorModule::set_max_allowed_uids(netuid, n); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_max_registrations_per_block(netuid, n); SubtensorModule::set_target_registrations_per_interval(netuid, n); SubtensorModule::set_min_allowed_weights(netuid, 0); @@ -2688,7 +2683,6 @@ fn setup_yuma_3_scenario(netuid: NetUid, n: u16, sparse: bool, max_stake: u64, s assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), n); SubtensorModule::set_max_registrations_per_block(netuid, n); SubtensorModule::set_target_registrations_per_interval(netuid, n); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_min_allowed_weights(netuid, 1); SubtensorModule::set_bonds_penalty(netuid, 0); SubtensorModule::set_alpha_sigmoid_steepness(netuid, 1000); @@ -3624,7 +3618,6 @@ fn test_epoch_masks_incoming_to_sniped_uid_prevents_inheritance() { /* validator weights uid‑1 */ SubtensorModule::set_commit_reveal_weights_enabled(netuid, false); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); assert_ok!(SubtensorModule::set_weights( RuntimeOrigin::signed(val_hot), netuid, @@ -3696,7 +3689,6 @@ fn test_epoch_no_mask_when_commit_reveal_disabled() { 1_000.into(), ); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); assert_ok!(SubtensorModule::set_weights( RuntimeOrigin::signed(hot), netuid, @@ -3789,7 +3781,6 @@ fn test_epoch_does_not_mask_outside_window_but_masks_inside() { /* vote */ SubtensorModule::set_commit_reveal_weights_enabled(netuid, false); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); assert_ok!(SubtensorModule::set_weights( RuntimeOrigin::signed(v_hot), netuid, @@ -3836,7 +3827,6 @@ fn test_last_update_size_mismatch() { stake_amount + ExistentialDeposit::get(), ); register_ok_neuron(netuid, hotkey, coldkey, 1); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey), diff --git a/pallets/subtensor/src/tests/epoch_logs.rs b/pallets/subtensor/src/tests/epoch_logs.rs index 38e3a1b734..420a9e0eac 100644 --- a/pallets/subtensor/src/tests/epoch_logs.rs +++ b/pallets/subtensor/src/tests/epoch_logs.rs @@ -62,7 +62,6 @@ fn setup_epoch(neurons: Vec, mechanism_count: u8) { SubnetworkN::::insert(netuid, network_n); ActivityCutoff::::insert(netuid, ACTIVITY_CUTOFF); Tempo::::insert(netuid, TEMPO); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); MechanismCountCurrent::::insert(netuid, MechId::from(mechanism_count)); // Setup neurons diff --git a/pallets/subtensor/src/tests/mechanism.rs b/pallets/subtensor/src/tests/mechanism.rs index 9e6450e09c..d096cd0515 100644 --- a/pallets/subtensor/src/tests/mechanism.rs +++ b/pallets/subtensor/src/tests/mechanism.rs @@ -1062,7 +1062,6 @@ fn test_commit_reveal_mechanism_weights_ok() { // Enable commit-reveal path and make caller a validator with stake SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, uid1, true); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); SubtensorModule::add_balance_to_coldkey_account(&ck1, 1); @@ -1146,7 +1145,6 @@ fn test_commit_reveal_above_mechanism_count_fails() { // Enable commit-reveal path and make caller a validator with stake SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, uid1, true); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); SubtensorModule::add_balance_to_coldkey_account(&ck1, 1); @@ -1227,7 +1225,6 @@ fn test_reveal_crv3_commits_sub_success() { register_ok_neuron(netuid, hotkey1, U256::from(3), 100_000); register_ok_neuron(netuid, hotkey2, U256::from(4), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); @@ -1333,7 +1330,6 @@ fn test_crv3_above_mechanism_count_fails() { register_ok_neuron(netuid, hotkey1, U256::from(3), 100_000); register_ok_neuron(netuid, hotkey2, U256::from(4), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); @@ -1387,101 +1383,6 @@ fn test_crv3_above_mechanism_count_fails() { }); } -#[test] -fn test_do_commit_crv3_mechanism_weights_committing_too_fast() { - new_test_ext(1).execute_with(|| { - let netuid = NetUid::from(1); - let mecid = MechId::from(1u8); - let hotkey: AccountId = U256::from(1); - let commit_data_1: Vec = vec![1, 2, 3]; - let commit_data_2: Vec = vec![4, 5, 6]; - let reveal_round: u64 = 1000; - - add_network(netuid, 5, 0); - MechanismCountCurrent::::insert(netuid, MechId::from(2u8)); // allow subids {0,1} - - register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); - SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - - let uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey).expect("uid"); - let idx1 = SubtensorModule::get_mechanism_storage_index(netuid, mecid); - SubtensorModule::set_last_update_for_uid(idx1, uid, 0); - - // make validator with stake - SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_validator_permit_for_uid(netuid, uid, true); - SubtensorModule::add_balance_to_coldkey_account(&U256::from(2), 1); - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( - &hotkey, - &U256::from(2), - netuid, - 1.into(), - ); - - // first commit OK on mecid=1 - assert_ok!(SubtensorModule::commit_timelocked_mechanism_weights( - RuntimeOrigin::signed(hotkey), - netuid, - mecid, - commit_data_1.clone().try_into().expect("bounded"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - )); - - // immediate second commit on SAME mecid blocked - assert_noop!( - SubtensorModule::commit_timelocked_mechanism_weights( - RuntimeOrigin::signed(hotkey), - netuid, - mecid, - commit_data_2.clone().try_into().expect("bounded"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - ), - Error::::CommittingWeightsTooFast - ); - - // BUT committing too soon on a DIFFERENT mecid is allowed - let other_subid = MechId::from(0u8); - let idx0 = SubtensorModule::get_mechanism_storage_index(netuid, other_subid); - SubtensorModule::set_last_update_for_uid(idx0, uid, 0); // baseline like above - assert_ok!(SubtensorModule::commit_timelocked_mechanism_weights( - RuntimeOrigin::signed(hotkey), - netuid, - other_subid, - commit_data_2.clone().try_into().expect("bounded"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - )); - - // still too fast on original mecid after 2 blocks - step_block(2); - assert_noop!( - SubtensorModule::commit_timelocked_mechanism_weights( - RuntimeOrigin::signed(hotkey), - netuid, - mecid, - commit_data_2.clone().try_into().expect("bounded"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - ), - Error::::CommittingWeightsTooFast - ); - - // after enough blocks, OK again on original mecid - step_block(3); - assert_ok!(SubtensorModule::commit_timelocked_mechanism_weights( - RuntimeOrigin::signed(hotkey), - netuid, - mecid, - commit_data_2.try_into().expect("bounded"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - )); - }); -} - #[test] fn epoch_mechanism_emergency_mode_distributes_by_stake() { new_test_ext(1).execute_with(|| { diff --git a/pallets/subtensor/src/tests/migration.rs b/pallets/subtensor/src/tests/migration.rs index bed77e797f..03b6c289e1 100644 --- a/pallets/subtensor/src/tests/migration.rs +++ b/pallets/subtensor/src/tests/migration.rs @@ -22,13 +22,15 @@ use frame_support::{ use crate::migrations::migrate_storage; use frame_system::Config; use pallet_drand::types::RoundNumber; +use rate_limiting_interface::RateLimitingInterface; use scale_info::prelude::collections::VecDeque; use sp_core::{H256, U256, crypto::Ss58Codec}; use sp_io::hashing::twox_128; +use sp_runtime::SaturatedConversion; use sp_runtime::traits::Zero; use substrate_fixed::types::extra::U2; use substrate_fixed::types::{I96F32, U64F64}; -use subtensor_runtime_common::{NetUidStorageIndex, TaoCurrency}; +use subtensor_runtime_common::{NetUidStorageIndex, TaoCurrency, rate_limiting}; #[allow(clippy::arithmetic_side_effects)] fn close(value: u64, target: u64, eps: u64) { @@ -827,317 +829,6 @@ fn test_migrate_remove_commitments_rate_limit() { }); } -#[test] -fn test_migrate_network_last_registered() { - new_test_ext(1).execute_with(|| { - // ------------------------------ - // Step 1: Simulate Old Storage Entry - // ------------------------------ - const MIGRATION_NAME: &str = "migrate_network_last_registered"; - - let pallet_name = "SubtensorModule"; - let storage_name = "NetworkLastRegistered"; - let pallet_name_hash = twox_128(pallet_name.as_bytes()); - let storage_name_hash = twox_128(storage_name.as_bytes()); - let prefix = [pallet_name_hash, storage_name_hash].concat(); - - let mut full_key = prefix.clone(); - - let original_value: u64 = 123; - put_raw(&full_key, &original_value.encode()); - - let stored_before = get_raw(&full_key).expect("Expected RateLimit to exist"); - assert_eq!( - u64::decode(&mut &stored_before[..]).expect("Failed to decode RateLimit"), - original_value - ); - - assert!( - !HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), - "Migration should not have run yet" - ); - - // ------------------------------ - // Step 2: Run the Migration - // ------------------------------ - let weight = crate::migrations::migrate_rate_limiting_last_blocks:: - migrate_obsolete_rate_limiting_last_blocks_storage::(); - - assert!( - HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), - "Migration should be marked as completed" - ); - - // ------------------------------ - // Step 3: Verify Migration Effects - // ------------------------------ - - assert_eq!( - SubtensorModule::get_network_last_lock_block(), - original_value - ); - assert_eq!( - get_raw(&full_key), - None, - "RateLimit storage should have been cleared" - ); - - assert!(!weight.is_zero(), "Migration weight should be non-zero"); - }); -} - -#[allow(deprecated)] -#[test] -fn test_migrate_last_block_tx() { - new_test_ext(1).execute_with(|| { - // ------------------------------ - // Step 1: Simulate Old Storage Entry - // ------------------------------ - const MIGRATION_NAME: &str = "migrate_last_tx_block"; - - let test_account: U256 = U256::from(1); - let original_value: u64 = 123; - - LastTxBlock::::insert(test_account, original_value); - - assert!( - !HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), - "Migration should not have run yet" - ); - - // ------------------------------ - // Step 2: Run the Migration - // ------------------------------ - let weight = crate::migrations::migrate_rate_limiting_last_blocks:: - migrate_obsolete_rate_limiting_last_blocks_storage::(); - - assert!( - HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), - "Migration should be marked as completed" - ); - - // ------------------------------ - // Step 3: Verify Migration Effects - // ------------------------------ - - assert_eq!( - SubtensorModule::get_last_tx_block(&test_account), - original_value - ); - assert!( - !LastTxBlock::::contains_key(test_account), - "RateLimit storage should have been cleared" - ); - - assert!(!weight.is_zero(), "Migration weight should be non-zero"); - }); -} - -#[allow(deprecated)] -#[test] -fn test_migrate_last_tx_block_childkey_take() { - new_test_ext(1).execute_with(|| { - // ------------------------------ - // Step 1: Simulate Old Storage Entry - // ------------------------------ - const MIGRATION_NAME: &str = "migrate_last_tx_block_childkey_take"; - - let test_account: U256 = U256::from(1); - let original_value: u64 = 123; - - LastTxBlockChildKeyTake::::insert(test_account, original_value); - - assert!( - !HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), - "Migration should not have run yet" - ); - - // ------------------------------ - // Step 2: Run the Migration - // ------------------------------ - let weight = crate::migrations::migrate_rate_limiting_last_blocks:: - migrate_obsolete_rate_limiting_last_blocks_storage::(); - - assert!( - HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), - "Migration should be marked as completed" - ); - - // ------------------------------ - // Step 3: Verify Migration Effects - // ------------------------------ - - assert_eq!( - SubtensorModule::get_last_tx_block_childkey_take(&test_account), - original_value - ); - assert!( - !LastTxBlockChildKeyTake::::contains_key(test_account), - "RateLimit storage should have been cleared" - ); - - assert!(!weight.is_zero(), "Migration weight should be non-zero"); - }); -} - -#[allow(deprecated)] -#[test] -fn test_migrate_last_tx_block_delegate_take() { - new_test_ext(1).execute_with(|| { - // ------------------------------ - // Step 1: Simulate Old Storage Entry - // ------------------------------ - const MIGRATION_NAME: &str = "migrate_last_tx_block_delegate_take"; - - let test_account: U256 = U256::from(1); - let original_value: u64 = 123; - - LastTxBlockDelegateTake::::insert(test_account, original_value); - - assert!( - !HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), - "Migration should not have run yet" - ); - - // ------------------------------ - // Step 2: Run the Migration - // ------------------------------ - let weight = crate::migrations::migrate_rate_limiting_last_blocks:: - migrate_last_tx_block_delegate_take::(); - - assert!( - HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), - "Migration should be marked as completed" - ); - - // ------------------------------ - // Step 3: Verify Migration Effects - // ------------------------------ - - assert_eq!( - SubtensorModule::get_last_tx_block_delegate_take(&test_account), - original_value - ); - assert!( - !LastTxBlockDelegateTake::::contains_key(test_account), - "RateLimit storage should have been cleared" - ); - - assert!(!weight.is_zero(), "Migration weight should be non-zero"); - }); -} - -#[test] -fn test_migrate_rate_limit_keys() { - new_test_ext(1).execute_with(|| { - const MIGRATION_NAME: &[u8] = b"migrate_rate_limit_keys"; - let prefix = { - let pallet_prefix = twox_128("SubtensorModule".as_bytes()); - let storage_prefix = twox_128("LastRateLimitedBlock".as_bytes()); - [pallet_prefix, storage_prefix].concat() - }; - - // Seed new-format entries that must survive the migration untouched. - let new_last_account = U256::from(10); - SubtensorModule::set_last_tx_block(&new_last_account, 555); - let new_child_account = U256::from(11); - SubtensorModule::set_last_tx_block_childkey(&new_child_account, 777); - let new_delegate_account = U256::from(12); - SubtensorModule::set_last_tx_block_delegate_take(&new_delegate_account, 888); - - // Legacy NetworkLastRegistered entry (index 1) - let mut legacy_network_key = prefix.clone(); - legacy_network_key.push(1u8); - sp_io::storage::set(&legacy_network_key, &111u64.encode()); - - // Legacy LastTxBlock entry (index 2) for an account that already has a new-format value. - let mut legacy_last_key = prefix.clone(); - legacy_last_key.push(2u8); - legacy_last_key.extend_from_slice(&new_last_account.encode()); - sp_io::storage::set(&legacy_last_key, &666u64.encode()); - - // Legacy LastTxBlockChildKeyTake entry (index 3) - let legacy_child_account = U256::from(3); - ChildKeys::::insert( - legacy_child_account, - NetUid::from(0), - vec![(0u64, U256::from(99))], - ); - let mut legacy_child_key = prefix.clone(); - legacy_child_key.push(3u8); - legacy_child_key.extend_from_slice(&legacy_child_account.encode()); - sp_io::storage::set(&legacy_child_key, &333u64.encode()); - - // Legacy LastTxBlockDelegateTake entry (index 4) - let legacy_delegate_account = U256::from(4); - Delegates::::insert(legacy_delegate_account, 500u16); - let mut legacy_delegate_key = prefix.clone(); - legacy_delegate_key.push(4u8); - legacy_delegate_key.extend_from_slice(&legacy_delegate_account.encode()); - sp_io::storage::set(&legacy_delegate_key, &444u64.encode()); - - let weight = crate::migrations::migrate_rate_limit_keys::migrate_rate_limit_keys::(); - assert!( - HasMigrationRun::::get(MIGRATION_NAME.to_vec()), - "Migration should be marked as executed" - ); - assert!(!weight.is_zero(), "Migration weight should be non-zero"); - - // Legacy entries were migrated and cleared. - assert_eq!( - SubtensorModule::get_network_last_lock_block(), - 111u64, - "Network last lock block should match migrated value" - ); - assert!( - sp_io::storage::get(&legacy_network_key).is_none(), - "Legacy network entry should be cleared" - ); - - assert_eq!( - SubtensorModule::get_last_tx_block(&new_last_account), - 666u64, - "LastTxBlock should reflect the merged legacy value" - ); - assert!( - sp_io::storage::get(&legacy_last_key).is_none(), - "Legacy LastTxBlock entry should be cleared" - ); - - assert_eq!( - SubtensorModule::get_last_tx_block_childkey_take(&legacy_child_account), - 333u64, - "Child key take block should be migrated" - ); - assert!( - sp_io::storage::get(&legacy_child_key).is_none(), - "Legacy child take entry should be cleared" - ); - - assert_eq!( - SubtensorModule::get_last_tx_block_delegate_take(&legacy_delegate_account), - 444u64, - "Delegate take block should be migrated" - ); - assert!( - sp_io::storage::get(&legacy_delegate_key).is_none(), - "Legacy delegate take entry should be cleared" - ); - - // New-format entries remain untouched. - assert_eq!( - SubtensorModule::get_last_tx_block_childkey_take(&new_child_account), - 777u64, - "Existing child take entry should be preserved" - ); - assert_eq!( - SubtensorModule::get_last_tx_block_delegate_take(&new_delegate_account), - 888u64, - "Existing delegate take entry should be preserved" - ); - }); -} - #[test] fn test_migrate_fix_staking_hot_keys() { new_test_ext(1).execute_with(|| { @@ -2019,70 +1710,8 @@ fn test_migrate_subnet_limit_to_default() { }); } -#[test] -fn test_migrate_network_lock_reduction_interval_and_decay() { - new_test_ext(0).execute_with(|| { - const FOUR_DAYS: u64 = 28_800; - const EIGHT_DAYS: u64 = 57_600; - const ONE_WEEK_BLOCKS: u64 = 50_400; - - // ── pre ────────────────────────────────────────────────────────────── - assert!( - !HasMigrationRun::::get(b"migrate_network_lock_reduction_interval".to_vec()), - "HasMigrationRun should be false before migration" - ); - - // ensure current_block > 0 - step_block(1); - let current_block_before = Pallet::::get_current_block_as_u64(); - - // ── run migration ──────────────────────────────────────────────────── - let weight = crate::migrations::migrate_network_lock_reduction_interval::migrate_network_lock_reduction_interval::(); - assert!(!weight.is_zero(), "migration weight should be > 0"); - - // ── params & flags ─────────────────────────────────────────────────── - assert_eq!(NetworkLockReductionInterval::::get(), EIGHT_DAYS); - assert_eq!(NetworkRateLimit::::get(), FOUR_DAYS); - assert_eq!( - Pallet::::get_network_last_lock(), - 1_000_000_000_000u64.into(), // 1000 TAO in rao - "last_lock should be 1_000_000_000_000 rao" - ); - - // last_lock_block should be set one week in the future - let last_lock_block = Pallet::::get_network_last_lock_block(); - let expected_block = current_block_before + ONE_WEEK_BLOCKS; - assert_eq!( - last_lock_block, - expected_block, - "last_lock_block should be current + ONE_WEEK_BLOCKS" - ); - - // registration start block should match the same future block - assert_eq!( - NetworkRegistrationStartBlock::::get(), - expected_block, - "NetworkRegistrationStartBlock should equal last_lock_block" - ); - - // lock cost should be 2000 TAO immediately after migration - let lock_cost_now = Pallet::::get_network_lock_cost(); - assert_eq!( - lock_cost_now, - 2_000_000_000_000u64.into(), - "lock cost should be 2000 TAO right after migration" - ); - - assert!( - HasMigrationRun::::get(b"migrate_network_lock_reduction_interval".to_vec()), - "HasMigrationRun should be true after migration" - ); - }); -} - #[test] fn test_migrate_restore_subnet_locked_65_128() { - use sp_runtime::traits::SaturatedConversion; new_test_ext(0).execute_with(|| { let name = b"migrate_restore_subnet_locked".to_vec(); assert!( @@ -2206,116 +1835,6 @@ fn test_migrate_restore_subnet_locked_65_128() { }); } -#[test] -fn test_migrate_network_lock_cost_2500_sets_price_and_decay() { - new_test_ext(0).execute_with(|| { - // ── constants ─────────────────────────────────────────────────────── - const RAO_PER_TAO: u64 = 1_000_000_000; - const TARGET_COST_TAO: u64 = 2_500; - const TARGET_COST_RAO: u64 = TARGET_COST_TAO * RAO_PER_TAO; - const NEW_LAST_LOCK_RAO: u64 = (TARGET_COST_TAO / 2) * RAO_PER_TAO; - - let migration_key = b"migrate_network_lock_cost_2500".to_vec(); - - // ── pre ────────────────────────────────────────────────────────────── - assert!( - !HasMigrationRun::::get(migration_key.clone()), - "HasMigrationRun should be false before migration" - ); - - // Ensure current_block > 0 so mult == 2 in get_network_lock_cost() - step_block(1); - let current_block_before = Pallet::::get_current_block_as_u64(); - - // Snapshot interval to ensure migration doesn't change it - let interval_before = NetworkLockReductionInterval::::get(); - - // ── run migration ──────────────────────────────────────────────────── - let weight = crate::migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::(); - assert!(!weight.is_zero(), "migration weight should be > 0"); - - // ── asserts: params & flags ───────────────────────────────────────── - assert_eq!( - Pallet::::get_network_last_lock(), - NEW_LAST_LOCK_RAO.into(), - "last_lock should be set to 1,250 TAO (in rao)" - ); - assert_eq!( - Pallet::::get_network_last_lock_block(), - current_block_before, - "last_lock_block should be set to the current block" - ); - - // Lock cost should be exactly 2,500 TAO immediately after migration - let lock_cost_now = Pallet::::get_network_lock_cost(); - assert_eq!( - lock_cost_now, - TARGET_COST_RAO.into(), - "lock cost should be 2,500 TAO right after migration" - ); - - // Interval should be unchanged by this migration - assert_eq!( - NetworkLockReductionInterval::::get(), - interval_before, - "lock reduction interval should not be modified by this migration" - ); - - assert!( - HasMigrationRun::::get(migration_key.clone()), - "HasMigrationRun should be true after migration" - ); - - // ── decay check (1 block later) ───────────────────────────────────── - // Expected: cost = max(min_lock, 2*L - floor(L / eff_interval) * delta_blocks) - let eff_interval = Pallet::::get_lock_reduction_interval(); - let per_block_decrement: u64 = if eff_interval == 0 { - 0 - } else { - NEW_LAST_LOCK_RAO / eff_interval - }; - - let min_lock_rao: u64 = Pallet::::get_network_min_lock().to_u64(); - - step_block(1); - let expected_after_1: u64 = - core::cmp::max(min_lock_rao, TARGET_COST_RAO - per_block_decrement); - let lock_cost_after_1 = Pallet::::get_network_lock_cost(); - assert_eq!( - lock_cost_after_1, - expected_after_1.into(), - "lock cost should decay by one per-block step after 1 block" - ); - - // ── idempotency: running the migration again should do nothing ────── - let last_lock_before_rerun = Pallet::::get_network_last_lock(); - let last_lock_block_before_rerun = Pallet::::get_network_last_lock_block(); - let cost_before_rerun = Pallet::::get_network_lock_cost(); - - let _weight2 = crate::migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::(); - - assert!( - HasMigrationRun::::get(migration_key.clone()), - "HasMigrationRun remains true on second run" - ); - assert_eq!( - Pallet::::get_network_last_lock(), - last_lock_before_rerun, - "second run should not modify last_lock" - ); - assert_eq!( - Pallet::::get_network_last_lock_block(), - last_lock_block_before_rerun, - "second run should not modify last_lock_block" - ); - assert_eq!( - Pallet::::get_network_lock_cost(), - cost_before_rerun, - "second run should not change current lock cost" - ); - }); -} - #[test] fn test_migrate_kappa_map_to_default() { new_test_ext(1).execute_with(|| { diff --git a/pallets/subtensor/src/tests/mock.rs b/pallets/subtensor/src/tests/mock.rs index b744c9b771..255ce1a93d 100644 --- a/pallets/subtensor/src/tests/mock.rs +++ b/pallets/subtensor/src/tests/mock.rs @@ -20,6 +20,7 @@ use frame_system as system; use frame_system::{EnsureRoot, RawOrigin, limits, offchain::CreateTransactionBase}; use pallet_subtensor_proxy as pallet_proxy; use pallet_subtensor_utility as pallet_utility; +use rate_limiting_interface::{RateLimitingInterface, TryIntoRateLimitTarget}; use sp_core::{ConstU64, Get, H256, U256, offchain::KeyTypeId}; use sp_runtime::Perbill; use sp_runtime::{ @@ -28,7 +29,7 @@ use sp_runtime::{ }; use sp_std::{cell::RefCell, cmp::Ordering, sync::OnceLock}; use sp_tracing::tracing_subscriber; -use subtensor_runtime_common::{NetUid, TaoCurrency}; +use subtensor_runtime_common::{NetUid, TaoCurrency, rate_limiting::RateLimitUsageKey}; use subtensor_swap_interface::{Order, SwapHandler}; use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt}; type Block = frame_system::mocking::MockBlock; @@ -178,9 +179,6 @@ parameter_types! { pub const InitialMinChildKeyTake: u16 = 0; // 0 %; pub const InitialMaxChildKeyTake: u16 = 11_796; // 18 %; pub const InitialWeightsVersionKey: u16 = 0; - pub const InitialServingRateLimit: u64 = 0; // No limit. - pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing - pub const InitialTxDelegateTakeRateLimit: u64 = 1; // 1 block take rate limit for testing pub const InitialTxChildKeyTakeRateLimit: u64 = 1; // 1 block take rate limit for testing pub const InitialBurn: u64 = 0; pub const InitialMinBurn: u64 = 500_000; @@ -206,7 +204,6 @@ parameter_types! { pub const InitialNetworkMinLockCost: u64 = 100_000_000_000; pub const InitialSubnetOwnerCut: u16 = 0; // 0%. 100% of rewards go to validators + miners. pub const InitialNetworkLockReductionInterval: u64 = 2; // 2 blocks. - pub const InitialNetworkRateLimit: u64 = 0; pub const InitialKeySwapCost: u64 = 1_000_000_000; 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 @@ -264,9 +261,6 @@ impl crate::Config for Test { type InitialWeightsVersionKey = InitialWeightsVersionKey; type InitialMaxDifficulty = InitialMaxDifficulty; type InitialMinDifficulty = InitialMinDifficulty; - type InitialServingRateLimit = InitialServingRateLimit; - type InitialTxRateLimit = InitialTxRateLimit; - type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit; type InitialBurn = InitialBurn; type InitialMaxBurn = InitialMaxBurn; type InitialMinBurn = InitialMinBurn; @@ -277,7 +271,6 @@ impl crate::Config for Test { type InitialNetworkMinLockCost = InitialNetworkMinLockCost; type InitialSubnetOwnerCut = InitialSubnetOwnerCut; type InitialNetworkLockReductionInterval = InitialNetworkLockReductionInterval; - type InitialNetworkRateLimit = InitialNetworkRateLimit; type KeySwapCost = InitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; @@ -298,6 +291,7 @@ impl crate::Config for Test { type GetCommitments = (); type MaxImmuneUidsPercentage = MaxImmuneUidsPercentage; type CommitmentsInterface = CommitmentsI; + type RateLimiting = NoRateLimiting; type EvmKeyAssociateRateLimit = EvmKeyAssociateRateLimit; } @@ -336,6 +330,42 @@ impl CommitmentsInterface for CommitmentsI { fn purge_netuid(_netuid: NetUid) {} } +pub struct NoRateLimiting; + +impl RateLimitingInterface for NoRateLimiting { + type GroupId = subtensor_runtime_common::rate_limiting::GroupId; + type CallMetadata = RuntimeCall; + type Limit = BlockNumber; + type Scope = subtensor_runtime_common::NetUid; + type UsageKey = RateLimitUsageKey; + + fn rate_limit(_target: TargetArg, _scope: Option) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + None + } + + fn last_seen( + _target: TargetArg, + _usage_key: Option, + ) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + None + } + + fn set_last_seen( + _target: TargetArg, + _usage_key: Option, + _block: Option, + ) where + TargetArg: TryIntoRateLimitTarget, + { + } +} + parameter_types! { pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * BlockWeights::get().max_block; @@ -893,7 +923,6 @@ pub fn increase_stake_on_coldkey_hotkey_account( tao_staked, ::SwapInterface::max_price(), false, - false, ) .unwrap(); } @@ -913,10 +942,6 @@ pub fn increase_stake_on_hotkey_account(hotkey: &U256, increment: TaoCurrency, n ); } -pub(crate) fn remove_stake_rate_limit_for_tests(hotkey: &U256, coldkey: &U256, netuid: NetUid) { - StakingOperationRateLimiter::::remove((hotkey, coldkey, netuid)); -} - pub(crate) fn setup_reserves(netuid: NetUid, tao: TaoCurrency, alpha: AlphaCurrency) { SubnetTAO::::set(netuid, tao); SubnetAlphaIn::::set(netuid, alpha); @@ -996,8 +1021,6 @@ pub fn assert_last_event( #[allow(dead_code)] pub fn commit_dummy(who: U256, netuid: NetUid) { - SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // any 32‑byte value is fine; hash is never opened let hash = sp_core::H256::from_low_u64_be(0xDEAD_BEEF); assert_ok!(SubtensorModule::do_commit_weights( diff --git a/pallets/subtensor/src/tests/move_stake.rs b/pallets/subtensor/src/tests/move_stake.rs index dfd9927da4..523adaab1e 100644 --- a/pallets/subtensor/src/tests/move_stake.rs +++ b/pallets/subtensor/src/tests/move_stake.rs @@ -35,7 +35,6 @@ fn test_do_move_success() { stake_amount, ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -112,7 +111,6 @@ fn test_do_move_different_subnets() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -180,7 +178,6 @@ fn test_do_move_nonexistent_subnet() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -284,7 +281,6 @@ fn test_do_move_nonexistent_destination_hotkey() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -349,7 +345,6 @@ fn test_do_move_partial_stake() { total_stake.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -418,7 +413,6 @@ fn test_do_move_multiple_times() { initial_stake.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = @@ -430,7 +424,7 @@ fn test_do_move_multiple_times() { let alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey1, &coldkey, netuid, ); - remove_stake_rate_limit_for_tests(&hotkey1, &coldkey, netuid); + assert_ok!(SubtensorModule::do_move_stake( RuntimeOrigin::signed(coldkey), hotkey1, @@ -442,7 +436,7 @@ fn test_do_move_multiple_times() { let alpha2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey2, &coldkey, netuid, ); - remove_stake_rate_limit_for_tests(&hotkey2, &coldkey, netuid); + assert_ok!(SubtensorModule::do_move_stake( RuntimeOrigin::signed(coldkey), hotkey2, @@ -490,7 +484,6 @@ fn test_do_move_wrong_origin() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -557,7 +550,6 @@ fn test_do_move_same_hotkey_fails() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = @@ -608,7 +600,6 @@ fn test_do_move_event_emission() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -669,7 +660,6 @@ fn test_do_move_storage_updates() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -736,7 +726,6 @@ fn test_move_full_amount_same_netuid() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -804,7 +793,6 @@ fn test_do_move_max_values() { max_stake.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -871,7 +859,6 @@ fn test_moving_too_little_unstakes() { (amount.to_u64() + fee * 2).into() )); - remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, netuid); assert_err!( SubtensorModule::move_stake( RuntimeOrigin::signed(coldkey_account_id), @@ -910,7 +897,6 @@ fn test_do_transfer_success() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -1019,7 +1005,6 @@ fn test_do_transfer_insufficient_stake() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1060,7 +1045,6 @@ fn test_do_transfer_wrong_origin() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1098,7 +1082,6 @@ fn test_do_transfer_minimum_stake_check() { stake_amount, ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1146,7 +1129,6 @@ fn test_do_transfer_different_subnets() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1212,7 +1194,6 @@ fn test_do_swap_success() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha_before = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -1320,7 +1301,6 @@ fn test_do_swap_insufficient_stake() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1355,7 +1335,6 @@ fn test_do_swap_wrong_origin() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1393,7 +1372,6 @@ fn test_do_swap_minimum_stake_check() { total_stake, ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1429,7 +1407,6 @@ fn test_do_swap_same_subnet() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1474,7 +1451,6 @@ fn test_do_swap_partial_stake() { total_stake_tao.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let total_stake_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -1526,7 +1502,6 @@ fn test_do_swap_storage_updates() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1586,7 +1561,6 @@ fn test_do_swap_multiple_times() { initial_stake.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -1596,7 +1570,6 @@ fn test_do_swap_multiple_times() { &hotkey, &coldkey, netuid1, ); if !alpha1.is_zero() { - remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid1); assert_ok!(SubtensorModule::do_swap_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -1612,7 +1585,7 @@ fn test_do_swap_multiple_times() { let (tao_equivalent, _) = mock::swap_alpha_to_tao_ext(netuid2, alpha2, true); // we do this in the loop, because we need the value before the swap expected_alpha = mock::swap_tao_to_alpha(netuid1, tao_equivalent).0; - remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid2); + assert_ok!(SubtensorModule::do_swap_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -1657,7 +1630,6 @@ fn test_do_swap_allows_non_owned_hotkey() { stake_amount.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); let alpha_before = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -1752,7 +1724,7 @@ fn test_move_stake_specific_stake_into_subnet_fail() { // Move stake to destination subnet let (tao_equivalent, _) = mock::swap_alpha_to_tao_ext(origin_netuid, alpha_to_move, true); let (expected_value, _) = mock::swap_tao_to_alpha(netuid, tao_equivalent); - remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, origin_netuid); + assert_ok!(SubtensorModule::move_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, @@ -1783,146 +1755,3 @@ fn test_move_stake_specific_stake_into_subnet_fail() { ); }); } - -#[test] -fn test_transfer_stake_rate_limited() { - new_test_ext(1).execute_with(|| { - let subnet_owner_coldkey = U256::from(1001); - let subnet_owner_hotkey = U256::from(1002); - let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - - let origin_coldkey = U256::from(1); - let destination_coldkey = U256::from(2); - let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get().to_u64() * 10; - - SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); - SubtensorModule::create_account_if_non_existent(&destination_coldkey, &hotkey); - SubtensorModule::stake_into_subnet( - &hotkey, - &origin_coldkey, - netuid, - stake_amount.into(), - ::SwapInterface::max_price(), - true, - false, - ) - .unwrap(); - let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( - &hotkey, - &origin_coldkey, - netuid, - ); - - assert_err!( - SubtensorModule::do_transfer_stake( - RuntimeOrigin::signed(origin_coldkey), - destination_coldkey, - hotkey, - netuid, - netuid, - alpha - ), - Error::::StakingOperationRateLimitExceeded - ); - }); -} - -#[test] -fn test_transfer_stake_doesnt_limit_destination_coldkey() { - new_test_ext(1).execute_with(|| { - let subnet_owner_coldkey = U256::from(1001); - let subnet_owner_hotkey = U256::from(1002); - let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let netuid2 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - - let origin_coldkey = U256::from(1); - let destination_coldkey = U256::from(2); - let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get().to_u64() * 10; - - SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); - SubtensorModule::create_account_if_non_existent(&destination_coldkey, &hotkey); - SubtensorModule::stake_into_subnet( - &hotkey, - &origin_coldkey, - netuid, - stake_amount.into(), - ::SwapInterface::max_price(), - false, - false, - ) - .unwrap(); - let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( - &hotkey, - &origin_coldkey, - netuid, - ); - - assert_ok!(SubtensorModule::do_transfer_stake( - RuntimeOrigin::signed(origin_coldkey), - destination_coldkey, - hotkey, - netuid, - netuid2, - alpha - ),); - - assert!(!StakingOperationRateLimiter::::contains_key(( - hotkey, - destination_coldkey, - netuid2 - ))); - }); -} - -#[test] -fn test_swap_stake_limits_destination_netuid() { - new_test_ext(1).execute_with(|| { - let subnet_owner_coldkey = U256::from(1001); - let subnet_owner_hotkey = U256::from(1002); - let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let netuid2 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - - let origin_coldkey = U256::from(1); - let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get().to_u64() * 10; - - SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); - SubtensorModule::stake_into_subnet( - &hotkey, - &origin_coldkey, - netuid, - stake_amount.into(), - ::SwapInterface::max_price(), - false, - false, - ) - .unwrap(); - let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( - &hotkey, - &origin_coldkey, - netuid, - ); - - assert_ok!(SubtensorModule::do_swap_stake( - RuntimeOrigin::signed(origin_coldkey), - hotkey, - netuid, - netuid2, - alpha - ),); - - assert!(!StakingOperationRateLimiter::::contains_key(( - hotkey, - origin_coldkey, - netuid - ))); - - assert!(StakingOperationRateLimiter::::contains_key(( - hotkey, - origin_coldkey, - netuid2 - ))); - }); -} diff --git a/pallets/subtensor/src/tests/networks.rs b/pallets/subtensor/src/tests/networks.rs index 49114aa3fc..5d590fe513 100644 --- a/pallets/subtensor/src/tests/networks.rs +++ b/pallets/subtensor/src/tests/networks.rs @@ -388,7 +388,6 @@ fn dissolve_clears_all_per_subnet_storages() { PendingOwnerCut::::insert(net, AlphaCurrency::from(1)); BlocksSinceLastStep::::insert(net, 1u64); LastMechansimStepBlock::::insert(net, 1u64); - ServingRateLimit::::insert(net, 1u64); Rho::::insert(net, 1u16); AlphaSigmoidSteepness::::insert(net, 1i16); @@ -399,7 +398,6 @@ fn dissolve_clears_all_per_subnet_storages() { BondsMovingAverage::::insert(net, 1u64); BondsPenalty::::insert(net, 1u16); BondsResetOn::::insert(net, true); - WeightsSetRateLimit::::insert(net, 1u64); ValidatorPruneLen::::insert(net, 1u64); ScalingLawPower::::insert(net, 1u16); TargetRegistrationsPerInterval::::insert(net, 1u16); @@ -548,7 +546,6 @@ fn dissolve_clears_all_per_subnet_storages() { assert!(!PendingOwnerCut::::contains_key(net)); assert!(!BlocksSinceLastStep::::contains_key(net)); assert!(!LastMechansimStepBlock::::contains_key(net)); - assert!(!ServingRateLimit::::contains_key(net)); assert!(!Rho::::contains_key(net)); assert!(!AlphaSigmoidSteepness::::contains_key(net)); @@ -559,7 +556,6 @@ fn dissolve_clears_all_per_subnet_storages() { assert!(!BondsMovingAverage::::contains_key(net)); assert!(!BondsPenalty::::contains_key(net)); assert!(!BondsResetOn::::contains_key(net)); - assert!(!WeightsSetRateLimit::::contains_key(net)); assert!(!ValidatorPruneLen::::contains_key(net)); assert!(!ScalingLawPower::::contains_key(net)); assert!(!TargetRegistrationsPerInterval::::contains_key(net)); @@ -1757,23 +1753,6 @@ fn test_register_subnet_high_lock_cost() { }) } -#[test] -fn test_tempo_greater_than_weight_set_rate_limit() { - new_test_ext(1).execute_with(|| { - let subnet_owner_hotkey = U256::from(1); - let subnet_owner_coldkey = U256::from(2); - - let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - - // Get tempo - let tempo = SubtensorModule::get_tempo(netuid); - - let weights_set_rate_limit = SubtensorModule::get_weights_set_rate_limit(netuid); - - assert!(tempo as u64 >= weights_set_rate_limit); - }) -} - #[allow(clippy::indexing_slicing)] #[test] fn massive_dissolve_refund_and_reregistration_flow_is_lossless_and_cleans_state() { diff --git a/pallets/subtensor/src/tests/serving.rs b/pallets/subtensor/src/tests/serving.rs index b52666bf26..d56787cccd 100644 --- a/pallets/subtensor/src/tests/serving.rs +++ b/pallets/subtensor/src/tests/serving.rs @@ -281,23 +281,19 @@ fn test_axon_serving_rate_limit_exceeded() { placeholder1, placeholder2 )); - SubtensorModule::set_serving_rate_limit(netuid, 2); run_to_block(2); // Go to block 2 - // Needs to be 2 blocks apart, we are only 1 block apart - assert_eq!( - SubtensorModule::serve_axon( - <::RuntimeOrigin>::signed(hotkey_account_id), - netuid, - version, - ip, - port, - ip_type, - protocol, - placeholder1, - placeholder2 - ), - Err(Error::::ServingRateLimitExceeded.into()) - ); + // Rate limiting is enforced by the transaction extension, not the pallet call. + assert_ok!(SubtensorModule::serve_axon( + <::RuntimeOrigin>::signed(hotkey_account_id), + netuid, + version, + ip, + port, + ip_type, + protocol, + placeholder1, + placeholder2 + )); }); } @@ -479,19 +475,15 @@ fn test_prometheus_serving_rate_limit_exceeded() { port, ip_type )); - SubtensorModule::set_serving_rate_limit(netuid, 1); - // Same block, need 1 block to pass - assert_eq!( - SubtensorModule::serve_prometheus( - <::RuntimeOrigin>::signed(hotkey_account_id), - netuid, - version, - ip, - port, - ip_type - ), - Err(Error::::ServingRateLimitExceeded.into()) - ); + // Rate limiting is enforced by the transaction extension, not the pallet call. + assert_ok!(SubtensorModule::serve_prometheus( + <::RuntimeOrigin>::signed(hotkey_account_id), + netuid, + version, + ip, + port, + ip_type + )); }); } diff --git a/pallets/subtensor/src/tests/staking.rs b/pallets/subtensor/src/tests/staking.rs index 4146786709..79ea76fa32 100644 --- a/pallets/subtensor/src/tests/staking.rs +++ b/pallets/subtensor/src/tests/staking.rs @@ -872,7 +872,6 @@ fn test_remove_stake_insufficient_liquidity() { amount_staked.into(), ::SwapInterface::max_price(), false, - false, ) .unwrap(); @@ -954,8 +953,6 @@ fn test_remove_stake_total_issuance_no_change() { let total_fee = mock::swap_alpha_to_tao(netuid, stake).1 + fee; - remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, netuid); - assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, @@ -1060,7 +1057,6 @@ fn test_remove_prev_epoch_stake() { netuid, ); - remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, netuid); let fee = mock::swap_alpha_to_tao(netuid, stake).1 + fee; assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(coldkey_account_id), @@ -1661,7 +1657,7 @@ fn test_clear_small_nominations() { SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold1, netuid); let unstake_amount1 = AlphaCurrency::from(alpha_stake1.to_u64() * 997 / 1000); let small1 = alpha_stake1 - unstake_amount1; - remove_stake_rate_limit_for_tests(&hot1, &cold1, netuid); + assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(cold1), hot1, @@ -1685,7 +1681,7 @@ fn test_clear_small_nominations() { SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold2, netuid); let unstake_amount2 = AlphaCurrency::from(alpha_stake2.to_u64() * 997 / 1000); let small2 = alpha_stake2 - unstake_amount2; - remove_stake_rate_limit_for_tests(&hot1, &cold2, netuid); + assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(cold2), hot1, @@ -1871,7 +1867,7 @@ fn test_delegate_take_can_be_increased() { SubtensorModule::get_min_delegate_take() ); - step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); + step_block(1); // Coldkey / hotkey 0 decreases take to 12.5% assert_ok!(SubtensorModule::do_increase_take( @@ -1945,7 +1941,7 @@ fn test_delegate_take_can_be_increased_to_limit() { SubtensorModule::get_min_delegate_take() ); - step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); + step_block(1); // Coldkey / hotkey 0 tries to increase take to InitialDefaultDelegateTake+1 assert_ok!(SubtensorModule::do_increase_take( @@ -2002,129 +1998,6 @@ fn test_delegate_take_can_not_be_increased_beyond_limit() { }); } -// Test rate-limiting on increase_take -#[test] -fn test_rate_limits_enforced_on_increase_take() { - new_test_ext(1).execute_with(|| { - // Make account - let hotkey0 = U256::from(1); - let coldkey0 = U256::from(3); - - // Add balance - SubtensorModule::add_balance_to_coldkey_account(&coldkey0, 100000); - - // Register the neuron to a new network - let netuid = NetUid::from(1); - add_network(netuid, 1, 0); - register_ok_neuron(netuid, hotkey0, coldkey0, 124124); - - // Coldkey / hotkey 0 become delegates with 9% take - Delegates::::insert(hotkey0, SubtensorModule::get_min_delegate_take()); - assert_eq!( - SubtensorModule::get_hotkey_take(&hotkey0), - SubtensorModule::get_min_delegate_take() - ); - - // Increase take first time - assert_ok!(SubtensorModule::do_increase_take( - RuntimeOrigin::signed(coldkey0), - hotkey0, - SubtensorModule::get_min_delegate_take() + 1 - )); - - // Increase again - assert_eq!( - SubtensorModule::do_increase_take( - RuntimeOrigin::signed(coldkey0), - hotkey0, - SubtensorModule::get_min_delegate_take() + 2 - ), - Err(Error::::DelegateTxRateLimitExceeded.into()) - ); - assert_eq!( - SubtensorModule::get_hotkey_take(&hotkey0), - SubtensorModule::get_min_delegate_take() + 1 - ); - - step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); - - // Can increase after waiting - assert_ok!(SubtensorModule::do_increase_take( - RuntimeOrigin::signed(coldkey0), - hotkey0, - SubtensorModule::get_min_delegate_take() + 2 - )); - assert_eq!( - SubtensorModule::get_hotkey_take(&hotkey0), - SubtensorModule::get_min_delegate_take() + 2 - ); - }); -} - -// Test rate-limiting on an increase take just after a decrease take -// Prevents a Validator from decreasing take and then increasing it immediately after. -#[test] -fn test_rate_limits_enforced_on_decrease_before_increase_take() { - new_test_ext(1).execute_with(|| { - // Make account - let hotkey0 = U256::from(1); - let coldkey0 = U256::from(3); - - // Add balance - SubtensorModule::add_balance_to_coldkey_account(&coldkey0, 100000); - - // Register the neuron to a new network - let netuid = NetUid::from(1); - add_network(netuid, 1, 0); - register_ok_neuron(netuid, hotkey0, coldkey0, 124124); - - // Coldkey / hotkey 0 become delegates with 9% take - Delegates::::insert(hotkey0, SubtensorModule::get_min_delegate_take() + 1); - assert_eq!( - SubtensorModule::get_hotkey_take(&hotkey0), - SubtensorModule::get_min_delegate_take() + 1 - ); - - // Decrease take - assert_ok!(SubtensorModule::do_decrease_take( - RuntimeOrigin::signed(coldkey0), - hotkey0, - SubtensorModule::get_min_delegate_take() - )); // Verify decrease - assert_eq!( - SubtensorModule::get_hotkey_take(&hotkey0), - SubtensorModule::get_min_delegate_take() - ); - - // Increase take immediately after - assert_eq!( - SubtensorModule::do_increase_take( - RuntimeOrigin::signed(coldkey0), - hotkey0, - SubtensorModule::get_min_delegate_take() + 1 - ), - Err(Error::::DelegateTxRateLimitExceeded.into()) - ); // Verify no change - assert_eq!( - SubtensorModule::get_hotkey_take(&hotkey0), - SubtensorModule::get_min_delegate_take() - ); - - step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); - - // Can increase after waiting - assert_ok!(SubtensorModule::do_increase_take( - RuntimeOrigin::signed(coldkey0), - hotkey0, - SubtensorModule::get_min_delegate_take() + 1 - )); // Verify increase - assert_eq!( - SubtensorModule::get_hotkey_take(&hotkey0), - SubtensorModule::get_min_delegate_take() + 1 - ); - }); -} - // cargo test --package pallet-subtensor --lib -- tests::staking::test_get_total_delegated_stake_after_unstaking --exact --show-output #[test] fn test_get_total_delegated_stake_after_unstaking() { @@ -2168,10 +2041,10 @@ fn test_get_total_delegated_stake_after_unstaking() { &delegator, netuid, ); - remove_stake_rate_limit_for_tests(&delegator, &delegate_hotkey, netuid); + // Unstake part of the delegation let unstake_amount_alpha = delegated_alpha / 2.into(); - remove_stake_rate_limit_for_tests(&delegate_hotkey, &delegator, netuid); + assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(delegator), delegate_hotkey, @@ -2424,7 +2297,6 @@ fn test_mining_emission_distribution_validator_valiminer_miner() { &miner_coldkey, stake + ExistentialDeposit::get(), ); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); step_block(subnet_tempo); SubnetOwnerCut::::set(0); // There are two validators and three neurons @@ -3918,7 +3790,7 @@ fn test_remove_stake_limit_ok() { let fee: u64 = (expected_alpha_reduction as f64 * 0.003) as u64; // Remove stake with slippage safety - remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, netuid); + assert_ok!(SubtensorModule::remove_stake_limit( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, @@ -4110,7 +3982,7 @@ fn test_remove_99_9991_per_cent_stake_removes_all() { &coldkey_account_id, netuid, ); - remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, netuid); + let remove_amount = AlphaCurrency::from( (U64F64::from_num(alpha) * U64F64::from_num(0.999991)).to_num::(), ); @@ -4167,7 +4039,7 @@ fn test_remove_99_9989_per_cent_stake_leaves_a_little() { )); // Remove 99.9989% stake - remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, netuid); + let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey_account_id, &coldkey_account_id, @@ -4394,8 +4266,6 @@ fn test_unstake_all_alpha_works() { stake_amount.into() )); - remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); - // Setup the pool so that removing all the TAO will keep liq above min mock::setup_reserves( netuid, @@ -4452,7 +4322,6 @@ fn test_unstake_all_works() { (stake_amount * 10).into(), (stake_amount * 100).into(), ); - remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); // Unstake all alpha to free balance assert_ok!(SubtensorModule::unstake_all( @@ -4506,7 +4375,6 @@ fn test_stake_into_subnet_ok() { amount.into(), TaoCurrency::MAX, false, - false, )); let fee_rate = pallet_subtensor_swap::FeeRate::::get(NetUid::from(netuid)) as f64 / u16::MAX as f64; @@ -4560,7 +4428,6 @@ fn test_stake_into_subnet_low_amount() { amount.into(), TaoCurrency::MAX, false, - false, )); let expected_stake = AlphaCurrency::from(((amount as f64) * 0.997 / current_price) as u64); @@ -4608,7 +4475,6 @@ fn test_unstake_from_subnet_low_amount() { amount.into(), TaoCurrency::MAX, false, - false, )); // Remove stake @@ -4722,7 +4588,6 @@ fn test_unstake_from_subnet_prohibitive_limit() { amount.into(), TaoCurrency::MAX, false, - false, )); // Remove stake @@ -4798,7 +4663,6 @@ fn test_unstake_full_amount() { amount.into(), TaoCurrency::MAX, false, - false, )); // Remove stake @@ -4940,7 +4804,7 @@ fn test_swap_fees_tao_correctness() { &coldkey, netuid, ); - remove_stake_rate_limit_for_tests(&owner_hotkey, &coldkey, netuid); + assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(coldkey), owner_hotkey, @@ -5179,7 +5043,7 @@ fn test_default_min_stake_sufficiency() { let fee_stake = (fee_rate * amount as f64) as u64; let current_price_after_stake = ::SwapInterface::current_alpha_price(netuid.into()); - remove_stake_rate_limit_for_tests(&owner_hotkey, &coldkey, netuid); + let user_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &owner_hotkey, &coldkey, @@ -5263,8 +5127,6 @@ fn test_update_position_fees() { amount.into(), )); - remove_stake_rate_limit_for_tests(&owner_hotkey, &coldkey, netuid); - let user_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &owner_hotkey, &coldkey, @@ -5409,54 +5271,6 @@ fn test_large_swap() { }); } -#[test] -fn test_stake_rate_limits() { - new_test_ext(0).execute_with(|| { - // Create subnet and accounts. - let subnet_owner_coldkey = U256::from(10); - let subnet_owner_hotkey = U256::from(20); - let hot1 = U256::from(1); - let cold1 = U256::from(3); - let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let amount = DefaultMinStake::::get().to_u64() * 10; - let fee = DefaultMinStake::::get().to_u64(); - let init_balance = amount + fee + ExistentialDeposit::get(); - - register_ok_neuron(netuid, hot1, cold1, 0); - Delegates::::insert(hot1, SubtensorModule::get_min_delegate_take()); - assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot1), cold1); - - SubtensorModule::add_balance_to_coldkey_account(&cold1, init_balance); - assert_ok!(SubtensorModule::add_stake( - RuntimeOrigin::signed(cold1), - hot1, - netuid, - (amount + fee).into() - )); - - assert_err!( - SubtensorModule::remove_stake( - RuntimeOrigin::signed(cold1), - hot1, - netuid, - amount.into() - ), - Error::::StakingOperationRateLimitExceeded - ); - - // Test limit clear each block - assert!(StakingOperationRateLimiter::::contains_key(( - hot1, cold1, netuid - ))); - - next_block(); - - assert!(!StakingOperationRateLimiter::::contains_key(( - hot1, cold1, netuid - ))); - }); -} - // cargo test --package pallet-subtensor --lib -- tests::staking::test_add_root_updates_counters --exact --show-output #[test] fn test_add_root_updates_counters() { @@ -5612,7 +5426,6 @@ fn test_staking_records_flow() { amount.into(), TaoCurrency::MAX, false, - false, )); let fee_rate = pallet_subtensor_swap::FeeRate::::get(NetUid::from(netuid)) as f64 / u16::MAX as f64; diff --git a/pallets/subtensor/src/tests/subnet.rs b/pallets/subtensor/src/tests/subnet.rs index a547b30a14..8792278d90 100644 --- a/pallets/subtensor/src/tests/subnet.rs +++ b/pallets/subtensor/src/tests/subnet.rs @@ -618,8 +618,6 @@ fn test_subtoken_enable_trading_ok_with_enable() { stake_amount )); - remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, netuid); - assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, @@ -650,8 +648,6 @@ fn test_subtoken_enable_trading_ok_with_enable() { unstake_amount, )); - remove_stake_rate_limit_for_tests(&hotkey_account_2_id, &coldkey_account_id, netuid); - assert_ok!(SubtensorModule::transfer_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, diff --git a/pallets/subtensor/src/tests/swap_coldkey.rs b/pallets/subtensor/src/tests/swap_coldkey.rs index 9d3bdbfc62..10637057b1 100644 --- a/pallets/subtensor/src/tests/swap_coldkey.rs +++ b/pallets/subtensor/src/tests/swap_coldkey.rs @@ -2437,8 +2437,6 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() { CustomTransactionError::ColdkeyInSwapSchedule.into() ); - remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); - // Remove stake let call = RuntimeCall::SubtensorModule(SubtensorCall::remove_stake { hotkey, diff --git a/pallets/subtensor/src/tests/swap_hotkey.rs b/pallets/subtensor/src/tests/swap_hotkey.rs index 71191d1951..7500b3e919 100644 --- a/pallets/subtensor/src/tests/swap_hotkey.rs +++ b/pallets/subtensor/src/tests/swap_hotkey.rs @@ -710,64 +710,6 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { }); } -// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_hotkey_tx_rate_limit_exceeded --exact --nocapture -#[test] -fn test_swap_hotkey_tx_rate_limit_exceeded() { - new_test_ext(1).execute_with(|| { - let netuid = NetUid::from(1); - let tempo: u16 = 13; - let old_hotkey = U256::from(1); - let new_hotkey_1 = U256::from(2); - let new_hotkey_2 = U256::from(4); - let coldkey = U256::from(3); - let swap_cost = 1_000_000_000u64 * 2; - - let tx_rate_limit = 1; - - // Get the current transaction rate limit - let current_tx_rate_limit = SubtensorModule::get_tx_rate_limit(); - log::info!("current_tx_rate_limit: {current_tx_rate_limit:?}"); - - // Set the transaction rate limit - SubtensorModule::set_tx_rate_limit(tx_rate_limit); - // assert the rate limit is set to 1000 blocks - assert_eq!(SubtensorModule::get_tx_rate_limit(), tx_rate_limit); - - // Setup initial state - add_network(netuid, tempo, 0); - register_ok_neuron(netuid, old_hotkey, coldkey, 0); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, swap_cost); - - // Perform the first swap - assert_ok!(SubtensorModule::do_swap_hotkey( - <::RuntimeOrigin>::signed(coldkey), - &old_hotkey, - &new_hotkey_1, - None - )); - - // Attempt to perform another swap immediately, which should fail due to rate limit - assert_err!( - SubtensorModule::do_swap_hotkey( - <::RuntimeOrigin>::signed(coldkey), - &new_hotkey_1, - &new_hotkey_2, - None - ), - Error::::HotKeySetTxRateLimitExceeded - ); - - // move in time past the rate limit - step_block(1001); - assert_ok!(SubtensorModule::do_swap_hotkey( - <::RuntimeOrigin>::signed(coldkey), - &new_hotkey_1, - &new_hotkey_2, - None - )); - }); -} - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_do_swap_hotkey_err_not_owner --exact --nocapture #[test] fn test_do_swap_hotkey_err_not_owner() { @@ -1003,7 +945,6 @@ fn test_swap_hotkey_error_cases() { // Set up initial state Owner::::insert(old_hotkey, coldkey); TotalNetworks::::put(1); - SubtensorModule::set_last_tx_block(&coldkey, 0); // Test not enough balance let swap_cost = SubtensorModule::get_key_swap_cost(); @@ -1380,51 +1321,6 @@ fn test_swap_hotkey_is_sn_owner_hotkey() { }); } -// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_hotkey_swap_rate_limits --exact --nocapture -#[test] -fn test_swap_hotkey_swap_rate_limits() { - new_test_ext(1).execute_with(|| { - let old_hotkey = U256::from(1); - let new_hotkey = U256::from(2); - let coldkey = U256::from(3); - let netuid = add_dynamic_network(&old_hotkey, &coldkey); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, u64::MAX); - - let last_tx_block = 123; - let delegate_take_block = 4567; - let child_key_take_block = 8910; - - // Set the last tx block for the old hotkey - SubtensorModule::set_last_tx_block(&old_hotkey, last_tx_block); - // Set the last delegate take block for the old hotkey - SubtensorModule::set_last_tx_block_delegate_take(&old_hotkey, delegate_take_block); - // Set last childkey take block for the old hotkey - SubtensorModule::set_last_tx_block_childkey(&old_hotkey, child_key_take_block); - - // Perform the swap - assert_ok!(SubtensorModule::do_swap_hotkey( - RuntimeOrigin::signed(coldkey), - &old_hotkey, - &new_hotkey, - None - )); - - // Check for new hotkey - assert_eq!( - SubtensorModule::get_last_tx_block(&new_hotkey), - last_tx_block - ); - assert_eq!( - SubtensorModule::get_last_tx_block_delegate_take(&new_hotkey), - delegate_take_block - ); - assert_eq!( - SubtensorModule::get_last_tx_block_childkey_take(&new_hotkey), - child_key_take_block - ); - }); -} - #[test] fn test_swap_parent_hotkey_self_loops_in_pending() { new_test_ext(1).execute_with(|| { diff --git a/pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs b/pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs index 6e423c1269..e1f816d199 100644 --- a/pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs +++ b/pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs @@ -751,66 +751,6 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { }); } -// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey_with_subnet -- test_swap_hotkey_tx_rate_limit_exceeded --exact --nocapture -#[test] -fn test_swap_hotkey_tx_rate_limit_exceeded() { - new_test_ext(1).execute_with(|| { - let netuid = NetUid::from(1); - let tempo: u16 = 13; - let old_hotkey = U256::from(1); - let new_hotkey_1 = U256::from(2); - let new_hotkey_2 = U256::from(4); - let coldkey = U256::from(3); - let swap_cost = 1_000_000_000u64 * 2; - - let tx_rate_limit = 1; - - // Get the current transaction rate limit - let current_tx_rate_limit = SubtensorModule::get_tx_rate_limit(); - log::info!("current_tx_rate_limit: {current_tx_rate_limit:?}"); - - // Set the transaction rate limit - SubtensorModule::set_tx_rate_limit(tx_rate_limit); - // assert the rate limit is set to 1000 blocks - assert_eq!(SubtensorModule::get_tx_rate_limit(), tx_rate_limit); - - // Setup initial state - add_network(netuid, tempo, 0); - register_ok_neuron(netuid, old_hotkey, coldkey, 0); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, swap_cost); - - // Perform the first swap - System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get()); - assert_ok!(SubtensorModule::do_swap_hotkey( - RuntimeOrigin::signed(coldkey), - &old_hotkey, - &new_hotkey_1, - Some(netuid) - ),); - - // Attempt to perform another swap immediately, which should fail due to rate limit - assert_err!( - SubtensorModule::do_swap_hotkey( - RuntimeOrigin::signed(coldkey), - &old_hotkey, - &new_hotkey_1, - Some(netuid) - ), - Error::::HotKeySetTxRateLimitExceeded - ); - - // move in time past the rate limit - step_block(1001); - System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get()); - assert_ok!(SubtensorModule::do_swap_hotkey( - <::RuntimeOrigin>::signed(coldkey), - &new_hotkey_1, - &new_hotkey_2, - None - )); - }); -} - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey_with_subnet -- test_do_swap_hotkey_err_not_owner --exact --nocapture #[test] fn test_do_swap_hotkey_err_not_owner() { @@ -997,7 +937,6 @@ fn test_swap_hotkey_error_cases() { // Set up initial state Owner::::insert(old_hotkey, coldkey); TotalNetworks::::put(1); - SubtensorModule::set_last_tx_block(&coldkey, 0); // Test not enough balance let swap_cost = SubtensorModule::get_key_swap_cost(); @@ -1430,53 +1369,6 @@ fn test_swap_hotkey_is_sn_owner_hotkey() { }); } -// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey_with_subnet -- test_swap_hotkey_swap_rate_limits --exact --nocapture -#[test] -fn test_swap_hotkey_swap_rate_limits() { - new_test_ext(1).execute_with(|| { - let old_hotkey = U256::from(1); - let new_hotkey = U256::from(2); - let coldkey = U256::from(3); - - let last_tx_block = 123; - let delegate_take_block = 4567; - let child_key_take_block = 8910; - - let netuid = add_dynamic_network(&old_hotkey, &coldkey); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, u64::MAX); - - // Set the last tx block for the old hotkey - SubtensorModule::set_last_tx_block(&old_hotkey, last_tx_block); - // Set the last delegate take block for the old hotkey - SubtensorModule::set_last_tx_block_delegate_take(&old_hotkey, delegate_take_block); - // Set last childkey take block for the old hotkey - SubtensorModule::set_last_tx_block_childkey(&old_hotkey, child_key_take_block); - - // Perform the swap - System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get()); - assert_ok!(SubtensorModule::do_swap_hotkey( - RuntimeOrigin::signed(coldkey), - &old_hotkey, - &new_hotkey, - Some(netuid) - ),); - - // Check for new hotkey - assert_eq!( - SubtensorModule::get_last_tx_block(&new_hotkey), - last_tx_block - ); - assert_eq!( - SubtensorModule::get_last_tx_block_delegate_take(&new_hotkey), - delegate_take_block - ); - assert_eq!( - SubtensorModule::get_last_tx_block_childkey_take(&new_hotkey), - child_key_take_block - ); - }); -} - #[test] fn test_swap_owner_failed_interval_not_passed() { new_test_ext(1).execute_with(|| { diff --git a/pallets/subtensor/src/tests/weights.rs b/pallets/subtensor/src/tests/weights.rs index 20ace5ee0d..4dce86d0ad 100644 --- a/pallets/subtensor/src/tests/weights.rs +++ b/pallets/subtensor/src/tests/weights.rs @@ -905,68 +905,6 @@ fn test_weights_version_key() { }); } -// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::weights::test_weights_err_setting_weights_too_fast --exact --show-output --nocapture -// Test ensures that uid has validator permit to set non-self weights. -#[test] -fn test_weights_err_setting_weights_too_fast() { - new_test_ext(0).execute_with(|| { - let hotkey_account_id = U256::from(55); - let netuid = NetUid::from(1); - let tempo: u16 = 13; - add_network_disable_commit_reveal(netuid, tempo, 0); - SubtensorModule::set_min_allowed_weights(netuid, 0); - SubtensorModule::set_max_allowed_uids(netuid, 3); - register_ok_neuron(netuid, hotkey_account_id, U256::from(66), 0); - register_ok_neuron(netuid, U256::from(1), U256::from(1), 65555); - register_ok_neuron(netuid, U256::from(2), U256::from(2), 75555); - - let neuron_uid: u16 = - SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey_account_id) - .expect("Not registered."); - SubtensorModule::set_validator_permit_for_uid(netuid, neuron_uid, true); - SubtensorModule::add_balance_to_coldkey_account(&U256::from(66), 1); - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( - &hotkey_account_id, - &(U256::from(66)), - netuid, - 1.into(), - ); - SubtensorModule::set_weights_set_rate_limit(netuid, 10); - assert_eq!(SubtensorModule::get_weights_set_rate_limit(netuid), 10); - - let weights_keys: Vec = vec![1, 2]; - let weight_values: Vec = vec![1, 2]; - - // Note that LastUpdate has default 0 for new uids, but if they have actually set weights on block 0 - // then they are allowed to set weights again once more without a wait restriction, to accommodate the default. - let result = SubtensorModule::set_weights( - RuntimeOrigin::signed(hotkey_account_id), - netuid, - weights_keys.clone(), - weight_values.clone(), - 0, - ); - assert_ok!(result); - run_to_block(1); - - for i in 1..100 { - let result = SubtensorModule::set_weights( - RuntimeOrigin::signed(hotkey_account_id), - netuid, - weights_keys.clone(), - weight_values.clone(), - 0, - ); - if i % 10 == 1 { - assert_ok!(result); - } else { - assert_eq!(result, Err(Error::::SettingWeightsTooFast.into())); - } - run_to_block(i + 1); - } - }); -} - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::weights::test_weights_err_weights_vec_not_equal_size --exact --show-output --nocapture // Test ensures that uids -- weights must have the same size. #[test] @@ -1662,7 +1600,6 @@ fn test_reveal_weights_when_commit_reveal_disabled() { // Register neurons and set up configurations register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); @@ -1723,7 +1660,6 @@ fn test_commit_reveal_weights_ok() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); @@ -1791,7 +1727,6 @@ fn test_commit_reveal_tempo_interval() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); @@ -1927,7 +1862,6 @@ fn test_commit_reveal_hash() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); SubtensorModule::add_balance_to_coldkey_account(&U256::from(0), 1); @@ -2027,7 +1961,6 @@ fn test_commit_reveal_disabled_or_enabled() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); SubtensorModule::add_balance_to_coldkey_account(&U256::from(0), 1); @@ -2106,7 +2039,6 @@ fn test_toggle_commit_reveal_weights_and_set_weights() { SubtensorModule::set_stake_threshold(0); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::add_balance_to_coldkey_account(&U256::from(0), 1); SubtensorModule::add_balance_to_coldkey_account(&U256::from(1), 1); SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( @@ -2189,7 +2121,6 @@ fn test_tempo_change_during_commit_reveal_process() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); @@ -2338,7 +2269,6 @@ fn test_commit_reveal_multiple_commits() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); @@ -2740,7 +2670,6 @@ fn test_expired_commits_handling_in_commit_and_reveal() { add_network(netuid, tempo, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); // Register neurons register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); @@ -2940,7 +2869,6 @@ fn test_reveal_at_exact_epoch() { add_network(netuid, tempo, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); @@ -3104,8 +3032,6 @@ fn test_tempo_and_reveal_period_change_during_commit_reveal_process() { assert_ok!(SubtensorModule::set_reveal_period(netuid, initial_reveal_period)); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); - register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); SubtensorModule::set_stake_threshold(0); @@ -3291,7 +3217,6 @@ fn test_commit_reveal_order_enforcement() { add_network(netuid, tempo, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); @@ -3394,7 +3319,6 @@ fn test_reveal_at_exact_block() { add_network_disable_commit_reveal(netuid, tempo, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); @@ -3550,7 +3474,6 @@ fn test_successful_batch_reveal() { add_network(netuid, tempo, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); @@ -3628,7 +3551,6 @@ fn test_batch_reveal_with_expired_commits() { add_network(netuid, tempo, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); @@ -3880,7 +3802,6 @@ fn test_batch_reveal_before_reveal_period() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); @@ -3938,7 +3859,6 @@ fn test_batch_reveal_after_commits_expired() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); @@ -4045,7 +3965,6 @@ fn test_batch_reveal_with_out_of_order_commits() { add_network(netuid, tempo, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); @@ -4156,7 +4075,6 @@ fn test_highly_concurrent_commits_and_reveals_with_multiple_hotkeys() { // ==== Setup Network ==== add_network(netuid, initial_tempo, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); assert_ok!(SubtensorModule::set_reveal_period(netuid, initial_reveal_period)); SubtensorModule::set_max_registrations_per_block(netuid, u16::MAX); SubtensorModule::set_target_registrations_per_interval(netuid, u16::MAX); @@ -4449,7 +4367,6 @@ fn test_get_reveal_blocks() { register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); @@ -4556,151 +4473,6 @@ fn test_get_reveal_blocks() { }) } -// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::weights::test_commit_weights_rate_limit --exact --show-output --nocapture -#[test] -fn test_commit_weights_rate_limit() { - new_test_ext(1).execute_with(|| { - let netuid = NetUid::from(1); - let uids: Vec = vec![0, 1]; - let weight_values: Vec = vec![10, 10]; - let salt: Vec = vec![1, 2, 3, 4, 5, 6, 7, 8]; - let version_key: u64 = 0; - let hotkey: U256 = U256::from(1); - - let commit_hash: H256 = BlakeTwo256::hash_of(&( - hotkey, - netuid, - uids.clone(), - weight_values.clone(), - salt.clone(), - version_key, - )); - System::set_block_number(11); - - let tempo: u16 = 5; - add_network(netuid, tempo, 0); - - register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000); - register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000); - SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 10); // Rate limit is 10 blocks - SubtensorModule::set_validator_permit_for_uid(netuid, 0, true); - SubtensorModule::set_validator_permit_for_uid(netuid, 1, true); - SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::add_balance_to_coldkey_account(&U256::from(0), 1); - SubtensorModule::add_balance_to_coldkey_account(&U256::from(1), 1); - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( - &(U256::from(0)), - &(U256::from(0)), - netuid, - 1.into(), - ); - SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( - &(U256::from(1)), - &(U256::from(1)), - netuid, - 1.into(), - ); - - let neuron_uid = - SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey).expect("expected uid"); - SubtensorModule::set_last_update_for_uid(NetUidStorageIndex::from(netuid), neuron_uid, 0); - - assert_ok!(SubtensorModule::commit_weights( - RuntimeOrigin::signed(hotkey), - netuid, - commit_hash - )); - - let new_salt: Vec = vec![9; 8]; - let new_commit_hash: H256 = BlakeTwo256::hash_of(&( - hotkey, - netuid, - uids.clone(), - weight_values.clone(), - new_salt.clone(), - version_key, - )); - assert_err!( - SubtensorModule::commit_weights(RuntimeOrigin::signed(hotkey), netuid, new_commit_hash), - Error::::CommittingWeightsTooFast - ); - - step_block(5); - assert_err!( - SubtensorModule::commit_weights(RuntimeOrigin::signed(hotkey), netuid, new_commit_hash), - Error::::CommittingWeightsTooFast - ); - - step_block(5); // Current block is now 21 - - assert_ok!(SubtensorModule::commit_weights( - RuntimeOrigin::signed(hotkey), - netuid, - new_commit_hash - )); - - SubtensorModule::set_commit_reveal_weights_enabled(netuid, false); - let weights_keys: Vec = vec![0]; - let weight_values: Vec = vec![1]; - - assert_err!( - SubtensorModule::set_weights( - RuntimeOrigin::signed(hotkey), - netuid, - weights_keys.clone(), - weight_values.clone(), - 0 - ), - Error::::SettingWeightsTooFast - ); - - step_block(10); - - assert_ok!(SubtensorModule::set_weights( - RuntimeOrigin::signed(hotkey), - netuid, - weights_keys.clone(), - weight_values.clone(), - 0 - )); - - assert_err!( - SubtensorModule::set_weights( - RuntimeOrigin::signed(hotkey), - netuid, - weights_keys.clone(), - weight_values.clone(), - 0 - ), - Error::::SettingWeightsTooFast - ); - - step_block(5); - - assert_err!( - SubtensorModule::set_weights( - RuntimeOrigin::signed(hotkey), - netuid, - weights_keys.clone(), - weight_values.clone(), - 0 - ), - Error::::SettingWeightsTooFast - ); - - step_block(5); - - assert_ok!(SubtensorModule::set_weights( - RuntimeOrigin::signed(hotkey), - netuid, - weights_keys.clone(), - weight_values.clone(), - 0 - )); - }); -} - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::weights::tlock_encrypt_decrypt_drand_quicknet_works --exact --show-output --nocapture #[test] pub fn tlock_encrypt_decrypt_drand_quicknet_works() { @@ -4764,7 +4536,6 @@ fn test_reveal_crv3_commits_success() { register_ok_neuron(netuid, hotkey1, U256::from(3), 100_000); register_ok_neuron(netuid, hotkey2, U256::from(4), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); @@ -4916,7 +4687,6 @@ fn test_reveal_crv3_commits_cannot_reveal_after_reveal_epoch() { add_network(netuid, 5, 0); register_ok_neuron(netuid, hotkey1, U256::from(3), 100_000); register_ok_neuron(netuid, hotkey2, U256::from(4), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); @@ -5042,7 +4812,6 @@ fn test_do_commit_crv3_weights_success() { add_network(netuid, 5, 0); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::do_commit_timelocked_weights( @@ -5078,7 +4847,6 @@ fn test_do_commit_crv3_weights_disabled() { add_network(netuid, 5, 0); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_commit_reveal_weights_enabled(netuid, false); assert_err!( @@ -5108,7 +4876,6 @@ fn test_do_commit_crv3_weights_hotkey_not_registered() { add_network(netuid, 5, 0); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_err!( @@ -5126,79 +4893,6 @@ fn test_do_commit_crv3_weights_hotkey_not_registered() { }); } -// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::weights::test_do_commit_crv3_weights_committing_too_fast --exact --show-output --nocapture -#[test] -fn test_do_commit_crv3_weights_committing_too_fast() { - new_test_ext(1).execute_with(|| { - let netuid = NetUid::from(1); - let hotkey: AccountId = U256::from(1); - let commit_data_1: Vec = vec![1, 2, 3]; - let commit_data_2: Vec = vec![4, 5, 6]; - let reveal_round: u64 = 1000; - - add_network(netuid, 5, 0); - register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 5); - SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - let neuron_uid = - SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey).expect("Expected uid"); - SubtensorModule::set_last_update_for_uid(NetUidStorageIndex::from(netuid), neuron_uid, 0); - - assert_ok!(SubtensorModule::do_commit_timelocked_weights( - RuntimeOrigin::signed(hotkey), - netuid, - commit_data_1 - .clone() - .try_into() - .expect("Failed to convert commit data into bounded vector"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - )); - - assert_err!( - SubtensorModule::do_commit_timelocked_weights( - RuntimeOrigin::signed(hotkey), - netuid, - commit_data_2 - .clone() - .try_into() - .expect("Failed to convert commit data into bounded vector"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - ), - Error::::CommittingWeightsTooFast - ); - - step_block(2); - - assert_err!( - SubtensorModule::do_commit_timelocked_weights( - RuntimeOrigin::signed(hotkey), - netuid, - commit_data_2 - .clone() - .try_into() - .expect("Failed to convert commit data into bounded vector"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - ), - Error::::CommittingWeightsTooFast - ); - - step_block(3); - - assert_ok!(SubtensorModule::do_commit_timelocked_weights( - RuntimeOrigin::signed(hotkey), - netuid, - commit_data_2 - .try_into() - .expect("Failed to convert commit data into bounded vector"), - reveal_round, - SubtensorModule::get_commit_reveal_weights_version() - )); - }); -} - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::weights::test_do_commit_crv3_weights_too_many_unrevealed_commits --exact --show-output --nocapture #[test] fn test_do_commit_crv3_weights_too_many_unrevealed_commits() { @@ -5212,7 +4906,6 @@ fn test_do_commit_crv3_weights_too_many_unrevealed_commits() { register_ok_neuron(netuid, hotkey1, U256::from(2), 100_000); register_ok_neuron(netuid, hotkey2, U256::from(3), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); // Hotkey1 submits 10 commits successfully for i in 0..10 { @@ -5320,7 +5013,6 @@ fn test_reveal_crv3_commits_decryption_failure() { add_network(netuid, 5, 0); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); let commit_bytes: Vec = vec![0xff; 100]; @@ -5376,7 +5068,6 @@ fn test_reveal_crv3_commits_multiple_commits_some_fail_some_succeed() { register_ok_neuron(netuid, hotkey2, U256::from(4), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 1)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); // Prepare a valid payload for hotkey1 let neuron_uid1 = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey1) @@ -5499,7 +5190,6 @@ fn test_reveal_crv3_commits_do_set_weights_failure() { register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); // Prepare payload with mismatched uids and values lengths let version_key = SubtensorModule::get_weights_version_key(netuid); @@ -5585,7 +5275,6 @@ fn test_reveal_crv3_commits_payload_decoding_failure() { register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); let invalid_payload = vec![0u8; 10]; // Not a valid encoding of WeightsTlockPayload @@ -5663,7 +5352,6 @@ fn test_reveal_crv3_commits_signature_deserialization_failure() { register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); let version_key = SubtensorModule::get_weights_version_key(netuid); let payload = WeightsTlockPayload { @@ -5744,7 +5432,6 @@ fn test_do_commit_crv3_weights_commit_size_exceeds_limit() { add_network(netuid, 5, 0); register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); let max_commit_size = MAX_CRV3_COMMIT_SIZE_BYTES as usize; let commit_data_exceeding: Vec = vec![0u8; max_commit_size + 1]; // Exceeds max size @@ -5785,7 +5472,6 @@ fn test_reveal_crv3_commits_with_empty_commit_queue() { add_network(netuid, 5, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); step_epochs(2, netuid); @@ -5809,7 +5495,6 @@ fn test_reveal_crv3_commits_with_incorrect_identity_message() { register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 1)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); // Prepare a valid payload but use incorrect identity message during encryption let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey) @@ -5897,7 +5582,6 @@ fn test_multiple_commits_by_same_hotkey_within_limit() { register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 1)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); for i in 0..10 { let commit_data: Vec = vec![i; 5]; @@ -5936,7 +5620,6 @@ fn test_reveal_crv3_commits_removes_past_epoch_commits() { register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 1)); // reveal_period = 1 epoch - SubtensorModule::set_weights_set_rate_limit(netuid, 0); // --------------------------------------------------------------------- // Put dummy commits into the two epochs immediately *before* current. @@ -6001,7 +5684,6 @@ fn test_reveal_crv3_commits_multiple_valid_commits_all_processed() { add_network(netuid, 5, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 1)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_stake_threshold(0); SubtensorModule::set_max_registrations_per_block(netuid, 100); SubtensorModule::set_target_registrations_per_interval(netuid, 100); @@ -6117,7 +5799,6 @@ fn test_reveal_crv3_commits_max_neurons() { add_network(netuid, 5, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 1)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_stake_threshold(0); SubtensorModule::set_max_registrations_per_block(netuid, 10_000); SubtensorModule::set_target_registrations_per_interval(netuid, 10_000); @@ -6343,7 +6024,6 @@ fn test_reveal_crv3_commits_hotkey_check() { register_ok_neuron(netuid, hotkey1, U256::from(3), 100_000); register_ok_neuron(netuid, hotkey2, U256::from(4), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); @@ -6460,7 +6140,6 @@ fn test_reveal_crv3_commits_hotkey_check() { register_ok_neuron(netuid, hotkey1, U256::from(3), 100_000); register_ok_neuron(netuid, hotkey2, U256::from(4), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); @@ -6612,7 +6291,6 @@ fn test_reveal_crv3_commits_retry_on_missing_pulse() { register_ok_neuron(netuid, hotkey, U256::from(3), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_stake_threshold(0); let uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey).unwrap(); @@ -6727,7 +6405,6 @@ fn test_reveal_crv3_commits_legacy_payload_success() { register_ok_neuron(netuid, hotkey2, U256::from(4), 100_000); SubtensorModule::set_stake_threshold(0); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); assert_ok!(SubtensorModule::set_reveal_period(netuid, 3)); diff --git a/pallets/subtensor/src/transaction_extension.rs b/pallets/subtensor/src/transaction_extension.rs index cf1d410ea9..e227e2d483 100644 --- a/pallets/subtensor/src/transaction_extension.rs +++ b/pallets/subtensor/src/transaction_extension.rs @@ -70,9 +70,6 @@ where CustomTransactionError::HotKeyNotRegisteredInNetwork.into() } Error::::InvalidIpAddress => CustomTransactionError::InvalidIpAddress.into(), - Error::::ServingRateLimitExceeded => { - CustomTransactionError::ServingRateLimitExceeded.into() - } Error::::InvalidPort => CustomTransactionError::InvalidPort.into(), _ => CustomTransactionError::BadRequest.into(), }) diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index 10fc0535f0..6fd8d22ff6 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -1,12 +1,15 @@ use super::*; use crate::Error; use crate::system::{ensure_signed, ensure_signed_or_root, pallet_prelude::BlockNumberFor}; +use rate_limiting_interface::RateLimitingInterface; use safe_math::*; use sp_core::Get; use sp_core::U256; -use sp_runtime::Saturating; +use sp_runtime::{SaturatedConversion, Saturating}; use substrate_fixed::types::{I32F32, I64F64, U64F64, U96F32}; -use subtensor_runtime_common::{AlphaCurrency, NetUid, NetUidStorageIndex, TaoCurrency}; +use subtensor_runtime_common::{ + AlphaCurrency, NetUid, NetUidStorageIndex, TaoCurrency, rate_limiting, +}; impl Pallet { pub fn ensure_subnet_owner_or_root( @@ -106,27 +109,34 @@ impl Pallet { Tempo::::insert(netuid, tempo); Self::deposit_event(Event::TempoSet(netuid, tempo)); } + pub fn set_last_adjustment_block(netuid: NetUid, last_adjustment_block: u64) { LastAdjustmentBlock::::insert(netuid, last_adjustment_block); } + pub fn set_blocks_since_last_step(netuid: NetUid, blocks_since_last_step: u64) { BlocksSinceLastStep::::insert(netuid, blocks_since_last_step); } + pub fn set_registrations_this_block(netuid: NetUid, registrations_this_block: u16) { RegistrationsThisBlock::::insert(netuid, registrations_this_block); } + pub fn set_last_mechanism_step_block(netuid: NetUid, last_mechanism_step_block: u64) { LastMechansimStepBlock::::insert(netuid, last_mechanism_step_block); } + pub fn set_registrations_this_interval(netuid: NetUid, registrations_this_interval: u16) { RegistrationsThisInterval::::insert(netuid, registrations_this_interval); } + pub fn set_pow_registrations_this_interval( netuid: NetUid, pow_registrations_this_interval: u16, ) { POWRegistrationsThisInterval::::insert(netuid, pow_registrations_this_interval); } + pub fn set_burn_registrations_this_interval( netuid: NetUid, burn_registrations_this_interval: u16, @@ -140,6 +150,7 @@ impl Pallet { pub fn get_total_issuance() -> TaoCurrency { TotalIssuance::::get() } + pub fn get_current_block_as_u64() -> u64 { TryInto::try_into(>::block_number()) .ok() @@ -152,24 +163,31 @@ impl Pallet { pub fn get_rank(netuid: NetUid) -> Vec { Rank::::get(netuid) } + pub fn get_trust(netuid: NetUid) -> Vec { Trust::::get(netuid) } + pub fn get_active(netuid: NetUid) -> Vec { Active::::get(netuid) } + pub fn get_emission(netuid: NetUid) -> Vec { Emission::::get(netuid) } + pub fn get_consensus(netuid: NetUid) -> Vec { Consensus::::get(netuid) } + pub fn get_incentive(netuid: NetUidStorageIndex) -> Vec { Incentive::::get(netuid) } + pub fn get_dividends(netuid: NetUid) -> Vec { Dividends::::get(netuid) } + /// Fetch LastUpdate for `netuid` and ensure its length is at least `get_subnetwork_n(netuid)`, /// padding with zeros if needed. Returns the (possibly padded) vector. pub fn get_last_update(netuid_index: NetUidStorageIndex) -> Vec { @@ -181,12 +199,15 @@ impl Pallet { } v } + pub fn get_pruning_score(netuid: NetUid) -> Vec { PruningScores::::get(netuid) } + pub fn get_validator_trust(netuid: NetUid) -> Vec { ValidatorTrust::::get(netuid) } + pub fn get_validator_permit(netuid: NetUid) -> Vec { ValidatorPermit::::get(netuid) } @@ -202,6 +223,7 @@ impl Pallet { *updated_last_update = last_update; LastUpdate::::insert(netuid, updated_last_update_vec); } + pub fn set_active_for_uid(netuid: NetUid, uid: u16, active: bool) { let mut updated_active_vec = Self::get_active(netuid); let Some(updated_active) = updated_active_vec.get_mut(uid as usize) else { @@ -210,6 +232,7 @@ impl Pallet { *updated_active = active; Active::::insert(netuid, updated_active_vec); } + pub fn set_validator_permit_for_uid(netuid: NetUid, uid: u16, validator_permit: bool) { let mut updated_validator_permits = Self::get_validator_permit(netuid); let Some(updated_validator_permit) = updated_validator_permits.get_mut(uid as usize) else { @@ -218,6 +241,7 @@ impl Pallet { *updated_validator_permit = validator_permit; ValidatorPermit::::insert(netuid, updated_validator_permits); } + pub fn set_stake_threshold(min_stake: u64) { StakeThreshold::::put(min_stake); Self::deposit_event(Event::StakeThresholdSet(min_stake)); @@ -227,46 +251,57 @@ impl Pallet { let vec = Rank::::get(netuid); vec.get(uid as usize).copied().unwrap_or(0) } + pub fn get_trust_for_uid(netuid: NetUid, uid: u16) -> u16 { let vec = Trust::::get(netuid); vec.get(uid as usize).copied().unwrap_or(0) } + pub fn get_emission_for_uid(netuid: NetUid, uid: u16) -> AlphaCurrency { let vec = Emission::::get(netuid); vec.get(uid as usize).copied().unwrap_or_default() } + pub fn get_active_for_uid(netuid: NetUid, uid: u16) -> bool { let vec = Active::::get(netuid); vec.get(uid as usize).copied().unwrap_or(false) } + pub fn get_consensus_for_uid(netuid: NetUid, uid: u16) -> u16 { let vec = Consensus::::get(netuid); vec.get(uid as usize).copied().unwrap_or(0) } + pub fn get_incentive_for_uid(netuid: NetUidStorageIndex, uid: u16) -> u16 { let vec = Incentive::::get(netuid); vec.get(uid as usize).copied().unwrap_or(0) } + pub fn get_dividends_for_uid(netuid: NetUid, uid: u16) -> u16 { let vec = Dividends::::get(netuid); vec.get(uid as usize).copied().unwrap_or(0) } + pub fn get_last_update_for_uid(netuid: NetUidStorageIndex, uid: u16) -> u64 { let vec = LastUpdate::::get(netuid); vec.get(uid as usize).copied().unwrap_or(0) } + pub fn get_pruning_score_for_uid(netuid: NetUid, uid: u16) -> u16 { let vec = PruningScores::::get(netuid); vec.get(uid as usize).copied().unwrap_or(u16::MAX) } + pub fn get_validator_trust_for_uid(netuid: NetUid, uid: u16) -> u16 { let vec = ValidatorTrust::::get(netuid); vec.get(uid as usize).copied().unwrap_or(0) } + pub fn get_validator_permit_for_uid(netuid: NetUid, uid: u16) -> bool { let vec = ValidatorPermit::::get(netuid); vec.get(uid as usize).copied().unwrap_or(false) } + pub fn get_stake_threshold() -> u64 { StakeThreshold::::get() } @@ -277,33 +312,43 @@ impl Pallet { pub fn get_tempo(netuid: NetUid) -> u16 { Tempo::::get(netuid) } + pub fn get_last_adjustment_block(netuid: NetUid) -> u64 { LastAdjustmentBlock::::get(netuid) } + pub fn get_blocks_since_last_step(netuid: NetUid) -> u64 { BlocksSinceLastStep::::get(netuid) } + pub fn get_difficulty(netuid: NetUid) -> U256 { U256::from(Self::get_difficulty_as_u64(netuid)) } + pub fn get_registrations_this_block(netuid: NetUid) -> u16 { RegistrationsThisBlock::::get(netuid) } + pub fn get_last_mechanism_step_block(netuid: NetUid) -> u64 { LastMechansimStepBlock::::get(netuid) } + pub fn get_registrations_this_interval(netuid: NetUid) -> u16 { RegistrationsThisInterval::::get(netuid) } + pub fn get_pow_registrations_this_interval(netuid: NetUid) -> u16 { POWRegistrationsThisInterval::::get(netuid) } + pub fn get_burn_registrations_this_interval(netuid: NetUid) -> u16 { BurnRegistrationsThisInterval::::get(netuid) } + pub fn get_neuron_block_at_registration(netuid: NetUid, neuron_uid: u16) -> u64 { BlockAtRegistration::::get(netuid, neuron_uid) } + /// Returns the minimum number of non-immortal & non-immune UIDs that must remain in a subnet. pub fn get_min_non_immune_uids(netuid: NetUid) -> u16 { MinNonImmuneUids::::get(netuid) @@ -340,6 +385,7 @@ impl Pallet { pub fn recycle_tao(amount: TaoCurrency) { TotalIssuance::::put(TotalIssuance::::get().saturating_sub(amount)); } + pub fn increase_issuance(amount: TaoCurrency) { TotalIssuance::::put(TotalIssuance::::get().saturating_add(amount)); } @@ -347,9 +393,11 @@ impl Pallet { pub fn set_subnet_locked_balance(netuid: NetUid, amount: TaoCurrency) { SubnetLocked::::insert(netuid, amount); } + pub fn get_subnet_locked_balance(netuid: NetUid) -> TaoCurrency { SubnetLocked::::get(netuid) } + pub fn get_total_subnet_locked() -> TaoCurrency { let mut total_subnet_locked: u64 = 0; for (_, locked) in SubnetLocked::::iter() { @@ -367,58 +415,54 @@ impl Pallet { // ======================== // Configure tx rate limiting - pub fn get_tx_rate_limit() -> u64 { - TxRateLimit::::get() - } - pub fn set_tx_rate_limit(tx_rate_limit: u64) { - TxRateLimit::::put(tx_rate_limit); - Self::deposit_event(Event::TxRateLimitSet(tx_rate_limit)); - } - pub fn get_tx_delegate_take_rate_limit() -> u64 { - TxDelegateTakeRateLimit::::get() - } - pub fn set_tx_delegate_take_rate_limit(tx_rate_limit: u64) { - TxDelegateTakeRateLimit::::put(tx_rate_limit); - Self::deposit_event(Event::TxDelegateTakeRateLimitSet(tx_rate_limit)); - } pub fn set_min_delegate_take(take: u16) { MinDelegateTake::::put(take); Self::deposit_event(Event::MinDelegateTakeSet(take)); } + pub fn set_max_delegate_take(take: u16) { MaxDelegateTake::::put(take); Self::deposit_event(Event::MaxDelegateTakeSet(take)); } + pub fn get_min_delegate_take() -> u16 { MinDelegateTake::::get() } + pub fn get_max_delegate_take() -> u16 { MaxDelegateTake::::get() } + pub fn get_default_delegate_take() -> u16 { // Default to maximum MaxDelegateTake::::get() } + // get_default_childkey_take pub fn get_default_childkey_take() -> u16 { // Default to maximum MinChildkeyTake::::get() } + pub fn get_tx_childkey_take_rate_limit() -> u64 { TxChildkeyTakeRateLimit::::get() } + pub fn set_tx_childkey_take_rate_limit(tx_rate_limit: u64) { TxChildkeyTakeRateLimit::::put(tx_rate_limit); Self::deposit_event(Event::TxChildKeyTakeRateLimitSet(tx_rate_limit)); } + pub fn set_min_childkey_take(take: u16) { MinChildkeyTake::::put(take); Self::deposit_event(Event::MinChildKeyTakeSet(take)); } + pub fn set_max_childkey_take(take: u16) { MaxChildkeyTake::::put(take); Self::deposit_event(Event::MaxChildKeyTakeSet(take)); } + pub fn get_min_childkey_take() -> u16 { MinChildkeyTake::::get() } @@ -428,16 +472,15 @@ impl Pallet { } pub fn get_serving_rate_limit(netuid: NetUid) -> u64 { - ServingRateLimit::::get(netuid) - } - pub fn set_serving_rate_limit(netuid: NetUid, serving_rate_limit: u64) { - ServingRateLimit::::insert(netuid, serving_rate_limit); - Self::deposit_event(Event::ServingRateLimitSet(netuid, serving_rate_limit)); + T::RateLimiting::rate_limit(rate_limiting::GROUP_SERVE, Some(netuid)) + .unwrap_or_default() + .saturated_into() } pub fn get_min_difficulty(netuid: NetUid) -> u64 { MinDifficulty::::get(netuid) } + pub fn set_min_difficulty(netuid: NetUid, min_difficulty: u64) { MinDifficulty::::insert(netuid, min_difficulty); Self::deposit_event(Event::MinDifficultySet(netuid, min_difficulty)); @@ -446,6 +489,7 @@ impl Pallet { pub fn get_max_difficulty(netuid: NetUid) -> u64 { MaxDifficulty::::get(netuid) } + pub fn set_max_difficulty(netuid: NetUid, max_difficulty: u64) { MaxDifficulty::::insert(netuid, max_difficulty); Self::deposit_event(Event::MaxDifficultySet(netuid, max_difficulty)); @@ -454,25 +498,22 @@ impl Pallet { pub fn get_weights_version_key(netuid: NetUid) -> u64 { WeightsVersionKey::::get(netuid) } + pub fn set_weights_version_key(netuid: NetUid, weights_version_key: u64) { WeightsVersionKey::::insert(netuid, weights_version_key); Self::deposit_event(Event::WeightsVersionKeySet(netuid, weights_version_key)); } pub fn get_weights_set_rate_limit(netuid: NetUid) -> u64 { - WeightsSetRateLimit::::get(netuid) - } - pub fn set_weights_set_rate_limit(netuid: NetUid, weights_set_rate_limit: u64) { - WeightsSetRateLimit::::insert(netuid, weights_set_rate_limit); - Self::deposit_event(Event::WeightsSetRateLimitSet( - netuid, - weights_set_rate_limit, - )); + T::RateLimiting::rate_limit(rate_limiting::GROUP_WEIGHTS_SET, Some(netuid)) + .unwrap_or_default() + .saturated_into() } pub fn get_adjustment_interval(netuid: NetUid) -> u16 { AdjustmentInterval::::get(netuid) } + pub fn set_adjustment_interval(netuid: NetUid, adjustment_interval: u16) { AdjustmentInterval::::insert(netuid, adjustment_interval); Self::deposit_event(Event::AdjustmentIntervalSet(netuid, adjustment_interval)); @@ -481,6 +522,7 @@ impl Pallet { pub fn get_adjustment_alpha(netuid: NetUid) -> u64 { AdjustmentAlpha::::get(netuid) } + pub fn set_adjustment_alpha(netuid: NetUid, adjustment_alpha: u64) { AdjustmentAlpha::::insert(netuid, adjustment_alpha); Self::deposit_event(Event::AdjustmentAlphaSet(netuid, adjustment_alpha)); @@ -494,6 +536,7 @@ impl Pallet { pub fn get_scaling_law_power(netuid: NetUid) -> u16 { ScalingLawPower::::get(netuid) } + pub fn set_scaling_law_power(netuid: NetUid, scaling_law_power: u16) { ScalingLawPower::::insert(netuid, scaling_law_power); Self::deposit_event(Event::ScalingLawPowerSet(netuid, scaling_law_power)); @@ -507,10 +550,12 @@ impl Pallet { pub fn get_immunity_period(netuid: NetUid) -> u16 { ImmunityPeriod::::get(netuid) } + pub fn set_immunity_period(netuid: NetUid, immunity_period: u16) { ImmunityPeriod::::insert(netuid, immunity_period); Self::deposit_event(Event::ImmunityPeriodSet(netuid, immunity_period)); } + /// Check if a neuron is in immunity based on the current block pub fn get_neuron_is_immune(netuid: NetUid, uid: u16) -> bool { let registered_at = Self::get_neuron_block_at_registration(netuid, uid); @@ -530,6 +575,7 @@ impl Pallet { pub fn get_min_allowed_uids(netuid: NetUid) -> u16 { MinAllowedUids::::get(netuid) } + pub fn set_min_allowed_uids(netuid: NetUid, min_allowed: u16) { MinAllowedUids::::insert(netuid, min_allowed); Self::deposit_event(Event::MinAllowedUidsSet(netuid, min_allowed)); @@ -538,6 +584,7 @@ impl Pallet { pub fn get_max_allowed_uids(netuid: NetUid) -> u16 { MaxAllowedUids::::get(netuid) } + pub fn set_max_allowed_uids(netuid: NetUid, max_allowed: u16) { MaxAllowedUids::::insert(netuid, max_allowed); Self::deposit_event(Event::MaxAllowedUidsSet(netuid, max_allowed)); @@ -546,27 +593,34 @@ impl Pallet { pub fn get_kappa(netuid: NetUid) -> u16 { Kappa::::get(netuid) } + pub fn set_kappa(netuid: NetUid, kappa: u16) { Kappa::::insert(netuid, kappa); Self::deposit_event(Event::KappaSet(netuid, kappa)); } + pub fn get_commit_reveal_weights_enabled(netuid: NetUid) -> bool { CommitRevealWeightsEnabled::::get(netuid) } + pub fn set_commit_reveal_weights_enabled(netuid: NetUid, enabled: bool) { CommitRevealWeightsEnabled::::set(netuid, enabled); Self::deposit_event(Event::CommitRevealEnabled(netuid, enabled)); } + pub fn get_commit_reveal_weights_version() -> u16 { CommitRevealWeightsVersion::::get() } + pub fn set_commit_reveal_weights_version(version: u16) { CommitRevealWeightsVersion::::set(version); Self::deposit_event(Event::CommitRevealVersionSet(version)); } + pub fn get_rho(netuid: NetUid) -> u16 { Rho::::get(netuid) } + pub fn set_rho(netuid: NetUid, rho: u16) { Rho::::insert(netuid, rho); } @@ -574,6 +628,7 @@ impl Pallet { pub fn get_activity_cutoff(netuid: NetUid) -> u16 { ActivityCutoff::::get(netuid) } + pub fn set_activity_cutoff(netuid: NetUid, activity_cutoff: u16) { ActivityCutoff::::insert(netuid, activity_cutoff); Self::deposit_event(Event::ActivityCutoffSet(netuid, activity_cutoff)); @@ -583,6 +638,7 @@ impl Pallet { pub fn get_network_registration_allowed(netuid: NetUid) -> bool { NetworkRegistrationAllowed::::get(netuid) } + pub fn set_network_registration_allowed(netuid: NetUid, registration_allowed: bool) { NetworkRegistrationAllowed::::insert(netuid, registration_allowed); Self::deposit_event(Event::RegistrationAllowed(netuid, registration_allowed)); @@ -591,6 +647,7 @@ impl Pallet { pub fn get_network_pow_registration_allowed(netuid: NetUid) -> bool { NetworkPowRegistrationAllowed::::get(netuid) } + pub fn set_network_pow_registration_allowed(netuid: NetUid, registration_allowed: bool) { NetworkPowRegistrationAllowed::::insert(netuid, registration_allowed); Self::deposit_event(Event::PowRegistrationAllowed(netuid, registration_allowed)); @@ -599,6 +656,7 @@ impl Pallet { pub fn get_target_registrations_per_interval(netuid: NetUid) -> u16 { TargetRegistrationsPerInterval::::get(netuid) } + pub fn set_target_registrations_per_interval( netuid: NetUid, target_registrations_per_interval: u16, @@ -613,6 +671,7 @@ impl Pallet { pub fn get_burn(netuid: NetUid) -> TaoCurrency { Burn::::get(netuid) } + pub fn set_burn(netuid: NetUid, burn: TaoCurrency) { Burn::::insert(netuid, burn); } @@ -620,6 +679,7 @@ impl Pallet { pub fn get_min_burn(netuid: NetUid) -> TaoCurrency { MinBurn::::get(netuid) } + pub fn set_min_burn(netuid: NetUid, min_burn: TaoCurrency) { MinBurn::::insert(netuid, min_burn); Self::deposit_event(Event::MinBurnSet(netuid, min_burn)); @@ -628,6 +688,7 @@ impl Pallet { pub fn get_max_burn(netuid: NetUid) -> TaoCurrency { MaxBurn::::get(netuid) } + pub fn set_max_burn(netuid: NetUid, max_burn: TaoCurrency) { MaxBurn::::insert(netuid, max_burn); Self::deposit_event(Event::MaxBurnSet(netuid, max_burn)); @@ -636,6 +697,7 @@ impl Pallet { pub fn get_difficulty_as_u64(netuid: NetUid) -> u64 { Difficulty::::get(netuid) } + pub fn set_difficulty(netuid: NetUid, difficulty: u64) { Difficulty::::insert(netuid, difficulty); Self::deposit_event(Event::DifficultySet(netuid, difficulty)); @@ -644,6 +706,7 @@ impl Pallet { pub fn get_max_allowed_validators(netuid: NetUid) -> u16 { MaxAllowedValidators::::get(netuid) } + pub fn set_max_allowed_validators(netuid: NetUid, max_allowed_validators: u16) { MaxAllowedValidators::::insert(netuid, max_allowed_validators); Self::deposit_event(Event::MaxAllowedValidatorsSet( @@ -655,6 +718,7 @@ impl Pallet { pub fn get_bonds_moving_average(netuid: NetUid) -> u64 { BondsMovingAverage::::get(netuid) } + pub fn set_bonds_moving_average(netuid: NetUid, bonds_moving_average: u64) { BondsMovingAverage::::insert(netuid, bonds_moving_average); Self::deposit_event(Event::BondsMovingAverageSet(netuid, bonds_moving_average)); @@ -671,6 +735,7 @@ impl Pallet { pub fn get_bonds_reset(netuid: NetUid) -> bool { BondsResetOn::::get(netuid) } + pub fn set_bonds_reset(netuid: NetUid, bonds_reset: bool) { BondsResetOn::::insert(netuid, bonds_reset); Self::deposit_event(Event::BondsResetOnSet(netuid, bonds_reset)); @@ -679,6 +744,7 @@ impl Pallet { pub fn get_max_registrations_per_block(netuid: NetUid) -> u16 { MaxRegistrationsPerBlock::::get(netuid) } + pub fn set_max_registrations_per_block(netuid: NetUid, max_registrations_per_block: u16) { MaxRegistrationsPerBlock::::insert(netuid, max_registrations_per_block); Self::deposit_event(Event::MaxRegistrationsPerBlockSet( @@ -690,13 +756,16 @@ impl Pallet { pub fn get_subnet_owner(netuid: NetUid) -> T::AccountId { SubnetOwner::::get(netuid) } + pub fn get_subnet_owner_cut() -> u16 { SubnetOwnerCut::::get() } + pub fn get_float_subnet_owner_cut() -> U96F32 { U96F32::saturating_from_num(SubnetOwnerCut::::get()) .safe_div(U96F32::saturating_from_num(u16::MAX)) } + pub fn set_subnet_owner_cut(subnet_owner_cut: u16) { SubnetOwnerCut::::set(subnet_owner_cut); Self::deposit_event(Event::SubnetOwnerCutSet(subnet_owner_cut)); @@ -705,6 +774,7 @@ impl Pallet { pub fn get_owned_hotkeys(coldkey: &T::AccountId) -> Vec { OwnedHotkeys::::get(coldkey) } + pub fn get_all_staked_hotkeys(coldkey: &T::AccountId) -> Vec { StakingHotkeys::::get(coldkey) } @@ -716,10 +786,12 @@ impl Pallet { pub fn get_rao_recycled(netuid: NetUid) -> TaoCurrency { RAORecycledForRegistration::::get(netuid) } + pub fn set_rao_recycled(netuid: NetUid, rao_recycled: TaoCurrency) { RAORecycledForRegistration::::insert(netuid, rao_recycled); Self::deposit_event(Event::RAORecycledForRegistrationSet(netuid, rao_recycled)); } + pub fn increase_rao_recycled(netuid: NetUid, inc_rao_recycled: TaoCurrency) { let curr_rao_recycled = Self::get_rao_recycled(netuid); let rao_recycled = curr_rao_recycled.saturating_add(inc_rao_recycled); diff --git a/pallets/subtensor/src/utils/rate_limiting.rs b/pallets/subtensor/src/utils/rate_limiting.rs index 85f58cfc64..946d980d57 100644 --- a/pallets/subtensor/src/utils/rate_limiting.rs +++ b/pallets/subtensor/src/utils/rate_limiting.rs @@ -1,3 +1,5 @@ +use codec::{Decode, Encode}; +use scale_info::TypeInfo; use subtensor_runtime_common::NetUid; use super::*; @@ -24,7 +26,10 @@ impl TransactionType { match self { Self::SetChildren => 150, // 30 minutes Self::SetChildkeyTake => TxChildkeyTakeRateLimit::::get(), - Self::RegisterNetwork => NetworkRateLimit::::get(), + Self::RegisterNetwork => { + /*DEPRECATED*/ + 0 + } Self::MechanismCountUpdate => MechanismCountSetRateLimit::::get(), Self::MechanismEmission => MechanismEmissionRateLimit::::get(), Self::MaxUidsTrimming => MaxUidsTrimmingRateLimit::::get(), @@ -78,7 +83,10 @@ impl TransactionType { /// Get the block number of the last transaction for a specific key, and transaction type pub fn last_block(&self, key: &T::AccountId) -> u64 { match self { - Self::RegisterNetwork => Pallet::::get_network_last_lock_block(), + Self::RegisterNetwork => { + /*DEPRECATED*/ + 0 + } _ => self.last_block_on_subnet::(key, NetUid::ROOT), } } @@ -87,7 +95,10 @@ impl TransactionType { /// type pub fn last_block_on_subnet(&self, hotkey: &T::AccountId, netuid: NetUid) -> u64 { match self { - Self::RegisterNetwork => Pallet::::get_network_last_lock_block(), + Self::RegisterNetwork => { + /*DEPRECATED*/ + 0 + } Self::SetSNOwnerHotkey => { Pallet::::get_rate_limited_last_block(&RateLimitKey::SetSNOwnerHotkey(netuid)) } @@ -110,7 +121,7 @@ impl TransactionType { block: u64, ) { match self { - Self::RegisterNetwork => Pallet::::set_network_last_lock_block(block), + Self::RegisterNetwork => { /*DEPRECATED*/ } Self::SetSNOwnerHotkey => Pallet::::set_rate_limited_last_block( &RateLimitKey::SetSNOwnerHotkey(netuid), block, @@ -205,28 +216,9 @@ impl Pallet { // ==== Rate Limiting ===== // ======================== - pub fn remove_last_tx_block(key: &T::AccountId) { - Self::remove_rate_limited_last_block(&RateLimitKey::LastTxBlock(key.clone())) - } - pub fn set_last_tx_block(key: &T::AccountId, block: u64) { - Self::set_rate_limited_last_block(&RateLimitKey::LastTxBlock(key.clone()), block); - } - pub fn get_last_tx_block(key: &T::AccountId) -> u64 { - Self::get_rate_limited_last_block(&RateLimitKey::LastTxBlock(key.clone())) - } - pub fn remove_last_tx_block_delegate_take(key: &T::AccountId) { Self::remove_rate_limited_last_block(&RateLimitKey::LastTxBlockDelegateTake(key.clone())) } - pub fn set_last_tx_block_delegate_take(key: &T::AccountId, block: u64) { - Self::set_rate_limited_last_block( - &RateLimitKey::LastTxBlockDelegateTake(key.clone()), - block, - ); - } - pub fn get_last_tx_block_delegate_take(key: &T::AccountId) -> u64 { - Self::get_rate_limited_last_block(&RateLimitKey::LastTxBlockDelegateTake(key.clone())) - } pub fn get_last_tx_block_childkey_take(key: &T::AccountId) -> u64 { Self::get_rate_limited_last_block(&RateLimitKey::LastTxBlockChildKeyTake(key.clone())) } @@ -239,20 +231,4 @@ impl Pallet { block, ); } - pub fn exceeds_tx_rate_limit(prev_tx_block: u64, current_block: u64) -> bool { - let rate_limit: u64 = Self::get_tx_rate_limit(); - if rate_limit == 0 || prev_tx_block == 0 { - return false; - } - - current_block.saturating_sub(prev_tx_block) <= rate_limit - } - pub fn exceeds_tx_delegate_take_rate_limit(prev_tx_block: u64, current_block: u64) -> bool { - let rate_limit: u64 = Self::get_tx_delegate_take_rate_limit(); - if rate_limit == 0 || prev_tx_block == 0 { - return false; - } - - current_block.saturating_sub(prev_tx_block) <= rate_limit - } } diff --git a/pallets/transaction-fee/Cargo.toml b/pallets/transaction-fee/Cargo.toml index b5f08ac7c3..86bea7c843 100644 --- a/pallets/transaction-fee/Cargo.toml +++ b/pallets/transaction-fee/Cargo.toml @@ -27,6 +27,7 @@ pallet-scheduler = { workspace = true, default-features = false, optional = true [dev-dependencies] frame-executive.workspace = true pallet-evm-chain-id.workspace = true +rate-limiting-interface.workspace = true scale-info.workspace = true sp-consensus-aura.workspace = true sp-consensus-grandpa.workspace = true @@ -56,6 +57,7 @@ std = [ "pallet-subtensor/std", "pallet-subtensor-swap/std", "pallet-transaction-payment/std", + "rate-limiting-interface/std", "scale-info/std", "sp-runtime/std", "sp-std/std", diff --git a/pallets/transaction-fee/src/tests/mock.rs b/pallets/transaction-fee/src/tests/mock.rs index 3bad4a275f..9cc9927815 100644 --- a/pallets/transaction-fee/src/tests/mock.rs +++ b/pallets/transaction-fee/src/tests/mock.rs @@ -12,6 +12,7 @@ use frame_system::{ self as system, EnsureRoot, RawOrigin, limits, offchain::CreateTransactionBase, }; pub use pallet_subtensor::*; +use rate_limiting_interface::{RateLimitingInterface, TryIntoRateLimitTarget}; pub use sp_core::U256; use sp_core::{ConstU64, H256}; use sp_runtime::{ @@ -21,7 +22,9 @@ use sp_runtime::{ }; use sp_std::cmp::Ordering; use sp_weights::Weight; -pub use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; +pub use subtensor_runtime_common::{ + AlphaCurrency, Currency, NetUid, TaoCurrency, rate_limiting::RateLimitUsageKey, +}; use subtensor_swap_interface::{Order, SwapHandler}; use crate::SubtensorTxFeeHandler; @@ -168,9 +171,6 @@ parameter_types! { pub const InitialMinChildKeyTake: u16 = 0; // Allow 0 % pub const InitialMaxChildKeyTake: u16 = 11_796; // 18 %; pub const InitialWeightsVersionKey: u16 = 0; - pub const InitialServingRateLimit: u64 = 0; // No limit. - pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing - pub const InitialTxDelegateTakeRateLimit: u64 = 0; // Disable rate limit for testing pub const InitialTxChildKeyTakeRateLimit: u64 = 0; // Disable rate limit for testing pub const InitialBurn: u64 = 0; pub const InitialMinBurn: u64 = 500_000; @@ -197,7 +197,6 @@ parameter_types! { pub const InitialSubnetOwnerCut: u16 = 0; // 0%. 100% of rewards go to validators + miners. pub const InitialNetworkLockReductionInterval: u64 = 2; // 2 blocks. // pub const InitialSubnetLimit: u16 = 10; // (DEPRECATED) - pub const InitialNetworkRateLimit: u64 = 0; pub const InitialKeySwapCost: u64 = 1_000_000_000; 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 @@ -254,9 +253,6 @@ impl pallet_subtensor::Config for Test { type InitialWeightsVersionKey = InitialWeightsVersionKey; type InitialMaxDifficulty = InitialMaxDifficulty; type InitialMinDifficulty = InitialMinDifficulty; - type InitialServingRateLimit = InitialServingRateLimit; - type InitialTxRateLimit = InitialTxRateLimit; - type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit; type InitialTxChildKeyTakeRateLimit = InitialTxChildKeyTakeRateLimit; type InitialBurn = InitialBurn; type InitialMaxBurn = InitialMaxBurn; @@ -268,7 +264,6 @@ impl pallet_subtensor::Config for Test { type InitialNetworkMinLockCost = InitialNetworkMinLockCost; type InitialSubnetOwnerCut = InitialSubnetOwnerCut; type InitialNetworkLockReductionInterval = InitialNetworkLockReductionInterval; - type InitialNetworkRateLimit = InitialNetworkRateLimit; type KeySwapCost = InitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; @@ -289,6 +284,7 @@ impl pallet_subtensor::Config for Test { type GetCommitments = (); type MaxImmuneUidsPercentage = MaxImmuneUidsPercentage; type CommitmentsInterface = CommitmentsI; + type RateLimiting = NoRateLimiting; type EvmKeyAssociateRateLimit = EvmKeyAssociateRateLimit; } @@ -421,6 +417,42 @@ impl pallet_subtensor::CommitmentsInterface for CommitmentsI { fn purge_netuid(_netuid: NetUid) {} } +pub struct NoRateLimiting; + +impl RateLimitingInterface for NoRateLimiting { + type GroupId = subtensor_runtime_common::rate_limiting::GroupId; + type CallMetadata = RuntimeCall; + type Limit = u64; + type Scope = subtensor_runtime_common::NetUid; + type UsageKey = RateLimitUsageKey; + + fn rate_limit(_target: TargetArg, _scope: Option) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + None + } + + fn last_seen( + _target: TargetArg, + _usage_key: Option, + ) -> Option + where + TargetArg: TryIntoRateLimitTarget, + { + None + } + + fn set_last_seen( + _target: TargetArg, + _usage_key: Option, + _block: Option, + ) where + TargetArg: TryIntoRateLimitTarget, + { + } +} + parameter_types! { pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * BlockWeights::get().max_block; @@ -703,20 +735,15 @@ pub fn setup_subnets(sncount: u16, neurons: u16) -> TestSetup { } } -pub(crate) fn remove_stake_rate_limit_for_tests(hotkey: &U256, coldkey: &U256, netuid: NetUid) { - StakingOperationRateLimiter::::remove((hotkey, coldkey, netuid)); -} - #[allow(dead_code)] pub fn setup_stake(netuid: NetUid, coldkey: &U256, hotkey: &U256, amount: u64) { // Stake to hotkey account, and check if the result is ok SubtensorModule::add_balance_to_coldkey_account(coldkey, amount + ExistentialDeposit::get()); - remove_stake_rate_limit_for_tests(hotkey, coldkey, netuid); + assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(*coldkey), *hotkey, netuid, amount.into() )); - remove_stake_rate_limit_for_tests(hotkey, coldkey, netuid); } diff --git a/precompiles/Cargo.toml b/precompiles/Cargo.toml index 400e1caa9f..f768fb91c3 100644 --- a/precompiles/Cargo.toml +++ b/precompiles/Cargo.toml @@ -11,7 +11,6 @@ repository = "https://github.com/opentensor/subtensor/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -# codec.workspace = true ed25519-dalek = { workspace = true, features = ["alloc"] } fp-evm.workspace = true frame-support.workspace = true @@ -36,6 +35,7 @@ substrate-fixed.workspace = true pallet-subtensor.workspace = true pallet-subtensor-swap.workspace = true pallet-admin-utils.workspace = true +pallet-rate-limiting.workspace = true subtensor-swap-interface.workspace = true pallet-crowdloan.workspace = true @@ -45,7 +45,6 @@ workspace = true [features] default = ["std"] std = [ - # "codec/std", "ed25519-dalek/std", "fp-evm/std", "frame-support/std", @@ -60,6 +59,7 @@ std = [ "pallet-evm-precompile-sha3fips/std", "pallet-evm-precompile-simple/std", "pallet-evm/std", + "pallet-rate-limiting/std", "pallet-subtensor-proxy/std", "pallet-subtensor-swap/std", "pallet-subtensor/std", diff --git a/precompiles/src/balance_transfer.rs b/precompiles/src/balance_transfer.rs index c1cdab6ca5..e8eb6d2e6e 100644 --- a/precompiles/src/balance_transfer.rs +++ b/precompiles/src/balance_transfer.rs @@ -17,6 +17,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + Send + Sync @@ -42,6 +43,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + Send + Sync diff --git a/precompiles/src/crowdloan.rs b/precompiles/src/crowdloan.rs index f0971015d9..5fda02e0c1 100644 --- a/precompiles/src/crowdloan.rs +++ b/precompiles/src/crowdloan.rs @@ -24,6 +24,7 @@ where + pallet_crowdloan::Config + pallet_evm::Config + pallet_proxy::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + Send + Sync @@ -48,6 +49,7 @@ where + pallet_crowdloan::Config + pallet_evm::Config + pallet_proxy::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + Send + Sync diff --git a/precompiles/src/extensions.rs b/precompiles/src/extensions.rs index 5d73a75f9b..f729d9eb05 100644 --- a/precompiles/src/extensions.rs +++ b/precompiles/src/extensions.rs @@ -10,6 +10,7 @@ use pallet_evm::{ AddressMapping, BalanceConverter, EvmBalance, ExitError, GasWeightMapping, Precompile, PrecompileFailure, PrecompileHandle, PrecompileResult, }; +use pallet_rate_limiting::RateLimitTransactionExtension; use pallet_subtensor::transaction_extension::SubtensorTransactionExtension; use precompile_utils::EvmResult; use scale_info::TypeInfo; @@ -24,6 +25,12 @@ use sp_runtime::{ }; use sp_std::vec::Vec; +type RuntimeCallOf = ::RuntimeCall; +type Extensions = ( + SubtensorTransactionExtension, + RateLimitTransactionExtension, +); + pub(crate) trait PrecompileHandleExt: PrecompileHandle { fn caller_account_id(&self) -> R::AccountId where @@ -57,24 +64,29 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config> + pallet_subtensor::Config + Send + Sync + TypeInfo, - ::RuntimeCall: From, - ::RuntimeCall: GetDispatchInfo + RuntimeCallOf: From, + RuntimeCallOf: GetDispatchInfo + Dispatchable + IsSubType> + IsSubType>, ::RuntimeOrigin: From> + AsSystemOriginSigner + Clone, { - let call = ::RuntimeCall::from(call); + let call = RuntimeCallOf::::from(call); let mut info = GetDispatchInfo::get_dispatch_info(&call); - let subtensor_extension = SubtensorTransactionExtension::::new(); + + let extensions = ( + SubtensorTransactionExtension::::new(), + RateLimitTransactionExtension::::new(), + ); info.extension_weight = info .extension_weight - .saturating_add(subtensor_extension.weight(&call)); + .saturating_add(extensions.weight(&call)); let target_gas = self.gas_limit(); if let Some(gas) = target_gas { @@ -94,18 +106,19 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { )?; let origin = ::RuntimeOrigin::from(origin); - let (_, val, origin) = subtensor_extension + let implicit = extensions.implicit().map_err(extension_error)?; + let (_, val, origin) = extensions .validate( origin, &call, &info, 0, - (), + implicit, &TxBaseImplication(()), TransactionSource::External, ) .map_err(extension_error)?; - subtensor_extension + let pre = extensions .prepare(val, &origin, &call, &info, 0) .map_err(extension_error)?; @@ -113,9 +126,13 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { Ok(mut post_info) => { post_info.set_extension_weight(&info); let result: DispatchResult = Ok(()); - as TransactionExtension< - ::RuntimeCall, - >>::post_dispatch((), &info, &mut post_info, 0, &result) + as TransactionExtension>>::post_dispatch( + pre, + &info, + &mut post_info, + 0, + &result, + ) .map_err(extension_error)?; log::debug!("Dispatch succeeded. Post info: {post_info:?}"); self.charge_and_refund_after_dispatch::(&info, &post_info)?; @@ -127,9 +144,13 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { let mut post_info = e.post_info; post_info.set_extension_weight(&info); let result: DispatchResult = Err(e.error); - as TransactionExtension< - ::RuntimeCall, - >>::post_dispatch((), &info, &mut post_info, 0, &result) + as TransactionExtension>>::post_dispatch( + pre, + &info, + &mut post_info, + 0, + &result, + ) .map_err(extension_error)?; log::info!("Precompile dispatch failed. message as: {e:?}"); self.charge_and_refund_after_dispatch::(&info, &post_info)?; diff --git a/precompiles/src/leasing.rs b/precompiles/src/leasing.rs index 01a8db4354..d4951a658f 100644 --- a/precompiles/src/leasing.rs +++ b/precompiles/src/leasing.rs @@ -24,6 +24,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + pallet_crowdloan::Config + Send @@ -48,6 +49,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + pallet_crowdloan::Config + Send diff --git a/precompiles/src/lib.rs b/precompiles/src/lib.rs index 864119d89f..192b314e12 100644 --- a/precompiles/src/lib.rs +++ b/precompiles/src/lib.rs @@ -70,7 +70,11 @@ where + pallet_subtensor_swap::Config + pallet_proxy::Config + pallet_crowdloan::Config - + Send + + pallet_rate_limiting::Config< + LimitScope = subtensor_runtime_common::NetUid, + GroupId = subtensor_runtime_common::rate_limiting::GroupId, + RuntimeCall = ::RuntimeCall, + > + Send + Sync + scale_info::TypeInfo, R::AccountId: From<[u8; 32]> + ByteArray + Into<[u8; 32]>, @@ -79,6 +83,7 @@ where + From> + From> + From> + + From> + From> + GetDispatchInfo + Dispatchable @@ -103,7 +108,11 @@ where + pallet_subtensor_swap::Config + pallet_proxy::Config + pallet_crowdloan::Config - + Send + + pallet_rate_limiting::Config< + LimitScope = subtensor_runtime_common::NetUid, + GroupId = subtensor_runtime_common::rate_limiting::GroupId, + RuntimeCall = ::RuntimeCall, + > + Send + Sync + scale_info::TypeInfo, R::AccountId: From<[u8; 32]> + ByteArray + Into<[u8; 32]>, @@ -112,6 +121,7 @@ where + From> + From> + From> + + From> + From> + GetDispatchInfo + Dispatchable @@ -166,7 +176,11 @@ where + pallet_subtensor_swap::Config + pallet_proxy::Config + pallet_crowdloan::Config - + Send + + pallet_rate_limiting::Config< + LimitScope = subtensor_runtime_common::NetUid, + GroupId = subtensor_runtime_common::rate_limiting::GroupId, + RuntimeCall = ::RuntimeCall, + > + Send + Sync + scale_info::TypeInfo, R::AccountId: From<[u8; 32]> + ByteArray + Into<[u8; 32]>, @@ -175,6 +189,7 @@ where + From> + From> + From> + + From> + From> + GetDispatchInfo + Dispatchable diff --git a/precompiles/src/neuron.rs b/precompiles/src/neuron.rs index 0b998b3c07..e7411cd2b2 100644 --- a/precompiles/src/neuron.rs +++ b/precompiles/src/neuron.rs @@ -18,6 +18,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + Send + Sync @@ -40,6 +41,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + Send + Sync diff --git a/precompiles/src/proxy.rs b/precompiles/src/proxy.rs index 5139477f00..a366590f53 100644 --- a/precompiles/src/proxy.rs +++ b/precompiles/src/proxy.rs @@ -28,6 +28,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + pallet_proxy::Config + Send @@ -54,6 +55,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + pallet_proxy::Config + Send diff --git a/precompiles/src/staking.rs b/precompiles/src/staking.rs index 9cc2a2aaa4..dcd0aa18e9 100644 --- a/precompiles/src/staking.rs +++ b/precompiles/src/staking.rs @@ -55,6 +55,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + pallet_proxy::Config + Send @@ -80,6 +81,7 @@ where R: frame_system::Config + pallet_balances::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + pallet_proxy::Config + Send @@ -448,6 +450,7 @@ impl PrecompileExt for StakingPrecompile where R: frame_system::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + pallet_proxy::Config + pallet_balances::Config @@ -475,6 +478,7 @@ impl StakingPrecompile where R: frame_system::Config + pallet_evm::Config + + pallet_rate_limiting::Config::RuntimeCall> + pallet_subtensor::Config + pallet_proxy::Config + pallet_balances::Config diff --git a/precompiles/src/subnet.rs b/precompiles/src/subnet.rs index 4df373c116..7c35adf333 100644 --- a/precompiles/src/subnet.rs +++ b/precompiles/src/subnet.rs @@ -5,11 +5,12 @@ use frame_support::traits::ConstU32; use frame_support::traits::IsSubType; use frame_system::RawOrigin; use pallet_evm::{AddressMapping, PrecompileHandle}; +use pallet_rate_limiting::{RateLimitKind, RateLimitTarget}; use precompile_utils::{EvmResult, prelude::BoundedString}; use sp_core::H256; -use sp_runtime::traits::{AsSystemOriginSigner, Dispatchable}; +use sp_runtime::traits::{AsSystemOriginSigner, Dispatchable, SaturatedConversion}; use sp_std::vec; -use subtensor_runtime_common::{Currency, NetUid}; +use subtensor_runtime_common::{Currency, NetUid, rate_limiting}; use crate::{PrecompileExt, PrecompileHandleExt}; @@ -22,13 +23,18 @@ where + pallet_evm::Config + pallet_subtensor::Config + pallet_admin_utils::Config - + Send + + pallet_rate_limiting::Config< + LimitScope = NetUid, + GroupId = subtensor_runtime_common::rate_limiting::GroupId, + RuntimeCall = ::RuntimeCall, + > + Send + Sync + scale_info::TypeInfo, R::AccountId: From<[u8; 32]>, ::RuntimeOrigin: AsSystemOriginSigner + Clone, ::RuntimeCall: From> + From> + + From> + GetDispatchInfo + Dispatchable + IsSubType> @@ -46,13 +52,18 @@ where + pallet_evm::Config + pallet_subtensor::Config + pallet_admin_utils::Config - + Send + + pallet_rate_limiting::Config< + LimitScope = NetUid, + GroupId = subtensor_runtime_common::rate_limiting::GroupId, + RuntimeCall = ::RuntimeCall, + > + Send + Sync + scale_info::TypeInfo, R::AccountId: From<[u8; 32]>, ::RuntimeOrigin: AsSystemOriginSigner + Clone, ::RuntimeCall: From> + From> + + From> + GetDispatchInfo + Dispatchable + IsSubType> @@ -156,9 +167,9 @@ where #[precompile::public("getServingRateLimit(uint16)")] #[precompile::view] fn get_serving_rate_limit(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { - Ok(pallet_subtensor::ServingRateLimit::::get(NetUid::from( - netuid, - ))) + Ok(pallet_subtensor::Pallet::::get_serving_rate_limit( + NetUid::from(netuid), + )) } #[precompile::public("setServingRateLimit(uint16,uint64)")] @@ -168,9 +179,10 @@ where netuid: u16, serving_rate_limit: u64, ) -> EvmResult<()> { - let call = pallet_admin_utils::Call::::sudo_set_serving_rate_limit { - netuid: netuid.into(), - serving_rate_limit, + let call = pallet_rate_limiting::Call::::set_rate_limit { + target: RateLimitTarget::Group(subtensor_runtime_common::rate_limiting::GROUP_SERVE), + scope: Some(netuid.into()), + limit: RateLimitKind::Exact(serving_rate_limit.saturated_into()), }; handle.try_dispatch_runtime_call::( @@ -260,20 +272,30 @@ where #[precompile::public("getWeightsSetRateLimit(uint16)")] #[precompile::view] fn get_weights_set_rate_limit(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { - Ok(pallet_subtensor::WeightsSetRateLimit::::get( - NetUid::from(netuid), - )) + let target = RateLimitTarget::Group(rate_limiting::GROUP_WEIGHTS_SET); + let scope = Some(NetUid::from(netuid)); + let limit = + pallet_rate_limiting::Pallet::::resolved_limit(&target, &scope).unwrap_or_default(); + Ok(limit.saturated_into()) } #[precompile::public("setWeightsSetRateLimit(uint16,uint64)")] #[precompile::payable] fn set_weights_set_rate_limit( - _handle: &mut impl PrecompileHandle, - _netuid: u16, - _weights_set_rate_limit: u64, + handle: &mut impl PrecompileHandle, + netuid: u16, + weights_set_rate_limit: u64, ) -> EvmResult<()> { - // DEPRECATED. Subnet owner cannot set weight setting rate limits - Ok(()) + let call = pallet_rate_limiting::Call::::set_rate_limit { + target: RateLimitTarget::Group(rate_limiting::GROUP_WEIGHTS_SET), + scope: Some(netuid.into()), + limit: RateLimitKind::Exact(weights_set_rate_limit.saturated_into()), + }; + + handle.try_dispatch_runtime_call::( + call, + RawOrigin::Signed(handle.caller_account_id::()), + ) } #[precompile::public("getAdjustmentAlpha(uint16)")] diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index b3aced2160..af62850e09 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -38,9 +38,12 @@ frame-system = { workspace = true } frame-try-runtime = { workspace = true, optional = true } pallet-timestamp.workspace = true pallet-transaction-payment.workspace = true +pallet-rate-limiting.workspace = true +pallet-rate-limiting-runtime-api.workspace = true pallet-subtensor-utility.workspace = true frame-executive.workspace = true frame-metadata-hash-extension.workspace = true +serde.workspace = true sp-api.workspace = true sp-block-builder.workspace = true sp-consensus-aura.workspace = true @@ -52,6 +55,7 @@ sp-inherents.workspace = true sp-offchain.workspace = true sp-runtime.workspace = true sp-session.workspace = true +sp-io.workspace = true sp-std.workspace = true sp-transaction-pool.workspace = true sp-version.workspace = true @@ -156,7 +160,6 @@ ethereum.workspace = true [dev-dependencies] frame-metadata.workspace = true -sp-io.workspace = true sp-tracing.workspace = true [build-dependencies] @@ -190,6 +193,8 @@ std = [ "pallet-timestamp/std", "pallet-transaction-payment-rpc-runtime-api/std", "pallet-transaction-payment/std", + "pallet-rate-limiting/std", + "pallet-rate-limiting-runtime-api/std", "pallet-subtensor-utility/std", "pallet-sudo/std", "pallet-multisig/std", @@ -198,6 +203,7 @@ std = [ "pallet-preimage/std", "pallet-commitments/std", "precompile-utils/std", + "serde/std", "sp-api/std", "sp-block-builder/std", "sp-core/std", @@ -315,6 +321,7 @@ runtime-benchmarks = [ "pallet-hotfix-sufficients/runtime-benchmarks", "pallet-drand/runtime-benchmarks", "pallet-transaction-payment/runtime-benchmarks", + "pallet-rate-limiting/runtime-benchmarks", "pallet-subtensor-swap/runtime-benchmarks", # Smart Tx fees pallet @@ -333,6 +340,7 @@ try-runtime = [ "pallet-insecure-randomness-collective-flip/try-runtime", "pallet-timestamp/try-runtime", "pallet-transaction-payment/try-runtime", + "pallet-rate-limiting/try-runtime", "pallet-subtensor-utility/try-runtime", "pallet-safe-mode/try-runtime", "pallet-subtensor/try-runtime", diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 225c2e2706..c8de95ed96 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -8,10 +8,12 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +use core::marker::PhantomData; use core::num::NonZeroU64; pub mod check_nonce; mod migrations; +pub mod rate_limiting; pub mod sudo_wrapper; pub mod transaction_payment_wrapper; @@ -45,6 +47,7 @@ use pallet_subtensor_proxy as pallet_proxy; use pallet_subtensor_swap_runtime_api::SimSwapResult; use pallet_subtensor_utility as pallet_utility; use runtime_common::prod_or_fast; +use scale_info::TypeInfo; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_consensus_babe::BabeConfiguration; @@ -73,6 +76,13 @@ use sp_version::RuntimeVersion; use subtensor_precompiles::Precompiles; use subtensor_runtime_common::{AlphaCurrency, TaoCurrency, time::*, *}; use subtensor_swap_interface::{Order, SwapHandler}; +use subtensor_transaction_fee::{SubtensorTxFeeHandler, TransactionFeeHandler}; +// Frontier +use fp_rpc::TransactionStatus; +use pallet_ethereum::{Call::transact, PostLogContent, Transaction as EthereumTransaction}; +use pallet_evm::{ + Account as EVMAccount, BalanceConverter, EvmBalance, FeeCalculator, Runner, SubstrateBalance, +}; // A few exports that help ease life for downstream crates. pub use frame_support::{ @@ -94,21 +104,13 @@ pub use pallet_balances::Call as BalancesCall; use pallet_commitments::GetCommitments; pub use pallet_timestamp::Call as TimestampCall; use pallet_transaction_payment::{ConstFeeMultiplier, Multiplier}; +pub use rate_limiting::{ + ScopeResolver as RuntimeScopeResolver, UsageResolver as RuntimeUsageResolver, +}; #[cfg(any(feature = "std", test))] pub use sp_runtime::BuildStorage; pub use sp_runtime::{Perbill, Permill}; -use subtensor_transaction_fee::{SubtensorTxFeeHandler, TransactionFeeHandler}; - -use core::marker::PhantomData; - -use scale_info::TypeInfo; - -// Frontier -use fp_rpc::TransactionStatus; -use pallet_ethereum::{Call::transact, PostLogContent, Transaction as EthereumTransaction}; -use pallet_evm::{ - Account as EVMAccount, BalanceConverter, EvmBalance, FeeCalculator, Runner, SubstrateBalance, -}; +pub use subtensor_runtime_common::rate_limiting::RateLimitUsageKey; // Drand impl pallet_drand::Config for Runtime { @@ -179,8 +181,11 @@ impl frame_system::offchain::CreateSignedTransaction SudoTransactionExtension::::new(), pallet_subtensor::transaction_extension::SubtensorTransactionExtension::::new( ), - pallet_drand::drand_priority::DrandPriority::::new(), - frame_metadata_hash_extension::CheckMetadataHash::::new(true), + ( + pallet_drand::drand_priority::DrandPriority::::new(), + frame_metadata_hash_extension::CheckMetadataHash::::new(true), + ), + pallet_rate_limiting::RateLimitTransactionExtension::::new(), ); let raw_payload = SignedPayload::new(call.clone(), extra.clone()).ok()?; @@ -244,7 +249,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_version: 371, impl_version: 1, apis: RUNTIME_API_VERSIONS, - transaction_version: 1, + transaction_version: 2, system_version: 1, }; @@ -1028,7 +1033,6 @@ parameter_types! { pub const SubtensorInitialWeightsVersionKey: u64 = 0; pub const SubtensorInitialMinDifficulty: u64 = 10_000_000; pub const SubtensorInitialMaxDifficulty: u64 = u64::MAX / 4; - pub const SubtensorInitialServingRateLimit: u64 = 50; pub const SubtensorInitialBurn: u64 = 100_000_000; // 0.1 tao pub const SubtensorInitialMinBurn: u64 = 500_000; // 500k RAO pub const SubtensorInitialMaxBurn: u64 = 100_000_000_000; // 100 tao @@ -1101,14 +1105,11 @@ impl pallet_subtensor::Config for Runtime { type InitialWeightsVersionKey = SubtensorInitialWeightsVersionKey; type InitialMaxDifficulty = SubtensorInitialMaxDifficulty; type InitialMinDifficulty = SubtensorInitialMinDifficulty; - type InitialServingRateLimit = SubtensorInitialServingRateLimit; type InitialBurn = SubtensorInitialBurn; type InitialMaxBurn = SubtensorInitialMaxBurn; type InitialMinBurn = SubtensorInitialMinBurn; type MinBurnUpperBound = MinBurnUpperBound; type MaxBurnLowerBound = MaxBurnLowerBound; - type InitialTxRateLimit = SubtensorInitialTxRateLimit; - type InitialTxDelegateTakeRateLimit = SubtensorInitialTxDelegateTakeRateLimit; type InitialTxChildKeyTakeRateLimit = SubtensorInitialTxChildKeyTakeRateLimit; type InitialMaxChildKeyTake = SubtensorInitialMaxChildKeyTake; type InitialRAORecycledForRegistration = SubtensorInitialRAORecycledForRegistration; @@ -1116,7 +1117,6 @@ impl pallet_subtensor::Config for Runtime { type InitialNetworkMinLockCost = SubtensorInitialMinLockCost; type InitialNetworkLockReductionInterval = SubtensorInitialNetworkLockReductionInterval; type InitialSubnetOwnerCut = SubtensorInitialSubnetOwnerCut; - type InitialNetworkRateLimit = SubtensorInitialNetworkRateLimit; type KeySwapCost = SubtensorInitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; @@ -1137,9 +1137,32 @@ impl pallet_subtensor::Config for Runtime { type GetCommitments = GetCommitmentsStruct; type MaxImmuneUidsPercentage = MaxImmuneUidsPercentage; type CommitmentsInterface = CommitmentsI; + type RateLimiting = RateLimiting; type EvmKeyAssociateRateLimit = EvmKeyAssociateRateLimit; } +parameter_types! { + pub const RateLimitingMaxGroupMembers: u32 = 64; + pub const RateLimitingMaxGroupNameLength: u32 = 64; +} + +impl pallet_rate_limiting::Config for Runtime { + type RuntimeCall = RuntimeCall; + type AdminOrigin = EnsureRoot; + type LimitSettingRule = rate_limiting::LimitSettingRule; + type DefaultLimitSettingRule = rate_limiting::DefaultLimitSettingRule; + type LimitSettingOrigin = rate_limiting::LimitSettingOrigin; + type LimitScope = NetUid; + type LimitScopeResolver = RuntimeScopeResolver; + type UsageKey = RateLimitUsageKey; + type UsageResolver = RuntimeUsageResolver; + type GroupId = subtensor_runtime_common::rate_limiting::GroupId; + type MaxGroupMembers = RateLimitingMaxGroupMembers; + type MaxGroupNameLength = RateLimitingMaxGroupNameLength; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = (); +} + parameter_types! { pub const SwapProtocolId: PalletId = PalletId(*b"ten/swap"); pub const SwapMaxFeeRate: u16 = 10000; // 15.26% @@ -1642,6 +1665,7 @@ construct_runtime!( Swap: pallet_subtensor_swap = 28, Contracts: pallet_contracts = 29, MevShield: pallet_shield = 30, + RateLimiting: pallet_rate_limiting = 31, } ); @@ -1652,6 +1676,13 @@ pub type Header = generic::Header; // Block type as expected by this runtime. pub type Block = generic::Block; // The extensions to the basic transaction logic. +// ================================================================================================= +// IMPORTANT: If you add a new TransactionExtension here, review precompile runtime-call dispatch +// paths (currently `PrecompileHandleExt::try_dispatch_runtime_call`) and decide whether the new +// extension must be triggered manually (not all extensions apply to precompiles). +// ================================================================================================= +// Note: The SDK only implements TransactionExtension for tuples up to 12 items, so we nest the last +// two extensions to keep order/encoding while staying under the limit. pub type TransactionExtensions = ( frame_system::CheckNonZeroSender, frame_system::CheckSpecVersion, @@ -1663,16 +1694,22 @@ pub type TransactionExtensions = ( ChargeTransactionPaymentWrapper, SudoTransactionExtension, pallet_subtensor::transaction_extension::SubtensorTransactionExtension, - pallet_drand::drand_priority::DrandPriority, - frame_metadata_hash_extension::CheckMetadataHash, + ( + pallet_drand::drand_priority::DrandPriority, + frame_metadata_hash_extension::CheckMetadataHash, + ), + pallet_rate_limiting::RateLimitTransactionExtension, ); type Migrations = ( - // Leave this migration in the runtime, so every runtime upgrade tiny rounding errors (fractions of fractions - // of a cent) are cleaned up. These tiny rounding errors occur due to floating point coversion. + // Leave this migration in the runtime, so every runtime upgrade tiny rounding errors (fractions + // of fractions of a cent) are cleaned up. These tiny rounding errors occur due to floating + // point coversion. pallet_subtensor::migrations::migrate_init_total_issuance::initialise_total_issuance::Migration< Runtime, >, + migrations::rate_limiting::Migration, + migrations::subtensor_module::Migration, ); // Unchecked extrinsic type as expected by this runtime. @@ -2222,6 +2259,50 @@ impl_runtime_apis! { } } + impl pallet_rate_limiting_runtime_api::RateLimitingRuntimeApi for Runtime { + fn get_rate_limit( + pallet: Vec, + extrinsic: Vec, + ) -> Option { + use pallet_rate_limiting::{Pallet as RateLimiting, RateLimit}; + use pallet_rate_limiting_runtime_api::RateLimitRpcResponse; + + let pallet_name = sp_std::str::from_utf8(&pallet).ok()?; + let extrinsic_name = sp_std::str::from_utf8(&extrinsic).ok()?; + + let identifier = RateLimiting::::identifier_for_call_names( + pallet_name, + extrinsic_name, + )?; + let target = + RateLimiting::::config_target(&identifier).ok()?; + let limits = + pallet_rate_limiting::Limits::::get(target)?; + let default_limit = + pallet_rate_limiting::DefaultLimit::::get(); + let resolved = + RateLimiting::::resolved_limit(&target, &None); + + let (global, contextual) = match limits { + RateLimit::Global(kind) => (Some(kind), sp_std::vec::Vec::new()), + RateLimit::Scoped(entries) => ( + None, + entries + .into_iter() + .map(|(scope, kind)| (scope.encode(), kind)) + .collect(), + ), + }; + + Some(RateLimitRpcResponse { + global, + contextual, + default_limit, + resolved, + }) + } + } + impl pallet_contracts::ContractsApi for Runtime { diff --git a/runtime/src/migrations/mod.rs b/runtime/src/migrations/mod.rs index ecc48efcdb..c906c32874 100644 --- a/runtime/src/migrations/mod.rs +++ b/runtime/src/migrations/mod.rs @@ -1 +1,4 @@ -//! Export migrations from here. +//! Runtime-level migrations. + +pub mod rate_limiting; +pub mod subtensor_module; diff --git a/runtime/src/migrations/rate_limiting.rs b/runtime/src/migrations/rate_limiting.rs new file mode 100644 index 0000000000..abcfac0754 --- /dev/null +++ b/runtime/src/migrations/rate_limiting.rs @@ -0,0 +1,1657 @@ +use core::{convert::TryFrom, marker::PhantomData}; + +use frame_support::{BoundedBTreeSet, BoundedVec, weights::Weight}; +use frame_system::pallet_prelude::BlockNumberFor; +use log::{info, warn}; +use pallet_rate_limiting::{ + GroupSharing, RateLimit, RateLimitGroup, RateLimitKind, RateLimitTarget, TransactionIdentifier, +}; +use pallet_subtensor::{ + self, AssociatedEvmAddress, Axons, Config as SubtensorConfig, HasMigrationRun, LastUpdate, + Pallet, Prometheus, +}; +use sp_runtime::traits::SaturatedConversion; +use sp_std::{ + collections::{btree_map::BTreeMap, btree_set::BTreeSet}, + vec, + vec::Vec, +}; +use subtensor_runtime_common::{ + NetUid, + rate_limiting::{ + GROUP_DELEGATE_TAKE, GROUP_OWNER_HPARAMS, GROUP_REGISTER_NETWORK, GROUP_SERVE, + GROUP_STAKING_OPS, GROUP_SWAP_KEYS, GROUP_WEIGHTS_SET, GroupId, RateLimitUsageKey, + ServingEndpoint, + }, +}; + +use crate::{ + AccountId, Runtime, + rate_limiting::{ + LimitSettingRule, + legacy::{ + Hyperparameter, RateLimitKey, TransactionType, defaults as legacy_defaults, + storage as legacy_storage, + }, + }, +}; + +type GroupNameOf = BoundedVec::MaxGroupNameLength>; +type GroupMembersOf = + BoundedBTreeSet::MaxGroupMembers>; + +// Pallet index assigned to `pallet_subtensor` in `construct_runtime!`. +const SUBTENSOR_PALLET_INDEX: u8 = 7; +// Pallet index assigned to `pallet_admin_utils` in `construct_runtime!`. +const ADMIN_UTILS_PALLET_INDEX: u8 = 19; + +/// Marker stored in `pallet_subtensor::HasMigrationRun` once the migration finishes. +pub const MIGRATION_NAME: &[u8] = b"migrate_rate_limiting"; + +// `set_children` is rate-limited to once every 150 blocks, it's hard-coded in the legacy code. +const SET_CHILDREN_RATE_LIMIT: u64 = 150; + +// Hyperparameter extrinsics routed through owner-or-root rate limiting. +const HYPERPARAMETERS: &[Hyperparameter] = &[ + Hyperparameter::ServingRateLimit, + Hyperparameter::MaxDifficulty, + Hyperparameter::AdjustmentAlpha, + Hyperparameter::ImmunityPeriod, + Hyperparameter::MinAllowedWeights, + Hyperparameter::MaxAllowedUids, + Hyperparameter::Kappa, + Hyperparameter::Rho, + Hyperparameter::ActivityCutoff, + Hyperparameter::PowRegistrationAllowed, + Hyperparameter::MinBurn, + Hyperparameter::MaxBurn, + Hyperparameter::BondsMovingAverage, + Hyperparameter::BondsPenalty, + Hyperparameter::CommitRevealEnabled, + Hyperparameter::LiquidAlphaEnabled, + Hyperparameter::AlphaValues, + Hyperparameter::WeightCommitInterval, + Hyperparameter::TransferEnabled, + Hyperparameter::AlphaSigmoidSteepness, + Hyperparameter::Yuma3Enabled, + Hyperparameter::BondsResetEnabled, + Hyperparameter::ImmuneNeuronLimit, + Hyperparameter::RecycleOrBurn, +]; + +/// Runtime hook that executes the rate-limiting migration. +pub struct Migration(PhantomData); + +impl frame_support::traits::OnRuntimeUpgrade for Migration +where + T: SubtensorConfig + pallet_rate_limiting::Config, + RateLimitUsageKey: Into<::UsageKey>, +{ + fn on_runtime_upgrade() -> Weight { + migrate_rate_limiting() + } +} + +pub fn migrate_rate_limiting() -> Weight { + let mut weight = ::DbWeight::get().reads(1); + if HasMigrationRun::::get(MIGRATION_NAME) { + info!("Rate-limiting migration already executed. Skipping."); + return weight; + } + + let (groups, commits, reads) = commits(); + weight = weight.saturating_add(::DbWeight::get().reads(reads)); + + let (limit_commits, last_seen_commits) = commits.into_iter().fold( + (Vec::new(), Vec::new()), + |(mut limits, mut seen), commit| { + match commit.kind { + CommitKind::Limit(limit) => limits.push((commit.target, limit)), + CommitKind::LastSeen(ls) => seen.push((commit.target, ls)), + } + (limits, seen) + }, + ); + + let (group_writes, group_count) = migrate_grouping(&groups); + let (limit_writes, limits_len) = migrate_limits(limit_commits); + let (last_seen_writes, last_seen_len) = migrate_last_seen(last_seen_commits); + + let mut writes = group_writes + .saturating_add(limit_writes) + .saturating_add(last_seen_writes); + + // Legacy parity: serving-rate-limit configuration is allowed for root OR subnet owner. + // Everything else remains default (`AdminOrigin` / root in this runtime). + pallet_rate_limiting::LimitSettingRules::::insert( + RateLimitTarget::Group(GROUP_SERVE), + LimitSettingRule::RootOrSubnetOwnerAdminWindow, + ); + writes += 1; + + HasMigrationRun::::insert(MIGRATION_NAME, true); + writes += 1; + + weight = + weight.saturating_add(::DbWeight::get().writes(writes)); + + info!( + "New migration wrote {} limits, {} last-seen entries, and {} groups into pallet-rate-limiting", + limits_len, last_seen_len, group_count + ); + + weight +} + +// Main entrypoint: build all groups and commits, along with storage reads. +fn commits() -> (Vec, Vec, u64) { + let mut groups = Vec::new(); + let mut commits = Vec::new(); + + // grouped + let mut reads = build_serving(&mut groups, &mut commits); + reads = reads.saturating_add(build_delegate_take(&mut groups, &mut commits)); + reads = reads.saturating_add(build_weights(&mut groups, &mut commits)); + reads = reads.saturating_add(build_register_network(&mut groups, &mut commits)); + reads = reads.saturating_add(build_owner_hparams(&mut groups, &mut commits)); + reads = reads.saturating_add(build_staking_ops(&mut groups, &mut commits)); + reads = reads.saturating_add(build_swap_keys(&mut groups, &mut commits)); + + // standalone + reads = reads.saturating_add(build_childkey_take(&mut commits)); + reads = reads.saturating_add(build_set_children(&mut commits)); + reads = reads.saturating_add(build_weights_version_key(&mut commits)); + reads = reads.saturating_add(build_sn_owner_hotkey(&mut commits)); + reads = reads.saturating_add(build_associate_evm(&mut commits)); + reads = reads.saturating_add(build_mechanism_count(&mut commits)); + reads = reads.saturating_add(build_mechanism_emission(&mut commits)); + reads = reads.saturating_add(build_trim_max_uids(&mut commits)); + + (groups, commits, reads) +} + +fn migrate_grouping(groups: &[GroupConfig]) -> (u64, usize) { + let mut writes: u64 = 0; + let mut max_group_id: Option = None; + + for group in groups { + let Ok(name) = GroupNameOf::::try_from(group.name.clone()) else { + warn!( + "rate-limiting migration: group name exceeds bounds, skipping id {}", + group.id + ); + continue; + }; + + pallet_rate_limiting::Groups::::insert( + group.id, + RateLimitGroup { + id: group.id, + name: name.clone(), + sharing: group.sharing, + }, + ); + pallet_rate_limiting::GroupNameIndex::::insert(name, group.id); + writes += 2; + + let mut member_set = BTreeSet::new(); + for call in &group.members { + member_set.insert(call.identifier()); + pallet_rate_limiting::CallGroups::::insert(call.identifier(), group.id); + writes += 1; + if call.read_only { + pallet_rate_limiting::CallReadOnly::::insert(call.identifier(), true); + writes += 1; + } + } + let Ok(bounded) = GroupMembersOf::::try_from(member_set) else { + warn!( + "rate-limiting migration: group {} has too many members, skipping assignment", + group.id + ); + continue; + }; + pallet_rate_limiting::GroupMembers::::insert(group.id, bounded); + writes += 1; + + max_group_id = Some(max_group_id.map_or(group.id, |current| current.max(group.id))); + } + + let next_group_id = max_group_id.map_or(0, |id| id.saturating_add(1)); + pallet_rate_limiting::NextGroupId::::put(next_group_id); + writes += 1; + + (writes, groups.len()) +} + +fn migrate_limits(limit_commits: Vec<(RateLimitTarget, MigratedLimit)>) -> (u64, usize) { + let mut writes: u64 = 0; + let mut limits: BTreeMap, RateLimit>> = + BTreeMap::new(); + + for (target, MigratedLimit { span, scope }) in limit_commits { + let entry = limits.entry(target).or_insert_with(|| match scope { + Some(s) => RateLimit::scoped_single(s, RateLimitKind::Exact(span)), + None => RateLimit::global(RateLimitKind::Exact(span)), + }); + + if let Some(netuid) = scope { + match entry { + RateLimit::Global(_) => { + *entry = RateLimit::scoped_single(netuid, RateLimitKind::Exact(span)); + } + RateLimit::Scoped(map) => { + map.insert(netuid, RateLimitKind::Exact(span)); + } + } + } else { + *entry = RateLimit::global(RateLimitKind::Exact(span)); + } + } + + let len = limits.len(); + for (target, limit) in limits { + pallet_rate_limiting::Limits::::insert(target, limit); + writes += 1; + } + + (writes, len) +} + +fn migrate_last_seen( + last_seen_commits: Vec<(RateLimitTarget, MigratedLastSeen)>, +) -> (u64, usize) { + let mut writes: u64 = 0; + let mut last_seen: BTreeMap< + ( + RateLimitTarget, + Option>, + ), + BlockNumberFor, + > = BTreeMap::new(); + + for (target, MigratedLastSeen { block, usage }) in last_seen_commits { + let key = (target, usage); + last_seen + .entry(key) + .and_modify(|existing| { + if block > *existing { + *existing = block; + } + }) + .or_insert(block); + } + + let len = last_seen.len(); + for ((target, usage), block) in last_seen { + pallet_rate_limiting::LastSeen::::insert(target, usage, block); + writes += 1; + } + + (writes, len) +} + +// Serving group (config+usage shared). +// scope: netuid +// usage: account+netuid, but different keys (endpoint value) for axon/prometheus +// legacy sources: ServingRateLimit (per netuid), Axons/Prometheus +fn build_serving(groups: &mut Vec, commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + // Create the group with all its members. + groups.push(GroupConfig { + id: GROUP_SERVE, + name: b"serving".to_vec(), + sharing: GroupSharing::ConfigAndUsage, + members: vec![ + MigratedCall::subtensor(4, false), // serve_axon + MigratedCall::subtensor(40, false), // serve_axon_tls + MigratedCall::subtensor(5, false), // serve_prometheus + ], + }); + + let (serving_limits, serving_reads) = legacy_storage::serving_rate_limits(); + reads = reads.saturating_add(serving_reads); + // Limits per netuid (written to the group target). + // Merge live subnets (which may rely on default rate-limit values) with any legacy entries that + // exist only in storage, so we migrate both current and previously stored netuids without + // duplicates. + let mut netuids = Pallet::::get_all_subnet_netuids(); + for (&netuid, _) in &serving_limits { + if !netuids.contains(&netuid) { + netuids.push(netuid); + } + } + let default_limit = legacy_defaults::serving_rate_limit(); + for netuid in netuids { + reads = reads.saturating_add(1); + push_limit_commit_if_non_zero( + commits, + RateLimitTarget::Group(GROUP_SERVE), + serving_limits + .get(&netuid) + .copied() + .unwrap_or(default_limit), + Some(netuid), + ); + } + + // Axon last-seen (group-shared usage). + for (netuid, hotkey, axon) in Axons::::iter() { + reads = reads.saturating_add(1); + if let Some(block) = block_number::(axon.block) { + commits.push(Commit { + target: RateLimitTarget::Group(GROUP_SERVE), + kind: CommitKind::LastSeen(MigratedLastSeen { + block, + usage: Some(RateLimitUsageKey::AccountSubnetServing { + account: hotkey.clone(), + netuid, + endpoint: ServingEndpoint::Axon, + }), + }), + }); + } + } + + // Prometheus last-seen (group-shared usage). + for (netuid, hotkey, prom) in Prometheus::::iter() { + reads = reads.saturating_add(1); + if let Some(block) = block_number::(prom.block) { + commits.push(Commit { + target: RateLimitTarget::Group(GROUP_SERVE), + kind: CommitKind::LastSeen(MigratedLastSeen { + block, + usage: Some(RateLimitUsageKey::AccountSubnetServing { + account: hotkey, + netuid, + endpoint: ServingEndpoint::Prometheus, + }), + }), + }); + } + } + + reads +} + +// Delegate take group (config + usage shared). +// usage: account +// legacy sources: TxDelegateTakeRateLimit, LastTxBlockDelegateTake +fn build_delegate_take(groups: &mut Vec, commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + groups.push(GroupConfig { + id: GROUP_DELEGATE_TAKE, + name: b"delegate-take".to_vec(), + sharing: GroupSharing::ConfigAndUsage, + members: vec![ + MigratedCall::subtensor(66, false), // increase_take + MigratedCall::subtensor(65, false), // decrease_take + ], + }); + + let target = RateLimitTarget::Group(GROUP_DELEGATE_TAKE); + let (delegate_take_limit, delegate_reads) = legacy_storage::tx_delegate_take_rate_limit(); + reads = reads.saturating_add(delegate_reads); + push_limit_commit_if_non_zero(commits, target, delegate_take_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_last_rate_limited_block( + commits, + |key| match key { + RateLimitKey::LastTxBlockDelegateTake(account) => { + Some((target, Some(RateLimitUsageKey::Account(account)))) + } + _ => None, + }, + ), + ); + + reads +} + +// Weights group (config + usage shared). +// scope: netuid +// usage: netuid+neuron/netuid+mechanism+neuron +// legacy source: WeightsSetRateLimit, LastUpdate (subnet/mechanism) +fn build_weights(groups: &mut Vec, commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + groups.push(GroupConfig { + id: GROUP_WEIGHTS_SET, + name: b"weights".to_vec(), + sharing: GroupSharing::ConfigAndUsage, + members: vec![ + MigratedCall::subtensor(0, false), // set_weights + MigratedCall::subtensor(80, false), // batch_set_weights + MigratedCall::subtensor(96, false), // commit_weights + MigratedCall::subtensor(100, false), // batch_commit_weights + MigratedCall::subtensor(113, false), // commit_timelocked_weights + MigratedCall::subtensor(97, false), // reveal_weights + MigratedCall::subtensor(98, false), // batch_reveal_weights + MigratedCall::subtensor(119, false), // set_mechanism_weights + MigratedCall::subtensor(115, false), // commit_mechanism_weights + MigratedCall::subtensor(117, false), // commit_crv3_mechanism_weights + MigratedCall::subtensor(118, false), // commit_timelocked_mechanism_weights + MigratedCall::subtensor(116, false), // reveal_mechanism_weights + ], + }); + + let (weights_limits, weights_reads) = legacy_storage::weights_set_rate_limits(); + reads = reads.saturating_add(weights_reads); + let default_limit = legacy_defaults::weights_set_rate_limit(); + for netuid in Pallet::::get_all_subnet_netuids() { + reads = reads.saturating_add(1); + push_limit_commit_if_non_zero( + commits, + RateLimitTarget::Group(GROUP_WEIGHTS_SET), + weights_limits + .get(&netuid) + .copied() + .unwrap_or(default_limit), + Some(netuid), + ); + } + + for (index, blocks) in LastUpdate::::iter() { + reads = reads.saturating_add(1); + let (netuid, mecid) = + Pallet::::get_netuid_and_subid(index).unwrap_or((NetUid::ROOT, 0.into())); + for (uid, last_block) in blocks.into_iter().enumerate() { + let Some(block) = block_number::(last_block) else { + continue; + }; + let Ok(uid_u16) = u16::try_from(uid) else { + continue; + }; + let usage = if mecid == 0.into() { + RateLimitUsageKey::SubnetNeuron { + netuid, + uid: uid_u16, + } + } else { + RateLimitUsageKey::SubnetMechanismNeuron { + netuid, + mecid, + uid: uid_u16, + } + }; + commits.push(Commit { + target: RateLimitTarget::Group(GROUP_WEIGHTS_SET), + kind: CommitKind::LastSeen(MigratedLastSeen { + block, + usage: Some(usage), + }), + }); + } + } + + reads +} + +// Register network group (config + usage shared). +// legacy sources: NetworkRateLimit, NetworkLastRegistered +fn build_register_network(groups: &mut Vec, commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + groups.push(GroupConfig { + id: GROUP_REGISTER_NETWORK, + name: b"register-network".to_vec(), + sharing: GroupSharing::ConfigAndUsage, + members: vec![ + MigratedCall::subtensor(59, false), // register_network + MigratedCall::subtensor(79, false), // register_network_with_identity + ], + }); + + let target = RateLimitTarget::Group(GROUP_REGISTER_NETWORK); + let (network_rate_limit, network_reads) = legacy_storage::network_rate_limit(); + reads = reads.saturating_add(network_reads); + push_limit_commit_if_non_zero(commits, target, network_rate_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_last_rate_limited_block( + commits, + |key| match key { + RateLimitKey::NetworkLastRegistered => Some((target, None)), + _ => None, + }, + ), + ); + + reads +} + +// Owner hyperparameter group (config shared, usage per call). +// usage: netuid +// legacy sources: OwnerHyperparamRateLimit * tempo, LastRateLimitedBlock per OwnerHyperparamUpdate +fn build_owner_hparams(groups: &mut Vec, commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + groups.push(GroupConfig { + id: GROUP_OWNER_HPARAMS, + name: b"owner-hparams".to_vec(), + sharing: GroupSharing::ConfigOnly, + members: HYPERPARAMETERS + .iter() + .filter_map(|h| identifier_for_hyperparameter(*h)) + .collect(), + }); + + let group_target = RateLimitTarget::Group(GROUP_OWNER_HPARAMS); + let (owner_limit, owner_reads) = legacy_storage::owner_hyperparam_rate_limit(); + reads = reads.saturating_add(owner_reads); + push_limit_commit_if_non_zero(commits, group_target, owner_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_last_rate_limited_block( + commits, + |key| match key { + RateLimitKey::OwnerHyperparamUpdate(netuid, hyper) => { + let Some(identifier) = identifier_for_hyperparameter(hyper) else { + return None; + }; + Some(( + RateLimitTarget::Transaction(identifier.identifier()), + Some(RateLimitUsageKey::Subnet(netuid)), + )) + } + _ => None, + }, + ), + ); + + reads +} + +// Staking ops group (config + usage shared, all ops 1 block). +// usage: coldkey+hotkey+netuid +// legacy sources: StakingOperationRateLimiter (reset every block for staking ops) +fn build_staking_ops(groups: &mut Vec, commits: &mut Vec) -> u64 { + groups.push(GroupConfig { + id: GROUP_STAKING_OPS, + name: b"staking-ops".to_vec(), + sharing: GroupSharing::ConfigAndUsage, + members: vec![ + MigratedCall::subtensor(2, false), // add_stake + MigratedCall::subtensor(88, false), // add_stake_limit + MigratedCall::subtensor(3, true), // remove_stake + MigratedCall::subtensor(89, true), // remove_stake_limit + MigratedCall::subtensor(103, true), // remove_stake_full_limit + MigratedCall::subtensor(85, false), // move_stake + MigratedCall::subtensor(86, true), // transfer_stake + MigratedCall::subtensor(87, false), // swap_stake + MigratedCall::subtensor(90, false), // swap_stake_limit + ], + }); + + push_limit_commit_if_non_zero(commits, RateLimitTarget::Group(GROUP_STAKING_OPS), 1, None); + + // we don't need to migrate last-seen since the limiter is reset every block. + + 0 +} + +// Swap hotkey/coldkey share the lock and usage; swap_coldkey bypasses enforcement but records +// usage. +// usage: account (coldkey) +// legacy sources: TxRateLimit, LastRateLimitedBlock per LastTxBlock +// +// NOTE: HotkeySwapOnSubnetInterval (per coldkey+netuid) remains enforced in pallet-subtensor +// (LastHotkeySwapOnNetuid). It is a separate legacy gate with its own span, and +// pallet-rate-limiting currently supports only one span per target, so we do not migrate it into +// this group. +fn build_swap_keys(groups: &mut Vec, commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + groups.push(GroupConfig { + id: GROUP_SWAP_KEYS, + name: b"swap-keys".to_vec(), + sharing: GroupSharing::ConfigAndUsage, + members: vec![ + MigratedCall::subtensor(70, false), // swap_hotkey + MigratedCall::subtensor(71, false), // swap_coldkey + ], + }); + + let target = RateLimitTarget::Group(GROUP_SWAP_KEYS); + let (tx_rate_limit, tx_reads) = legacy_storage::tx_rate_limit(); + reads = reads.saturating_add(tx_reads); + // Legacy check blocks at delta <= limit; pallet-rate-limiting blocks at delta < span. + // Add one block to preserve legacy behavior when legacy rate-limiting is removed. + let effective_limit = if tx_rate_limit == 0 { + 0 + } else { + tx_rate_limit.saturating_add(1) + }; + push_limit_commit_if_non_zero(commits, target, effective_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_last_rate_limited_block( + commits, + |key| match key { + RateLimitKey::LastTxBlock(account) => { + Some((target, Some(RateLimitUsageKey::Account(account)))) + } + _ => None, + }, + ), + ); + + reads +} + +// Standalone set_childkey_take. +// usage: account+netuid +// legacy sources: TxChildkeyTakeRateLimit, TransactionKeyLastBlock per SetChildkeyTake +fn build_childkey_take(commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + let target = + RateLimitTarget::Transaction(TransactionIdentifier::new(SUBTENSOR_PALLET_INDEX, 75)); + let (childkey_limit, childkey_reads) = legacy_storage::tx_childkey_take_rate_limit(); + reads = reads.saturating_add(childkey_reads); + push_limit_commit_if_non_zero(commits, target, childkey_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_transaction_key_last_block( + commits, + target, + TransactionType::SetChildkeyTake, + ), + ); + + reads +} + +// Standalone set_children. +// usage: account+netuid +// legacy sources: SET_CHILDREN_RATE_LIMIT (constant 150), TransactionKeyLastBlock per SetChildren +fn build_set_children(commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + let target = + RateLimitTarget::Transaction(TransactionIdentifier::new(SUBTENSOR_PALLET_INDEX, 67)); + push_limit_commit_if_non_zero(commits, target, SET_CHILDREN_RATE_LIMIT, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_transaction_key_last_block( + commits, + target, + TransactionType::SetChildren, + ), + ); + + reads +} + +// Standalone set_weights_version_key. +// scope: netuid +// usage: account+netuid +// legacy sources: WeightsVersionKeyRateLimit * tempo, +// TransactionKeyLastBlock per SetWeightsVersionKey +fn build_weights_version_key(commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + let target = + RateLimitTarget::Transaction(TransactionIdentifier::new(ADMIN_UTILS_PALLET_INDEX, 6)); + let (weights_version_limit, weights_version_reads) = + legacy_storage::weights_version_key_rate_limit(); + reads = reads.saturating_add(weights_version_reads); + push_limit_commit_if_non_zero(commits, target, weights_version_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_transaction_key_last_block( + commits, + target, + TransactionType::SetWeightsVersionKey, + ), + ); + + reads +} + +// Standalone set_sn_owner_hotkey. +// usage: netuid +// legacy sources: DefaultSetSNOwnerHotkeyRateLimit, LastRateLimitedBlock per SetSNOwnerHotkey +fn build_sn_owner_hotkey(commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + let target = + RateLimitTarget::Transaction(TransactionIdentifier::new(ADMIN_UTILS_PALLET_INDEX, 67)); + let sn_owner_limit = legacy_defaults::sn_owner_hotkey_rate_limit(); + reads += 1; + push_limit_commit_if_non_zero(commits, target, sn_owner_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_last_rate_limited_block( + commits, + |key| match key { + RateLimitKey::SetSNOwnerHotkey(netuid) => { + Some((target, Some(RateLimitUsageKey::Subnet(netuid)))) + } + _ => None, + }, + ), + ); + + reads +} + +// Standalone associate_evm_key. +// usage: netuid+neuron +// legacy sources: EvmKeyAssociateRateLimit, AssociatedEvmAddress +fn build_associate_evm(commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + let target = + RateLimitTarget::Transaction(TransactionIdentifier::new(SUBTENSOR_PALLET_INDEX, 93)); + reads += 1; + push_limit_commit_if_non_zero( + commits, + target, + ::EvmKeyAssociateRateLimit::get(), + None, + ); + + for (netuid, uid, (_, block)) in AssociatedEvmAddress::::iter() { + reads = reads.saturating_add(1); + let Some(block) = block_number::(block) else { + continue; + }; + commits.push(Commit { + target, + kind: CommitKind::LastSeen(MigratedLastSeen { + block, + usage: Some(RateLimitUsageKey::SubnetNeuron { netuid, uid }), + }), + }); + } + + reads +} + +// Standalone mechanism count. +// usage: account+netuid +// legacy sources: MechanismCountSetRateLimit, TransactionKeyLastBlock per MechanismCountUpdate +// sudo_set_mechanism_count +fn build_mechanism_count(commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + let target = + RateLimitTarget::Transaction(TransactionIdentifier::new(ADMIN_UTILS_PALLET_INDEX, 76)); + let mechanism_limit = legacy_defaults::mechanism_count_rate_limit(); + push_limit_commit_if_non_zero(commits, target, mechanism_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_transaction_key_last_block( + commits, + target, + TransactionType::MechanismCountUpdate, + ), + ); + + reads +} + +// Standalone mechanism emission. +// usage: account+netuid +// legacy sources: MechanismEmissionRateLimit, TransactionKeyLastBlock per MechanismEmission +// sudo_set_mechanism_emission_split +fn build_mechanism_emission(commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + let target = + RateLimitTarget::Transaction(TransactionIdentifier::new(ADMIN_UTILS_PALLET_INDEX, 77)); + let emission_limit = legacy_defaults::mechanism_emission_rate_limit(); + push_limit_commit_if_non_zero(commits, target, emission_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_transaction_key_last_block( + commits, + target, + TransactionType::MechanismEmission, + ), + ); + + reads +} + +// Standalone trim_to_max_allowed_uids. +// usage: account+netuid +// legacy sources: MaxUidsTrimmingRateLimit, TransactionKeyLastBlock per MaxUidsTrimming +// sudo_trim_to_max_allowed_uids +fn build_trim_max_uids(commits: &mut Vec) -> u64 { + let mut reads: u64 = 0; + let target = + RateLimitTarget::Transaction(TransactionIdentifier::new(ADMIN_UTILS_PALLET_INDEX, 78)); + let trim_limit = legacy_defaults::max_uids_trimming_rate_limit(); + push_limit_commit_if_non_zero(commits, target, trim_limit, None); + + reads = reads.saturating_add( + last_seen_helpers::collect_last_seen_from_transaction_key_last_block( + commits, + target, + TransactionType::MaxUidsTrimming, + ), + ); + + reads +} + +struct Commit { + target: RateLimitTarget, + kind: CommitKind, +} + +enum CommitKind { + Limit(MigratedLimit), + LastSeen(MigratedLastSeen), +} + +struct MigratedLimit { + span: BlockNumberFor, + scope: Option, +} + +struct MigratedLastSeen { + block: BlockNumberFor, + usage: Option>, +} + +struct GroupConfig { + id: GroupId, + name: Vec, + sharing: GroupSharing, + members: Vec, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct MigratedCall { + identifier: TransactionIdentifier, + read_only: bool, +} + +impl MigratedCall { + const fn new(pallet_index: u8, call_index: u8, read_only: bool) -> Self { + Self { + identifier: TransactionIdentifier::new(pallet_index, call_index), + read_only, + } + } + + const fn subtensor(call_index: u8, read_only: bool) -> Self { + Self::new(SUBTENSOR_PALLET_INDEX, call_index, read_only) + } + + const fn admin(call_index: u8, read_only: bool) -> Self { + Self::new(ADMIN_UTILS_PALLET_INDEX, call_index, read_only) + } + + pub fn identifier(&self) -> TransactionIdentifier { + self.identifier + } +} + +fn push_limit_commit_if_non_zero( + commits: &mut Vec, + target: RateLimitTarget, + span: u64, + scope: Option, +) { + if let Some(span) = block_number::(span) { + commits.push(Commit { + target, + kind: CommitKind::Limit(MigratedLimit { span, scope }), + }); + } +} + +mod last_seen_helpers { + use core::mem::discriminant; + + use super::*; + + pub(super) fn collect_last_seen_from_last_rate_limited_block( + commits: &mut Vec, + map: impl Fn( + RateLimitKey, + ) -> Option<( + RateLimitTarget, + Option>, + )>, + ) -> u64 { + let mut reads: u64 = 0; + + let (entries, iter_reads) = legacy_storage::last_rate_limited_blocks(); + reads = reads.saturating_add(iter_reads); + for (key, block) in entries { + let Some((target, usage)) = map(key) else { + continue; + }; + let Some(block) = block_number::(block) else { + continue; + }; + commits.push(Commit { + target, + kind: CommitKind::LastSeen(MigratedLastSeen { block, usage }), + }); + } + + reads + } + + pub(super) fn collect_last_seen_from_transaction_key_last_block( + commits: &mut Vec, + target: RateLimitTarget, + tx_filter: TransactionType, + ) -> u64 { + let mut reads: u64 = 0; + + let (entries, iter_reads) = legacy_storage::transaction_key_last_block(); + reads = reads.saturating_add(iter_reads); + for ((account, netuid, tx_kind), block) in entries { + let tx = TransactionType::from(tx_kind); + if discriminant(&tx) != discriminant(&tx_filter) { + continue; + } + let Some(usage) = usage_key_from_transaction_type(tx, &account, netuid) else { + continue; + }; + let Some(block) = block_number::(block) else { + continue; + }; + commits.push(Commit { + target, + kind: CommitKind::LastSeen(MigratedLastSeen { + block, + usage: Some(usage), + }), + }); + } + + reads + } +} + +// Produces the usage key for a `TransactionType` that was stored in `TransactionKeyLastBlock`. +fn usage_key_from_transaction_type( + tx: TransactionType, + account: &AccountId, + netuid: NetUid, +) -> Option> { + match tx { + TransactionType::MechanismCountUpdate + | TransactionType::MaxUidsTrimming + | TransactionType::MechanismEmission + | TransactionType::SetChildkeyTake + | TransactionType::SetChildren + | TransactionType::SetWeightsVersionKey => Some(RateLimitUsageKey::AccountSubnet { + account: account.clone(), + netuid, + }), + TransactionType::SetSNOwnerHotkey | TransactionType::OwnerHyperparamUpdate(_) => { + Some(RateLimitUsageKey::Subnet(netuid)) + } + _ => None, + } +} + +// Returns the migrated call wrapper for the admin-utils extrinsic that controls `hparam`. +// +// Only hyperparameters that are currently rate-limited (i.e. routed through +// `ensure_sn_owner_or_root_with_limits`) are mapped; others return `None`. +fn identifier_for_hyperparameter(hparam: Hyperparameter) -> Option { + use Hyperparameter::*; + + let identifier = match hparam { + ServingRateLimit => MigratedCall::admin(3, false), + MaxDifficulty => MigratedCall::admin(5, false), + AdjustmentAlpha => MigratedCall::admin(9, false), + ImmunityPeriod => MigratedCall::admin(13, false), + MinAllowedWeights => MigratedCall::admin(14, false), + MaxAllowedUids => MigratedCall::admin(15, false), + Kappa => MigratedCall::admin(16, false), + Rho => MigratedCall::admin(17, false), + ActivityCutoff => MigratedCall::admin(18, false), + PowRegistrationAllowed => MigratedCall::admin(20, false), + MinBurn => MigratedCall::admin(22, false), + MaxBurn => MigratedCall::admin(23, false), + BondsMovingAverage => MigratedCall::admin(26, false), + BondsPenalty => MigratedCall::admin(60, false), + CommitRevealEnabled => MigratedCall::admin(49, false), + LiquidAlphaEnabled => MigratedCall::admin(50, false), + AlphaValues => MigratedCall::admin(51, false), + WeightCommitInterval => MigratedCall::admin(57, false), + TransferEnabled => MigratedCall::admin(61, false), + AlphaSigmoidSteepness => MigratedCall::admin(68, false), + Yuma3Enabled => MigratedCall::admin(69, false), + BondsResetEnabled => MigratedCall::admin(70, false), + ImmuneNeuronLimit => MigratedCall::admin(72, false), + RecycleOrBurn => MigratedCall::admin(80, false), + _ => return None, + }; + + Some(identifier) +} + +fn block_number(value: u64) -> Option> { + if value == 0 { + return None; + } + Some(value.saturated_into::>()) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used)] +mod tests { + use frame_support::traits::OnRuntimeUpgrade; + use frame_system::pallet_prelude::BlockNumberFor; + use pallet_rate_limiting::{ + RateLimit, RateLimitKind, RateLimitScopeResolver, RateLimitTarget, RateLimitUsageResolver, + TransactionIdentifier, + }; + use pallet_subtensor::{ + AxonInfo, Call as SubtensorCall, HasMigrationRun, LastRateLimitedBlock, LastUpdate, + NetworksAdded, PrometheusInfo, RateLimitKey, TransactionKeyLastBlock, + WeightsVersionKeyRateLimit, utils::rate_limiting::TransactionType, + }; + use sp_core::{H160, ecdsa}; + use sp_io::TestExternalities; + use sp_runtime::traits::{SaturatedConversion, Zero}; + + use super::*; + use crate::{ + BuildStorage, RuntimeCall, RuntimeOrigin, RuntimeScopeResolver, RuntimeUsageResolver, + SubtensorModule, System, + }; + use subtensor_runtime_common::NetUidStorageIndex; + + const ACCOUNT: [u8; 32] = [7u8; 32]; + const DELEGATE_TAKE_GROUP_ID: GroupId = GROUP_DELEGATE_TAKE; + type UsageKey = RateLimitUsageKey; + + fn new_test_ext() -> TestExternalities { + sp_tracing::try_init_simple(); + let mut ext: TestExternalities = crate::RuntimeGenesisConfig::default() + .build_storage() + .expect("runtime storage") + .into(); + ext.execute_with(|| crate::System::set_block_number(1)); + ext + } + + fn new_ext() -> TestExternalities { + new_test_ext() + } + + fn account(n: u8) -> AccountId { + AccountId::from([n; 32]) + } + + fn resolve_target(identifier: TransactionIdentifier) -> RateLimitTarget { + if let Some(group) = pallet_rate_limiting::CallGroups::::get(identifier) { + RateLimitTarget::Group(group) + } else { + RateLimitTarget::Transaction(identifier) + } + } + + fn exact_span(span: u64) -> BlockNumberFor { + span.saturated_into::>() + } + + fn clear_rate_limiting_storage() { + let limit = u32::MAX; + let _ = pallet_rate_limiting::Limits::::clear(limit, None); + let _ = pallet_rate_limiting::LastSeen::::clear(limit, None); + let _ = pallet_rate_limiting::Groups::::clear(limit, None); + let _ = pallet_rate_limiting::GroupMembers::::clear(limit, None); + let _ = pallet_rate_limiting::GroupNameIndex::::clear(limit, None); + let _ = pallet_rate_limiting::CallGroups::::clear(limit, None); + pallet_rate_limiting::NextGroupId::::kill(); + } + + fn parity_check( + now: u64, + call: RuntimeCall, + origin: RuntimeOrigin, + usage_override: Option>, + scope_override: Option>, + legacy_check: F, + ) where + F: Fn() -> bool, + { + System::set_block_number(now.saturated_into()); + HasMigrationRun::::remove(MIGRATION_NAME); + clear_rate_limiting_storage(); + + // Run migration to hydrate pallet-rate-limiting state. + Migration::::on_runtime_upgrade(); + + let identifier = TransactionIdentifier::from_call(&call).expect("identifier for call"); + let scope = scope_override.or_else(|| RuntimeScopeResolver::context(&origin, &call)); + let usage: Option::UsageKey>> = + usage_override.or_else(|| RuntimeUsageResolver::context(&origin, &call)); + let target = resolve_target(identifier); + + // Use the runtime-adjusted span (handles tempo scaling for admin-utils). + let span = match scope.as_ref() { + None => pallet_rate_limiting::Pallet::::effective_span( + &origin.clone().into(), + &call, + &target, + &None, + ) + .unwrap_or_default(), + Some(scopes) => scopes + .iter() + .filter_map(|scope| { + pallet_rate_limiting::Pallet::::effective_span( + &origin.clone().into(), + &call, + &target, + &Some(*scope), + ) + }) + .max() + .unwrap_or_default(), + }; + let span_u64: u64 = span.saturated_into(); + + let usage_keys: Vec::UsageKey>> = + match usage { + None => vec![None], + Some(keys) => keys.into_iter().map(Some).collect(), + }; + + let within = usage_keys.iter().all(|key| { + pallet_rate_limiting::Pallet::::is_within_limit( + &origin.clone().into(), + &call, + &identifier, + &scope, + key, + ) + .expect("pallet rate limit result") + }); + assert_eq!(within, legacy_check(), "parity at now for {:?}", identifier); + + // Advance beyond the span and re-check (span==0 treated as allow). + let advance: BlockNumberFor = span.saturating_add(exact_span(1)); + System::set_block_number(System::block_number().saturating_add(advance)); + + let within_after = usage_keys.iter().all(|key| { + pallet_rate_limiting::Pallet::::is_within_limit( + &origin.clone().into(), + &call, + &identifier, + &scope, + key, + ) + .expect("pallet rate limit result (after)") + }); + assert!( + within_after || span_u64 == 0, + "parity after window for {:?}", + identifier + ); + } + + #[test] + fn maps_hyperparameters() { + assert_eq!( + identifier_for_hyperparameter(Hyperparameter::ServingRateLimit), + Some(MigratedCall::admin(3, false)) + ); + assert!(identifier_for_hyperparameter(Hyperparameter::MaxWeightLimit).is_none()); + } + + #[test] + fn migration_populates_limits_last_seen_and_groups() { + new_test_ext().execute_with(|| { + let account: AccountId = ACCOUNT.into(); + pallet_subtensor::HasMigrationRun::::remove(MIGRATION_NAME); + + legacy_storage::set_tx_rate_limit(10); + legacy_storage::set_tx_delegate_take_rate_limit(3); + legacy_storage::set_last_rate_limited_block( + super::RateLimitKey::LastTxBlock(account.clone()), + 5, + ); + + let weight = migrate_rate_limiting(); + assert!(!weight.is_zero()); + assert!(pallet_subtensor::HasMigrationRun::::get( + MIGRATION_NAME + )); + + let tx_target = RateLimitTarget::Group(GROUP_SWAP_KEYS); + let delegate_group = RateLimitTarget::Group(DELEGATE_TAKE_GROUP_ID); + + assert_eq!( + pallet_rate_limiting::Limits::::get(tx_target), + Some(RateLimit::Global(RateLimitKind::Exact( + 10u64.saturated_into() + ))) + ); + assert_eq!( + pallet_rate_limiting::Limits::::get(delegate_group), + Some(RateLimit::Global(RateLimitKind::Exact( + 3u64.saturated_into() + ))) + ); + + let usage_key = RateLimitUsageKey::Account(account.clone()); + assert_eq!( + pallet_rate_limiting::LastSeen::::get(tx_target, Some(usage_key.clone())), + Some(5u64.saturated_into()) + ); + + let group = pallet_rate_limiting::Groups::::get(DELEGATE_TAKE_GROUP_ID) + .expect("group stored"); + assert_eq!(group.id, DELEGATE_TAKE_GROUP_ID); + assert_eq!(group.name.as_slice(), b"delegate-take"); + assert_eq!( + pallet_rate_limiting::CallGroups::::get( + MigratedCall::subtensor(66, false).identifier() + ), + Some(DELEGATE_TAKE_GROUP_ID) + ); + assert_eq!(pallet_rate_limiting::NextGroupId::::get(), 7); + + let serve_target = RateLimitTarget::Group(GROUP_SERVE); + assert!(pallet_rate_limiting::LimitSettingRules::::contains_key(serve_target)); + assert_eq!( + pallet_rate_limiting::LimitSettingRules::::get(serve_target), + crate::rate_limiting::LimitSettingRule::RootOrSubnetOwnerAdminWindow + ); + }); + } + + #[test] + fn migrates_global_register_network_last_seen() { + new_test_ext().execute_with(|| { + HasMigrationRun::::remove(MIGRATION_NAME); + + // Seed legacy global register rate-limit state. + LastRateLimitedBlock::::insert(RateLimitKey::NetworkLastRegistered, 10u64); + System::set_block_number(12); + + // Run migration. + Migration::::on_runtime_upgrade(); + + let target = RateLimitTarget::Group(GROUP_REGISTER_NETWORK); + + // LastSeen preserved globally (usage = None). + let stored = pallet_rate_limiting::LastSeen::::get(target, None::) + .expect("last seen entry"); + assert_eq!(stored, 10u64.saturated_into::>()); + }); + } + + #[test] + fn sn_owner_hotkey_limit_not_tempo_scaled_and_last_seen_preserved() { + new_test_ext().execute_with(|| { + HasMigrationRun::::remove(MIGRATION_NAME); + + let netuid = NetUid::from(1); + // Give the subnet a non-1 tempo to catch accidental scaling. + SubtensorModule::set_tempo(netuid, 5); + LastRateLimitedBlock::::insert(RateLimitKey::SetSNOwnerHotkey(netuid), 100u64); + + Migration::::on_runtime_upgrade(); + + let target = RateLimitTarget::Transaction(TransactionIdentifier::new(19, 67)); + + // Limit should remain the fixed default (50400 blocks), not tempo-scaled. + let limit = pallet_rate_limiting::Limits::::get(target).expect("limit stored"); + assert!( + matches!(limit, RateLimit::Global(kind) if kind == RateLimitKind::Exact(50_400)) + ); + + // LastSeen preserved per subnet. + let usage: Option<::UsageKey> = + Some(UsageKey::Subnet(netuid).into()); + let stored = pallet_rate_limiting::LastSeen::::get(target, usage) + .expect("last seen entry"); + assert_eq!(stored, 100u64.saturated_into::>()); + }); + } + + #[test] + fn register_network_parity() { + new_ext().execute_with(|| { + HasMigrationRun::::remove(MIGRATION_NAME); + let now = 100u64; + let span = 5u64; + System::set_block_number(now.saturated_into()); + LastRateLimitedBlock::::insert(RateLimitKey::NetworkLastRegistered, now - 1); + legacy_storage::set_network_rate_limit(span); + + Migration::::on_runtime_upgrade(); + + let target = RateLimitTarget::Group(GROUP_REGISTER_NETWORK); + let limit = pallet_rate_limiting::Limits::::get(target).expect("limit stored"); + assert!( + matches!(limit, RateLimit::Global(kind) if kind == RateLimitKind::Exact(exact_span(span))) + ); + + let stored = pallet_rate_limiting::LastSeen::::get(target, None::) + .expect("last seen entry"); + assert_eq!(stored, (now - 1).saturated_into::>()); + }); + } + + #[test] + fn swap_hotkey_parity() { + new_ext().execute_with(|| { + let now = 200u64; + let cold = account(10); + let old_hot = account(11); + let new_hot = account(12); + let span = 10u64; + LastRateLimitedBlock::::insert( + RateLimitKey::LastTxBlock(cold.clone()), + now - 1, + ); + legacy_storage::set_tx_rate_limit(span); + + let call = RuntimeCall::SubtensorModule(SubtensorCall::swap_hotkey { + hotkey: old_hot, + new_hotkey: new_hot, + netuid: None, + }); + let origin = RuntimeOrigin::signed(cold.clone()); + let legacy = || { + let last = now - 1; + if span == 0 || last == 0 { + return true; + } + now - last > span + }; + parity_check(now, call, origin, None, None, legacy); + }); + } + + #[test] + fn increase_take_parity() { + new_ext().execute_with(|| { + let now = 300u64; + let hot = account(20); + let span = 3u64; + LastRateLimitedBlock::::insert( + RateLimitKey::LastTxBlockDelegateTake(hot.clone()), + now - 1, + ); + legacy_storage::set_tx_delegate_take_rate_limit(span); + + let call = RuntimeCall::SubtensorModule(SubtensorCall::increase_take { + hotkey: hot.clone(), + take: 5, + }); + let origin = RuntimeOrigin::signed(account(21)); + let legacy = || { + let last = now - 1; + if span == 0 || last == 0 { + return true; + } + now - last > span + }; + parity_check(now, call, origin, None, None, legacy); + }); + } + + #[test] + fn set_childkey_take_parity() { + new_ext().execute_with(|| { + let now = 400u64; + let hot = account(30); + let netuid = NetUid::from(1u16); + let span = 7u64; + let tx_kind: u16 = TransactionType::SetChildkeyTake.into(); + TransactionKeyLastBlock::::insert((hot.clone(), netuid, tx_kind), now - 1); + pallet_subtensor::TxChildkeyTakeRateLimit::::put(span); + + let call = RuntimeCall::SubtensorModule(SubtensorCall::set_childkey_take { + hotkey: hot.clone(), + netuid, + take: 1, + }); + let origin = RuntimeOrigin::signed(account(31)); + let legacy = || { + TransactionType::SetChildkeyTake + .passes_rate_limit_on_subnet::(&hot, netuid) + }; + parity_check(now, call, origin, None, None, legacy); + }); + } + + #[test] + fn set_children_parity() { + new_ext().execute_with(|| { + let now = 500u64; + let hot = account(40); + let netuid = NetUid::from(2u16); + let tx_kind: u16 = TransactionType::SetChildren.into(); + TransactionKeyLastBlock::::insert((hot.clone(), netuid, tx_kind), now - 1); + + let call = RuntimeCall::SubtensorModule(SubtensorCall::set_children { + hotkey: hot.clone(), + netuid, + children: Vec::new(), + }); + let origin = RuntimeOrigin::signed(account(41)); + let legacy = || { + TransactionType::SetChildren.passes_rate_limit_on_subnet::(&hot, netuid) + }; + parity_check(now, call, origin, None, None, legacy); + }); + } + + #[test] + fn serving_parity() { + new_ext().execute_with(|| { + let now = 600u64; + let hot = account(50); + let netuid = NetUid::from(3u16); + let span = 5u64; + legacy_storage::set_serving_rate_limit(netuid, span); + pallet_subtensor::Axons::::insert( + netuid, + hot.clone(), + AxonInfo { + block: now - 1, + ..Default::default() + }, + ); + pallet_subtensor::Prometheus::::insert( + netuid, + hot.clone(), + PrometheusInfo { + block: now - 1, + ..Default::default() + }, + ); + + // Axon + let axon_call = RuntimeCall::SubtensorModule(SubtensorCall::serve_axon { + netuid, + version: 1, + ip: 0, + port: 0, + ip_type: 4, + protocol: 0, + placeholder1: 0, + placeholder2: 0, + }); + let origin = RuntimeOrigin::signed(hot.clone()); + let legacy_axon = || { + let info = AxonInfo { + block: now.saturating_sub(1), + ..Default::default() + }; + now.saturating_sub(info.block) >= span + }; + parity_check(now, axon_call, origin.clone(), None, None, legacy_axon); + + // Prometheus + let prom_call = RuntimeCall::SubtensorModule(SubtensorCall::serve_prometheus { + netuid, + version: 1, + ip: 0, + port: 0, + ip_type: 4, + }); + let legacy_prom = || { + let info = PrometheusInfo { + block: now.saturating_sub(1), + ..Default::default() + }; + now.saturating_sub(info.block) >= span + }; + parity_check(now, prom_call, origin, None, None, legacy_prom); + }); + } + + #[test] + fn weights_and_hparam_parity() { + new_ext().execute_with(|| { + let now = 700u64; + let hot = account(60); + let netuid = NetUid::from(4u16); + let uid: u16 = 0; + let weights_span = 4u64; + let tempo = 3u16; + // Ensure subnet exists so LastUpdate is imported. + NetworksAdded::::insert(netuid, true); + SubtensorModule::set_tempo(netuid, tempo); + legacy_storage::set_weights_set_rate_limit(netuid, weights_span); + LastUpdate::::insert(NetUidStorageIndex::from(netuid), vec![now - 1]); + + let weights_call = RuntimeCall::SubtensorModule(SubtensorCall::set_weights { + netuid, + dests: Vec::new(), + weights: Vec::new(), + version_key: 0, + }); + let origin = RuntimeOrigin::signed(hot.clone()); + let scope = Some(vec![netuid]); + let usage = Some(vec![UsageKey::SubnetNeuron { netuid, uid }]); + + let legacy_weights = || { + let last = LastUpdate::::get(NetUidStorageIndex::from(netuid)) + .get(uid as usize) + .copied() + .unwrap_or_default(); + let limit = legacy_storage::get_weights_set_rate_limit(netuid); + now.saturating_sub(last) >= limit + }; + parity_check( + now, + weights_call, + origin.clone(), + usage, + scope, + legacy_weights, + ); + + // Hyperparam (activity_cutoff) with tempo scaling. + let hparam_span_epochs = 2u16; + pallet_subtensor::OwnerHyperparamRateLimit::::put(hparam_span_epochs); + LastRateLimitedBlock::::insert( + RateLimitKey::OwnerHyperparamUpdate( + netuid, + pallet_subtensor::utils::rate_limiting::Hyperparameter::ActivityCutoff, + ), + now - 1, + ); + let hparam_call = + RuntimeCall::AdminUtils(pallet_admin_utils::Call::sudo_set_activity_cutoff { + netuid, + activity_cutoff: 1, + }); + let hparam_origin = RuntimeOrigin::signed(hot); + let legacy_hparam = || { + let span = (tempo as u64) * (hparam_span_epochs as u64); + let last = now - 1; + // same logic as TransactionType::OwnerHyperparamUpdate in legacy: passes if delta >= span. + let delta = now.saturating_sub(last); + delta >= span + }; + parity_check(now, hparam_call, hparam_origin, None, None, legacy_hparam); + }); + } + + #[test] + fn weights_version_parity() { + new_ext().execute_with(|| { + let now = 800u64; + let hot = account(70); + let netuid = NetUid::from(5u16); + NetworksAdded::::insert(netuid, true); + SubtensorModule::set_tempo(netuid, 4); + WeightsVersionKeyRateLimit::::put(2u64); + let tx_kind_wvk: u16 = TransactionType::SetWeightsVersionKey.into(); + TransactionKeyLastBlock::::insert((hot.clone(), netuid, tx_kind_wvk), now - 1); + + let wvk_call = + RuntimeCall::AdminUtils(pallet_admin_utils::Call::sudo_set_weights_version_key { + netuid, + weights_version_key: 0, + }); + let origin = RuntimeOrigin::signed(hot.clone()); + let legacy_wvk = || { + let limit = SubtensorModule::get_tempo(netuid) as u64 + * WeightsVersionKeyRateLimit::::get(); + let delta = now.saturating_sub(now - 1); + delta >= limit + }; + parity_check(now, wvk_call, origin, None, None, legacy_wvk); + }); + } + + #[test] + fn associate_evm_key_parity() { + new_ext().execute_with(|| { + let now = 900u64; + let hot = account(80); + let netuid = NetUid::from(6u16); + let uid: u16 = 0; + NetworksAdded::::insert(netuid, true); + pallet_subtensor::AssociatedEvmAddress::::insert( + netuid, + uid, + (H160::zero(), now - 1), + ); + + let call = RuntimeCall::SubtensorModule(SubtensorCall::associate_evm_key { + netuid, + evm_key: H160::zero(), + block_number: now, + signature: ecdsa::Signature::from_raw([0u8; 65]), + }); + let origin = RuntimeOrigin::signed(hot.clone()); + let usage = Some(vec![UsageKey::SubnetNeuron { netuid, uid }]); + let scope = Some(vec![netuid]); + let limit = ::EvmKeyAssociateRateLimit::get(); + let legacy = || { + let last = now - 1; + let delta = now.saturating_sub(last); + delta >= limit + }; + parity_check(now, call, origin, usage, scope, legacy); + }); + } + + #[test] + fn migration_skips_when_already_run() { + new_test_ext().execute_with(|| { + pallet_subtensor::HasMigrationRun::::insert(MIGRATION_NAME, true); + legacy_storage::set_tx_rate_limit(99); + + let base_weight = ::DbWeight::get().reads(1); + let weight = migrate_rate_limiting(); + + assert_eq!(weight, base_weight); + assert!( + pallet_rate_limiting::Limits::::iter() + .next() + .is_none() + ); + assert!( + pallet_rate_limiting::LastSeen::::iter() + .next() + .is_none() + ); + }); + } +} diff --git a/runtime/src/migrations/subtensor_module.rs b/runtime/src/migrations/subtensor_module.rs new file mode 100644 index 0000000000..54e2cdddd8 --- /dev/null +++ b/runtime/src/migrations/subtensor_module.rs @@ -0,0 +1,372 @@ +use core::marker::PhantomData; + +use frame_support::{traits::Get, traits::OnRuntimeUpgrade, weights::Weight}; +use frame_system::pallet_prelude::BlockNumberFor; +use log; +use pallet_rate_limiting::{RateLimit, RateLimitKind, RateLimitTarget}; +use scale_info::prelude::string::String; +use sp_runtime::traits::SaturatedConversion; +use subtensor_runtime_common::TaoCurrency; +use subtensor_runtime_common::rate_limiting::{GROUP_REGISTER_NETWORK, GroupId}; + +use pallet_subtensor::{ + Config as SubtensorConfig, HasMigrationRun, NetworkLockReductionInterval, + NetworkRegistrationStartBlock, Pallet as SubtensorPallet, +}; + +const FOUR_DAYS: u64 = 28_800; +const EIGHT_DAYS: u64 = 57_600; +const ONE_WEEK_BLOCKS: u64 = 50_400; + +pub struct Migration(PhantomData); + +impl OnRuntimeUpgrade for Migration +where + T: SubtensorConfig + + pallet_rate_limiting::Config< + LimitScope = subtensor_runtime_common::NetUid, + GroupId = GroupId, + >, +{ + fn on_runtime_upgrade() -> Weight { + migrate_network_lock_reduction_interval::() + .saturating_add(migrate_network_lock_cost_2500::()) + } +} + +pub fn migrate_network_lock_reduction_interval() -> Weight +where + T: SubtensorConfig + + pallet_rate_limiting::Config< + LimitScope = subtensor_runtime_common::NetUid, + GroupId = GroupId, + >, +{ + let migration_name = b"migrate_network_lock_reduction_interval".to_vec(); + let mut weight = T::DbWeight::get().reads(1); + + // Skip if already executed + if HasMigrationRun::::get(&migration_name) { + log::info!( + target: "runtime", + "Migration '{}' already run - skipping.", + String::from_utf8_lossy(&migration_name) + ); + return weight; + } + + let current_block = SubtensorPallet::::get_current_block_as_u64(); + + // -- 1) Set new values -------------------------------------------------- + NetworkLockReductionInterval::::put(EIGHT_DAYS); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + pallet_rate_limiting::Limits::::insert( + RateLimitTarget::Group(GROUP_REGISTER_NETWORK), + RateLimit::Global(RateLimitKind::Exact(FOUR_DAYS.saturated_into())), + ); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + SubtensorPallet::::set_network_last_lock(TaoCurrency::from(1_000_000_000_000)); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + // Hold price at 2000 TAO until day 7, then begin linear decay + let last_lock_block = current_block.saturating_add(ONE_WEEK_BLOCKS); + + // Allow registrations starting at day 7 + NetworkRegistrationStartBlock::::put(last_lock_block); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + // Mirror the register-network last seen in pallet-rate-limiting. + let last_seen_block: BlockNumberFor = last_lock_block.saturated_into(); + pallet_rate_limiting::LastSeen::::insert( + RateLimitTarget::Group(GROUP_REGISTER_NETWORK), + None::, + last_seen_block, + ); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + // -- 2) Mark migration done -------------------------------------------- + HasMigrationRun::::insert(&migration_name, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + log::info!( + target: "runtime", + "Migration '{}' completed.", + String::from_utf8_lossy(&migration_name), + ); + + weight +} + +pub fn migrate_network_lock_cost_2500() -> Weight +where + T: SubtensorConfig + + pallet_rate_limiting::Config< + LimitScope = subtensor_runtime_common::NetUid, + GroupId = GroupId, + >, +{ + const RAO_PER_TAO: u64 = 1_000_000_000; + const TARGET_COST_TAO: u64 = 2_500; + const NEW_LAST_LOCK_RAO: u64 = (TARGET_COST_TAO / 2) * RAO_PER_TAO; // 1,250 TAO + + let migration_name = b"migrate_network_lock_cost_2500".to_vec(); + let mut weight = T::DbWeight::get().reads(1); + + // Skip if already executed + if HasMigrationRun::::get(&migration_name) { + log::info!( + target: "runtime", + "Migration '{}' already run - skipping.", + String::from_utf8_lossy(&migration_name) + ); + return weight; + } + + // Use the current block; ensure it's non-zero so mult == 2 in get_network_lock_cost() + let current_block = SubtensorPallet::::get_current_block_as_u64(); + let block_to_set = if current_block == 0 { 1 } else { current_block }; + + // Set last_lock so that price = 2 * last_lock = 2,500 TAO at this block + SubtensorPallet::::set_network_last_lock(TaoCurrency::from(NEW_LAST_LOCK_RAO)); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + // Mirror the register-network last seen in pallet-rate-limiting. + let last_seen_block: BlockNumberFor = block_to_set.saturated_into(); + pallet_rate_limiting::LastSeen::::insert( + RateLimitTarget::Group(GROUP_REGISTER_NETWORK), + None::, + last_seen_block, + ); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + // Mark migration done + HasMigrationRun::::insert(&migration_name, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + log::info!( + target: "runtime", + "Migration '{}' completed. lock_cost set to 2,500 TAO at block {}.", + String::from_utf8_lossy(&migration_name), + block_to_set + ); + + weight +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used)] +mod tests { + use frame_support::pallet_prelude::Zero; + use frame_system::pallet_prelude::BlockNumberFor; + use pallet_rate_limiting::{RateLimit, RateLimitKind, RateLimitTarget}; + use sp_io::TestExternalities; + use sp_runtime::traits::SaturatedConversion; + use subtensor_runtime_common::Currency; + use subtensor_runtime_common::rate_limiting::GROUP_REGISTER_NETWORK; + + use super::*; + use crate::{BuildStorage, Runtime, System}; + + fn new_test_ext() -> TestExternalities { + sp_tracing::try_init_simple(); + let mut ext: TestExternalities = crate::RuntimeGenesisConfig::default() + .build_storage() + .expect("runtime storage") + .into(); + ext.execute_with(|| System::set_block_number(1u64.saturated_into())); + ext + } + + fn step_block(blocks: u64) { + let next = System::block_number().saturating_add(blocks.saturated_into()); + System::set_block_number(next); + } + + fn register_network_last_seen() -> Option> { + pallet_rate_limiting::LastSeen::::get( + RateLimitTarget::Group(GROUP_REGISTER_NETWORK), + None::<::UsageKey>, + ) + } + + #[test] + fn test_migrate_network_lock_reduction_interval_and_decay() { + new_test_ext().execute_with(|| { + // -- pre -------------------------------------------------------------- + assert!( + !HasMigrationRun::::get( + b"migrate_network_lock_reduction_interval".to_vec() + ), + "HasMigrationRun should be false before migration" + ); + + // ensure current_block > 0 + step_block(1); + let current_block_before = SubtensorPallet::::get_current_block_as_u64(); + + // -- run migration --------------------------------------------------- + let weight = migrate_network_lock_reduction_interval::(); + assert!(!weight.is_zero(), "migration weight should be > 0"); + + // -- params & flags -------------------------------------------------- + assert_eq!(NetworkLockReductionInterval::::get(), EIGHT_DAYS); + assert_eq!( + pallet_rate_limiting::Limits::::get(RateLimitTarget::Group( + GROUP_REGISTER_NETWORK + )), + Some(RateLimit::Global(RateLimitKind::Exact( + FOUR_DAYS.saturated_into() + ))) + ); + assert_eq!( + SubtensorPallet::::get_network_last_lock(), + 1_000_000_000_000u64.into(), // 1000 TAO in rao + "last_lock should be 1_000_000_000_000 rao" + ); + + // last_lock_block should be set one week in the future + let last_lock_block = register_network_last_seen().expect("last seen entry"); + let expected_block = current_block_before + ONE_WEEK_BLOCKS; + assert_eq!( + last_lock_block, + expected_block.saturated_into::>(), + "last_lock_block should be current + ONE_WEEK_BLOCKS" + ); + + // registration start block should match the same future block + assert_eq!( + NetworkRegistrationStartBlock::::get(), + expected_block, + "NetworkRegistrationStartBlock should equal last_lock_block" + ); + + // lock cost should be 2000 TAO immediately after migration + let lock_cost_now = SubtensorPallet::::get_network_lock_cost(); + assert_eq!( + lock_cost_now, + 2_000_000_000_000u64.into(), + "lock cost should be 2000 TAO right after migration" + ); + + assert!( + HasMigrationRun::::get( + b"migrate_network_lock_reduction_interval".to_vec() + ), + "HasMigrationRun should be true after migration" + ); + }); + } + + #[test] + fn test_migrate_network_lock_cost_2500_sets_price_and_decay() { + new_test_ext().execute_with(|| { + // -- constants ------------------------------------------------------- + const RAO_PER_TAO: u64 = 1_000_000_000; + const TARGET_COST_TAO: u64 = 2_500; + const TARGET_COST_RAO: u64 = TARGET_COST_TAO * RAO_PER_TAO; + const NEW_LAST_LOCK_RAO: u64 = (TARGET_COST_TAO / 2) * RAO_PER_TAO; + + let migration_key = b"migrate_network_lock_cost_2500".to_vec(); + + // -- pre -------------------------------------------------------------- + assert!( + !HasMigrationRun::::get(migration_key.clone()), + "HasMigrationRun should be false before migration" + ); + + // Ensure current_block > 0 so mult == 2 in get_network_lock_cost() + step_block(1); + let current_block_before = SubtensorPallet::::get_current_block_as_u64(); + + // Snapshot interval to ensure migration doesn't change it + let interval_before = NetworkLockReductionInterval::::get(); + + // -- run migration --------------------------------------------------- + let weight = migrate_network_lock_cost_2500::(); + assert!(!weight.is_zero(), "migration weight should be > 0"); + + // -- asserts: params & flags ----------------------------------------- + assert_eq!( + SubtensorPallet::::get_network_last_lock(), + NEW_LAST_LOCK_RAO.into(), + "last_lock should be set to 1,250 TAO (in rao)" + ); + assert_eq!( + register_network_last_seen().expect("last seen entry"), + current_block_before.saturated_into::>(), + "last_lock_block should be set to the current block" + ); + + // Lock cost should be exactly 2,500 TAO immediately after migration + let lock_cost_now = SubtensorPallet::::get_network_lock_cost(); + assert_eq!( + lock_cost_now, + TARGET_COST_RAO.into(), + "lock cost should be 2,500 TAO right after migration" + ); + + // Interval should be unchanged by this migration + assert_eq!( + NetworkLockReductionInterval::::get(), + interval_before, + "lock reduction interval should not be modified by this migration" + ); + + assert!( + HasMigrationRun::::get(migration_key.clone()), + "HasMigrationRun should be true after migration" + ); + + // -- decay check (1 block later) ------------------------------------- + // Expected: cost = max(min_lock, 2*L - floor(L / eff_interval) * delta_blocks) + let eff_interval = SubtensorPallet::::get_lock_reduction_interval(); + let per_block_decrement: u64 = if eff_interval == 0 { + 0 + } else { + NEW_LAST_LOCK_RAO / eff_interval + }; + + let min_lock_rao: u64 = SubtensorPallet::::get_network_min_lock().to_u64(); + + step_block(1); + let expected_after_1: u64 = + core::cmp::max(min_lock_rao, TARGET_COST_RAO - per_block_decrement); + let lock_cost_after_1 = SubtensorPallet::::get_network_lock_cost(); + assert_eq!( + lock_cost_after_1, + expected_after_1.into(), + "lock cost should decay by one per-block step after 1 block" + ); + + // -- idempotency: running the migration again should do nothing ------ + let last_lock_before_rerun = SubtensorPallet::::get_network_last_lock(); + let last_lock_block_before_rerun = + register_network_last_seen().expect("last seen entry"); + let cost_before_rerun = SubtensorPallet::::get_network_lock_cost(); + + let _weight2 = migrate_network_lock_cost_2500::(); + + assert!( + HasMigrationRun::::get(migration_key.clone()), + "HasMigrationRun remains true on second run" + ); + assert_eq!( + SubtensorPallet::::get_network_last_lock(), + last_lock_before_rerun, + "second run should not modify last_lock" + ); + assert_eq!( + register_network_last_seen().expect("last seen entry"), + last_lock_block_before_rerun, + "second run should not modify last_lock_block" + ); + assert_eq!( + SubtensorPallet::::get_network_lock_cost(), + cost_before_rerun, + "second run should not change current lock cost" + ); + }); + } +} diff --git a/runtime/src/rate_limiting/legacy.rs b/runtime/src/rate_limiting/legacy.rs new file mode 100644 index 0000000000..dfdbdc41b7 --- /dev/null +++ b/runtime/src/rate_limiting/legacy.rs @@ -0,0 +1,372 @@ +#![allow(dead_code)] + +use codec::{Decode, Encode}; +use frame_support::{Identity, migration::storage_key_iter}; +use runtime_common::prod_or_fast; +use scale_info::TypeInfo; +use sp_io::{ + hashing::twox_128, + storage::{self as io_storage, next_key}, +}; +use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; +use subtensor_runtime_common::{NetUid, NetUidStorageIndex}; + +use super::AccountId; +use crate::{ + SubtensorInitialNetworkRateLimit, SubtensorInitialTxChildKeyTakeRateLimit, + SubtensorInitialTxDelegateTakeRateLimit, SubtensorInitialTxRateLimit, +}; + +pub use types::{Hyperparameter, RateLimitKey, TransactionType}; + +const PALLET_PREFIX: &[u8] = b"SubtensorModule"; +const BLAKE2_128_PREFIX_LEN: usize = 16; + +pub mod storage { + use super::*; + + pub fn serving_rate_limits() -> (BTreeMap, u64) { + let items: Vec<_> = + storage_key_iter::(PALLET_PREFIX, b"ServingRateLimit").collect(); + let reads = items.len() as u64; + (items.into_iter().collect(), reads) + } + + pub fn weights_set_rate_limits() -> (BTreeMap, u64) { + let items: Vec<_> = + storage_key_iter::(PALLET_PREFIX, b"WeightsSetRateLimit") + .collect(); + let reads = items.len() as u64; + (items.into_iter().collect(), reads) + } + + pub fn get_weights_set_rate_limit(netuid: NetUid) -> u64 { + let mut key = storage_prefix(PALLET_PREFIX, b"WeightsSetRateLimit"); + key.extend(netuid.encode()); + io_storage::get(&key) + .and_then(|bytes| Decode::decode(&mut &bytes[..]).ok()) + .unwrap_or_else(defaults::weights_set_rate_limit) + } + + pub fn set_weights_set_rate_limit(netuid: NetUid, span: u64) { + let mut key = storage_prefix(PALLET_PREFIX, b"WeightsSetRateLimit"); + key.extend(netuid.encode()); + io_storage::set(&key, &span.encode()); + } + + pub fn last_updates() -> (Vec<(NetUidStorageIndex, Vec)>, u64) { + let items: Vec<_> = storage_key_iter::, Identity>( + PALLET_PREFIX, + b"LastUpdate", + ) + .collect(); + let reads = items.len() as u64; + (items, reads) + } + + pub fn set_last_update(netuid_index: NetUidStorageIndex, blocks: Vec) { + let mut key = storage_prefix(PALLET_PREFIX, b"LastUpdate"); + key.extend(netuid_index.encode()); + io_storage::set(&key, &blocks.encode()); + } + + pub fn get_last_update(netuid_index: NetUidStorageIndex) -> Vec { + let mut key = storage_prefix(PALLET_PREFIX, b"LastUpdate"); + key.extend(netuid_index.encode()); + io_storage::get(&key) + .and_then(|bytes| Decode::decode(&mut &bytes[..]).ok()) + .unwrap_or_default() + } + + pub fn set_serving_rate_limit(netuid: NetUid, span: u64) { + let mut key = storage_prefix(PALLET_PREFIX, b"ServingRateLimit"); + key.extend(netuid.encode()); + io_storage::set(&key, &span.encode()); + } + + pub fn tx_rate_limit() -> (u64, u64) { + value_with_default(b"TxRateLimit", defaults::tx_rate_limit()) + } + + pub fn set_tx_rate_limit(span: u64) { + let key = storage_prefix(PALLET_PREFIX, b"TxRateLimit"); + io_storage::set(&key, &span.encode()); + } + + pub fn tx_delegate_take_rate_limit() -> (u64, u64) { + value_with_default( + b"TxDelegateTakeRateLimit", + defaults::tx_delegate_take_rate_limit(), + ) + } + + pub fn set_tx_delegate_take_rate_limit(span: u64) { + let key = storage_prefix(PALLET_PREFIX, b"TxDelegateTakeRateLimit"); + io_storage::set(&key, &span.encode()); + } + + pub fn tx_childkey_take_rate_limit() -> (u64, u64) { + value_with_default( + b"TxChildkeyTakeRateLimit", + defaults::tx_childkey_take_rate_limit(), + ) + } + + pub fn network_rate_limit() -> (u64, u64) { + value_with_default(b"NetworkRateLimit", defaults::network_rate_limit()) + } + + pub fn set_network_rate_limit(span: u64) { + let key = storage_prefix(PALLET_PREFIX, b"NetworkRateLimit"); + io_storage::set(&key, &span.encode()); + } + + pub fn owner_hyperparam_rate_limit() -> (u64, u64) { + let (value, reads) = value_with_default::( + b"OwnerHyperparamRateLimit", + defaults::owner_hyperparam_rate_limit(), + ); + (u64::from(value), reads) + } + + pub fn weights_version_key_rate_limit() -> (u64, u64) { + value_with_default( + b"WeightsVersionKeyRateLimit", + defaults::weights_version_key_rate_limit(), + ) + } + + pub fn last_rate_limited_blocks() -> (Vec<(RateLimitKey, u64)>, u64) { + let entries: Vec<_> = storage_key_iter::, u64, Identity>( + PALLET_PREFIX, + b"LastRateLimitedBlock", + ) + .collect(); + let reads = entries.len() as u64; + (entries, reads) + } + + pub fn set_last_rate_limited_block(key: RateLimitKey, block: u64) { + let mut storage_key = storage_prefix(PALLET_PREFIX, b"LastRateLimitedBlock"); + storage_key.extend(key.encode()); + io_storage::set(&storage_key, &block.encode()); + } + + pub fn transaction_key_last_block() -> (Vec<((AccountId, NetUid, u16), u64)>, u64) { + let prefix = storage_prefix(PALLET_PREFIX, b"TransactionKeyLastBlock"); + let mut cursor = prefix.clone(); + let mut entries = Vec::new(); + + while let Some(next) = next_key(&cursor) { + if !next.starts_with(&prefix) { + break; + } + if let Some(value) = io_storage::get(&next) { + let key_bytes = &next[prefix.len()..]; + if let (Some(key), Some(decoded_value)) = ( + decode_transaction_key(key_bytes), + decode_value::(&value), + ) { + entries.push((key, decoded_value)); + } + } + cursor = next; + } + + let reads = entries.len() as u64; + (entries, reads) + } + + fn storage_prefix(pallet: &[u8], storage: &[u8]) -> Vec { + [twox_128(pallet), twox_128(storage)].concat() + } + + fn value_with_default(storage_name: &[u8], default: V) -> (V, u64) { + let key = storage_prefix(PALLET_PREFIX, storage_name); + let value = io_storage::get(&key) + .and_then(|bytes| Decode::decode(&mut &bytes[..]).ok()) + .unwrap_or(default); + (value, 1) + } + + fn decode_value(bytes: &[u8]) -> Option { + Decode::decode(&mut &bytes[..]).ok() + } + + fn decode_transaction_key( + encoded: &[u8], + ) -> Option<(AccountId, NetUid, u16)> { + if encoded.len() < BLAKE2_128_PREFIX_LEN { + return None; + } + let mut slice = &encoded[BLAKE2_128_PREFIX_LEN..]; + let account = AccountId::decode(&mut slice).ok()?; + let netuid = NetUid::decode(&mut slice).ok()?; + let tx_kind = u16::decode(&mut slice).ok()?; + + Some((account, netuid, tx_kind)) + } +} + +pub mod defaults { + use super::*; + + pub fn serving_rate_limit() -> u64 { + // SubtensorInitialServingRateLimit::get() + 50 + } + + pub fn weights_set_rate_limit() -> u64 { + 100 + } + + pub fn tx_rate_limit() -> u64 { + SubtensorInitialTxRateLimit::get() + } + + pub fn tx_delegate_take_rate_limit() -> u64 { + SubtensorInitialTxDelegateTakeRateLimit::get() + } + + pub fn tx_childkey_take_rate_limit() -> u64 { + SubtensorInitialTxChildKeyTakeRateLimit::get() + } + + pub fn network_rate_limit() -> u64 { + if cfg!(feature = "pow-faucet") { + 0 + } else { + SubtensorInitialNetworkRateLimit::get() + } + } + + pub fn owner_hyperparam_rate_limit() -> u16 { + 2 + } + + pub fn weights_version_key_rate_limit() -> u64 { + 5 + } + + pub fn sn_owner_hotkey_rate_limit() -> u64 { + 50_400 + } + + pub fn mechanism_count_rate_limit() -> u64 { + prod_or_fast!(7_200, 1) + } + + pub fn mechanism_emission_rate_limit() -> u64 { + prod_or_fast!(7_200, 1) + } + + pub fn max_uids_trimming_rate_limit() -> u64 { + prod_or_fast!(30 * 7_200, 1) + } +} + +pub mod types { + use super::*; + + #[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)] + pub enum RateLimitKey { + #[codec(index = 0)] + SetSNOwnerHotkey(NetUid), + #[codec(index = 1)] + OwnerHyperparamUpdate(NetUid, Hyperparameter), + #[codec(index = 2)] + NetworkLastRegistered, + #[codec(index = 3)] + LastTxBlock(AccountId), + #[codec(index = 4)] + LastTxBlockChildKeyTake(AccountId), + #[codec(index = 5)] + LastTxBlockDelegateTake(AccountId), + } + + #[derive(Clone, Copy, Debug, PartialEq, Eq)] + #[non_exhaustive] + pub enum TransactionType { + SetChildren, + SetChildkeyTake, + Unknown, + RegisterNetwork, + SetWeightsVersionKey, + SetSNOwnerHotkey, + OwnerHyperparamUpdate(Hyperparameter), + MechanismCountUpdate, + MechanismEmission, + MaxUidsTrimming, + } + + impl From for TransactionType { + fn from(value: u16) -> Self { + match value { + 0 => TransactionType::SetChildren, + 1 => TransactionType::SetChildkeyTake, + 3 => TransactionType::RegisterNetwork, + 4 => TransactionType::SetWeightsVersionKey, + 5 => TransactionType::SetSNOwnerHotkey, + 6 => TransactionType::OwnerHyperparamUpdate(Hyperparameter::Unknown), + 7 => TransactionType::MechanismCountUpdate, + 8 => TransactionType::MechanismEmission, + 9 => TransactionType::MaxUidsTrimming, + _ => TransactionType::Unknown, + } + } + } + + impl From for u16 { + fn from(tx_type: TransactionType) -> Self { + match tx_type { + TransactionType::SetChildren => 0, + TransactionType::SetChildkeyTake => 1, + TransactionType::Unknown => 2, + TransactionType::RegisterNetwork => 3, + TransactionType::SetWeightsVersionKey => 4, + TransactionType::SetSNOwnerHotkey => 5, + TransactionType::OwnerHyperparamUpdate(_) => 6, + TransactionType::MechanismCountUpdate => 7, + TransactionType::MechanismEmission => 8, + TransactionType::MaxUidsTrimming => 9, + } + } + } + + #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug, TypeInfo)] + #[non_exhaustive] + pub enum Hyperparameter { + Unknown = 0, + ServingRateLimit = 1, + MaxDifficulty = 2, + AdjustmentAlpha = 3, + MaxWeightLimit = 4, + ImmunityPeriod = 5, + MinAllowedWeights = 6, + Kappa = 7, + Rho = 8, + ActivityCutoff = 9, + PowRegistrationAllowed = 10, + MinBurn = 11, + MaxBurn = 12, + BondsMovingAverage = 13, + BondsPenalty = 14, + CommitRevealEnabled = 15, + LiquidAlphaEnabled = 16, + AlphaValues = 17, + WeightCommitInterval = 18, + TransferEnabled = 19, + AlphaSigmoidSteepness = 20, + Yuma3Enabled = 21, + BondsResetEnabled = 22, + ImmuneNeuronLimit = 23, + RecycleOrBurn = 24, + MaxAllowedUids = 25, + } + + impl From for TransactionType { + fn from(param: Hyperparameter) -> Self { + Self::OwnerHyperparamUpdate(param) + } + } +} diff --git a/runtime/src/rate_limiting/mod.rs b/runtime/src/rate_limiting/mod.rs new file mode 100644 index 0000000000..123e3995fe --- /dev/null +++ b/runtime/src/rate_limiting/mod.rs @@ -0,0 +1,443 @@ +//! Runtime-level rate limiting wiring and resolvers. +//! +//! `pallet-rate-limiting` supports multiple independent instances, and is intended to be deployed +//! as “one instance per pallet” with pallet-specific scope/usage-key types and resolvers. +//! +//! This runtime module is centralized today because `pallet-subtensor` is currently centralized and +//! coupled with `pallet-admin-utils`; both share a single `pallet-rate-limiting` instance and a +//! single resolver implementation. +//! +//! For new pallets, do not reuse or extend the centralized scope/usage-key types or resolvers. +//! Prefer defining pallet-local types/resolvers and using a dedicated `pallet-rate-limiting` +//! instance. +//! +//! Long-term, we should refactor `pallet-subtensor` into smaller pallets and move to dedicated +//! `pallet-rate-limiting` instances per pallet. + +use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen}; +use frame_support::traits::Get; +use frame_system::RawOrigin; +use pallet_admin_utils::Call as AdminUtilsCall; +use pallet_rate_limiting::{ + BypassDecision, EnsureLimitSettingRule, RateLimitScopeResolver, RateLimitUsageResolver, +}; +use pallet_subtensor::{Call as SubtensorCall, Tempo}; +use scale_info::TypeInfo; +use serde::{Deserialize, Serialize}; +use sp_runtime::DispatchError; +use sp_std::{collections::btree_set::BTreeSet, vec, vec::Vec}; +use subtensor_runtime_common::{ + BlockNumber, NetUid, + rate_limiting::{RateLimitUsageKey, ServingEndpoint}, +}; + +use crate::{AccountId, Runtime, RuntimeCall, RuntimeOrigin}; + +pub mod legacy; + +/// Authorization rules for configuring rate limits via `pallet-rate-limiting::set_rate_limit`. +/// +/// Legacy note: historically, all rate-limit setters were `Root`-only except +/// `admin-utils::sudo_set_serving_rate_limit` (subnet-owner-or-root). We preserve that behavior by +/// requiring a `scope` value when using the [`LimitSettingRule::RootOrSubnetOwnerAdminWindow`] rule +/// and validating subnet ownership against that `scope` (`netuid`). +#[derive( + Encode, + Decode, + DecodeWithMemTracking, + Serialize, + Deserialize, + Clone, + PartialEq, + Eq, + TypeInfo, + MaxEncodedLen, + Debug, +)] +pub enum LimitSettingRule { + /// Require `Root`. + Root, + /// Allow `Root` or the subnet owner for the provided `netuid` scope. + /// + /// This rule requires `scope == Some(netuid)`. + RootOrSubnetOwnerAdminWindow, +} + +pub struct DefaultLimitSettingRule; + +impl Get for DefaultLimitSettingRule { + fn get() -> LimitSettingRule { + LimitSettingRule::Root + } +} + +pub struct LimitSettingOrigin; + +impl EnsureLimitSettingRule for LimitSettingOrigin { + fn ensure_origin( + origin: RuntimeOrigin, + rule: &LimitSettingRule, + scope: &Option, + ) -> frame_support::dispatch::DispatchResult { + match rule { + LimitSettingRule::Root => frame_system::ensure_root(origin).map_err(Into::into), + LimitSettingRule::RootOrSubnetOwnerAdminWindow => { + let netuid = scope.ok_or(DispatchError::BadOrigin)?; + pallet_subtensor::Pallet::::ensure_admin_window_open(netuid)?; + pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid) + .map(|_| ()) + .map_err(Into::into) + } + } + } +} + +#[derive(Default)] +pub struct ScopeResolver; + +impl RateLimitScopeResolver for ScopeResolver { + fn context(_origin: &RuntimeOrigin, call: &RuntimeCall) -> Option> { + match call { + RuntimeCall::SubtensorModule(inner) => match inner { + SubtensorCall::serve_axon { netuid, .. } + | SubtensorCall::serve_axon_tls { netuid, .. } + | SubtensorCall::serve_prometheus { netuid, .. } + | SubtensorCall::set_weights { netuid, .. } + | SubtensorCall::commit_weights { netuid, .. } + | SubtensorCall::reveal_weights { netuid, .. } + | SubtensorCall::batch_reveal_weights { netuid, .. } + | SubtensorCall::commit_timelocked_weights { netuid, .. } + | SubtensorCall::set_mechanism_weights { netuid, .. } + | SubtensorCall::commit_mechanism_weights { netuid, .. } + | SubtensorCall::reveal_mechanism_weights { netuid, .. } + | SubtensorCall::commit_crv3_mechanism_weights { netuid, .. } + | SubtensorCall::commit_timelocked_mechanism_weights { netuid, .. } => { + Some(vec![*netuid]) + } + SubtensorCall::batch_set_weights { netuids, .. } + | SubtensorCall::batch_commit_weights { netuids, .. } => { + let scopes: BTreeSet = + netuids.iter().map(|netuid| (*netuid).into()).collect(); + if scopes.is_empty() { + None + } else { + Some(scopes.into_iter().collect()) + } + } + _ => None, + }, + _ => None, + } + } + + fn should_bypass(origin: &RuntimeOrigin, call: &RuntimeCall) -> BypassDecision { + if let RuntimeCall::SubtensorModule(inner) = call { + if matches!(origin.clone().into(), Ok(RawOrigin::Root)) { + // swap_coldkey should record last-seen but never fail; other root calls skip. + if matches!(inner, SubtensorCall::swap_coldkey { .. }) { + return BypassDecision::bypass_and_record(); + } + return BypassDecision::bypass_and_skip(); + } + + match inner { + SubtensorCall::move_stake { + origin_netuid, + destination_netuid, + .. + } if origin_netuid == destination_netuid => { + // Legacy: same-netuid moves enforced but did not record usage. + return BypassDecision::new(false, false); + } + SubtensorCall::set_childkey_take { + hotkey, + netuid, + take, + .. + } => { + let current = + pallet_subtensor::Pallet::::get_childkey_take(hotkey, *netuid); + return if *take <= current { + BypassDecision::bypass_and_record() + } else { + BypassDecision::enforce_and_record() + }; + } + SubtensorCall::add_stake { .. } + | SubtensorCall::add_stake_limit { .. } + | SubtensorCall::decrease_take { .. } + | SubtensorCall::swap_coldkey { .. } => { + return BypassDecision::bypass_and_record(); + } + SubtensorCall::reveal_weights { netuid, .. } + | SubtensorCall::batch_reveal_weights { netuid, .. } + | SubtensorCall::reveal_mechanism_weights { netuid, .. } => { + if pallet_subtensor::Pallet::::get_commit_reveal_weights_enabled( + *netuid, + ) { + // Legacy: reveals are not rate-limited while commit-reveal is enabled. + return BypassDecision::bypass_and_skip(); + } + } + _ => {} + } + } + + BypassDecision::enforce_and_record() + } + + fn adjust_span(_origin: &RuntimeOrigin, call: &RuntimeCall, span: BlockNumber) -> BlockNumber { + match call { + RuntimeCall::AdminUtils(inner) => { + if let Some(netuid) = owner_hparam_netuid(inner) { + if span == 0 { + return span; + } + let tempo = BlockNumber::from(Tempo::::get(netuid) as u32); + span.saturating_mul(tempo) + } else if let AdminUtilsCall::sudo_set_weights_version_key { netuid, .. } = inner { + if span == 0 { + return span; + } + let tempo = BlockNumber::from(Tempo::::get(netuid) as u32); + span.saturating_mul(tempo) + } else { + span + } + } + _ => span, + } + } +} + +#[derive(Default)] +pub struct UsageResolver; + +impl RateLimitUsageResolver> + for UsageResolver +{ + fn context( + origin: &RuntimeOrigin, + call: &RuntimeCall, + ) -> Option>> { + match call { + RuntimeCall::SubtensorModule(inner) => match inner { + SubtensorCall::swap_coldkey { new_coldkey, .. } => { + Some(vec![RateLimitUsageKey::::Account( + new_coldkey.clone(), + )]) + } + SubtensorCall::swap_hotkey { .. } => { + // Enforce only by coldkey; new_hotkey last-seen is recorded in pallet-subtensor + // to avoid double enforcement while preserving legacy tracking. + let coldkey = signed_origin(origin)?; + Some(vec![RateLimitUsageKey::::Account(coldkey)]) + } + SubtensorCall::increase_take { hotkey, .. } + | SubtensorCall::decrease_take { hotkey, .. } => { + Some(vec![RateLimitUsageKey::::Account( + hotkey.clone(), + )]) + } + SubtensorCall::set_childkey_take { hotkey, netuid, .. } + | SubtensorCall::set_children { hotkey, netuid, .. } => { + Some(vec![RateLimitUsageKey::::AccountSubnet { + account: hotkey.clone(), + netuid: *netuid, + }]) + } + SubtensorCall::batch_set_weights { netuids, .. } + | SubtensorCall::batch_commit_weights { netuids, .. } => { + let mut usage = BTreeSet::new(); + for netuid in netuids { + let netuid: NetUid = (*netuid).into(); + let uid = neuron_identity(origin, netuid)?; + usage.insert(RateLimitUsageKey::::SubnetNeuron { netuid, uid }); + } + if usage.is_empty() { + None + } else { + Some(usage.into_iter().collect()) + } + } + SubtensorCall::set_weights { netuid, .. } + | SubtensorCall::commit_weights { netuid, .. } + | SubtensorCall::reveal_weights { netuid, .. } + | SubtensorCall::batch_reveal_weights { netuid, .. } + | SubtensorCall::commit_timelocked_weights { netuid, .. } => { + let uid = neuron_identity(origin, *netuid)?; + Some(vec![RateLimitUsageKey::::SubnetNeuron { + netuid: *netuid, + uid, + }]) + } + // legacy implementation still used netuid only, but it was recalculating it using + // mecid, so switching to netuid AND mecid is logical here + SubtensorCall::set_mechanism_weights { netuid, mecid, .. } + | SubtensorCall::commit_mechanism_weights { netuid, mecid, .. } + | SubtensorCall::reveal_mechanism_weights { netuid, mecid, .. } + | SubtensorCall::commit_crv3_mechanism_weights { netuid, mecid, .. } + | SubtensorCall::commit_timelocked_mechanism_weights { netuid, mecid, .. } => { + let uid = neuron_identity(origin, *netuid)?; + Some(vec![ + RateLimitUsageKey::::SubnetMechanismNeuron { + netuid: *netuid, + mecid: *mecid, + uid, + }, + ]) + } + SubtensorCall::serve_axon { netuid, .. } + | SubtensorCall::serve_axon_tls { netuid, .. } => { + let hotkey = signed_origin(origin)?; + Some(vec![RateLimitUsageKey::::AccountSubnetServing { + account: hotkey, + netuid: *netuid, + endpoint: ServingEndpoint::Axon, + }]) + } + SubtensorCall::serve_prometheus { netuid, .. } => { + let hotkey = signed_origin(origin)?; + Some(vec![RateLimitUsageKey::::AccountSubnetServing { + account: hotkey, + netuid: *netuid, + endpoint: ServingEndpoint::Prometheus, + }]) + } + SubtensorCall::associate_evm_key { netuid, .. } => { + let hotkey = signed_origin(origin)?; + let uid = pallet_subtensor::Pallet::::get_uid_for_net_and_hotkey( + *netuid, &hotkey, + ) + .ok()?; + Some(vec![RateLimitUsageKey::::SubnetNeuron { + netuid: *netuid, + uid, + }]) + } + // Staking calls share a group lock; only add_* write usage, the rest are read-only. + // Keep the usage key granular so the lock applies per (coldkey, hotkey, netuid). + SubtensorCall::add_stake { hotkey, netuid, .. } + | SubtensorCall::add_stake_limit { hotkey, netuid, .. } + | SubtensorCall::remove_stake { hotkey, netuid, .. } + | SubtensorCall::remove_stake_limit { hotkey, netuid, .. } + | SubtensorCall::remove_stake_full_limit { hotkey, netuid, .. } + | SubtensorCall::transfer_stake { + hotkey, + origin_netuid: netuid, + .. + } => { + let coldkey = signed_origin(origin)?; + Some(vec![RateLimitUsageKey::::ColdkeyHotkeySubnet { + coldkey, + hotkey: hotkey.clone(), + netuid: *netuid, + }]) + } + SubtensorCall::swap_stake { + hotkey, + destination_netuid: netuid, + .. + } + | SubtensorCall::swap_stake_limit { + hotkey, + destination_netuid: netuid, + .. + } => { + let coldkey = signed_origin(origin)?; + Some(vec![RateLimitUsageKey::::ColdkeyHotkeySubnet { + coldkey, + hotkey: hotkey.clone(), + netuid: *netuid, + }]) + } + SubtensorCall::move_stake { + origin_hotkey, + destination_hotkey, + origin_netuid, + destination_netuid, + .. + } => { + let coldkey = signed_origin(origin)?; + let (hotkey, netuid) = if origin_netuid == destination_netuid { + (origin_hotkey, origin_netuid) + } else { + (destination_hotkey, destination_netuid) + }; + Some(vec![RateLimitUsageKey::::ColdkeyHotkeySubnet { + coldkey, + hotkey: hotkey.clone(), + netuid: *netuid, + }]) + } + _ => None, + }, + RuntimeCall::AdminUtils(inner) => { + if let Some(netuid) = owner_hparam_netuid(inner) { + Some(vec![RateLimitUsageKey::::Subnet(netuid)]) + } else { + match inner { + AdminUtilsCall::sudo_set_sn_owner_hotkey { netuid, .. } => { + Some(vec![RateLimitUsageKey::::Subnet(*netuid)]) + } + AdminUtilsCall::sudo_set_weights_version_key { netuid, .. } + | AdminUtilsCall::sudo_set_mechanism_count { netuid, .. } + | AdminUtilsCall::sudo_set_mechanism_emission_split { netuid, .. } + | AdminUtilsCall::sudo_trim_to_max_allowed_uids { netuid, .. } => { + let who = signed_origin(origin)?; + Some(vec![RateLimitUsageKey::::AccountSubnet { + account: who, + netuid: *netuid, + }]) + } + _ => None, + } + } + } + _ => None, + } + } +} + +fn neuron_identity(origin: &RuntimeOrigin, netuid: NetUid) -> Option { + let hotkey = signed_origin(origin)?; + let uid = + pallet_subtensor::Pallet::::get_uid_for_net_and_hotkey(netuid, &hotkey).ok()?; + Some(uid) +} + +fn signed_origin(origin: &RuntimeOrigin) -> Option { + match origin.clone().into() { + Ok(RawOrigin::Signed(who)) => Some(who), + _ => None, + } +} + +fn owner_hparam_netuid(call: &AdminUtilsCall) -> Option { + match call { + AdminUtilsCall::sudo_set_activity_cutoff { netuid, .. } + | AdminUtilsCall::sudo_set_adjustment_alpha { netuid, .. } + | AdminUtilsCall::sudo_set_alpha_sigmoid_steepness { netuid, .. } + | AdminUtilsCall::sudo_set_alpha_values { netuid, .. } + | AdminUtilsCall::sudo_set_bonds_moving_average { netuid, .. } + | AdminUtilsCall::sudo_set_bonds_penalty { netuid, .. } + | AdminUtilsCall::sudo_set_bonds_reset_enabled { netuid, .. } + | AdminUtilsCall::sudo_set_commit_reveal_weights_enabled { netuid, .. } + | AdminUtilsCall::sudo_set_commit_reveal_weights_interval { netuid, .. } + | AdminUtilsCall::sudo_set_immunity_period { netuid, .. } + | AdminUtilsCall::sudo_set_liquid_alpha_enabled { netuid, .. } + | AdminUtilsCall::sudo_set_max_allowed_uids { netuid, .. } + | AdminUtilsCall::sudo_set_max_burn { netuid, .. } + | AdminUtilsCall::sudo_set_max_difficulty { netuid, .. } + | AdminUtilsCall::sudo_set_min_allowed_weights { netuid, .. } + | AdminUtilsCall::sudo_set_min_burn { netuid, .. } + | AdminUtilsCall::sudo_set_network_pow_registration_allowed { netuid, .. } + | AdminUtilsCall::sudo_set_owner_immune_neuron_limit { netuid, .. } + | AdminUtilsCall::sudo_set_recycle_or_burn { netuid, .. } + | AdminUtilsCall::sudo_set_rho { netuid, .. } + | AdminUtilsCall::sudo_set_serving_rate_limit { netuid, .. } + | AdminUtilsCall::sudo_set_toggle_transfer { netuid, .. } + | AdminUtilsCall::sudo_set_yuma3_enabled { netuid, .. } => Some(*netuid), + _ => None, + } +} diff --git a/runtime/tests/common/mock.rs b/runtime/tests/common/mock.rs new file mode 100644 index 0000000000..fe3bacfc80 --- /dev/null +++ b/runtime/tests/common/mock.rs @@ -0,0 +1,52 @@ +use node_subtensor_runtime::{RuntimeGenesisConfig, System}; +use sp_io::TestExternalities; +use sp_runtime::BuildStorage; +use sp_runtime::traits::SaturatedConversion; +use subtensor_runtime_common::{AccountId, Balance}; + +pub struct ExtBuilder { + balances: Vec<(AccountId, Balance)>, + block_number: u64, +} + +impl Default for ExtBuilder { + fn default() -> Self { + Self { + balances: Vec::new(), + block_number: 1, + } + } +} + +impl ExtBuilder { + pub fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self { + self.balances = balances; + self + } + + pub fn with_block_number(mut self, block_number: u64) -> Self { + self.block_number = block_number; + self + } + + pub fn build(self) -> TestExternalities { + let mut ext: TestExternalities = RuntimeGenesisConfig { + balances: pallet_balances::GenesisConfig { + balances: self.balances, + dev_accounts: None, + }, + ..Default::default() + } + .build_storage() + .expect("runtime genesis config builds") + .into(); + + let block_number = self.block_number; + ext.execute_with(|| System::set_block_number(block_number.saturated_into())); + ext + } +} + +pub fn new_test_ext() -> TestExternalities { + ExtBuilder::default().build() +} diff --git a/runtime/tests/common/mod.rs b/runtime/tests/common/mod.rs new file mode 100644 index 0000000000..27c914e371 --- /dev/null +++ b/runtime/tests/common/mod.rs @@ -0,0 +1,4 @@ +#![allow(dead_code)] +pub mod mock; + +pub use mock::*; diff --git a/runtime/tests/rate_limiting.rs b/runtime/tests/rate_limiting.rs new file mode 100644 index 0000000000..7827624f6f --- /dev/null +++ b/runtime/tests/rate_limiting.rs @@ -0,0 +1,1167 @@ +#![allow(clippy::unwrap_used)] + +use codec::{Compact, Encode}; +use frame_support::{assert_ok, traits::Get}; +use node_subtensor_runtime::{ + Executive, HotkeySwapOnSubnetInterval, Runtime, RuntimeCall, SignedPayload, + SubtensorInitialTxDelegateTakeRateLimit, System, TransactionExtensions, UncheckedExtrinsic, + check_nonce, + rate_limiting::legacy::{RateLimitKey, storage as legacy_storage}, + sudo_wrapper, transaction_payment_wrapper, +}; +use pallet_rate_limiting::RateLimitTarget; +use sp_core::{H256, Pair, sr25519}; +use sp_runtime::{ + BoundedVec, MultiSignature, + generic::Era, + traits::SaturatedConversion, + transaction_validity::{InvalidTransaction, TransactionValidityError}, +}; +use subtensor_runtime_common::{ + AccountId, AlphaCurrency, Currency, MechId, NetUid, + rate_limiting::{GROUP_SWAP_KEYS, RateLimitUsageKey}, +}; + +use common::ExtBuilder; + +mod common; + +fn assert_extrinsic_ok(account_id: &AccountId, pair: &sr25519::Pair, call: RuntimeCall) { + let nonce = System::account(account_id).nonce; + let xt = signed_extrinsic(call, pair, nonce); + assert_ok!(Executive::apply_extrinsic(xt)); +} + +fn assert_sudo_extrinsic_ok( + sudo_account: &AccountId, + sudo_pair: &sr25519::Pair, + call: RuntimeCall, +) { + let sudo_call = RuntimeCall::Sudo(pallet_sudo::Call::sudo { + call: Box::new(call), + }); + assert_extrinsic_ok(sudo_account, sudo_pair, sudo_call); +} + +fn assert_extrinsic_rate_limited(account_id: &AccountId, pair: &sr25519::Pair, call: RuntimeCall) { + let nonce = System::account(account_id).nonce; + let xt = signed_extrinsic(call, pair, nonce); + assert!(matches!( + Executive::apply_extrinsic(xt).expect_err("rate limit enforced"), + TransactionValidityError::Invalid(InvalidTransaction::Custom(1)) + )); +} + +fn signed_extrinsic(call: RuntimeCall, pair: &sr25519::Pair, nonce: u32) -> UncheckedExtrinsic { + let check_metadata_hash = + frame_metadata_hash_extension::CheckMetadataHash::::new(false); + + let extra: TransactionExtensions = ( + frame_system::CheckNonZeroSender::::new(), + frame_system::CheckSpecVersion::::new(), + frame_system::CheckTxVersion::::new(), + frame_system::CheckGenesis::::new(), + frame_system::CheckEra::::from(Era::Immortal), + check_nonce::CheckNonce::::from(nonce).into(), + frame_system::CheckWeight::::new(), + transaction_payment_wrapper::ChargeTransactionPaymentWrapper::new( + pallet_transaction_payment::ChargeTransactionPayment::::from(0), + ), + sudo_wrapper::SudoTransactionExtension::::new(), + pallet_subtensor::transaction_extension::SubtensorTransactionExtension::::new(), + ( + pallet_drand::drand_priority::DrandPriority::::new(), + check_metadata_hash, + ), + pallet_rate_limiting::RateLimitTransactionExtension::::new(), + ); + + let payload = SignedPayload::new(call.clone(), extra.clone()).expect("signed payload"); + let signature = MultiSignature::from(pair.sign(payload.encode().as_slice())); + let address = sp_runtime::MultiAddress::Id(AccountId::from(pair.public())); + UncheckedExtrinsic::new_signed(call, address, signature, extra) +} + +mod register_network { + use super::*; + + #[test] + fn register_network_is_rate_limited_after_migration() { + let coldkey_pair = sr25519::Pair::from_seed(&[1u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let hotkey_a = AccountId::from([2u8; 32]); + let hotkey_b = AccountId::from([3u8; 32]); + let balance = 10_000_000_000_000_u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + + // Run runtime upgrades explicitly so rate-limiting config is seeded for tests. + Executive::execute_on_runtime_upgrade(); + + let call_a = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::register_network { + hotkey: hotkey_a, + }); + let call_b = RuntimeCall::SubtensorModule( + pallet_subtensor::Call::register_network_with_identity { + hotkey: hotkey_b, + identity: None, + }, + ); + let start_block = pallet_subtensor::NetworkRegistrationStartBlock::::get() + .saturated_into(); + + System::set_block_number(start_block); + + assert_extrinsic_ok(&coldkey, &coldkey_pair, call_a.clone()); + + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, call_b.clone()); + + // Migration sets register-network limit to 4 days (28_800 blocks). + let limit = start_block + 28_800; + + // Should still be rate-limited. + System::set_block_number(limit - 1); + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, call_a.clone()); + + // Should pass now. + System::set_block_number(limit); + assert_extrinsic_ok(&coldkey, &coldkey_pair, call_b); + + // Both calls share the same usage key and window. + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, call_a.clone()); + + System::set_block_number(limit + 28_800); + assert_extrinsic_ok(&coldkey, &coldkey_pair, call_a); + }); + } +} + +mod serving { + use super::*; + + #[test] + fn serving_is_rate_limited_after_migration() { + let coldkey_pair = sr25519::Pair::from_seed(&[4u8; 32]); + let hotkey_pair = sr25519::Pair::from_seed(&[5u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let hotkey = AccountId::from(hotkey_pair.public()); + let balance = 10_000_000_000_000_u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + // Run runtime upgrades explicitly so rate-limiting config is seeded for tests. + Executive::execute_on_runtime_upgrade(); + + assert_extrinsic_ok( + &coldkey, + &coldkey_pair, + RuntimeCall::SubtensorModule(pallet_subtensor::Call::root_register { + hotkey: hotkey.clone(), + }), + ); + + let netuid = NetUid::ROOT; + let start_block = System::block_number(); + let serve_axon = RuntimeCall::SubtensorModule(pallet_subtensor::Call::serve_axon { + netuid, + version: 1, + ip: 0, + port: 3030, + ip_type: 4, + protocol: 0, + placeholder1: 0, + placeholder2: 0, + }); + let serve_axon_tls = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::serve_axon_tls { + netuid, + version: 1, + ip: 0, + port: 3030, + ip_type: 4, + protocol: 0, + placeholder1: 0, + placeholder2: 0, + certificate: b"cert".to_vec(), + }); + let serve_prometheus = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::serve_prometheus { + netuid, + version: 1, + ip: 1_676_056_785, + port: 3031, + ip_type: 4, + }); + + assert_extrinsic_ok(&hotkey, &hotkey_pair, serve_axon.clone()); + + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, serve_axon_tls.clone()); + + assert_extrinsic_ok(&hotkey, &hotkey_pair, serve_prometheus.clone()); + + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, serve_prometheus.clone()); + + // Migration sets serving limit to 50 blocks by default. + let limit = start_block + 50; + + // Should still be rate-limited. + System::set_block_number(limit - 1); + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, serve_axon.clone()); + + // Should pass now. + System::set_block_number(limit); + assert_extrinsic_ok(&hotkey, &hotkey_pair, serve_axon_tls); + + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, serve_axon); + + assert_extrinsic_ok(&hotkey, &hotkey_pair, serve_prometheus.clone()); + + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, serve_prometheus); + }); + } +} + +mod delegate_take { + use super::*; + + #[test] + fn delegate_take_increase_is_rate_limited_after_migration() { + let coldkey_pair = sr25519::Pair::from_seed(&[6u8; 32]); + let hotkey_pair = sr25519::Pair::from_seed(&[7u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let hotkey = AccountId::from(hotkey_pair.public()); + let balance = 10_000_000_000_000_u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + // Run runtime upgrades explicitly so rate-limiting config is seeded for tests. + Executive::execute_on_runtime_upgrade(); + + assert_extrinsic_ok( + &coldkey, + &coldkey_pair, + RuntimeCall::SubtensorModule(pallet_subtensor::Call::root_register { + hotkey: hotkey.clone(), + }), + ); + + // Seed current take so increase_take passes take checks. + pallet_subtensor::Delegates::::insert(&hotkey, 1u16); + + let increase_once = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::increase_take { + hotkey: hotkey.clone(), + take: 2u16, + }); + let increase_twice = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::increase_take { + hotkey: hotkey.clone(), + take: 3u16, + }); + + let start_block = System::block_number(); + + assert_extrinsic_ok(&coldkey, &coldkey_pair, increase_once); + + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, increase_twice.clone()); + + let limit = SubtensorInitialTxDelegateTakeRateLimit::get(); + let limit_block = start_block + limit.saturated_into::(); + let allowed_block = limit_block + 1; + + System::set_block_number(limit_block - 1); + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, increase_twice.clone()); + + System::set_block_number(allowed_block); + assert_extrinsic_ok(&coldkey, &coldkey_pair, increase_twice); + }); + } + + #[test] + fn delegate_take_decrease_is_not_rate_limited_after_migration() { + let coldkey_pair = sr25519::Pair::from_seed(&[10u8; 32]); + let hotkey_pair = sr25519::Pair::from_seed(&[11u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let hotkey = AccountId::from(hotkey_pair.public()); + let balance = 10_000_000_000_000_u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + // Run runtime upgrades explicitly so rate-limiting config is seeded for tests. + Executive::execute_on_runtime_upgrade(); + + assert_extrinsic_ok( + &coldkey, + &coldkey_pair, + RuntimeCall::SubtensorModule(pallet_subtensor::Call::root_register { + hotkey: hotkey.clone(), + }), + ); + + // Seed current take so decreases are valid and deterministic. + pallet_subtensor::Delegates::::insert(&hotkey, 3u16); + + let decrease_once = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::decrease_take { + hotkey: hotkey.clone(), + take: 2u16, + }); + let decrease_twice = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::decrease_take { + hotkey: hotkey.clone(), + take: 1u16, + }); + + assert_extrinsic_ok(&coldkey, &coldkey_pair, decrease_once); + assert_extrinsic_ok(&coldkey, &coldkey_pair, decrease_twice); + }); + } + + #[test] + fn delegate_take_decrease_blocks_immediate_increase_after_migration() { + let coldkey_pair = sr25519::Pair::from_seed(&[8u8; 32]); + let hotkey_pair = sr25519::Pair::from_seed(&[9u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let hotkey = AccountId::from(hotkey_pair.public()); + let balance = 10_000_000_000_000_u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + // Run runtime upgrades explicitly so rate-limiting config is seeded for tests. + Executive::execute_on_runtime_upgrade(); + + assert_extrinsic_ok( + &coldkey, + &coldkey_pair, + RuntimeCall::SubtensorModule(pallet_subtensor::Call::root_register { + hotkey: hotkey.clone(), + }), + ); + + // Seed current take so decrease then increase remains valid. + pallet_subtensor::Delegates::::insert(&hotkey, 2u16); + + let decrease = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::decrease_take { + hotkey: hotkey.clone(), + take: 1u16, + }); + let increase = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::increase_take { + hotkey: hotkey.clone(), + take: 2u16, + }); + + let start_block = System::block_number(); + + assert_extrinsic_ok(&coldkey, &coldkey_pair, decrease); + + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, increase.clone()); + + let limit = SubtensorInitialTxDelegateTakeRateLimit::get(); + let limit_block = start_block + limit.saturated_into::(); + let allowed_block = limit_block + 1; + + System::set_block_number(limit_block - 1); + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, increase.clone()); + + System::set_block_number(allowed_block); + assert_extrinsic_ok(&coldkey, &coldkey_pair, increase); + }); + } +} + +mod weights { + use super::*; + + fn setup_weights_network(netuid: NetUid, hotkey: &AccountId, block: u64, mechanisms: u8) { + pallet_subtensor::Pallet::::init_new_network(netuid, 1); + if mechanisms > 1 { + pallet_subtensor::MechanismCountCurrent::::insert( + netuid, + MechId::from(mechanisms), + ); + } + System::set_block_number(block.saturated_into()); + pallet_subtensor::Pallet::::append_neuron(netuid, hotkey, block); + } + + #[test] + fn weights_set_is_rate_limited_after_migration() { + let hotkey_pair = sr25519::Pair::from_seed(&[12u8; 32]); + let hotkey = AccountId::from(hotkey_pair.public()); + let netuid = NetUid::from(1u16); + let span = 3u64; + let registration_block = 1u64; + + ExtBuilder::default() + .with_balances(vec![(hotkey.clone(), 10_000_000_000_000_u64)]) + .build() + .execute_with(|| { + setup_weights_network(netuid, &hotkey, registration_block, 1); + legacy_storage::set_weights_set_rate_limit(netuid, span); + + Executive::execute_on_runtime_upgrade(); + + pallet_subtensor::Pallet::::set_commit_reveal_weights_enabled( + netuid, false, + ); + + let version_key = pallet_subtensor::WeightsVersionKey::::get(netuid); + let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::set_weights { + netuid, + dests: vec![0], + weights: vec![u16::MAX], + version_key, + }); + + System::set_block_number(registration_block.saturated_into()); + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, call.clone()); + + System::set_block_number((registration_block + span - 1).saturated_into()); + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, call.clone()); + + System::set_block_number((registration_block + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, call.clone()); + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, call.clone()); + + System::set_block_number((registration_block + span + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, call); + }); + } + + #[test] + fn commit_weights_shares_rate_limit_with_set_weights() { + let hotkey_pair = sr25519::Pair::from_seed(&[13u8; 32]); + let hotkey = AccountId::from(hotkey_pair.public()); + let netuid = NetUid::from(2u16); + let span = 4u64; + let registration_block = 1u64; + let commit_hash = H256::from_low_u64_be(42); + + ExtBuilder::default() + .with_balances(vec![(hotkey.clone(), 10_000_000_000_000_u64)]) + .build() + .execute_with(|| { + setup_weights_network(netuid, &hotkey, registration_block, 1); + legacy_storage::set_weights_set_rate_limit(netuid, span); + + Executive::execute_on_runtime_upgrade(); + + let commit_call = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::commit_weights { + netuid, + commit_hash, + }); + + System::set_block_number((registration_block + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, commit_call); + + pallet_subtensor::Pallet::::set_commit_reveal_weights_enabled( + netuid, false, + ); + + let version_key = pallet_subtensor::WeightsVersionKey::::get(netuid); + let set_call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::set_weights { + netuid, + dests: vec![0], + weights: vec![u16::MAX], + version_key, + }); + + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, set_call.clone()); + + System::set_block_number((registration_block + span + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, set_call); + }); + } + + #[test] + fn commit_timelocked_weights_is_rate_limited_after_migration() { + let hotkey_pair = sr25519::Pair::from_seed(&[14u8; 32]); + let hotkey = AccountId::from(hotkey_pair.public()); + let netuid = NetUid::from(3u16); + let span = 4u64; + let registration_block = 1u64; + let commit = BoundedVec::try_from(vec![1u8; 16]).expect("commit payload within limit"); + let reveal_round = 10u64; + + ExtBuilder::default() + .with_balances(vec![(hotkey.clone(), 10_000_000_000_000_u64)]) + .build() + .execute_with(|| { + setup_weights_network(netuid, &hotkey, registration_block, 1); + legacy_storage::set_weights_set_rate_limit(netuid, span); + + Executive::execute_on_runtime_upgrade(); + + let commit_reveal_version = + pallet_subtensor::Pallet::::get_commit_reveal_weights_version(); + let commit_call = RuntimeCall::SubtensorModule( + pallet_subtensor::Call::commit_timelocked_weights { + netuid, + commit: commit.clone(), + reveal_round, + commit_reveal_version, + }, + ); + + System::set_block_number((registration_block + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, commit_call.clone()); + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, commit_call.clone()); + + System::set_block_number((registration_block + span + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, commit_call); + }); + } + + #[test] + fn commit_crv3_mechanism_weights_are_rate_limited_per_mechanism() { + let hotkey_pair = sr25519::Pair::from_seed(&[15u8; 32]); + let hotkey = AccountId::from(hotkey_pair.public()); + let netuid = NetUid::from(4u16); + let span = 4u64; + let registration_block = 1u64; + let commit = BoundedVec::try_from(vec![1u8; 16]).expect("commit payload within limit"); + let reveal_round = 10u64; + let mecid_a = MechId::from(0u8); + let mecid_b = MechId::from(1u8); + + ExtBuilder::default() + .with_balances(vec![(hotkey.clone(), 10_000_000_000_000_u64)]) + .build() + .execute_with(|| { + setup_weights_network(netuid, &hotkey, registration_block, 2); + legacy_storage::set_weights_set_rate_limit(netuid, span); + + Executive::execute_on_runtime_upgrade(); + + let commit_a = RuntimeCall::SubtensorModule( + pallet_subtensor::Call::commit_crv3_mechanism_weights { + netuid, + mecid: mecid_a, + commit: commit.clone(), + reveal_round, + }, + ); + let commit_b = RuntimeCall::SubtensorModule( + pallet_subtensor::Call::commit_crv3_mechanism_weights { + netuid, + mecid: mecid_b, + commit: commit.clone(), + reveal_round, + }, + ); + + System::set_block_number((registration_block + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, commit_a.clone()); + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, commit_a); + assert_extrinsic_ok(&hotkey, &hotkey_pair, commit_b); + }); + } + + #[test] + fn batch_set_weights_is_rate_limited_if_any_scope_is_within_span() { + let hotkey_pair = sr25519::Pair::from_seed(&[16u8; 32]); + let hotkey = AccountId::from(hotkey_pair.public()); + let netuid_a = NetUid::from(5u16); + let netuid_b = NetUid::from(6u16); + let span = 3u64; + let registration_block = 1u64; + + ExtBuilder::default() + .with_balances(vec![(hotkey.clone(), 10_000_000_000_000_u64)]) + .build() + .execute_with(|| { + setup_weights_network(netuid_a, &hotkey, registration_block, 1); + setup_weights_network(netuid_b, &hotkey, registration_block, 1); + legacy_storage::set_weights_set_rate_limit(netuid_a, span); + legacy_storage::set_weights_set_rate_limit(netuid_b, span); + + Executive::execute_on_runtime_upgrade(); + + pallet_subtensor::Pallet::::set_commit_reveal_weights_enabled( + netuid_a, false, + ); + pallet_subtensor::Pallet::::set_commit_reveal_weights_enabled( + netuid_b, false, + ); + + let version_key_a = pallet_subtensor::WeightsVersionKey::::get(netuid_a); + let version_key_b = pallet_subtensor::WeightsVersionKey::::get(netuid_b); + + let set_call_a = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::set_weights { + netuid: netuid_a, + dests: vec![0], + weights: vec![u16::MAX], + version_key: version_key_a, + }); + + System::set_block_number((registration_block + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, set_call_a); + + let batch_call = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::batch_set_weights { + netuids: vec![Compact(netuid_a), Compact(netuid_b)], + weights: vec![ + vec![(Compact(0u16), Compact(1u16))], + vec![(Compact(0u16), Compact(1u16))], + ], + version_keys: vec![Compact(version_key_a), Compact(version_key_b)], + }); + + assert_extrinsic_rate_limited(&hotkey, &hotkey_pair, batch_call.clone()); + + System::set_block_number((registration_block + span + span).saturated_into()); + assert_extrinsic_ok(&hotkey, &hotkey_pair, batch_call); + }); + } +} + +mod staking_ops { + use super::*; + + fn setup_staking_network(netuid: NetUid) { + pallet_subtensor::Pallet::::init_new_network(netuid, 1); + pallet_subtensor::SubtokenEnabled::::insert(netuid, true); + pallet_subtensor::TransferToggle::::insert(netuid, true); + } + + fn seed_stake(netuid: NetUid, hotkey: &AccountId, coldkey: &AccountId, alpha: u64) { + pallet_subtensor::Pallet::::create_account_if_non_existent(coldkey, hotkey); + pallet_subtensor::Pallet::::increase_stake_for_hotkey_and_coldkey_on_subnet( + hotkey, + coldkey, + netuid, + AlphaCurrency::from(alpha), + ); + } + + #[test] + fn staking_add_then_remove_is_rate_limited_after_migration() { + let coldkey_pair = sr25519::Pair::from_seed(&[20u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let hotkey = AccountId::from([21u8; 32]); + let netuid = NetUid::from(10u16); + let stake_amount = pallet_subtensor::DefaultMinStake::::get().to_u64() * 10; + let balance = stake_amount * 10; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + setup_staking_network(netuid); + pallet_subtensor::Pallet::::create_account_if_non_existent( + &coldkey, &hotkey, + ); + + Executive::execute_on_runtime_upgrade(); + + let add_call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::add_stake { + hotkey: hotkey.clone(), + netuid, + amount_staked: stake_amount.into(), + }); + assert_extrinsic_ok(&coldkey, &coldkey_pair, add_call); + + let alpha = + pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey, &coldkey, netuid, + ); + let remove_call = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::remove_stake { + hotkey, + netuid, + amount_unstaked: alpha, + }); + + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, remove_call.clone()); + + System::set_block_number(2); + assert_extrinsic_ok(&coldkey, &coldkey_pair, remove_call); + }); + } + + #[test] + fn transfer_stake_is_rate_limited_after_add_stake() { + let coldkey_pair = sr25519::Pair::from_seed(&[22u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let destination_coldkey = AccountId::from([23u8; 32]); + let hotkey = AccountId::from([24u8; 32]); + let netuid = NetUid::from(11u16); + let stake_amount = pallet_subtensor::DefaultMinStake::::get().to_u64() * 10; + let balance = stake_amount * 10; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + setup_staking_network(netuid); + pallet_subtensor::Pallet::::create_account_if_non_existent( + &coldkey, &hotkey, + ); + + Executive::execute_on_runtime_upgrade(); + + let add_call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::add_stake { + hotkey: hotkey.clone(), + netuid, + amount_staked: stake_amount.into(), + }); + assert_extrinsic_ok(&coldkey, &coldkey_pair, add_call); + + let alpha = + pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey, &coldkey, netuid, + ); + let transfer_call = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::transfer_stake { + destination_coldkey, + hotkey, + origin_netuid: netuid, + destination_netuid: netuid, + alpha_amount: alpha, + }); + + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, transfer_call); + }); + } + + #[test] + fn transfer_stake_does_not_limit_destination_coldkey() { + let coldkey_pair = sr25519::Pair::from_seed(&[25u8; 32]); + let destination_pair = sr25519::Pair::from_seed(&[26u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let destination_coldkey = AccountId::from(destination_pair.public()); + let hotkey = AccountId::from([27u8; 32]); + let origin_netuid = NetUid::from(12u16); + let destination_netuid = NetUid::from(13u16); + let stake_amount = pallet_subtensor::DefaultMinStake::::get().to_u64() * 10; + + ExtBuilder::default() + .with_balances(vec![ + (coldkey.clone(), stake_amount * 10), + (destination_coldkey.clone(), stake_amount * 10), + ]) + .build() + .execute_with(|| { + System::set_block_number(1); + setup_staking_network(origin_netuid); + setup_staking_network(destination_netuid); + seed_stake(origin_netuid, &hotkey, &coldkey, stake_amount); + + Executive::execute_on_runtime_upgrade(); + + let alpha = + pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey, + &coldkey, + origin_netuid, + ); + let transfer_call = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::transfer_stake { + destination_coldkey: destination_coldkey.clone(), + hotkey: hotkey.clone(), + origin_netuid, + destination_netuid, + alpha_amount: alpha, + }); + + assert_extrinsic_ok(&coldkey, &coldkey_pair, transfer_call); + + let destination_alpha = + pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey, + &destination_coldkey, + destination_netuid, + ); + let remove_call = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::remove_stake { + hotkey, + netuid: destination_netuid, + amount_unstaked: destination_alpha, + }); + + assert_extrinsic_ok(&destination_coldkey, &destination_pair, remove_call); + }); + } + + #[test] + fn swap_stake_limits_destination_netuid() { + let coldkey_pair = sr25519::Pair::from_seed(&[28u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let hotkey = AccountId::from([29u8; 32]); + let origin_netuid = NetUid::from(14u16); + let destination_netuid = NetUid::from(15u16); + let stake_amount = pallet_subtensor::DefaultMinStake::::get().to_u64() * 10; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), stake_amount * 10)]) + .build() + .execute_with(|| { + System::set_block_number(1); + setup_staking_network(origin_netuid); + setup_staking_network(destination_netuid); + seed_stake(origin_netuid, &hotkey, &coldkey, stake_amount); + + Executive::execute_on_runtime_upgrade(); + + let alpha = + pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey, + &coldkey, + origin_netuid, + ); + let swap_alpha = AlphaCurrency::from(alpha.to_u64() / 2); + let swap_call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_stake { + hotkey: hotkey.clone(), + origin_netuid, + destination_netuid, + alpha_amount: swap_alpha, + }); + + assert_extrinsic_ok(&coldkey, &coldkey_pair, swap_call); + + let destination_alpha = + pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey, + &coldkey, + destination_netuid, + ); + let remove_destination = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::remove_stake { + hotkey: hotkey.clone(), + netuid: destination_netuid, + amount_unstaked: destination_alpha, + }); + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, remove_destination); + + let origin_alpha = + pallet_subtensor::Pallet::::get_stake_for_hotkey_and_coldkey_on_subnet( + &hotkey, + &coldkey, + origin_netuid, + ); + let remove_origin = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::remove_stake { + hotkey, + netuid: origin_netuid, + amount_unstaked: origin_alpha, + }); + assert_extrinsic_ok(&coldkey, &coldkey_pair, remove_origin); + }); + } +} + +mod swap_keys { + use super::*; + + fn setup_swap_hotkey_state( + netuid: NetUid, + coldkey: &AccountId, + hotkey: &AccountId, + block: u64, + ) { + pallet_subtensor::Pallet::::init_new_network(netuid, 1); + System::set_block_number(block.saturated_into()); + pallet_subtensor::Pallet::::append_neuron(netuid, hotkey, block); + pallet_subtensor::Pallet::::create_account_if_non_existent(coldkey, hotkey); + } + + #[test] + fn swap_hotkey_tx_rate_limit_exceeded_all_subnets() { + let coldkey_pair = sr25519::Pair::from_seed(&[30u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let old_hotkey = AccountId::from([31u8; 32]); + let new_hotkey_a = AccountId::from([32u8; 32]); + let new_hotkey_b = AccountId::from([33u8; 32]); + let netuid = NetUid::from(20u16); + let balance = 10_000_000_000_000_u64; + let legacy_span = 3u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + setup_swap_hotkey_state(netuid, &coldkey, &old_hotkey, 1); + + legacy_storage::set_tx_rate_limit(legacy_span); + Executive::execute_on_runtime_upgrade(); + + let swap_first = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_hotkey { + hotkey: old_hotkey.clone(), + new_hotkey: new_hotkey_a.clone(), + netuid: None, + }); + let swap_second = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_hotkey { + hotkey: new_hotkey_a.clone(), + new_hotkey: new_hotkey_b.clone(), + netuid: None, + }); + + let start_block: u64 = System::block_number().saturated_into(); + + assert_extrinsic_ok(&coldkey, &coldkey_pair, swap_first); + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, swap_second.clone()); + + let limit_block = start_block.saturating_add(legacy_span); + System::set_block_number(limit_block.saturated_into()); + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, swap_second.clone()); + + System::set_block_number((limit_block + 1).saturated_into()); + assert_extrinsic_ok(&coldkey, &coldkey_pair, swap_second); + }); + } + + #[test] + fn swap_hotkey_tx_rate_limit_exceeded_on_subnet() { + let coldkey_pair = sr25519::Pair::from_seed(&[34u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let old_hotkey = AccountId::from([35u8; 32]); + let new_hotkey_a = AccountId::from([36u8; 32]); + let new_hotkey_b = AccountId::from([37u8; 32]); + let netuid = NetUid::from(21u16); + let balance = 10_000_000_000_000_u64; + let legacy_span = 3u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + setup_swap_hotkey_state(netuid, &coldkey, &old_hotkey, 1); + + legacy_storage::set_tx_rate_limit(legacy_span); + Executive::execute_on_runtime_upgrade(); + + let interval: u64 = HotkeySwapOnSubnetInterval::get().saturated_into(); + let start_block = interval.saturating_add(1); + System::set_block_number(start_block.saturated_into()); + + let swap_subnet = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_hotkey { + hotkey: old_hotkey.clone(), + new_hotkey: new_hotkey_a.clone(), + netuid: Some(netuid), + }); + let swap_subnet_again = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_hotkey { + hotkey: old_hotkey.clone(), + new_hotkey: new_hotkey_a.clone(), + netuid: Some(netuid), + }); + + assert_extrinsic_ok(&coldkey, &coldkey_pair, swap_subnet); + assert_extrinsic_rate_limited(&coldkey, &coldkey_pair, swap_subnet_again); + + let limit_block = start_block.saturating_add(legacy_span + 1); + System::set_block_number(limit_block.saturated_into()); + + let swap_all = RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_hotkey { + hotkey: new_hotkey_a.clone(), + new_hotkey: new_hotkey_b.clone(), + netuid: None, + }); + assert_extrinsic_ok(&coldkey, &coldkey_pair, swap_all); + }); + } + + #[test] + fn swap_hotkey_transfers_last_seen_all_subnets() { + let coldkey_pair = sr25519::Pair::from_seed(&[38u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let old_hotkey = AccountId::from([39u8; 32]); + let new_hotkey = AccountId::from([40u8; 32]); + let netuid = NetUid::from(22u16); + let balance = 10_000_000_000_000_u64; + let legacy_last_seen = 7u64; + let childkey_last_seen = 91u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + setup_swap_hotkey_state(netuid, &coldkey, &old_hotkey, 1); + + legacy_storage::set_last_rate_limited_block( + RateLimitKey::LastTxBlock(old_hotkey.clone()), + legacy_last_seen, + ); + pallet_subtensor::Pallet::::set_last_tx_block_childkey( + &old_hotkey, + childkey_last_seen, + ); + + Executive::execute_on_runtime_upgrade(); + + let target = RateLimitTarget::Group(GROUP_SWAP_KEYS); + let usage_old = RateLimitUsageKey::Account(old_hotkey.clone()); + let usage_new = RateLimitUsageKey::Account(new_hotkey.clone()); + assert_eq!( + pallet_rate_limiting::LastSeen::::get(target, Some(usage_old.clone())), + Some(legacy_last_seen.saturated_into()) + ); + + System::set_block_number(10); + let swap_call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_hotkey { + hotkey: old_hotkey.clone(), + new_hotkey: new_hotkey.clone(), + netuid: None, + }); + assert_extrinsic_ok(&coldkey, &coldkey_pair, swap_call); + + assert_eq!( + pallet_rate_limiting::LastSeen::::get(target, Some(usage_new)), + Some(legacy_last_seen.saturated_into()) + ); + assert_eq!( + pallet_rate_limiting::LastSeen::::get(target, Some(usage_old)), + None + ); + assert_eq!( + pallet_subtensor::Pallet::::get_last_tx_block_childkey_take( + &new_hotkey + ), + childkey_last_seen + ); + }); + } + + #[test] + fn swap_hotkey_transfers_last_seen_on_subnet() { + let coldkey_pair = sr25519::Pair::from_seed(&[41u8; 32]); + let coldkey = AccountId::from(coldkey_pair.public()); + let old_hotkey = AccountId::from([42u8; 32]); + let new_hotkey = AccountId::from([43u8; 32]); + let netuid = NetUid::from(23u16); + let balance = 10_000_000_000_000_u64; + let legacy_last_seen = 9u64; + let childkey_last_seen = 97u64; + + ExtBuilder::default() + .with_balances(vec![(coldkey.clone(), balance)]) + .build() + .execute_with(|| { + System::set_block_number(1); + setup_swap_hotkey_state(netuid, &coldkey, &old_hotkey, 1); + + legacy_storage::set_last_rate_limited_block( + RateLimitKey::LastTxBlock(old_hotkey.clone()), + legacy_last_seen, + ); + pallet_subtensor::Pallet::::set_last_tx_block_childkey( + &old_hotkey, + childkey_last_seen, + ); + + Executive::execute_on_runtime_upgrade(); + + let target = RateLimitTarget::Group(GROUP_SWAP_KEYS); + let usage_new = RateLimitUsageKey::Account(new_hotkey.clone()); + + let interval: u64 = HotkeySwapOnSubnetInterval::get().saturated_into(); + System::set_block_number(interval.saturating_add(1).saturated_into()); + + let swap_call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_hotkey { + hotkey: old_hotkey.clone(), + new_hotkey: new_hotkey.clone(), + netuid: Some(netuid), + }); + assert_extrinsic_ok(&coldkey, &coldkey_pair, swap_call); + + assert_eq!( + pallet_rate_limiting::LastSeen::::get(target, Some(usage_new)), + Some(legacy_last_seen.saturated_into()) + ); + assert_eq!( + pallet_subtensor::Pallet::::get_last_tx_block_childkey_take( + &new_hotkey + ), + childkey_last_seen + ); + }); + } + + // NOTE: This currently fails. When `swap_coldkey` is dispatched via `Sudo::sudo`, rate-limiting + // sees the outer sudo call, so `swap_coldkey` does not record usage in the swap-keys group. Keep + // this test to flag the issue until the rate-limiting extension unwraps sudo calls. + #[test] + fn swap_coldkey_records_usage_for_swap_keys_group() { + let sudo_pair = sr25519::Pair::from_seed(&[44u8; 32]); + let new_coldkey_pair = sr25519::Pair::from_seed(&[45u8; 32]); + let sudo_account = AccountId::from(sudo_pair.public()); + let old_coldkey = AccountId::from([46u8; 32]); + let new_coldkey = AccountId::from(new_coldkey_pair.public()); + let old_hotkey = AccountId::from([47u8; 32]); + let new_hotkey = AccountId::from([48u8; 32]); + let balance = 10_000_000_000_000_u64; + let legacy_span = 3u64; + let swap_cost = 1u64; + + ExtBuilder::default() + .with_balances(vec![ + (sudo_account.clone(), balance), + (old_coldkey.clone(), balance), + (new_coldkey.clone(), balance), + ]) + .build() + .execute_with(|| { + System::set_block_number(10); + pallet_sudo::Key::::put(sudo_account.clone()); + legacy_storage::set_tx_rate_limit(legacy_span); + Executive::execute_on_runtime_upgrade(); + + let swap_coldkey_call = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_coldkey { + old_coldkey: old_coldkey.clone(), + new_coldkey: new_coldkey.clone(), + swap_cost: swap_cost.into(), + }); + assert_sudo_extrinsic_ok(&sudo_account, &sudo_pair, swap_coldkey_call); + + let target = RateLimitTarget::Group(GROUP_SWAP_KEYS); + let usage_new = RateLimitUsageKey::Account(new_coldkey.clone()); + assert_eq!( + pallet_rate_limiting::LastSeen::::get(target, Some(usage_new.clone())), + Some(10u64.saturated_into()) + ); + + pallet_subtensor::Pallet::::create_account_if_non_existent( + &new_coldkey, + &old_hotkey, + ); + + let swap_hotkey_call = + RuntimeCall::SubtensorModule(pallet_subtensor::Call::swap_hotkey { + hotkey: old_hotkey.clone(), + new_hotkey: new_hotkey.clone(), + netuid: None, + }); + assert_extrinsic_rate_limited(&new_coldkey, &new_coldkey_pair, swap_hotkey_call); + }); + } +}