Skip to content

Commit e00342f

Browse files
committed
Implement rate-limiting resolvers for pallet-subtensor
1 parent 11ba1b6 commit e00342f

File tree

10 files changed

+357
-12
lines changed

10 files changed

+357
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ use sp_runtime::{
1515
use subtensor_macros::freeze_struct;
1616

1717
pub use currency::*;
18+
pub use rate_limiting::{RateLimitScope, RateLimitUsageKey};
1819

1920
mod currency;
21+
mod rate_limiting;
2022

2123
/// Balance of an account.
2224
pub type Balance = u64;

common/src/rate_limiting.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
2+
use frame_support::pallet_prelude::Parameter;
3+
use scale_info::TypeInfo;
4+
use serde::{Deserialize, Serialize};
5+
6+
use crate::{MechId, NetUid};
7+
8+
#[derive(
9+
Serialize,
10+
Deserialize,
11+
Encode,
12+
Decode,
13+
DecodeWithMemTracking,
14+
Clone,
15+
Copy,
16+
PartialEq,
17+
Eq,
18+
PartialOrd,
19+
Ord,
20+
Debug,
21+
TypeInfo,
22+
MaxEncodedLen,
23+
)]
24+
pub enum RateLimitScope {
25+
Subnet(NetUid),
26+
SubnetMechanism { netuid: NetUid, mecid: MechId },
27+
}
28+
29+
#[derive(
30+
Serialize,
31+
Deserialize,
32+
Encode,
33+
Decode,
34+
DecodeWithMemTracking,
35+
Clone,
36+
PartialEq,
37+
Eq,
38+
PartialOrd,
39+
Ord,
40+
Debug,
41+
TypeInfo,
42+
MaxEncodedLen,
43+
)]
44+
#[scale_info(skip_type_params(AccountId))]
45+
pub enum RateLimitUsageKey<AccountId: Parameter> {
46+
Account(AccountId),
47+
Subnet(NetUid),
48+
AccountSubnet {
49+
account: AccountId,
50+
netuid: NetUid,
51+
},
52+
ColdkeyHotkeySubnet {
53+
coldkey: AccountId,
54+
hotkey: AccountId,
55+
netuid: NetUid,
56+
},
57+
SubnetNeuron {
58+
netuid: NetUid,
59+
uid: u16,
60+
},
61+
SubnetMechanismNeuron {
62+
netuid: NetUid,
63+
mecid: MechId,
64+
uid: u16,
65+
},
66+
}

