|
1 | 1 | #![allow(unused, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)] |
2 | 2 | mod mock; |
3 | | -use frame_support::{assert_ok, weights::Weight}; |
| 3 | +use codec::{Decode, Encode}; |
| 4 | +use frame_support::{ |
| 5 | + assert_ok, |
| 6 | + storage::unhashed::{get_raw, put_raw}, |
| 7 | + traits::{StorageInstance, StoredMap}, |
| 8 | + weights::Weight, |
| 9 | + StorageHasher, Twox64Concat, |
| 10 | +}; |
4 | 11 | use frame_system::Config; |
5 | 12 | use mock::*; |
6 | 13 | use pallet_subtensor::*; |
7 | | -use sp_core::U256; |
| 14 | +use sp_core::{H256, U256}; |
| 15 | +use sp_runtime::traits::Zero; |
8 | 16 |
|
9 | 17 | #[test] |
10 | 18 | fn test_initialise_ti() { |
@@ -430,3 +438,79 @@ fn run_migration_and_check(migration_name: &'static str) -> frame_support::weigh |
430 | 438 | // Return the weight of the executed migration |
431 | 439 | weight |
432 | 440 | } |
| 441 | + |
| 442 | +#[test] |
| 443 | +fn test_migrate_commit_reveal_2() { |
| 444 | + new_test_ext(1).execute_with(|| { |
| 445 | + // ------------------------------ |
| 446 | + // Step 1: Simulate Old Storage Entries |
| 447 | + // ------------------------------ |
| 448 | + const MIGRATION_NAME: &str = "migrate_commit_reveal_2"; |
| 449 | + const WEIGHT_COMMIT_REVEAL_INTERVAL_PREFIX: &[u8] = |
| 450 | + b"pallet_subtensor::WeightCommitRevealInterval"; |
| 451 | + const WEIGHT_COMMITS_PREFIX: &[u8] = b"pallet_subtensor::WeightCommits"; |
| 452 | + |
| 453 | + let netuid: u16 = 1; |
| 454 | + let interval_value: u64 = 50u64; |
| 455 | + |
| 456 | + let mut interval_key = WEIGHT_COMMIT_REVEAL_INTERVAL_PREFIX.to_vec(); |
| 457 | + interval_key.extend_from_slice(&netuid.encode()); |
| 458 | + |
| 459 | + put_raw(&interval_key, &interval_value.encode()); |
| 460 | + |
| 461 | + let test_account: U256 = U256::from(1); |
| 462 | + |
| 463 | + let mut commit_key = WEIGHT_COMMITS_PREFIX.to_vec(); |
| 464 | + commit_key.extend_from_slice(&Twox64Concat::hash(&netuid.encode())); |
| 465 | + commit_key.extend_from_slice(&Twox64Concat::hash(&test_account.encode())); |
| 466 | + |
| 467 | + let commit_value: (H256, u64) = (H256::from_low_u64_be(42), 100); |
| 468 | + put_raw(&commit_key, &commit_value.encode()); |
| 469 | + |
| 470 | + let stored_interval = get_raw(&interval_key).expect("Expected to get a value"); |
| 471 | + assert_eq!( |
| 472 | + u64::decode(&mut &stored_interval[..]).expect("Failed to decode interval value"), |
| 473 | + interval_value |
| 474 | + ); |
| 475 | + |
| 476 | + let stored_commit = get_raw(&commit_key).expect("Expected to get a value"); |
| 477 | + assert_eq!( |
| 478 | + <(H256, u64)>::decode(&mut &stored_commit[..]).expect("Failed to decode commit value"), |
| 479 | + commit_value |
| 480 | + ); |
| 481 | + |
| 482 | + assert!( |
| 483 | + !HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()), |
| 484 | + "Migration should not have run yet" |
| 485 | + ); |
| 486 | + |
| 487 | + // ------------------------------ |
| 488 | + // Step 2: Run the Migration |
| 489 | + // ------------------------------ |
| 490 | + let weight = |
| 491 | + pallet_subtensor::migrations::migrate_commit_reveal_v2::migrate_commit_reveal_2::<Test>( |
| 492 | + ); |
| 493 | + |
| 494 | + assert!( |
| 495 | + HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()), |
| 496 | + "Migration should be marked as run" |
| 497 | + ); |
| 498 | + |
| 499 | + // ------------------------------ |
| 500 | + // Step 3: Verify Migration Effects |
| 501 | + // ------------------------------ |
| 502 | + let stored_interval_after = get_raw(&interval_key); |
| 503 | + assert!( |
| 504 | + stored_interval_after.is_none(), |
| 505 | + "WeightCommitRevealInterval should be cleared" |
| 506 | + ); |
| 507 | + |
| 508 | + let stored_commit_after = get_raw(&commit_key); |
| 509 | + assert!( |
| 510 | + stored_commit_after.is_none(), |
| 511 | + "WeightCommits entry should be cleared" |
| 512 | + ); |
| 513 | + |
| 514 | + assert!(!weight.is_zero(), "Migration weight should be non-zero"); |
| 515 | + }); |
| 516 | +} |
0 commit comments