From 811d38aac6b780f27ebce9ac08effb9a9ba8c1ab Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Fri, 15 Nov 2024 21:04:23 +0100 Subject: [PATCH] Setup rate limit runtime API --- node/src/client.rs | 4 +++- .../subtensor/src/rpc_info/rate_limit_info.rs | 12 ++++++------ runtime/src/lib.rs | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/node/src/client.rs b/node/src/client.rs index c7196b5a9..bd4fcc676 100644 --- a/node/src/client.rs +++ b/node/src/client.rs @@ -51,6 +51,7 @@ pub trait RuntimeApiCollection< + subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi + subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi + subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi + + subtensor_custom_rpc_runtime_api::RateLimitInfoRuntimeApi { } @@ -71,6 +72,7 @@ where + subtensor_custom_rpc_runtime_api::DelegateInfoRuntimeApi + subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi + subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi - + subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi, + + subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi + + subtensor_custom_rpc_runtime_api::RateLimitInfoRuntimeApi, { } diff --git a/pallets/subtensor/src/rpc_info/rate_limit_info.rs b/pallets/subtensor/src/rpc_info/rate_limit_info.rs index efa4e7c15..60b40ad08 100644 --- a/pallets/subtensor/src/rpc_info/rate_limit_info.rs +++ b/pallets/subtensor/src/rpc_info/rate_limit_info.rs @@ -1,14 +1,14 @@ -//! 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, + freeze_struct, utils::rate_limiting::TransactionType, Config, Pallet, TargetStakesPerInterval, + TxRateLimit, }; /// Transaction rate limits. -// #[freeze_struct("fe79d58173da662a")] +#[freeze_struct("e3734bd0690f2da8")] #[derive(Decode, Encode, Clone, Debug)] pub struct RateLimits { transaction: Compact, @@ -18,7 +18,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 { hotkey: AccountId, @@ -48,7 +48,7 @@ impl Pallet { 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, } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 547467f1a..41fdc61f8 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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, @@ -2014,6 +2015,24 @@ impl_runtime_apis! { SubtensorModule::get_network_lock_cost() } } + + impl subtensor_custom_rpc_runtime_api::RateLimitInfoRuntimeApi for Runtime { + fn get_rate_limits() -> Vec { + SubtensorModule::get_rate_limits().encode() + } + + fn get_limited_tx_info_for_hotkey(hotkey: Vec, netuid: u16) -> Vec { + 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, hotkey: Vec) -> 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)]