Skip to content

Commit

Permalink
Setup rate limit runtime API
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-otf committed Nov 15, 2024
1 parent 76f85ab commit e4f0073
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 3 additions & 1 deletion node/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub trait RuntimeApiCollection<
+ subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi<Block>
+ subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi<Block>
+ subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi<Block>
+ subtensor_custom_rpc_runtime_api::RateLimitInfoRuntimeApi<Block>
{
}

Expand All @@ -71,6 +72,7 @@ where
+ subtensor_custom_rpc_runtime_api::DelegateInfoRuntimeApi<Block>
+ subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi<Block>
+ subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi<Block>
+ subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi<Block>,
+ subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi<Block>
+ subtensor_custom_rpc_runtime_api::RateLimitInfoRuntimeApi<Block>
{
}
11 changes: 5 additions & 6 deletions pallets/subtensor/src/rpc_info/rate_limit_info.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! API for getting transaction rate limits associated with coldkeys and hotkeys.

//! API for getting rate-limited transactions info.
use codec::Compact;
use frame_support::pallet_prelude::{Decode, Encode};

use crate::{
utils::rate_limiting::TransactionType, Config, Pallet, TargetStakesPerInterval, TxRateLimit,
utils::rate_limiting::TransactionType, freeze_struct, Config, Pallet, TargetStakesPerInterval, TxRateLimit,
};

/// Transaction rate limits.
// #[freeze_struct("fe79d58173da662a")]
#[freeze_struct("e3734bd0690f2da8")]
#[derive(Decode, Encode, Clone, Debug)]
pub struct RateLimits {
transaction: Compact<u64>,
Expand All @@ -18,7 +17,7 @@ pub struct RateLimits {
}

/// Contains last blocks, when rate-limited transactions was evaluated.
// #[freeze_struct("fe79d58173da662a")]
#[freeze_struct("9154106cd720ce08")]
#[derive(Decode, Encode, Clone, Debug)]
pub struct HotkeyLimitedTxInfo<AccountId> {
hotkey: AccountId,
Expand Down Expand Up @@ -48,7 +47,7 @@ impl<T: Config> Pallet<T> {
Self::get_last_transaction_block(hotkey, netuid, &TransactionType::SetChildkeyTake)
.into();
HotkeyLimitedTxInfo {
hotkey: hotkey.to_owned(),
hotkey: hotkey.clone(),
last_block_set_children,
last_block_set_childkey_take,
}
Expand Down
19 changes: 19 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use scale_info::TypeInfo;
use smallvec::smallvec;
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::hexdisplay::AsBytesRef;
use sp_core::{
crypto::{ByteArray, KeyTypeId},
OpaqueMetadata, H160, H256, U256,
Expand Down Expand Up @@ -2014,6 +2015,24 @@ impl_runtime_apis! {
SubtensorModule::get_network_lock_cost()
}
}

impl subtensor_custom_rpc_runtime_api::RateLimitInfoRuntimeApi<Block> for Runtime {
fn get_rate_limits() -> Vec<u8> {
SubtensorModule::get_rate_limits().encode()
}

fn get_limited_tx_info_for_hotkey(hotkey: Vec<u8>, netuid: u16) -> Vec<u8> {
let hotkey = AccountId::decode(&mut hotkey.as_bytes_ref()).expect("Could not decode account ID");
SubtensorModule::get_limited_tx_info_for_hotkey(&hotkey, netuid).encode()
}

fn get_stakes_this_interval(coldkey: Vec<u8>, hotkey: Vec<u8>) -> u64 {
let coldkey = AccountId::decode(&mut coldkey.as_bytes_ref()).expect("Could not decode account ID");
let hotkey = AccountId::decode(&mut hotkey.as_bytes_ref()).expect("Could not decode account ID");

SubtensorModule::get_stakes_this_interval_for_coldkey_hotkey(&coldkey, &hotkey)
}
}
}

// #[cfg(test)]
Expand Down

0 comments on commit e4f0073

Please sign in to comment.