Skip to content

Commit 7cbc451

Browse files
authored
Merge pull request #1364 from opentensor/fix/hk-swap-use-old-hk-rate-limits
Fix/hk swap use old hk rate limits
2 parents b46dc4b + 263c1b4 commit 7cbc451

File tree

2 files changed

+50
-55
lines changed

2 files changed

+50
-55
lines changed

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,33 @@ impl<T: Config> Pallet<T> {
190190

191191
// 5. Swap LastTxBlock
192192
// LastTxBlock( hotkey ) --> u64 -- the last transaction block for the hotkey.
193+
let last_tx_block: u64 = LastTxBlock::<T>::get(old_hotkey);
193194
LastTxBlock::<T>::remove(old_hotkey);
194-
LastTxBlock::<T>::insert(new_hotkey, Self::get_current_block_as_u64());
195-
weight.saturating_accrue(T::DbWeight::get().reads_writes(0, 2));
195+
LastTxBlock::<T>::insert(new_hotkey, last_tx_block);
196+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
196197

197198
// 6. Swap LastTxBlockDelegateTake
198199
// LastTxBlockDelegateTake( hotkey ) --> u64 -- the last transaction block for the hotkey delegate take.
200+
let last_tx_block_delegate_take: u64 = LastTxBlockDelegateTake::<T>::get(old_hotkey);
199201
LastTxBlockDelegateTake::<T>::remove(old_hotkey);
200-
LastTxBlockDelegateTake::<T>::insert(new_hotkey, Self::get_current_block_as_u64());
202+
LastTxBlockDelegateTake::<T>::insert(new_hotkey, last_tx_block_delegate_take);
203+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
204+
205+
// 7. Swap LastTxBlockChildKeyTake
206+
// LastTxBlockChildKeyTake( hotkey ) --> u64 -- the last transaction block for the hotkey child key take.
207+
let last_tx_block_child_key_take: u64 = LastTxBlockChildKeyTake::<T>::get(old_hotkey);
208+
LastTxBlockChildKeyTake::<T>::remove(old_hotkey);
209+
LastTxBlockChildKeyTake::<T>::insert(new_hotkey, last_tx_block_child_key_take);
201210
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
202211

203-
// 7. Swap Senate members.
212+
// 8. Swap Senate members.
204213
// Senate( hotkey ) --> ?
205214
if T::SenateMembers::is_member(old_hotkey) {
206215
T::SenateMembers::swap_member(old_hotkey, new_hotkey).map_err(|e| e.error)?;
207216
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
208217
}
209218

210-
// 8. Swap delegates.
219+
// 9. Swap delegates.
211220
// Delegates( hotkey ) -> take value -- the hotkey delegate take value.
212221
if Delegates::<T>::contains_key(old_hotkey) {
213222
let old_delegate_take = Delegates::<T>::get(old_hotkey);

pallets/subtensor/src/tests/swap_hotkey.rs

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -116,56 +116,6 @@ fn test_swap_total_hotkey_stake() {
116116
});
117117
}
118118

119-
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_last_tx_block --exact --nocapture
120-
#[test]
121-
fn test_swap_last_tx_block() {
122-
new_test_ext(1).execute_with(|| {
123-
let old_hotkey = U256::from(1);
124-
let new_hotkey = U256::from(2);
125-
let coldkey = U256::from(3);
126-
let mut weight = Weight::zero();
127-
128-
LastTxBlock::<Test>::insert(old_hotkey, 1000);
129-
assert_ok!(SubtensorModule::perform_hotkey_swap(
130-
&old_hotkey,
131-
&new_hotkey,
132-
&coldkey,
133-
&mut weight
134-
));
135-
136-
assert!(!LastTxBlock::<Test>::contains_key(old_hotkey));
137-
assert_eq!(
138-
LastTxBlock::<Test>::get(new_hotkey),
139-
SubtensorModule::get_current_block_as_u64()
140-
);
141-
});
142-
}
143-
144-
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_last_tx_block_delegate_take --exact --nocapture
145-
#[test]
146-
fn test_swap_last_tx_block_delegate_take() {
147-
new_test_ext(1).execute_with(|| {
148-
let old_hotkey = U256::from(1);
149-
let new_hotkey = U256::from(2);
150-
let coldkey = U256::from(3);
151-
let mut weight = Weight::zero();
152-
153-
crate::LastTxBlockDelegateTake::<Test>::insert(old_hotkey, 1000);
154-
assert_ok!(SubtensorModule::perform_hotkey_swap(
155-
&old_hotkey,
156-
&new_hotkey,
157-
&coldkey,
158-
&mut weight
159-
));
160-
161-
assert!(!LastTxBlockDelegateTake::<Test>::contains_key(old_hotkey));
162-
assert_eq!(
163-
LastTxBlockDelegateTake::<Test>::get(new_hotkey),
164-
SubtensorModule::get_current_block_as_u64()
165-
);
166-
});
167-
}
168-
169119
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_senate_members --exact --nocapture
170120
#[test]
171121
fn test_swap_senate_members() {
@@ -1387,3 +1337,39 @@ fn test_swap_hotkey_is_sn_owner_hotkey() {
13871337
assert_eq!(SubnetOwnerHotkey::<Test>::get(netuid), new_hotkey);
13881338
});
13891339
}
1340+
1341+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_hotkey_swap_rate_limits --exact --nocapture
1342+
#[test]
1343+
fn test_swap_hotkey_swap_rate_limits() {
1344+
new_test_ext(1).execute_with(|| {
1345+
let old_hotkey = U256::from(1);
1346+
let new_hotkey = U256::from(2);
1347+
let coldkey = U256::from(3);
1348+
let mut weight = Weight::zero();
1349+
1350+
let last_tx_block = 123;
1351+
let delegate_take_block = 4567;
1352+
let child_key_take_block = 8910;
1353+
1354+
// Set the last tx block for the old hotkey
1355+
LastTxBlock::<Test>::insert(old_hotkey, last_tx_block);
1356+
// Set the last delegate take block for the old hotkey
1357+
LastTxBlockDelegateTake::<Test>::insert(old_hotkey, delegate_take_block);
1358+
// Set last childkey take block for the old hotkey
1359+
LastTxBlockChildKeyTake::<Test>::insert(old_hotkey, child_key_take_block);
1360+
1361+
// Perform the swap
1362+
SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight);
1363+
1364+
// Check for new hotkey
1365+
assert_eq!(LastTxBlock::<Test>::get(new_hotkey), last_tx_block);
1366+
assert_eq!(
1367+
LastTxBlockDelegateTake::<Test>::get(new_hotkey),
1368+
delegate_take_block
1369+
);
1370+
assert_eq!(
1371+
LastTxBlockChildKeyTake::<Test>::get(new_hotkey),
1372+
child_key_take_block
1373+
);
1374+
});
1375+
}

0 commit comments

Comments
 (0)