@@ -9,13 +9,19 @@ use frame_support;
9
9
use frame_support:: traits:: Currency ;
10
10
use frame_support:: { dispatch:: DispatchResultWithPostInfo , pallet_prelude:: * } ;
11
11
use sp_runtime:: traits:: StaticLookup ;
12
+ use sp_std:: convert:: TryInto ;
12
13
use sp_std:: prelude:: * ;
13
14
use substrate_validator_set;
14
- use sp_std:: convert:: TryInto ;
15
15
16
16
pub mod types;
17
17
pub use pallet:: * ;
18
18
19
+ #[ cfg( test) ]
20
+ mod tests;
21
+
22
+ #[ cfg( test) ]
23
+ mod mock;
24
+
19
25
#[ frame_support:: pallet]
20
26
pub mod pallet {
21
27
use super :: * ;
@@ -53,8 +59,8 @@ pub mod pallet {
53
59
#[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
54
60
pub enum Event < T : Config > {
55
61
Bonded ( T :: AccountId ) ,
56
- ValidatorCreated ( T :: AccountId , types:: Validator < T :: AccountId > ) ,
57
- ValidatorApproved ( types:: Validator < T :: AccountId > ) ,
62
+ ValidatorRequestCreated ( T :: AccountId , types:: Validator < T :: AccountId > ) ,
63
+ ValidatorRequestApproved ( types:: Validator < T :: AccountId > ) ,
58
64
ValidatorActivated ( types:: Validator < T :: AccountId > ) ,
59
65
ValidatorRemoved ( types:: Validator < T :: AccountId > ) ,
60
66
NodeValidatorChanged ( T :: AccountId ) ,
@@ -64,16 +70,17 @@ pub mod pallet {
64
70
#[ pallet:: error]
65
71
pub enum Error < T > {
66
72
BadOrigin ,
73
+ NotCouncilMember ,
67
74
AlreadyBonded ,
68
- StashNotBonded ,
69
- StashBondedWithWrongValidator ,
70
- CannotBondWithSameAccount ,
75
+ StashNotBonded , // TOCHECK: Unused
76
+ StashBondedWithWrongValidator , // TOCHECK: Unused
77
+ CannotBondWithSameAccount , // TOCHECK: Unused
71
78
DuplicateValidator ,
72
79
ValidatorNotFound ,
73
80
ValidatorNotApproved ,
74
- UnauthorizedToActivateValidator ,
81
+ UnauthorizedToActivateValidator , // TOCHECK: Unused
75
82
ValidatorValidatingAlready ,
76
- ValidatorNotValidating ,
83
+ ValidatorNotValidating , // TOCHECK: Unused
77
84
}
78
85
79
86
#[ pallet:: hooks]
@@ -105,11 +112,11 @@ pub mod pallet {
105
112
/// Validator node account: the account that will validate on consensus layer
106
113
/// Stash account: the "bank" account of the validator (where rewards should be sent to) the stash should be bonded to a validator
107
114
/// Description: why someone wants to become a validator
108
- /// Tf Connect ID: the threefold connect ID of the persion who wants to become a validator
115
+ /// Tf Connect ID: the threefold connect ID of the person who wants to become a validator
109
116
/// Info: some public info about the validator (website link, blog link, ..)
110
117
/// A user can only have 1 validator request at a time
111
118
#[ pallet:: weight( 100_000_000 ) ]
112
- pub fn create_validator (
119
+ pub fn create_validator_request (
113
120
origin : OriginFor < T > ,
114
121
validator_node_account : T :: AccountId ,
115
122
stash_account : T :: AccountId ,
@@ -137,7 +144,7 @@ pub mod pallet {
137
144
// Create a validator request object
138
145
<Validator < T > >:: insert ( & address, & request) ;
139
146
140
- Self :: deposit_event ( Event :: ValidatorCreated ( address, request. clone ( ) ) ) ;
147
+ Self :: deposit_event ( Event :: ValidatorRequestCreated ( address, request. clone ( ) ) ) ;
141
148
142
149
Ok ( ( ) . into ( ) )
143
150
}
@@ -191,10 +198,6 @@ pub mod pallet {
191
198
let mut validator = <Validator < T > >:: get ( & address)
192
199
. ok_or ( DispatchError :: from ( Error :: < T > :: ValidatorNotFound ) ) ?;
193
200
194
- // Set the new validator node account on the validator struct
195
- validator. validator_node_account = new_node_validator_account. clone ( ) ;
196
- <Validator < T > >:: insert ( address, & validator) ;
197
-
198
201
// if validator is validating, also remove old one from consensus and add new one.
199
202
if validator. state == types:: ValidatorRequestState :: Validating {
200
203
// Remove the old validator and rotate session
@@ -211,13 +214,19 @@ pub mod pallet {
211
214
frame_system:: RawOrigin :: Root . into ( ) ,
212
215
new_node_validator_account. clone ( ) ,
213
216
) ?;
214
- Self :: deposit_event ( Event :: NodeValidatorChanged ( new_node_validator_account) ) ;
217
+ Self :: deposit_event ( Event :: NodeValidatorChanged (
218
+ new_node_validator_account. clone ( ) ,
219
+ ) ) ;
215
220
}
216
221
222
+ // Set the new validator node account on the validator struct
223
+ validator. validator_node_account = new_node_validator_account;
224
+ <Validator < T > >:: insert ( address, & validator) ;
225
+
217
226
Ok ( ( ) . into ( ) )
218
227
}
219
228
220
- /// Bond an account to to a validator account
229
+ /// Bond an account to a validator account
221
230
/// Just proves that the stash account is indeed under control of the validator account
222
231
#[ pallet:: weight( 100_000_000 ) ]
223
232
pub fn bond (
@@ -230,6 +239,7 @@ pub mod pallet {
230
239
Err ( Error :: < T > :: AlreadyBonded ) ?
231
240
}
232
241
let validator = T :: Lookup :: lookup ( validator) ?;
242
+ // TOCHECK: enough to identify validator?
233
243
234
244
<Bonded < T > >:: insert ( & stash, & validator) ;
235
245
@@ -246,7 +256,9 @@ pub mod pallet {
246
256
origin : OriginFor < T > ,
247
257
validator_account : T :: AccountId ,
248
258
) -> DispatchResultWithPostInfo {
249
- T :: CouncilOrigin :: ensure_origin ( origin) ?;
259
+ let who = ensure_signed ( origin) ?;
260
+
261
+ Self :: is_council_member ( who) ?;
250
262
251
263
let mut validator = <Validator < T > >:: get ( & validator_account)
252
264
. ok_or ( DispatchError :: from ( Error :: < T > :: ValidatorNotFound ) ) ?;
@@ -261,7 +273,7 @@ pub mod pallet {
261
273
validator_account. clone ( ) ,
262
274
) ?;
263
275
264
- Self :: deposit_event ( Event :: ValidatorApproved ( validator) ) ;
276
+ Self :: deposit_event ( Event :: ValidatorRequestApproved ( validator) ) ;
265
277
266
278
Ok ( ( ) . into ( ) )
267
279
}
@@ -277,9 +289,9 @@ pub mod pallet {
277
289
origin : OriginFor < T > ,
278
290
validator : T :: AccountId ,
279
291
) -> DispatchResultWithPostInfo {
280
- if ! ( ensure_signed ( origin. clone ( ) ) ? == validator
281
- || T :: CouncilOrigin :: ensure_origin ( origin ) . is_ok ( ) )
282
- {
292
+ let who = ensure_signed ( origin. clone ( ) ) ?;
293
+
294
+ if ! ( who == validator || Self :: is_council_member ( who ) . is_ok ( ) ) {
283
295
Err ( Error :: < T > :: BadOrigin ) ?
284
296
}
285
297
@@ -318,3 +330,14 @@ pub mod pallet {
318
330
}
319
331
}
320
332
}
333
+
334
+ impl < T : Config > Pallet < T > {
335
+ fn is_council_member ( who : T :: AccountId ) -> DispatchResultWithPostInfo {
336
+ let council_members =
337
+ pallet_membership:: Pallet :: < T , pallet_membership:: Instance1 > :: members ( ) ;
338
+
339
+ ensure ! ( council_members. contains( & who) , Error :: <T >:: NotCouncilMember , ) ;
340
+
341
+ Ok ( ( ) . into ( ) )
342
+ }
343
+ }
0 commit comments