core/authority-discovery: Enable authorities to discover each other (#3452)

With the *authority-discovery* module an authoritative node makes itself
discoverable and is able to discover other authorities. Once discovered, a node
can directly connect to other authorities instead of multi-hop gossiping
information.

1. **Making itself discoverable**

    1. Retrieve its external addresses

    2. Adds its network peer id to the addresses

    3. Sign the above

    4. Put the signature and the addresses on the libp2p Kademlia DHT

2. **Discovering other authorities**

    1. Retrieve the current set of authorities

    2. Start DHT queries for the ids of the authorities

    3. Validate the signatures of the retrieved key value pairs

    4. Add the retrieved external addresses as ~reserved~ priority nodes to the
       peerset


* node/runtime: Add authority-discovery as session handler

The srml/authority-discovery module implements the OneSessionHandler in
order to keep its authority set in sync. This commit adds the module to
the set of session handlers.

* core/network: Make network worker return Dht events on poll

Instead of network worker implement the Future trait, have it implement
the Stream interface returning Dht events.

For now these events are ignored in build_network_future but will be
used by the core/authority-discovery module in subsequent commits.

* *: Add scaffolding and integration for core/authority-discovery module

* core/authority-discovery: Implement module logic itself
This commit is contained in:
Max Inden
2019-09-06 17:43:03 +02:00
committed by GitHub
parent ece0b57d8d
commit 027d88796b
19 changed files with 1041 additions and 62 deletions
+26 -12
View File
@@ -47,7 +47,9 @@ use elections::VoteIndex;
use version::NativeVersion;
use primitives::OpaqueMetadata;
use grandpa::{AuthorityId as GrandpaId, AuthorityWeight as GrandpaWeight};
use im_online::sr25519::{AuthorityId as ImOnlineId};
use im_online::sr25519::{AuthorityId as ImOnlineId, AuthoritySignature as ImOnlineSignature};
use authority_discovery_primitives::{AuthorityId as EncodedAuthorityId, Signature as EncodedSignature};
use codec::{Encode, Decode};
use system::offchain::TransactionSubmitter;
#[cfg(any(feature = "std", test))]
@@ -191,7 +193,7 @@ impl authorship::Trait for Runtime {
type EventHandler = Staking;
}
type SessionHandlers = (Grandpa, Babe, ImOnline);
type SessionHandlers = (Grandpa, Babe, ImOnline, AuthorityDiscovery);
impl_opaque_keys! {
pub struct SessionKeys {
@@ -617,20 +619,32 @@ impl_runtime_apis! {
}
}
impl authority_discovery_primitives::AuthorityDiscoveryApi<Block, ImOnlineId> for Runtime {
fn authority_id() -> Option<ImOnlineId> {
AuthorityDiscovery::authority_id()
}
fn authorities() -> Vec<ImOnlineId> {
AuthorityDiscovery::authorities()
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>, authority_id: ImOnlineId) -> Option<Vec<u8>> {
AuthorityDiscovery::sign(payload, authority_id)
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: Vec<u8>, public_key: ImOnlineId) -> bool {
AuthorityDiscovery::verify(payload, signature, public_key)
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)
}
}