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 18, 2024
1 parent 76f85ab commit f75a486
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 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>,
{
}
12 changes: 6 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,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<u64>,
Expand All @@ -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<AccountId> {
hotkey: AccountId,
Expand Down Expand Up @@ -48,7 +48,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
2 changes: 1 addition & 1 deletion pallets/subtensor/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ fn test_childkey_single_parent_emission() {
});

// Make all stakes old enough and viable
step_block((subnet_tempo * 2) as u16);
step_block((subnet_tempo * 2));

// Set parent-child relationship (with 50% proportion and 0 childkey take)
assert_ok!(SubtensorModule::do_set_children(
Expand Down
41 changes: 19 additions & 22 deletions pallets/subtensor/tests/emission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ fn test_maximum_nominators_scenario() {

assert_eq!(emission_tuples.len(), max_nominators + 1);
let total_distributed: u64 = emission_tuples
.iter()
.map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.values()
.map(|ev| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.sum();
assert_eq!(total_distributed, emission);
});
Expand Down Expand Up @@ -630,8 +630,8 @@ fn test_rounding_and_precision() {
);

let total_distributed: u64 = emission_tuples
.iter()
.map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.values()
.map(|ev| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.sum();
assert_eq!(
total_distributed, emission,
Expand Down Expand Up @@ -762,8 +762,8 @@ fn test_remainder_distribution() {
);

let total_distributed: u64 = emission_tuples
.iter()
.map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.values()
.map(|ev| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.sum();
assert_eq!(
total_distributed, emission,
Expand Down Expand Up @@ -803,8 +803,8 @@ fn test_different_network_ids_scenario() {
);

let total_distributed: u64 = emission_tuples
.iter()
.map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.values()
.map(|ev| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.sum();
assert_eq!(
total_distributed, emission,
Expand Down Expand Up @@ -845,8 +845,8 @@ fn test_large_emission_values() {
);

let total_distributed: u64 = emission_tuples
.iter()
.map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.values()
.map(|ev| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.sum();
assert_eq!(
total_distributed, emission,
Expand Down Expand Up @@ -904,8 +904,8 @@ fn test_small_emission_values() {
);

let total_distributed: u64 = emission_tuples
.iter()
.map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.values()
.map(|ev| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.sum();
assert_eq!(
total_distributed, emission,
Expand Down Expand Up @@ -994,8 +994,8 @@ fn test_performance_with_many_nominators() {
);

let total_distributed: u64 = emission_tuples
.iter()
.map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.values()
.map(|ev| ev.iter().map(|(_, amount)| amount).sum::<u64>())
.sum();
assert_eq!(
total_distributed, emission,
Expand Down Expand Up @@ -1103,7 +1103,7 @@ fn test_hotkey_take_calculation_scenario() {
let mining_emission = 0;

step_block(1000); // should be past stake adding block by 2 tempos
LastAddStakeIncrease::<Test>::insert(&hotkey, coldkey, 1);
LastAddStakeIncrease::<Test>::insert(hotkey, coldkey, 1);
ParentKeys::<Test>::insert(hotkey, netuid, vec![(1000, parent)]);

// Test with different childkey take values
Expand Down Expand Up @@ -1765,16 +1765,13 @@ fn test_fast_stake_unstake_protection_source_hotkey() {

// Set up stakes and delegations
add_network(netuid, tempo, 0);
Delegates::<Test>::insert(&hotkey, 16384); // 25% take
Delegates::<Test>::insert(hotkey, 16384); // 25% take
SubtensorModule::stake_into_subnet(&parent1, &coldkey, netuid, 500);
SubtensorModule::stake_into_subnet(&parent2, &coldkey, netuid, 500);
ParentKeys::<Test>::insert(
&hotkey,
hotkey,
netuid,
vec![
(u64::MAX / 2, parent1.clone()),
(u64::MAX / 2, parent2.clone()),
],
vec![(u64::MAX / 2, parent1), (u64::MAX / 2, parent2)],
);

let mut emission_tuples = Vec::new();
Expand Down Expand Up @@ -1824,7 +1821,7 @@ fn test_fast_stake_unstake_protection_source_nominator() {
// Set up stakes and delegations
SubtensorModule::stake_into_subnet(&hotkey, &nominator1, netuid, 500);
SubtensorModule::stake_into_subnet(&hotkey, &nominator2, netuid, 500);
Delegates::<Test>::insert(&hotkey, 16384); // 25% take
Delegates::<Test>::insert(hotkey, 16384); // 25% take
HotkeyEmissionTempo::<Test>::put(10);

let mut emission_tuples: BTreeMap<(U256, U256), Vec<(u16, u64)>> = BTreeMap::new();
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ pub fn increase_stake_on_coldkey_hotkey_account(
tao_staked: u64,
netuid: u16,
) {
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid, tao_staked);
SubtensorModule::stake_into_subnet(hotkey, coldkey, netuid, tao_staked);
}

/// Helper function to mock now missing get_total_stake_for_hotkey with
Expand Down Expand Up @@ -637,7 +637,7 @@ pub struct DynamicSubnetSetupParameters {

#[allow(dead_code)]
pub fn setup_dynamic_network(prm: &DynamicSubnetSetupParameters) {
add_network(prm.netuid, prm.subnet_tempo as u16, 0);
add_network(prm.netuid, prm.subnet_tempo, 0);
pallet_subtensor::SubnetMechanism::<Test>::insert(prm.netuid, 1);
pallet_subtensor::SubnetTAO::<Test>::insert(prm.netuid, 1);
pallet_subtensor::SubnetAlphaIn::<Test>::insert(prm.netuid, 1);
Expand Down Expand Up @@ -700,7 +700,7 @@ pub fn setup_dynamic_network(prm: &DynamicSubnetSetupParameters) {
uids.iter()
.zip(prm.weights.iter())
.for_each(|(uid, weights)| {
if weights.len() > 0 {
if !weights.is_empty() {
pallet_subtensor::Weights::<Test>::insert(prm.netuid, uid, weights);
}
pallet_subtensor::BlockAtRegistration::<Test>::set(prm.netuid, uid, 1);
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 f75a486

Please sign in to comment.