Skip to content

Commit 841c913

Browse files
committed
Fix validation for transfer toggle
1 parent ae25f59 commit 841c913

File tree

8 files changed

+209
-188
lines changed

8 files changed

+209
-188
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,6 @@ pub mod pallet {
13071307
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
13081308
pallet_subtensor::Pallet::<T>::toggle_transfer(netuid, toggle)
13091309
}
1310-
13111310
}
13121311
}
13131312

pallets/subtensor/src/coinbase/root.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,10 +722,14 @@ impl<T: Config> Pallet<T> {
722722
Self::deposit_event(Event::NetworkLockCostReductionIntervalSet(interval));
723723
}
724724
pub fn get_lock_reduction_interval() -> u64 {
725-
let interval: I64F64 = I64F64::saturating_from_num( NetworkLockReductionInterval::<T>::get() );
726-
let block_emission: I64F64 = I64F64::saturating_from_num( Self::get_block_emission().unwrap_or( 1_000_000_000 ) );
727-
let halving: I64F64 = block_emission.checked_div( I64F64::saturating_from_num( 1_000_000_000 ) ).unwrap_or( I64F64::saturating_from_num(0.0) );
728-
let halved_interval: I64F64 = interval.saturating_mul( halving );
725+
let interval: I64F64 =
726+
I64F64::saturating_from_num(NetworkLockReductionInterval::<T>::get());
727+
let block_emission: I64F64 =
728+
I64F64::saturating_from_num(Self::get_block_emission().unwrap_or(1_000_000_000));
729+
let halving: I64F64 = block_emission
730+
.checked_div(I64F64::saturating_from_num(1_000_000_000))
731+
.unwrap_or(I64F64::saturating_from_num(0.0));
732+
let halved_interval: I64F64 = interval.saturating_mul(halving);
729733
halved_interval.saturating_to_num::<u64>()
730734
}
731735
}

pallets/subtensor/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,7 @@ pub enum CustomTransactionError {
15951595
RateLimitExceeded,
15961596
InsufficientLiquidity,
15971597
SlippageTooHigh,
1598+
TransferDisallowed,
15981599
BadRequest,
15991600
}
16001601

@@ -1610,6 +1611,7 @@ impl From<CustomTransactionError> for u8 {
16101611
CustomTransactionError::RateLimitExceeded => 6,
16111612
CustomTransactionError::InsufficientLiquidity => 7,
16121613
CustomTransactionError::SlippageTooHigh => 8,
1614+
CustomTransactionError::TransferDisallowed => 9,
16131615
CustomTransactionError::BadRequest => 255,
16141616
}
16151617
}
@@ -1685,6 +1687,10 @@ where
16851687
CustomTransactionError::SlippageTooHigh.into(),
16861688
)
16871689
.into()),
1690+
Error::<T>::TransferDisallowed => Err(InvalidTransaction::Custom(
1691+
CustomTransactionError::TransferDisallowed.into(),
1692+
)
1693+
.into()),
16881694
_ => Err(
16891695
InvalidTransaction::Custom(CustomTransactionError::BadRequest.into()).into(),
16901696
),
@@ -1909,6 +1915,7 @@ where
19091915
*alpha_amount,
19101916
*alpha_amount,
19111917
None,
1918+
false,
19121919
))
19131920
}
19141921
Some(Call::transfer_stake {
@@ -1929,6 +1936,7 @@ where
19291936
*alpha_amount,
19301937
*alpha_amount,
19311938
None,
1939+
true,
19321940
))
19331941
}
19341942
Some(Call::swap_stake {
@@ -1948,6 +1956,7 @@ where
19481956
*alpha_amount,
19491957
*alpha_amount,
19501958
None,
1959+
false,
19511960
))
19521961
}
19531962
Some(Call::swap_stake_limit {
@@ -1976,6 +1985,7 @@ where
19761985
*alpha_amount,
19771986
max_amount,
19781987
Some(*allow_partial),
1988+
false,
19791989
))
19801990
}
19811991
Some(Call::register { netuid, .. } | Call::burned_register { netuid, .. }) => {

pallets/subtensor/src/macros/events.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,5 @@ mod events {
271271
/// Parameters:
272272
/// (netuid, bool)
273273
TransferToggle(u16, bool),
274-
275274
}
276275
}

