3
3
pub use pallet:: * ;
4
4
#[ frame_support:: pallet]
5
5
pub mod pallet {
6
+ use frame_support:: storage:: { storage_prefix, unhashed} ;
6
7
use frame_support:: { pallet_prelude:: * , traits:: Currency } ;
7
8
use frame_system:: pallet_prelude:: * ;
8
9
use pallet_balances:: { self as balances} ;
9
10
use sp_runtime:: traits:: UniqueSaturatedInto ;
11
+
10
12
#[ pallet:: pallet]
11
13
// #[pallet::generate_store(pub(super) trait Store)]
12
14
#[ pallet:: without_storage_info]
@@ -16,7 +18,9 @@ pub mod pallet {
16
18
#[ pallet:: generate_deposit( pub ( crate ) fn deposit_event) ]
17
19
pub enum Event < T : Config > {
18
20
// 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 ) ,
20
24
}
21
25
22
26
#[ pallet:: config]
@@ -27,17 +31,10 @@ pub mod pallet {
27
31
#[ pallet:: call]
28
32
impl < T : Config > Pallet < T > { }
29
33
30
- #[ pallet:: storage]
31
- #[ pallet:: getter( fn sudo_migration_completed) ]
32
- pub type SudoMigrationCompleted < T > = StorageValue < _ , bool , ValueQuery > ;
33
-
34
34
#[ pallet:: hooks]
35
35
impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
36
36
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 ( ) ;
41
38
let sudo_account = T :: AccountId :: decode (
42
39
& mut & [
43
40
12 , 32 , 23 , 164 , 241 , 21 , 192 , 19 , 216 , 153 , 180 , 148 , 201 , 85 , 167 , 236 , 76 ,
@@ -47,26 +44,31 @@ pub mod pallet {
47
44
. unwrap ( ) ;
48
45
let amount_to_add: T :: Balance = 10_000_000_000_000_000u128 . unique_saturated_into ( ) ;
49
46
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 {
51
64
let imbalance =
52
65
balances:: Pallet :: < T > :: deposit_creating ( & sudo_account, amount_to_add) ;
53
66
drop ( imbalance) ;
67
+ Self :: deposit_event ( Event :: SudoBalanceDeposited ( sudo_account, amount_to_add) ) ;
68
+ weight = weight. saturating_add ( T :: DbWeight :: get ( ) . writes ( 1 ) ) ;
54
69
}
55
70
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 ) )
70
72
}
71
73
}
72
74
}
0 commit comments