pallets/rate-limiting/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ frame-benchmarking = { workspace = true, optional = true }
1515
frame-support.workspace = true
1616
frame-system.workspace = true
1717
scale-info = { workspace = true, features = ["derive"] }
18-
serde = { workspace = true, features = ["derive"], optional = true }
18+
serde = { workspace = true, features = ["derive"] }
1919
sp-std.workspace = true
2020
sp-runtime.workspace = true
2121
subtensor-runtime-common.workspace = true
@@ -33,7 +33,7 @@ std = [
3333
"frame-support/std",
3434
"frame-system/std",
3535
"scale-info/std",
36-
"serde",
36+
"serde/std",
3737
"sp-std/std",
3838
"sp-runtime/std",
3939
"subtensor-runtime-common/std",

pallets/rate-limiting/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub mod pallet {
145145
};
146146
use frame_system::pallet_prelude::*;
147147
use sp_runtime::traits::{DispatchOriginOf, Dispatchable, Saturating, Zero};
148-
use sp_std::{convert::TryFrom, marker::PhantomData, vec::Vec};
148+
use sp_std::{boxed::Box, convert::TryFrom, marker::PhantomData, vec::Vec};
149149

150150
#[cfg(feature = "runtime-benchmarks")]
151151
use crate::benchmarking::BenchmarkHelper as BenchmarkHelperTrait;

pallets/rate-limiting/src/types.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
22
use frame_support::{pallet_prelude::DispatchError, traits::GetCallMetadata};
33
use scale_info::TypeInfo;
4+
use serde::{Deserialize, Serialize};
45
use sp_std::collections::btree_map::BTreeMap;
56

67
/// Resolves the optional identifier within which a rate limit applies and can optionally adjust
@@ -31,8 +32,9 @@ pub trait RateLimitUsageResolver<Origin, Call, Usage> {
3132
}
3233

3334
/// Identifies a runtime call by pallet and extrinsic indices.
34-
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
3535
#[derive(
36+
Serialize,
37+
Deserialize,
3638
Clone,
3739
Copy,
3840
PartialEq,
@@ -101,8 +103,9 @@ impl TransactionIdentifier {
101103
}
102104

103105
/// Policy describing the block span enforced by a rate limit.
104-
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
105106
#[derive(
107+
Serialize,
108+
Deserialize,
106109
Clone,
107110
Copy,
108111
PartialEq,
@@ -125,14 +128,21 @@ pub enum RateLimitKind<BlockNumber> {
125128
///
126129
/// The configuration is mutually exclusive: either the call is globally limited or it stores a set
127130
/// of per-scope spans.
128-
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
129-
#[cfg_attr(
130-
feature = "std",
131-
serde(
132-
bound = "Scope: Ord + serde::Serialize + serde::de::DeserializeOwned, BlockNumber: serde::Serialize + serde::de::DeserializeOwned"
133-
)
131+
#[derive(
132+
Serialize,
133+
Deserialize,
134+
Clone,
135+
PartialEq,
136+
Eq,
137+
Encode,
138+
Decode,
139+
DecodeWithMemTracking,
140+
TypeInfo,
141+
Debug,
142+
)]
143+
#[serde(
144+
bound = "Scope: Ord + serde::Serialize + serde::de::DeserializeOwned, BlockNumber: serde::Serialize + serde::de::DeserializeOwned"
134145
)]
135-
#[derive(Clone, PartialEq, Eq, Encode, Decode, DecodeWithMemTracking, TypeInfo, Debug)]
136146
pub enum RateLimit<Scope, BlockNumber> {
137147
/// Global span applied to every invocation.
138148
Global(RateLimitKind<BlockNumber>),

pallets/subtensor/src/utils/rate_limiting.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use codec::{Decode, Encode};
2+
use scale_info::TypeInfo;
13
use subtensor_runtime_common::NetUid;
24

35
use super::*;

runtime/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ frame-system = { workspace = true }
3838
frame-try-runtime = { workspace = true, optional = true }
3939
pallet-timestamp.workspace = true
4040
pallet-transaction-payment.workspace = true
41+
pallet-rate-limiting.workspace = true
4142
pallet-subtensor-utility.workspace = true
4243
frame-executive.workspace = true
4344
frame-metadata-hash-extension.workspace = true
@@ -187,6 +188,7 @@ std = [
187188
"pallet-timestamp/std",
188189
"pallet-transaction-payment-rpc-runtime-api/std",
189190
"pallet-transaction-payment/std",
191+
"pallet-rate-limiting/std",
190192
"pallet-subtensor-utility/std",
191193
"pallet-sudo/std",
192194
"pallet-multisig/std",
@@ -328,6 +330,7 @@ try-runtime = [
328330
"pallet-insecure-randomness-collective-flip/try-runtime",
329331
"pallet-timestamp/try-runtime",
330332
"pallet-transaction-payment/try-runtime",
333+
"pallet-rate-limiting/try-runtime",
331334
"pallet-subtensor-utility/try-runtime",
332335
"pallet-safe-mode/try-runtime",
333336
"pallet-subtensor/try-runtime",

runtime/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use core::num::NonZeroU64;
1212

1313
pub mod check_nonce;
1414
mod migrations;
15+
mod rate_limiting;
1516
pub mod transaction_payment_wrapper;
1617

1718
extern crate alloc;
@@ -70,6 +71,10 @@ use subtensor_precompiles::Precompiles;
7071
use subtensor_runtime_common::{AlphaCurrency, TaoCurrency, time::*, *};
7172
use subtensor_swap_interface::{Order, SwapHandler};
7273

74+
pub use rate_limiting::{
75+
ScopeResolver as RuntimeScopeResolver, UsageResolver as RuntimeUsageResolver,
76+
};
77+
7378
// A few exports that help ease life for downstream crates.
7479
pub use frame_support::{
7580
StorageValue, construct_runtime, parameter_types,

0 commit comments

Comments
 (0)