pallets/subtensor/src/staking/move_stake.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl<T: Config> Pallet<T> {
4747
alpha_amount,
4848
None,
4949
None,
50+
false,
5051
)?;
5152

5253
// Log the event.
@@ -96,20 +97,14 @@ impl<T: Config> Pallet<T> {
9697
///
9798
/// # Events
9899
/// Emits a `StakeTransferred` event upon successful completion of the transfer.
99-
pub fn toggle_transfer(
100-
netuid: u16,
101-
toggle: bool
102-
) -> dispatch::DispatchResult {
103-
TransferToggle::<T>::insert( netuid, toggle );
100+
pub fn toggle_transfer(netuid: u16, toggle: bool) -> dispatch::DispatchResult {
101+
TransferToggle::<T>::insert(netuid, toggle);
104102
log::debug!(
105103
"TransferToggle( netuid: {:?}, toggle: {:?} ) ",
106104
netuid,
107105
toggle
108106
);
109-
Self::deposit_event(Event::TransferToggle(
110-
netuid,
111-
toggle
112-
));
107+
Self::deposit_event(Event::TransferToggle(netuid, toggle));
113108
Ok(())
114109
}
115110
pub fn do_transfer_stake(
@@ -123,16 +118,6 @@ impl<T: Config> Pallet<T> {
123118
// Ensure the extrinsic is signed by the origin_coldkey.
124119
let coldkey = ensure_signed(origin)?;
125120

126-
// Ensure transfer is toggled.
127-
ensure!(
128-
TransferToggle::<T>::get( origin_netuid ),
129-
Error::<T>::TransferDisallowed
130-
);
131-
ensure!(
132-
TransferToggle::<T>::get( destination_netuid ),
133-
Error::<T>::TransferDisallowed
134-
);
135-
136121
// Validate input and move stake
137122
let tao_moved = Self::transition_stake_internal(
138123
&coldkey,
@@ -144,6 +129,7 @@ impl<T: Config> Pallet<T> {
144129
alpha_amount,
145130
None,
146131
None,
132+
true,
147133
)?;
148134

149135
// 9. Emit an event for logging/monitoring.
@@ -213,6 +199,7 @@ impl<T: Config> Pallet<T> {
213199
alpha_amount,
214200
None,
215201
None,
202+
false,
216203
)?;
217204

218205
// Emit an event for logging.
@@ -284,6 +271,7 @@ impl<T: Config> Pallet<T> {
284271
alpha_amount,
285272
Some(limit_price),
286273
Some(allow_partial),
274+
false,
287275
)?;
288276

289277
// Emit an event for logging.
@@ -319,6 +307,7 @@ impl<T: Config> Pallet<T> {
319307
alpha_amount: u64,
320308
maybe_limit_price: Option<u64>,
321309
maybe_allow_partial: Option<bool>,
310+
check_transfer_toggle: bool,
322311
) -> Result<u64, Error<T>> {
323312
// Calculate the maximum amount that can be executed
324313
let max_amount = if let Some(limit_price) = maybe_limit_price {
@@ -338,6 +327,7 @@ impl<T: Config> Pallet<T> {
338327
alpha_amount,
339328
max_amount,
340329
maybe_allow_partial,
330+
check_transfer_toggle,
341331
)?;
342332

343333
// Unstake from the origin subnet, returning TAO (or a 1:1 equivalent).

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ impl<T: Config> Pallet<T> {
845845
alpha_amount: u64,
846846
max_amount: u64,
847847
maybe_allow_partial: Option<bool>,
848+
check_transfer_toggle: bool,
848849
) -> Result<(), Error<T>> {
849850
// Ensure that both subnets exist.
850851
ensure!(
@@ -899,6 +900,18 @@ impl<T: Config> Pallet<T> {
899900
}
900901
}
901902

903+
if check_transfer_toggle {
904+
// Ensure transfer is toggled.
905+
ensure!(
906+
TransferToggle::<T>::get(origin_netuid),
907+
Error::<T>::TransferDisallowed
908+
);
909+
ensure!(
910+
TransferToggle::<T>::get(destination_netuid),
911+
Error::<T>::TransferDisallowed
912+
);
913+
}
914+
902915
Ok(())
903916
}
904917
}

0 commit comments

Comments
 (0)