Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add increase_take and decrease_take extrinsics, rate limiting, and tests #380

Merged
merged 8 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,21 @@ pub mod pallet {
}
Ok(())
}

#[pallet::call_index(45)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_set_tx_delegate_take_rate_limit(
origin: OriginFor<T>,
tx_rate_limit: u64,
) -> DispatchResult {
ensure_root(origin)?;
T::Subtensor::set_tx_delegate_take_rate_limit(tx_rate_limit);
log::info!(
"TxRateLimitDelegateTakeSet( tx_delegate_take_rate_limit: {:?} ) ",
tx_rate_limit
);
Ok(())
}
}
}

Expand All @@ -808,6 +823,7 @@ impl<A, M> AuraInterface<A, M> for () {
pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
fn set_default_take(default_take: u16);
fn set_tx_rate_limit(rate_limit: u64);
fn set_tx_delegate_take_rate_limit(rate_limit: u64);

fn set_serving_rate_limit(netuid: u16, rate_limit: u64);

Expand Down
6 changes: 6 additions & 0 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ parameter_types! {
pub const InitialWeightsVersionKey: u16 = 0;
pub const InitialServingRateLimit: u64 = 0; // No limit.
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing
pub const InitialTxDelegateTakeRateLimit: u64 = 0; // Disable rate limit for testing
pub const InitialBurn: u64 = 0;
pub const InitialMinBurn: u64 = 0;
pub const InitialMaxBurn: u64 = 1_000_000_000;
Expand Down Expand Up @@ -143,6 +144,7 @@ impl pallet_subtensor::Config for Test {
type InitialMinDifficulty = InitialMinDifficulty;
type InitialServingRateLimit = InitialServingRateLimit;
type InitialTxRateLimit = InitialTxRateLimit;
type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit;
type InitialBurn = InitialBurn;
type InitialMaxBurn = InitialMaxBurn;
type InitialMinBurn = InitialMinBurn;
Expand Down Expand Up @@ -211,6 +213,10 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
SubtensorModule::set_tx_rate_limit(rate_limit);
}

fn set_tx_delegate_take_rate_limit(rate_limit: u64) {
SubtensorModule::set_tx_delegate_take_rate_limit(rate_limit);
}

fn set_serving_rate_limit(netuid: u16, rate_limit: u64) {
SubtensorModule::set_serving_rate_limit(netuid, rate_limit);
}
Expand Down
27 changes: 27 additions & 0 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,30 @@ mod sudo_set_nominator_min_required_stake {
});
}
}

