22
22
//!
23
23
//! ## Dependencies
24
24
//!
25
- //! This module depends on the [I’m online module](../srml_im_online/index.html)
26
- //! using its session key .
25
+ //! This module depends on an externally defined session key type, specified via
26
+ //! `Trait::AuthorityId` in the respective node runtime implementation .
27
27
28
28
// Ensure we're `no_std` when compiling for Wasm.
29
29
#![ cfg_attr( not( feature = "std" ) , no_std) ]
30
30
31
31
use app_crypto:: RuntimeAppPublic ;
32
+ use codec:: { Decode , Encode } ;
32
33
use rstd:: prelude:: * ;
33
34
use support:: { decl_module, decl_storage} ;
34
35
35
- pub trait Trait : system:: Trait + session:: Trait + im_online:: Trait { }
36
-
37
- type AuthorityIdFor < T > = <T as im_online:: Trait >:: AuthorityId ;
38
- type AuthoritySignatureFor < T > =
39
- <<T as im_online:: Trait >:: AuthorityId as RuntimeAppPublic >:: Signature ;
36
+ /// The module's config trait.
37
+ pub trait Trait : system:: Trait + session:: Trait {
38
+ type AuthorityId : RuntimeAppPublic + Default + Decode + Encode + PartialEq ;
39
+ }
40
40
41
41
decl_storage ! {
42
42
trait Store for Module <T : Trait > as AuthorityDiscovery {
43
43
/// The current set of keys that may issue a heartbeat.
44
- Keys get( keys) : Vec <AuthorityIdFor < T > >;
44
+ Keys get( keys) : Vec <T :: AuthorityId >;
45
45
}
46
46
add_extra_genesis {
47
- config( keys) : Vec <AuthorityIdFor < T > >;
47
+ config( keys) : Vec <T :: AuthorityId >;
48
48
build( |config| Module :: <T >:: initialize_keys( & config. keys) )
49
49
}
50
50
}
@@ -59,10 +59,10 @@ impl<T: Trait> Module<T> {
59
59
/// set, otherwise this function returns None. The restriction might be
60
60
/// softened in the future in case a consumer needs to learn own authority
61
61
/// identifier.
62
- fn authority_id ( ) -> Option < AuthorityIdFor < T > > {
62
+ fn authority_id ( ) -> Option < T :: AuthorityId > {
63
63
let authorities = Keys :: < T > :: get ( ) ;
64
64
65
- let local_keys = < AuthorityIdFor < T > > :: all ( ) ;
65
+ let local_keys = T :: AuthorityId :: all ( ) ;
66
66
67
67
authorities. into_iter ( ) . find_map ( |authority| {
68
68
if local_keys. contains ( & authority) {
@@ -74,12 +74,17 @@ impl<T: Trait> Module<T> {
74
74
}
75
75
76
76
/// Retrieve authority identifiers of the current authority set.
77
- pub fn authorities ( ) -> Vec < AuthorityIdFor < T > > {
77
+ pub fn authorities ( ) -> Vec < T :: AuthorityId > {
78
78
Keys :: < T > :: get ( )
79
79
}
80
80
81
81
/// Sign the given payload with the private key corresponding to the given authority id.
82
- pub fn sign ( payload : & Vec < u8 > ) -> Option < ( AuthoritySignatureFor < T > , AuthorityIdFor < T > ) > {
82
+ pub fn sign (
83
+ payload : & Vec < u8 > ,
84
+ ) -> Option < (
85
+ <<T as Trait >:: AuthorityId as RuntimeAppPublic >:: Signature ,
86
+ T :: AuthorityId ,
87
+ ) > {
83
88
let authority_id = Module :: < T > :: authority_id ( ) ?;
84
89
authority_id. sign ( payload) . map ( |s| ( s, authority_id) )
85
90
}
@@ -88,13 +93,13 @@ impl<T: Trait> Module<T> {
88
93
/// authority identifier.
89
94
pub fn verify (
90
95
payload : & Vec < u8 > ,
91
- signature : AuthoritySignatureFor < T > ,
92
- authority_id : AuthorityIdFor < T > ,
96
+ signature : << T as Trait > :: AuthorityId as RuntimeAppPublic > :: Signature ,
97
+ authority_id : T :: AuthorityId ,
93
98
) -> bool {
94
99
authority_id. verify ( payload, & signature)
95
100
}
96
101
97
- fn initialize_keys ( keys : & [ AuthorityIdFor < T > ] ) {
102
+ fn initialize_keys ( keys : & [ T :: AuthorityId ] ) {
98
103
if !keys. is_empty ( ) {
99
104
assert ! ( Keys :: <T >:: get( ) . is_empty( ) , "Keys are already initialized!" ) ;
100
105
Keys :: < T > :: put_ref ( keys) ;
@@ -103,7 +108,7 @@ impl<T: Trait> Module<T> {
103
108
}
104
109
105
110
impl < T : Trait > session:: OneSessionHandler < T :: AccountId > for Module < T > {
106
- type Key = AuthorityIdFor < T > ;
111
+ type Key = T :: AuthorityId ;
107
112
108
113
fn on_genesis_session < ' a , I : ' a > ( validators : I )
109
114
where
@@ -144,9 +149,11 @@ mod tests {
144
149
145
150
#[ derive( Clone , Eq , PartialEq ) ]
146
151
pub struct Test ;
147
- impl Trait for Test { }
152
+ impl Trait for Test {
153
+ type AuthorityId = babe_primitives:: AuthorityId ;
154
+ }
148
155
149
- type AuthorityId = im_online :: sr25519 :: AuthorityId ;
156
+ type AuthorityId = babe_primitives :: AuthorityId ;
150
157
151
158
pub struct TestOnSessionEnding ;
152
159
impl session:: OnSessionEnding < AuthorityId > for TestOnSessionEnding {
@@ -176,18 +183,6 @@ mod tests {
176
183
type FullIdentificationOf = ( ) ;
177
184
}
178
185
179
- impl im_online:: Trait for Test {
180
- type AuthorityId = AuthorityId ;
181
- type Call = im_online:: Call < Test > ;
182
- type Event = ( ) ;
183
- type SubmitTransaction = system:: offchain:: TransactionSubmitter <
184
- ( ) ,
185
- im_online:: Call < Test > ,
186
- UncheckedExtrinsic < ( ) , im_online:: Call < Test > , ( ) , ( ) > ,
187
- > ;
188
- type ReportUnresponsiveness = ( ) ;
189
- }
190
-
191
186
pub type BlockNumber = u64 ;
192
187
193
188
parameter_types ! {
@@ -243,13 +238,13 @@ mod tests {
243
238
let key_store = KeyStore :: new ( ) ;
244
239
key_store
245
240
. write ( )
246
- . sr25519_generate_new ( key_types:: IM_ONLINE , None )
241
+ . sr25519_generate_new ( key_types:: BABE , None )
247
242
. expect ( "Generates key." ) ;
248
243
249
244
// Retrieve key to later check if we got the right one.
250
245
let public_key = key_store
251
246
. read ( )
252
- . sr25519_public_keys ( key_types:: IM_ONLINE )
247
+ . sr25519_public_keys ( key_types:: BABE )
253
248
. pop ( )
254
249
. unwrap ( ) ;
255
250
let authority_id = AuthorityId :: from ( public_key) ;
@@ -283,7 +278,7 @@ mod tests {
283
278
let key_store = KeyStore :: new ( ) ;
284
279
key_store
285
280
. write ( )
286
- . sr25519_generate_new ( key_types:: IM_ONLINE , None )
281
+ . sr25519_generate_new ( key_types:: BABE , None )
287
282
. expect ( "Generates key." ) ;
288
283
289
284
// Build genesis.
@@ -317,13 +312,13 @@ mod tests {
317
312
let key_store = KeyStore :: new ( ) ;
318
313
key_store
319
314
. write ( )
320
- . sr25519_generate_new ( key_types:: IM_ONLINE , None )
315
+ . sr25519_generate_new ( key_types:: BABE , None )
321
316
. expect ( "Generates key." ) ;
322
317
323
318
// Retrieve key to later check if we got the right one.
324
319
let public_key = key_store
325
320
. read ( )
326
- . sr25519_public_keys ( key_types:: IM_ONLINE )
321
+ . sr25519_public_keys ( key_types:: BABE )
327
322
. pop ( )
328
323
. unwrap ( ) ;
329
324
let authority_id = AuthorityId :: from ( public_key) ;
@@ -347,7 +342,11 @@ mod tests {
347
342
let payload = String :: from ( "test payload" ) . into_bytes ( ) ;
348
343
let ( sig, authority_id) = AuthorityDiscovery :: sign ( & payload) . expect ( "signature" ) ;
349
344
350
- assert ! ( AuthorityDiscovery :: verify( & payload, sig. clone( ) , authority_id. clone( ) , ) ) ;
345
+ assert ! ( AuthorityDiscovery :: verify(
346
+ & payload,
347
+ sig. clone( ) ,
348
+ authority_id. clone( ) ,
349
+ ) ) ;
351
350
352
351
assert ! ( !AuthorityDiscovery :: verify(
353
352
& String :: from( "other payload" ) . into_bytes( ) ,
0 commit comments