Skip to content

Commit de41a34

Browse files
author
Cameron Fairchild
committed
make check idempot
1 parent 67aad02 commit de41a34

File tree

1 file changed

+80
-34
lines changed

1 file changed

+80
-34
lines changed

pallets/subtensor/src/migrations/migrate_fix_pending_emission.rs

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,20 @@ pub fn do_migrate_fix_pending_emission<T: Config>() -> Weight {
148148
/// Required so we can read values in the v0 storage format during the migration.
149149
#[cfg(feature = "try-runtime")]
150150
mod v0 {
151-
use frame_support::storage_alias;
152151
use subtensor_macros::freeze_struct;
153152

154-
#[freeze_struct("7adbed79987ae83d")]
153+
#[freeze_struct("2228babfc0580c62")]
155154
#[derive(codec::Encode, codec::Decode, Clone, PartialEq, Debug)]
156155
pub struct OldStorage {
157156
pub total_issuance_before: u64,
157+
pub total_stake_before: u64,
158158
pub expected_taostats_new_hk_pending_emission: u64,
159159
pub expected_datura_new_hk_pending_emission: u64,
160+
pub old_migration_stake_taostats: u64,
160161
pub old_null_stake_taostats: u64,
162+
pub old_migration_stake_datura: u64,
161163
pub old_null_stake_datura: u64,
162164
}
163-
164-
/// V0 type for [`crate::Value`].
165-
#[storage_alias]
166-
pub type Value<T: crate::Config> = StorageValue<crate::Pallet<T>, OldStorage>;
167165
}
168166

169167
impl<T: Config> Pallet<T> {
@@ -184,7 +182,9 @@ impl<T: Config> Pallet<T> {
184182
let old = old_storage;
185183
let null_stake_total = old
186184
.old_null_stake_taostats
187-
.saturating_add(old.old_null_stake_datura);
185+
.saturating_add(old.old_null_stake_datura)
186+
.saturating_add(old.old_migration_stake_taostats)
187+
.saturating_add(old.old_migration_stake_datura);
188188

189189
match (
190190
taostats_old_hk_account,
@@ -209,10 +209,19 @@ impl<T: Config> Pallet<T> {
209209
0
210210
);
211211

212+
// Check the total hotkey stake is the same
213+
assert_eq!(
214+
TotalHotkeyStake::<T>::get(taostats_old_hk_acct),
215+
old.old_null_stake_taostats + old.old_migration_stake_taostats
216+
);
217+
212218
let new_null_stake_taostats =
213219
Self::get_stake_for_coldkey_and_hotkey(migration_ck_acct, taostats_old_hk_acct);
214220

215-
assert_eq!(new_null_stake_taostats, old.old_null_stake_taostats);
221+
assert_eq!(
222+
new_null_stake_taostats,
223+
old.old_null_stake_taostats + old.old_migration_stake_taostats
224+
);
216225
}
217226
_ => {
218227
log::warn!("Failed to get account id from ss58 for taostats hotkeys");
@@ -251,10 +260,19 @@ impl<T: Config> Pallet<T> {
251260
0
252261
);
253262

263+
// Check the total hotkey stake is the same
264+
assert_eq!(
265+
TotalHotkeyStake::<T>::get(datura_old_hk_acct),
266+
old.old_null_stake_datura + old.old_migration_stake_datura
267+
);
268+
254269
let new_null_stake_datura =
255270
Self::get_stake_for_coldkey_and_hotkey(migration_ck_acct, datura_old_hk_acct);
256271

257-
assert_eq!(new_null_stake_datura, old.old_null_stake_datura);
272+
assert_eq!(
273+
new_null_stake_datura,
274+
old.old_null_stake_datura + old.old_migration_stake_datura
275+
);
258276
}
259277
_ => {
260278
log::warn!("Failed to get account id from ss58 for datura hotkeys");
@@ -278,10 +296,11 @@ impl<T: Config> Pallet<T> {
278296

279297
// Check the total issuance is the SAME following migration (no TAO issued)
280298
let expected_total_issuance = old.total_issuance_before;
299+
let expected_total_stake = old.total_stake_before;
281300
assert_eq!(Self::get_total_issuance(), expected_total_issuance);
282301

283302
// Check total stake is the SAME following the migration (no new TAO staked)
284-
assert_eq!(TotalStake::<T>::get(), expected_total_issuance);
303+
assert_eq!(TotalStake::<T>::get(), expected_total_stake);
285304
// Check the total stake maps are updated following the migration (removal of old null_account stake entries)
286305
assert_eq!(TotalColdkeyStake::<T>::get(null_account), 0);
287306

@@ -303,7 +322,11 @@ pub mod migration {
303322

304323
#[cfg(feature = "try-runtime")]
305324
fn get_old_storage_values<T: Config>() -> Result<v0::OldStorage, sp_runtime::TryRuntimeError> {
325+
log::info!("Getting old storage values for migration");
326+
306327
let null_account = &DefaultAccount::<T>::get();
328+
let migration_coldkey = "5GeRjQYsobRWFnrbBmGe5ugme3rfnDVF69N45YtdBpUFsJG8";
329+
let migration_account = &get_account_id_from_ss58::<T>(migration_coldkey);
307330

308331
let taostats_old_hotkey = "5Hddm3iBFD2GLT5ik7LZnT3XJUnRnN8PoeCFgGQgawUVKNm8";
309332
let taostats_new_hotkey = "5GKH9FPPnWSUoeeTJp19wVtd84XqFW4pyK2ijV2GsFbhTrP1";
@@ -314,20 +337,27 @@ pub mod migration {
314337
let total_issuance_before = crate::Pallet::<T>::get_total_issuance();
315338
let mut expected_taostats_new_hk_pending_emission: u64 = 0;
316339
let mut expected_datura_new_hk_pending_emission: u64 = 0;
317-
let old_null_stake_taostats: u64 = match (taostats_old_hk_account, taostats_new_hk_account)
318-
{
319-
(Ok(taostats_old_hk_acct), Ok(taostats_new_hk_acct)) => {
340+
let (old_null_stake_taostats, old_migration_stake_taostats) = match (
341+
taostats_old_hk_account,
342+
taostats_new_hk_account,
343+
migration_account,
344+
) {
345+
(Ok(taostats_old_hk_acct), Ok(taostats_new_hk_acct), Ok(migration_acct)) => {
320346
expected_taostats_new_hk_pending_emission =
321347
expected_taostats_new_hk_pending_emission
322348
.saturating_add(PendingdHotkeyEmission::<T>::get(taostats_old_hk_acct))
323349
.saturating_add(PendingdHotkeyEmission::<T>::get(taostats_new_hk_acct));
324350

325-
Ok::<u64, sp_runtime::TryRuntimeError>(
351+
Ok::<(u64, u64), sp_runtime::TryRuntimeError>((
326352
crate::Pallet::<T>::get_stake_for_coldkey_and_hotkey(
327353
null_account,
328354
taostats_old_hk_acct,
329355
),
330-
)
356+
crate::Pallet::<T>::get_stake_for_coldkey_and_hotkey(
357+
migration_acct,
358+
taostats_old_hk_acct,
359+
),
360+
))
331361
}
332362
_ => {
333363
log::warn!("Failed to get account id from ss58 for taostats hotkeys");
@@ -341,33 +371,48 @@ pub mod migration {
341371
let datura_old_hk_account = &get_account_id_from_ss58::<T>(datura_old_hotkey);
342372
let datura_new_hk_account = &get_account_id_from_ss58::<T>(datura_new_hotkey);
343373

344-
let old_null_stake_datura: u64 = match (datura_old_hk_account, datura_new_hk_account) {
345-
(Ok(datura_old_hk_acct), Ok(datura_new_hk_acct)) => {
374+
let (old_null_stake_datura, old_migration_stake_datura) = match (
375+
datura_old_hk_account,
376+
datura_new_hk_account,
377+
migration_account,
378+
) {
379+
(Ok(datura_old_hk_acct), Ok(datura_new_hk_acct), Ok(migration_acct)) => {
346380
expected_datura_new_hk_pending_emission = expected_datura_new_hk_pending_emission
347381
.saturating_add(PendingdHotkeyEmission::<T>::get(datura_old_hk_acct))
348382
.saturating_add(PendingdHotkeyEmission::<T>::get(datura_new_hk_acct));
349383

350-
Ok::<u64, sp_runtime::TryRuntimeError>(
384+
Ok::<(u64, u64), sp_runtime::TryRuntimeError>((
351385
crate::Pallet::<T>::get_stake_for_coldkey_and_hotkey(
352386
null_account,
353387
datura_old_hk_acct,
354388
),
355-
)
389+
crate::Pallet::<T>::get_stake_for_coldkey_and_hotkey(
390+
migration_acct,
391+
datura_old_hk_acct,
392+
),
393+
))
356394
}
357395
_ => {
358396
log::warn!("Failed to get account id from ss58 for datura hotkeys");
359397
Err("Failed to get account id from ss58 for datura hotkeys".into())
360398
}
361399
}?;
362400

401+
let total_stake_before: u64 = crate::Pallet::<T>::get_total_stake();
402+
363403
let result = v0::OldStorage {
364404
total_issuance_before,
405+
total_stake_before,
365406
expected_taostats_new_hk_pending_emission,
366407
expected_datura_new_hk_pending_emission,
408+
old_migration_stake_taostats,
367409
old_null_stake_taostats,
410+
old_migration_stake_datura,
368411
old_null_stake_datura,
369412
};
370413

414+
log::info!("Got old storage values for migration");
415+
371416
Ok(result)
372417
}
373418

@@ -378,10 +423,18 @@ pub mod migration {
378423
use codec::Encode;
379424

380425
// Get the old storage values
381-
let old_storage = get_old_storage_values::<T>()?;
426+
match get_old_storage_values::<T>() {
427+
Ok(old_storage) => {
428+
log::info!("Successfully got old storage values for migration");
429+
let encoded = old_storage.encode();
382430

383-
// Return it as an encoded `Vec<u8>`
384-
Ok(old_storage.encode())
431+
Ok(encoded)
432+
}
433+
Err(e) => {
434+
log::error!("Failed to get old storage values for migration: {:?}", e);
435+
Err("Failed to get old storage values for migration".into())
436+
}
437+
}
385438
}
386439

387440
// Runs the migrate function for the fix_pending_emission migration
@@ -430,21 +483,14 @@ pub mod migration {
430483
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
431484
use codec::Decode;
432485

433-
let maybe_old_value =
434-
Option::<v0::OldStorage>::decode(&mut &state[..]).map_err(|_| {
486+
let old_storage: v0::OldStorage =
487+
v0::OldStorage::decode(&mut &state[..]).map_err(|_| {
435488
sp_runtime::TryRuntimeError::Other("Failed to decode old value from storage")
436489
})?;
437490

438-
match maybe_old_value {
439-
Some(old_value) => {
440-
// Verify that all null stake invariants are satisfied after the migration
441-
crate::Pallet::<T>::check_null_stake_invariants(old_value)?;
442-
}
443-
None => {
444-
log::warn!("Failed to decode old value from storage");
445-
return Err("Failed to decode old value from storage".into());
446-
}
447-
};
491+
// Verify that all null stake invariants are satisfied after the migration
492+
crate::Pallet::<T>::check_null_stake_invariants(old_storage)?;
493+
448494
Ok(())
449495
}
450496
}

0 commit comments

Comments
 (0)