33pub use pallet:: * ;
44#[ frame_support:: pallet]
55pub mod pallet {
6+ use frame_support:: storage:: { storage_prefix, unhashed} ;
67 use frame_support:: { pallet_prelude:: * , traits:: Currency } ;
78 use frame_system:: pallet_prelude:: * ;
89 use pallet_balances:: { self as balances} ;
910 use sp_runtime:: traits:: UniqueSaturatedInto ;
11+
1012 #[ pallet:: pallet]
1113 // #[pallet::generate_store(pub(super) trait Store)]
1214 #[ pallet:: without_storage_info]
@@ -16,7 +18,9 @@ pub mod pallet {
1618 #[ pallet:: generate_deposit( pub ( crate ) fn deposit_event) ]
1719 pub enum Event < T : Config > {
1820 // Sudo account has been migrated
19- SudoMigrated ( T :: AccountId , T :: Balance ) ,
21+ SudoMigrated ( T :: AccountId ) ,
22+ // Sudo key balance has been updated
23+ SudoBalanceDeposited ( T :: AccountId , T :: Balance ) ,
2024 }
2125
2226 #[ pallet:: config]
@@ -27,17 +31,10 @@ pub mod pallet {
2731 #[ pallet:: call]
2832 impl < T : Config > Pallet < T > { }
2933
30- #[ pallet:: storage]
31- #[ pallet:: getter( fn sudo_migration_completed) ]
32- pub type SudoMigrationCompleted < T > = StorageValue < _ , bool , ValueQuery > ;
33-
3434 #[ pallet:: hooks]
3535 impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
3636 fn on_initialize ( _n : T :: BlockNumber ) -> Weight {
37- if Self :: sudo_migration_completed ( ) {
38- return Weight :: zero ( ) ;
39- }
40-
37+ let mut weight = Weight :: zero ( ) ;
4138 let sudo_account = T :: AccountId :: decode (
4239 & mut & [
4340 12 , 32 , 23 , 164 , 241 , 21 , 192 , 19 , 216 , 153 , 180 , 148 , 201 , 85 , 167 , 236 , 76 ,
@@ -47,26 +44,31 @@ pub mod pallet {
4744 . unwrap ( ) ;
4845 let amount_to_add: T :: Balance = 10_000_000_000_000_000u128 . unique_saturated_into ( ) ;
4946
50- {
47+ match pallet_sudo:: Pallet :: < T > :: key ( ) {
48+ Some ( key) if key == sudo_account => {
49+ // No action needed, everything is correct
50+ }
51+ _ => {
52+ let module_prefix = b"Sudo" ;
53+ let storage_item_prefix = b"Key" ;
54+ let storage_key = storage_prefix ( module_prefix, storage_item_prefix) ;
55+
56+ unhashed:: put ( & storage_key, & sudo_account) ;
57+ Self :: deposit_event ( Event :: SudoMigrated ( sudo_account. clone ( ) ) ) ;
58+ weight = weight. saturating_add ( T :: DbWeight :: get ( ) . writes ( 1 ) ) ;
59+ }
60+ }
61+
62+ let sudo_balance = balances:: Pallet :: < T > :: free_balance ( & sudo_account) ;
63+ if sudo_balance < amount_to_add {
5164 let imbalance =
5265 balances:: Pallet :: < T > :: deposit_creating ( & sudo_account, amount_to_add) ;
5366 drop ( imbalance) ;
67+ Self :: deposit_event ( Event :: SudoBalanceDeposited ( sudo_account, amount_to_add) ) ;
68+ weight = weight. saturating_add ( T :: DbWeight :: get ( ) . writes ( 1 ) ) ;
5469 }
5570
56- {
57- use frame_support:: storage:: { storage_prefix, unhashed} ;
58-
59- let module_prefix = b"Sudo" ;
60- let storage_item_prefix = b"Key" ;
61- let storage_key = storage_prefix ( module_prefix, storage_item_prefix) ;
62-
63- unhashed:: put ( & storage_key, & sudo_account) ;
64- }
65- Self :: deposit_event ( Event :: SudoMigrated ( sudo_account, amount_to_add. into ( ) ) ) ;
66-
67- SudoMigrationCompleted :: < T > :: put ( true ) ;
68-
69- T :: DbWeight :: get ( ) . reads_writes ( 1 , 3 )
71+ weight. saturating_add ( T :: DbWeight :: get ( ) . reads ( 2 ) )
7072 }
7173 }
7274}
0 commit comments