#[test]
fn test_sudo_set_tx_delegate_take_rate_limit() {
new_test_ext().execute_with(|| {
let to_be_set: u64 = 10;
let init_value: u64 = SubtensorModule::get_tx_delegate_take_rate_limit();
assert_eq!(
AdminUtils::sudo_set_tx_delegate_take_rate_limit(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
to_be_set
),
Err(DispatchError::BadOrigin.into())
);
assert_eq!(
SubtensorModule::get_tx_delegate_take_rate_limit(),
init_value
);
assert_ok!(AdminUtils::sudo_set_tx_delegate_take_rate_limit(
<<Test as Config>::RuntimeOrigin>::root(),
to_be_set
));
assert_eq!(
SubtensorModule::get_tx_delegate_take_rate_limit(),
to_be_set
);
});
}
102 changes: 101 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use codec::{Decode, Encode};
use frame_support::sp_runtime::transaction_validity::InvalidTransaction;
use frame_support::sp_runtime::transaction_validity::ValidTransaction;
use scale_info::TypeInfo;
use sp_core::Get;
use sp_runtime::{
traits::{DispatchInfoOf, Dispatchable, PostDispatchInfoOf, SignedExtension},
transaction_validity::{TransactionValidity, TransactionValidityError},
Expand Down Expand Up @@ -165,6 +166,8 @@ pub mod pallet {
type InitialServingRateLimit: Get<u64>;
#[pallet::constant] // Initial transaction rate limit.
type InitialTxRateLimit: Get<u64>;
#[pallet::constant] // Initial delegate take transaction rate limit.
type InitialTxDelegateTakeRateLimit: Get<u64>;
#[pallet::constant] // Initial percentage of total stake required to join senate.
type InitialSenateRequiredStakePercentage: Get<u64>;
#[pallet::constant] // Initial adjustment alpha on burn and pow.
Expand Down Expand Up @@ -582,15 +585,25 @@ pub mod pallet {
T::InitialTxRateLimit::get()
}
#[pallet::type_value]
pub fn DefaultTxDelegateTakeRateLimit<T: Config>() -> u64 {
T::InitialTxDelegateTakeRateLimit::get()
}
#[pallet::type_value]
pub fn DefaultLastTxBlock<T: Config>() -> u64 {
0
}

#[pallet::storage] // --- ITEM ( tx_rate_limit )
pub(super) type TxRateLimit<T> = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit<T>>;
#[pallet::storage] // --- ITEM ( tx_rate_limit )
pub(super) type TxDelegateTakeRateLimit<T> =
StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit<T>>;
#[pallet::storage] // --- MAP ( key ) --> last_block
pub(super) type LastTxBlock<T: Config> =
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
#[pallet::storage] // --- MAP ( key ) --> last_block
pub(super) type LastTxBlockDelegateTake<T: Config> =
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;

#[pallet::type_value]
pub fn DefaultServingRateLimit<T: Config>() -> u64 {
Expand Down Expand Up @@ -899,7 +912,8 @@ pub mod pallet {
MaxBurnSet(u16, u64), // --- Event created when setting max burn on a network.
MinBurnSet(u16, u64), // --- Event created when setting min burn on a network.
TxRateLimitSet(u64), // --- Event created when setting the transaction rate limit.
Sudid(DispatchResult), // --- Event created when a sudo call is done.
TxDelegateTakeRateLimitSet(u64), // --- Event created when setting the delegate take transaction rate limit.
Sudid(DispatchResult), // --- Event created when a sudo call is done.
RegistrationAllowed(u16, bool), // --- Event created when registration is allowed/disallowed for a subnet.
PowRegistrationAllowed(u16, bool), // --- Event created when POW registration is allowed/disallowed for a subnet.
TempoSet(u16, u16), // --- Event created when setting tempo on a network
Expand All @@ -914,6 +928,8 @@ pub mod pallet {
NetworkMinLockCostSet(u64), // Event created when the network minimum locking cost is set.
SubnetLimitSet(u16), // Event created when the maximum number of subnets is set
NetworkLockCostReductionIntervalSet(u64), // Event created when the lock cost reduction is set
TakeDecreased(T::AccountId, T::AccountId, u16), // Event created when the take for a delegate is decreased.
TakeIncreased(T::AccountId, T::AccountId, u16), // Event created when the take for a delegate is increased.
HotkeySwapped {
coldkey: T::AccountId,
old_hotkey: T::AccountId,
Expand Down Expand Up @@ -986,6 +1002,7 @@ pub mod pallet {
NoNeuronIdAvailable, // -- Thrown when no neuron id is available
/// Thrown a stake would be below the minimum threshold for nominator validations
NomStakeBelowMinimumThreshold,
InvalidTake, // --- Thrown when delegate take is being set out of bounds
}

// ==================
Expand Down Expand Up @@ -1325,6 +1342,89 @@ pub mod pallet {
Self::do_become_delegate(origin, hotkey, Self::get_default_take())
}

// --- Allows delegates to decrease its take value.
//
// # Args:
// * 'origin': (<T as frame_system::Config>::Origin):
// - The signature of the caller's coldkey.
//
// * 'hotkey' (T::AccountId):
// - The hotkey we are delegating (must be owned by the coldkey.)
//
// * 'netuid' (u16):
// - Subnet ID to decrease take for
//
// * 'take' (u16):
// - The new stake proportion that this hotkey takes from delegations.
// The new value can be between 0 and 11_796 and should be strictly
// lower than the previous value. It T is the new value (rational number),
// the the parameter is calculated as [65535 * T]. For example, 1% would be
// [0.01 * 65535] = [655.35] = 655
//
// # Event:
// * TakeDecreased;
// - On successfully setting a decreased take for this hotkey.
//
// # Raises:
// * 'NotRegistered':
// - The hotkey we are delegating is not registered on the network.
//
// * 'NonAssociatedColdKey':
// - The hotkey we are delegating is not owned by the calling coldkey.
//
// * 'InvalidTransaction':
// - The delegate is setting a take which is not lower than the previous.
//
#[pallet::call_index(65)]
#[pallet::weight((0, DispatchClass::Normal, Pays::No))]
pub fn decrease_take(
origin: OriginFor<T>,
hotkey: T::AccountId,
take: u16,
) -> DispatchResult {
Self::do_decrease_take(origin, hotkey, take)
}

// --- Allows delegates to increase its take value. This call is rate-limited.
//
// # Args:
// * 'origin': (<T as frame_system::Config>::Origin):
// - The signature of the caller's coldkey.
//
// * 'hotkey' (T::AccountId):
// - The hotkey we are delegating (must be owned by the coldkey.)
//
// * 'take' (u16):
// - The new stake proportion that this hotkey takes from delegations.
// The new value can be between 0 and 11_796 and should be strictly
// greater than the previous value. It T is the new value (rational number),
// the the parameter is calculated as [65535 * T]. For example, 1% would be
// [0.01 * 65535] = [655.35] = 655
//
// # Event:
// * TakeDecreased;
// - On successfully setting a decreased take for this hotkey.
//
// # Raises:
// * 'NotRegistered':
// - The hotkey we are delegating is not registered on the network.
//
// * 'NonAssociatedColdKey':
// - The hotkey we are delegating is not owned by the calling coldkey.
//
// * 'InvalidTransaction':
// - The delegate is setting a take which is not lower than the previous.
//
#[pallet::call_index(66)]
#[pallet::weight((0, DispatchClass::Normal, Pays::No))]
pub fn increase_take(
origin: OriginFor<T>,
hotkey: T::AccountId,
take: u16,
) -> DispatchResult {
Self::do_increase_take(origin, hotkey, take)
}

// --- Adds stake to a hotkey. The call is made from the
// coldkey account linked in the hotkey.
// Only the associated coldkey is allowed to make staking and
Expand Down
Loading
Loading