From f75a48668b8ca4ee1ca94d7e75dc24a11a7a4886 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 +++--- pallets/subtensor/tests/children.rs | 2 +- pallets/subtensor/tests/emission.rs | 41 +++++++++---------- pallets/subtensor/tests/mock.rs | 6 +-- runtime/src/lib.rs | 19 +++++++++ 6 files changed, 51 insertions(+), 33 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/pallets/subtensor/tests/children.rs b/pallets/subtensor/tests/children.rs index d9fcf434f..cba915040 100644 --- a/pallets/subtensor/tests/children.rs +++ b/pallets/subtensor/tests/children.rs @@ -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( diff --git a/pallets/subtensor/tests/emission.rs b/pallets/subtensor/tests/emission.rs index 766f5031d..b70dc5969 100644 --- a/pallets/subtensor/tests/emission.rs +++ b/pallets/subtensor/tests/emission.rs @@ -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::()) + .values() + .map(|ev| ev.iter().map(|(_, amount)| amount).sum::()) .sum(); assert_eq!(total_distributed, emission); }); @@ -630,8 +630,8 @@ fn test_rounding_and_precision() { ); let total_distributed: u64 = emission_tuples - .iter() - .map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::()) + .values() + .map(|ev| ev.iter().map(|(_, amount)| amount).sum::()) .sum(); assert_eq!( total_distributed, emission, @@ -762,8 +762,8 @@ fn test_remainder_distribution() { ); let total_distributed: u64 = emission_tuples - .iter() - .map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::()) + .values() + .map(|ev| ev.iter().map(|(_, amount)| amount).sum::()) .sum(); assert_eq!( total_distributed, emission, @@ -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::()) + .values() + .map(|ev| ev.iter().map(|(_, amount)| amount).sum::()) .sum(); assert_eq!( total_distributed, emission, @@ -845,8 +845,8 @@ fn test_large_emission_values() { ); let total_distributed: u64 = emission_tuples - .iter() - .map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::()) + .values() + .map(|ev| ev.iter().map(|(_, amount)| amount).sum::()) .sum(); assert_eq!( total_distributed, emission, @@ -904,8 +904,8 @@ fn test_small_emission_values() { ); let total_distributed: u64 = emission_tuples - .iter() - .map(|(_, ev)| ev.iter().map(|(_, amount)| amount).sum::()) + .values() + .map(|ev| ev.iter().map(|(_, amount)| amount).sum::()) .sum(); assert_eq!( total_distributed, emission, @@ -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::()) + .values() + .map(|ev| ev.iter().map(|(_, amount)| amount).sum::()) .sum(); assert_eq!( total_distributed, emission, @@ -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::::insert(&hotkey, coldkey, 1); + LastAddStakeIncrease::::insert(hotkey, coldkey, 1); ParentKeys::::insert(hotkey, netuid, vec![(1000, parent)]); // Test with different childkey take values @@ -1765,16 +1765,13 @@ fn test_fast_stake_unstake_protection_source_hotkey() { // Set up stakes and delegations add_network(netuid, tempo, 0); - Delegates::::insert(&hotkey, 16384); // 25% take + Delegates::::insert(hotkey, 16384); // 25% take SubtensorModule::stake_into_subnet(&parent1, &coldkey, netuid, 500); SubtensorModule::stake_into_subnet(&parent2, &coldkey, netuid, 500); ParentKeys::::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(); @@ -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::::insert(&hotkey, 16384); // 25% take + Delegates::::insert(hotkey, 16384); // 25% take HotkeyEmissionTempo::::put(10); let mut emission_tuples: BTreeMap<(U256, U256), Vec<(u16, u64)>> = BTreeMap::new(); diff --git a/pallets/subtensor/tests/mock.rs b/pallets/subtensor/tests/mock.rs index 017727493..605c9a7d8 100644 --- a/pallets/subtensor/tests/mock.rs +++ b/pallets/subtensor/tests/mock.rs @@ -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 @@ -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::::insert(prm.netuid, 1); pallet_subtensor::SubnetTAO::::insert(prm.netuid, 1); pallet_subtensor::SubnetAlphaIn::::insert(prm.netuid, 1); @@ -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::::insert(prm.netuid, uid, weights); } pallet_subtensor::BlockAtRegistration::::set(prm.netuid, uid, 1); 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)]