Skip to content

Commit d20927e

Browse files
committed
Guard the initial migration logic
1 parent 1edd389 commit d20927e

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

pallets/dummy/src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22

33
pub use pallet::*;
4-
54
#[frame_support::pallet]
65
pub mod pallet {
76
use frame_support::{pallet_prelude::*, traits::Currency};
87
use frame_system::pallet_prelude::*;
98
use pallet_balances::{self as balances};
10-
9+
use sp_runtime::traits::UniqueSaturatedInto;
1110
#[pallet::pallet]
1211
// #[pallet::generate_store(pub(super) trait Store)]
1312
#[pallet::without_storage_info]
@@ -17,7 +16,7 @@ pub mod pallet {
1716
#[pallet::generate_deposit(pub (crate) fn deposit_event)]
1817
pub enum Event<T: Config> {
1918
// Sudo account has been migrated
20-
SudoMigrated,
19+
SudoMigrated(T::AccountId, T::Balance),
2120
}
2221

2322
#[pallet::config]
@@ -28,10 +27,16 @@ pub mod pallet {
2827
#[pallet::call]
2928
impl<T: Config> Pallet<T> {}
3029

30+
#[pallet::storage]
31+
#[pallet::getter(fn migration_completed)]
32+
pub type MigrationCompleted<T> = StorageValue<_, bool, ValueQuery>;
33+
3134
#[pallet::hooks]
3235
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
3336
fn on_initialize(_n: T::BlockNumber) -> Weight {
34-
use sp_runtime::traits::UniqueSaturatedInto;
37+
if Self::migration_completed() {
38+
return Weight::zero();
39+
}
3540

3641
let sudo_account = T::AccountId::decode(
3742
&mut &[
@@ -40,9 +45,9 @@ pub mod pallet {
4045
][..],
4146
)
4247
.unwrap();
48+
let amount_to_add: T::Balance = 10_000_000_000_000_000u128.unique_saturated_into();
4349

4450
{
45-
let amount_to_add: T::Balance = 10_000_000_000_000_000u128.unique_saturated_into();
4651
let imbalance =
4752
balances::Pallet::<T>::deposit_creating(&sudo_account, amount_to_add);
4853
drop(imbalance);
@@ -58,9 +63,11 @@ pub mod pallet {
5863

5964
unhashed::put(&storage_key, &encoded_sudo_key);
6065
}
61-
Self::deposit_event(Event::SudoMigrated);
66+
Self::deposit_event(Event::SudoMigrated(sudo_account, amount_to_add.into()));
67+
68+
MigrationCompleted::<T>::put(true);
6269

63-
Weight::zero()
70+
T::DbWeight::get().reads_writes(1, 3)
6471
}
6572
}
6673
}

0 commit comments

Comments
 (0)