Skip to content

Commit 7aaeb09

Browse files
author
Samuel Dare
committed
feat: move back to on_initialise, lints
1 parent e30c8f4 commit 7aaeb09

File tree

7 files changed

+48
-106
lines changed

7 files changed

+48
-106
lines changed

pallets/subtensor/src/delegate_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<T: Config> Pallet<T> {
142142

143143
for (delegator, stake) in Stake::<T>::iter_prefix(&hotkey) {
144144
if delegator != owner {
145-
total_delegated += stake;
145+
total_delegated = total_delegated.saturating_add(stake);
146146
}
147147
}
148148
}
@@ -162,7 +162,7 @@ impl<T: Config> Pallet<T> {
162162
// Iterate through all delegators for this hotkey
163163
for (delegator, stake) in Stake::<T>::iter_prefix(hotkey) {
164164
if delegator != Self::get_coldkey_for_hotkey(hotkey) {
165-
total_delegated += stake;
165+
total_delegated = total_delegated.saturating_add(stake);
166166
}
167167
}
168168

pallets/subtensor/src/lib.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,40 +1343,48 @@ pub mod pallet {
13431343

13441344
#[pallet::hooks]
13451345
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
1346-
fn on_idle(_n: BlockNumberFor<T>, remaining_weight: Weight) -> Weight {
1347-
// Unstake and transfer pending coldkeys up to the remaining weight
1348-
match Self::swap_coldkeys_this_block(&remaining_weight) {
1346+
fn on_idle(_n: BlockNumberFor<T>, _remaining_weight: Weight) -> Weight {
1347+
Weight::zero()
1348+
}
1349+
1350+
fn on_initialize(_block_number: BlockNumberFor<T>) -> Weight {
1351+
let mut total_weight = Weight::zero();
1352+
1353+
// Create a Weight::MAX value to pass to swap_coldkeys_this_block
1354+
let max_weight = Weight::MAX;
1355+
1356+
// Perform coldkey swapping
1357+
let swap_weight = match Self::swap_coldkeys_this_block(&max_weight) {
13491358
Ok(weight_used) => weight_used,
13501359
Err(e) => {
13511360
log::error!("Error while swapping coldkeys: {:?}", e);
1352-
Weight::default()
1361+
Weight::zero()
13531362
}
1354-
}
1355-
}
1363+
};
1364+
total_weight = total_weight.saturating_add(swap_weight);
13561365

1357-
// ---- Called on the initialization of this pallet. (the order of on_finalize calls is determined in the runtime)
1358-
//
1359-
// # Args:
1360-
// * 'n': (BlockNumberFor<T>):
1361-
// - The number of the block we are initializing.
1362-
fn on_initialize(_block_number: BlockNumberFor<T>) -> Weight {
1366+
// Perform block step
13631367
let block_step_result = Self::block_step();
13641368
match block_step_result {
13651369
Ok(_) => {
1366-
// --- If the block step was successful, return the weight.
13671370
log::debug!("Successfully ran block step.");
1368-
Weight::from_parts(110_634_229_000_u64, 0)
1369-
.saturating_add(T::DbWeight::get().reads(8304_u64))
1370-
.saturating_add(T::DbWeight::get().writes(110_u64))
1371+
total_weight = total_weight.saturating_add(
1372+
Weight::from_parts(110_634_229_000_u64, 0)
1373+
.saturating_add(T::DbWeight::get().reads(8304_u64))
1374+
.saturating_add(T::DbWeight::get().writes(110_u64)),
1375+
);
13711376
}
13721377
Err(e) => {
1373-
// --- If the block step was unsuccessful, return the weight anyway.
13741378
log::error!("Error while stepping block: {:?}", e);
1375-
Weight::from_parts(110_634_229_000_u64, 0)
1376-
.saturating_add(T::DbWeight::get().reads(8304_u64))
1377-
.saturating_add(T::DbWeight::get().writes(110_u64))
1379+
total_weight = total_weight.saturating_add(
1380+
Weight::from_parts(110_634_229_000_u64, 0)
1381+
.saturating_add(T::DbWeight::get().reads(8304_u64))
1382+
.saturating_add(T::DbWeight::get().writes(110_u64)),
1383+
);
13781384
}
13791385
}
1386+
1387+
total_weight
13801388
}
13811389

13821390
fn on_runtime_upgrade() -> frame_support::weights::Weight {

pallets/subtensor/src/schedule_coldkey_swap_info.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::*;
22
use codec::Compact;
33
use frame_support::pallet_prelude::{Decode, Encode};
4+
45
use sp_core::hexdisplay::AsBytesRef;
56

67
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
@@ -37,7 +38,10 @@ impl<T: Config> Pallet<T> {
3738

3839
Some(ScheduledColdkeySwapInfo {
3940
old_coldkey: coldkey,
40-
new_coldkey: destinations[0].clone(),
41+
new_coldkey: destinations.first().cloned().unwrap_or_else(|| {
42+
T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
43+
.expect("Infinite length input; no invalid inputs for type; qed")
44+
}),
4145
arbitration_block: arbitration_block.into(),
4246
})
4347
}

pallets/subtensor/src/staking.rs

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -868,75 +868,4 @@ impl<T: Config> Pallet<T> {
868868
Self::add_balance_to_coldkey_account(&delegate_coldkey_i, stake_i);
869869
}
870870
}
871-
872-
// pub fn x( coldkey_a: &T::AccountId, coldkey_b: &T::AccountId ) -> Weight {
873-
// let mut weight = frame_support::weights::Weight::from_parts(0, 0);
874-
875-
// // Get the hotkeys associated with coldkey_a.
876-
// let coldkey_a_hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::get( &coldkey_a );
877-
// weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 0));
878-
879-
// // Iterate over all the hotkeys associated with this coldkey
880-
// for hotkey_i in coldkey_a_hotkeys.iter() {
881-
882-
// // Get the current stake from coldkey_a to hotkey_i.
883-
// let all_current_stake_i: u64 = Self::get_stake_for_coldkey_and_hotkey( &coldkey_a, &hotkey_i );
884-
// weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 0));
885-
886-
// // We remove the balance from the hotkey acount equal to all of it.
887-
// Self::decrease_stake_on_coldkey_hotkey_account( &coldkey_a, &hotkey_i, all_current_stake_i );
888-
// weight = weight.saturating_add(T::DbWeight::get().reads_writes(0, 4));
889-
890-
// // We add the balance to the coldkey. If the above fails we will not credit this coldkey.
891-
// Self::add_balance_to_coldkey_account( &coldkey_a, all_current_stake_i );
892-
// weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 2));
893-
// }
894-
// Unstake all delegate stake make by this coldkey to non-owned hotkeys
895-
// let staking_hotkeys = StakingHotkeys::<T>::get(&current_coldkey);
896-
897-
// // iterate over all staking hotkeys.
898-
// for hotkey in staking_hotkeys {
899-
// // Get the current stake
900-
// let current_stake: u64 =
901-
// Self::get_stake_for_coldkey_and_hotkey(&current_coldkey, &hotkey);
902-
903-
// // Unstake all balance if there's any stake
904-
// if current_stake > 0 {
905-
// Self::do_remove_stake(
906-
// RawOrigin::Signed(current_coldkey.clone()).into(),
907-
// hotkey.clone(),
908-
// current_stake,
909-
// )?;
910-
// }
911-
// }
912-
913-
// let total_balance = Self::get_coldkey_balance(&current_coldkey);
914-
// log::info!("Total Bank Balance: {:?}", total_balance);
915-
916-
// // Get the total balance here.
917-
// let total_balance = Self::get_coldkey_balance( &coldkey_a );
918-
// weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 0));
919-
920-
// if !total_balance.is_zero() {
921-
// // Attempt to transfer the entire total balance to coldkey_b.
922-
// let _ = T::Currency::transfer(
923-
// coldkey_a,
924-
// coldkey_b,
925-
// total_balance,
926-
// Preservation::Expendable,
927-
// );
928-
// weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
929-
// }
930-
931-
// // Emit event
932-
// Self::deposit_event(
933-
// Event::AllBalanceUnstakedAndTransferredToNewColdkey {
934-
// current_coldkey: coldkey_a.clone(),
935-
// new_coldkey: coldkey_b.clone(),
936-
// total_balance
937-
// }
938-
// );
939-
940-
// weight
941-
// }
942871
}

pallets/subtensor/src/swap.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,11 @@ impl<T: Config> Pallet<T> {
203203
let all_staked_keys: Vec<T::AccountId> = StakingHotkeys::<T>::get(coldkey);
204204
let mut total_staking_balance: u64 = 0;
205205
for hotkey in all_staked_keys {
206-
total_staking_balance += Self::get_stake_for_coldkey_and_hotkey(coldkey, &hotkey);
206+
total_staking_balance = total_staking_balance
207+
.saturating_add(Self::get_stake_for_coldkey_and_hotkey(coldkey, &hotkey));
207208
}
208-
total_staking_balance += Self::get_coldkey_balance(coldkey);
209+
total_staking_balance =
210+
total_staking_balance.saturating_add(Self::get_coldkey_balance(coldkey));
209211
total_staking_balance >= MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP
210212
}
211213

@@ -414,15 +416,13 @@ impl<T: Config> Pallet<T> {
414416
// Swap the coldkey.
415417
let total_balance: u64 = Self::get_coldkey_balance(old_coldkey);
416418
if total_balance > 0 {
417-
// Attempt to transfer the entire total balance to coldkeyB.
418-
if let Err(e) = T::Currency::transfer(
419+
// Attempt to transfer the entire total balance to new_coldkey.
420+
T::Currency::transfer(
419421
old_coldkey,
420422
new_coldkey,
421423
total_balance,
422424
Preservation::Expendable,
423-
) {
424-
return Err(e.into());
425-
}
425+
)?;
426426
}
427427

428428
Ok(weight)
@@ -832,7 +832,7 @@ impl<T: Config> Pallet<T> {
832832
Owner::<T>::insert(hotkey, new_coldkey);
833833

834834
// Update the transaction weight
835-
*weight += T::DbWeight::get().reads_writes(2, 2);
835+
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
836836
}
837837
}
838838

@@ -858,18 +858,18 @@ impl<T: Config> Pallet<T> {
858858
);
859859

860860
// Update the transaction weight
861-
*weight += T::DbWeight::get().reads_writes(2, 2);
861+
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
862862
}
863863

864864
// Update the list of owned hotkeys for both old and new coldkeys
865865
OwnedHotkeys::<T>::remove(old_coldkey);
866866
OwnedHotkeys::<T>::insert(new_coldkey, old_owned_hotkeys);
867-
*weight += T::DbWeight::get().reads_writes(1, 2);
867+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
868868

869869
// Update the staking hotkeys for both old and new coldkeys
870870
let staking_hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::take(old_coldkey);
871871
StakingHotkeys::<T>::insert(new_coldkey, staking_hotkeys);
872-
*weight += T::DbWeight::get().reads_writes(1, 1);
872+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
873873

874874
// Log the total stake of old and new coldkeys after the swap
875875
log::info!(

pallets/subtensor/tests/staking.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(clippy::unwrap_used)]
2+
#![allow(clippy::arithmetic_side_effects)]
23

34
use frame_support::pallet_prelude::{
45
InvalidTransaction, TransactionValidity, TransactionValidityError,

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
139139
// `spec_version`, and `authoring_version` are the same between Wasm and native.
140140
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
141141
// the compatible custom types.
142-
spec_version: 189,
142+
spec_version: 187,
143143
impl_version: 1,
144144
apis: RUNTIME_API_VERSIONS,
145145
transaction_version: 1,

0 commit comments

Comments
 (0)