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

Add authority discovery module #444

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ safe-mix = { version = "1.0", default-features = false}
serde = { version = "1.0", default-features = false }
serde_derive = { version = "1.0", optional = true }

authority-discovery-primitives = { package = "substrate-authority-discovery-primitives", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
babe-primitives = { package = "substrate-consensus-babe-primitives", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
client = { package = "substrate-client", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
inherents = { package = "substrate-inherents", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
Expand All @@ -27,6 +28,7 @@ substrate-serializer = { git = "https://github.com/paritytech/substrate", defaul
substrate-session = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
version = { package = "sr-version", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }

authority-discovery = { package = "srml-authority-discovery", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
authorship = { package = "srml-authorship", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
babe = { package = "srml-babe", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
balances = { package = "srml-balances", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
Expand Down Expand Up @@ -68,6 +70,8 @@ default = ["std"]
no_std = []
only-staking = []
std = [
"authority-discovery-primitives/std",
"authority-discovery/std",
"bitvec/std",
"primitives/std",
"rustc-hex/std",
Expand Down
37 changes: 35 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ use sr_staking_primitives::SessionIndex;
use srml_support::{
parameter_types, construct_runtime, traits::{SplitTwoWays, Currency}
};
use im_online::sr25519::{AuthorityId as ImOnlineId};
use authority_discovery_primitives::{AuthorityId as EncodedAuthorityId, Signature as EncodedSignature};
use im_online::sr25519::{AuthorityId as ImOnlineId, AuthoritySignature as ImOnlineSignature};
use system::offchain::TransactionSubmitter;

#[cfg(feature = "std")]
Expand Down Expand Up @@ -242,7 +243,7 @@ parameter_types! {
pub const Offset: BlockNumber = 0;
}

type SessionHandlers = (Grandpa, Babe, ImOnline, Parachains);
type SessionHandlers = (Grandpa, Babe, ImOnline, AuthorityDiscovery, Parachains);
impl_opaque_keys! {
pub struct SessionKeys {
#[id(key_types::GRANDPA)]
Expand Down Expand Up @@ -444,6 +445,8 @@ impl im_online::Trait for Runtime {
type ReportUnresponsiveness = ();
}

impl authority_discovery::Trait for Runtime {}

impl grandpa::Trait for Runtime {
type Event = Event;
}
Expand Down Expand Up @@ -530,6 +533,7 @@ construct_runtime!(
FinalityTracker: finality_tracker::{Module, Call, Inherent},
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
ImOnline: im_online::{Module, Call, Storage, Event<T>, ValidateUnsigned, Config<T>},
AuthorityDiscovery: authority_discovery::{Module, Call, Config<T>},

// Governance stuff; uncallable initially.
Democracy: democracy::{Module, Call, Storage, Config, Event<T>},
Expand Down Expand Up @@ -684,6 +688,35 @@ impl_runtime_apis! {
}
}

impl authority_discovery_primitives::AuthorityDiscoveryApi<Block> for Runtime {
fn authorities() -> Vec<EncodedAuthorityId> {
AuthorityDiscovery::authorities().into_iter()
.map(|id| id.encode())
.map(EncodedAuthorityId)
.collect()
}

fn sign(payload: &Vec<u8>) -> Option<(EncodedSignature, EncodedAuthorityId)> {
AuthorityDiscovery::sign(payload).map(|(sig, id)| {
(EncodedSignature(sig.encode()), EncodedAuthorityId(id.encode()))
})
}

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

let authority_id = match ImOnlineId::decode(&mut &authority_id.0[..]) {
Ok(id) => id,
_ => return false,
};

AuthorityDiscovery::verify(payload, signature, authority_id)
}
}

impl substrate_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
let seed = seed.as_ref().map(|s| rstd::str::from_utf8(&s).expect("Seed is an utf8 string"));
Expand Down
1 change: 1 addition & 0 deletions service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ substrate-keystore = { git = "https://github.com/paritytech/substrate", branch =
srml-babe = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
srml-staking = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
im-online = { package = "srml-im-online", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
authority-discovery = { package = "substrate-authority-discovery", git = "https://github.com/paritytech/substrate", branch = "polkadot-master" }
babe = { package = "substrate-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "polkadot-master" }
babe-primitives = { package = "substrate-consensus-babe-primitives", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-master" }
2 changes: 2 additions & 0 deletions service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
babe: Some(Default::default()),
grandpa: Some(Default::default()),
im_online: Some(Default::default()),
authority_discovery: Some(Default::default()),
parachains: Some(ParachainsConfig {
authorities: vec![],
parachains: vec![],
Expand Down Expand Up @@ -285,6 +286,7 @@ pub fn testnet_genesis(
babe: Some(Default::default()),
grandpa: Some(Default::default()),
im_online: Some(Default::default()),
authority_discovery: Some(Default::default()),
parachains: Some(ParachainsConfig {
authorities: vec![],
parachains: vec![],
Expand Down
18 changes: 18 additions & 0 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
pub mod chain_spec;

use futures::prelude::*;
use futures::sync::mpsc;
use client::LongestChain;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -138,6 +139,8 @@ pub fn new_full(config: Configuration<CustomConfiguration, GenesisConfig>)
CallExecutor = impl CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync + 'static,
>, ServiceError>
{
use substrate_network::DhtEvent;

let is_authority = config.roles.is_authority();
let is_collator = config.custom.collating_for.is_some();
let force_authoring = config.force_authoring;
Expand All @@ -148,11 +151,19 @@ pub fn new_full(config: Configuration<CustomConfiguration, GenesisConfig>)

let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config);

// Dht event channel from the network to the authority discovery module. Use
// bounded channel to ensure back-pressure. Authority discovery is triggering one
// event per authority within the current authority set. This estimates the
// authority set size to be somewhere below 10 000 thereby setting the channel
// buffer size to 10 000.
let (dht_event_tx, dht_event_rx) = mpsc::channel::<DhtEvent>(10000);

let service = builder
.with_network_protocol(|config| Ok(PolkadotProtocol::new(config.custom.collating_for.clone())))?
.with_finality_proof_provider(|client, backend|
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _)
)?
.with_dht_event_tx(dht_event_tx)?
.build()?;

let (block_import, link_half, babe_link) = import_setup.take()
Expand Down Expand Up @@ -258,6 +269,13 @@ pub fn new_full(config: Configuration<CustomConfiguration, GenesisConfig>)
let babe = start_babe(babe_config)?;
let select = babe.select(service.on_exit()).then(|_| Ok(()));
service.spawn_essential_task(Box::new(select));

let authority_discovery = authority_discovery::AuthorityDiscovery::new(
service.client(),
service.network(),
dht_event_rx,
);
service.spawn_task(authority_discovery);
} else {
network_gossip::register_non_authority_validator(service.network());
}
Expand Down