Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 7fc0a91

Browse files
mxindenandresilva
authored andcommitted
srml/authority-discovery: Abstract session key type (#3698)
* srml/authority-discovery: Abstract session key type Previously `srml/authority-discovery` dependet on the `srml/im-online` session key type directly. With this patch `srml/authority-discovery` is generic over the session key type it is going to use, as long as it implements the RuntimeAppPublic trait. With this patch one can use the `srml/authority-discovery` module without the `srml/im-online` module. Next to the above, this patch configures `node/runtime` to use the babe session keys for the authority discovery module. * srml/authority-discovery: Fix line length * srml/authority-discovery/Cargo: Move babe to dev-dependencies * node/runtime: Bump implementation version * srml/authority-discovery: Add doc comment for authority discovery Trait
1 parent 55c9d14 commit 7fc0a91

File tree

4 files changed

+53
-53
lines changed

4 files changed

+53
-53
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/runtime/src/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use node_primitives::{
2929
AccountId, AccountIndex, Balance, BlockNumber, Hash, Index,
3030
Moment, Signature, ContractExecResult,
3131
};
32-
use babe_primitives::{AuthorityId as BabeId};
32+
use babe_primitives::{AuthorityId as BabeId, AuthoritySignature as BabeSignature};
3333
use grandpa::fg_primitives;
3434
use client::{
3535
block_builder::api::{self as block_builder_api, InherentData, CheckInherentsResult},
@@ -50,7 +50,7 @@ use elections::VoteIndex;
5050
use version::NativeVersion;
5151
use primitives::OpaqueMetadata;
5252
use grandpa::{AuthorityId as GrandpaId, AuthorityWeight as GrandpaWeight};
53-
use im_online::sr25519::{AuthorityId as ImOnlineId, AuthoritySignature as ImOnlineSignature};
53+
use im_online::sr25519::{AuthorityId as ImOnlineId};
5454
use authority_discovery_primitives::{AuthorityId as EncodedAuthorityId, Signature as EncodedSignature};
5555
use codec::{Encode, Decode};
5656
use system::offchain::TransactionSubmitter;
@@ -84,8 +84,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
8484
// and set impl_version to equal spec_version. If only runtime
8585
// implementation changes and behavior does not, then leave spec_version as
8686
// is and increment impl_version.
87-
spec_version: 167,
88-
impl_version: 167,
87+
spec_version: 168,
88+
impl_version: 168,
8989
apis: RUNTIME_API_VERSIONS,
9090
};
9191

@@ -437,7 +437,9 @@ impl offences::Trait for Runtime {
437437
type OnOffenceHandler = Staking;
438438
}
439439

440-
impl authority_discovery::Trait for Runtime {}
440+
impl authority_discovery::Trait for Runtime {
441+
type AuthorityId = BabeId;
442+
}
441443

442444
impl grandpa::Trait for Runtime {
443445
type Event = Event;
@@ -635,12 +637,12 @@ impl_runtime_apis! {
635637
}
636638

637639
fn verify(payload: &Vec<u8>, signature: &EncodedSignature, authority_id: &EncodedAuthorityId) -> bool {
638-
let signature = match ImOnlineSignature::decode(&mut &signature.0[..]) {
640+
let signature = match BabeSignature::decode(&mut &signature.0[..]) {
639641
Ok(s) => s,
640642
_ => return false,
641643
};
642644

643-
let authority_id = match ImOnlineId::decode(&mut &authority_id.0[..]) {
645+
let authority_id = match BabeId::decode(&mut &authority_id.0[..]) {
644646
Ok(id) => id,
645647
_ => return false,
646648
};

srml/authority-discovery/Cargo.toml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@ authors = ["Parity Technologies <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8+
app-crypto = { package = "substrate-application-crypto", path = "../../core/application-crypto", default-features = false }
89
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
9-
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
1010
primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false }
11-
app-crypto = { package = "substrate-application-crypto", path = "../../core/application-crypto", default-features = false }
1211
rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false }
12+
runtime-io = { package = "sr-io", path = "../../core/sr-io", default-features = false }
1313
serde = { version = "1.0", optional = true }
1414
session = { package = "srml-session", path = "../session", default-features = false }
15-
im-online = { package = "srml-im-online", path = "../im-online", default-features = false }
15+
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
1616
support = { package = "srml-support", path = "../support", default-features = false }
17-
runtime-io = { package = "sr-io", path = "../../core/sr-io", default-features = false }
1817
system = { package = "srml-system", path = "../system", default-features = false }
1918

2019
[dev-dependencies]
20+
babe-primitives = { package = "substrate-consensus-babe-primitives", path = "../../core/consensus/babe/primitives", default-features = false }
2121
sr-staking-primitives = { path = "../../core/sr-staking-primitives", default-features = false }
2222

2323
[features]
2424
default = ["std"]
2525
std = [
26+
"app-crypto/std",
2627
"codec/std",
27-
"sr-primitives/std",
2828
"primitives/std",
2929
"rstd/std",
30+
"runtime-io/std",
3031
"serde",
3132
"session/std",
32-
"im-online/std",
33+
"sr-primitives/std",
3334
"support/std",
34-
"runtime-io/std",
3535
"system/std",
36-
"app-crypto/std",
3736
]

srml/authority-discovery/src/lib.rs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,29 @@
2222
//!
2323
//! ## Dependencies
2424
//!
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.
2727
2828
// Ensure we're `no_std` when compiling for Wasm.
2929
#![cfg_attr(not(feature = "std"), no_std)]
3030

3131
use app_crypto::RuntimeAppPublic;
32+
use codec::{Decode, Encode};
3233
use rstd::prelude::*;
3334
use support::{decl_module, decl_storage};
3435

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+
}
4040

4141
decl_storage! {
4242
trait Store for Module<T: Trait> as AuthorityDiscovery {
4343
/// The current set of keys that may issue a heartbeat.
44-
Keys get(keys): Vec<AuthorityIdFor<T>>;
44+
Keys get(keys): Vec<T::AuthorityId>;
4545
}
4646
add_extra_genesis {
47-
config(keys): Vec<AuthorityIdFor<T>>;
47+
config(keys): Vec<T::AuthorityId>;
4848
build(|config| Module::<T>::initialize_keys(&config.keys))
4949
}
5050
}
@@ -59,10 +59,10 @@ impl<T: Trait> Module<T> {
5959
/// set, otherwise this function returns None. The restriction might be
6060
/// softened in the future in case a consumer needs to learn own authority
6161
/// identifier.
62-
fn authority_id() -> Option<AuthorityIdFor<T>> {
62+
fn authority_id() -> Option<T::AuthorityId> {
6363
let authorities = Keys::<T>::get();
6464

65-
let local_keys = <AuthorityIdFor<T>>::all();
65+
let local_keys = T::AuthorityId::all();
6666

6767
authorities.into_iter().find_map(|authority| {
6868
if local_keys.contains(&authority) {
@@ -74,12 +74,17 @@ impl<T: Trait> Module<T> {
7474
}
7575

7676
/// Retrieve authority identifiers of the current authority set.
77-
pub fn authorities() -> Vec<AuthorityIdFor<T>> {
77+
pub fn authorities() -> Vec<T::AuthorityId> {
7878
Keys::<T>::get()
7979
}
8080

8181
/// 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+
)> {
8388
let authority_id = Module::<T>::authority_id()?;
8489
authority_id.sign(payload).map(|s| (s, authority_id))
8590
}
@@ -88,13 +93,13 @@ impl<T: Trait> Module<T> {
8893
/// authority identifier.
8994
pub fn verify(
9095
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,
9398
) -> bool {
9499
authority_id.verify(payload, &signature)
95100
}
96101

97-
fn initialize_keys(keys: &[AuthorityIdFor<T>]) {
102+
fn initialize_keys(keys: &[T::AuthorityId]) {
98103
if !keys.is_empty() {
99104
assert!(Keys::<T>::get().is_empty(), "Keys are already initialized!");
100105
Keys::<T>::put_ref(keys);
@@ -103,7 +108,7 @@ impl<T: Trait> Module<T> {
103108
}
104109

105110
impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
106-
type Key = AuthorityIdFor<T>;
111+
type Key = T::AuthorityId;
107112

108113
fn on_genesis_session<'a, I: 'a>(validators: I)
109114
where
@@ -144,9 +149,11 @@ mod tests {
144149

145150
#[derive(Clone, Eq, PartialEq)]
146151
pub struct Test;
147-
impl Trait for Test {}
152+
impl Trait for Test {
153+
type AuthorityId = babe_primitives::AuthorityId;
154+
}
148155

149-
type AuthorityId = im_online::sr25519::AuthorityId;
156+
type AuthorityId = babe_primitives::AuthorityId;
150157

151158
pub struct TestOnSessionEnding;
152159
impl session::OnSessionEnding<AuthorityId> for TestOnSessionEnding {
@@ -176,18 +183,6 @@ mod tests {
176183
type FullIdentificationOf = ();
177184
}
178185

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-
191186
pub type BlockNumber = u64;
192187

193188
parameter_types! {
@@ -243,13 +238,13 @@ mod tests {
243238
let key_store = KeyStore::new();
244239
key_store
245240
.write()
246-
.sr25519_generate_new(key_types::IM_ONLINE, None)
241+
.sr25519_generate_new(key_types::BABE, None)
247242
.expect("Generates key.");
248243

249244
// Retrieve key to later check if we got the right one.
250245
let public_key = key_store
251246
.read()
252-
.sr25519_public_keys(key_types::IM_ONLINE)
247+
.sr25519_public_keys(key_types::BABE)
253248
.pop()
254249
.unwrap();
255250
let authority_id = AuthorityId::from(public_key);
@@ -283,7 +278,7 @@ mod tests {
283278
let key_store = KeyStore::new();
284279
key_store
285280
.write()
286-
.sr25519_generate_new(key_types::IM_ONLINE, None)
281+
.sr25519_generate_new(key_types::BABE, None)
287282
.expect("Generates key.");
288283

289284
// Build genesis.
@@ -317,13 +312,13 @@ mod tests {
317312
let key_store = KeyStore::new();
318313
key_store
319314
.write()
320-
.sr25519_generate_new(key_types::IM_ONLINE, None)
315+
.sr25519_generate_new(key_types::BABE, None)
321316
.expect("Generates key.");
322317

323318
// Retrieve key to later check if we got the right one.
324319
let public_key = key_store
325320
.read()
326-
.sr25519_public_keys(key_types::IM_ONLINE)
321+
.sr25519_public_keys(key_types::BABE)
327322
.pop()
328323
.unwrap();
329324
let authority_id = AuthorityId::from(public_key);
@@ -347,7 +342,11 @@ mod tests {
347342
let payload = String::from("test payload").into_bytes();
348343
let (sig, authority_id) = AuthorityDiscovery::sign(&payload).expect("signature");
349344

350-
assert!(AuthorityDiscovery::verify(&payload, sig.clone(), authority_id.clone(),));
345+
assert!(AuthorityDiscovery::verify(
346+
&payload,
347+
sig.clone(),
348+
authority_id.clone(),
349+
));
351350

352351
assert!(!AuthorityDiscovery::verify(
353352
&String::from("other payload").into_bytes(),

0 commit comments

Comments
 (0)