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

Commit

Permalink
*: Refactor authority discovery (key mngmt, runtime API) (#3955)
Browse files Browse the repository at this point in the history
* {core,srml}/authority-discovery: Move generic to specific session keys

* {srml,core}/authority-discovery: Verify signature outside of runtime

Given that the `core/authority-discovery` uses concrete authority
identifiers and signatures, one can verify a signature with the
authority discovery within `core`. Given the above, the `verify` runtime
api is obsolete and thus removed.

* *: Add authority discovery to the set of session keys

* *: Sign authority discovery DHT payload with keystore instead of runtime

Instead of calling a runtime function to sign a dht payload, which then
invokes the keystore, pass the keystore to the authority discovery
module and use it directly.

* core/authority-discovery: Give libp2p Kademlia time to start up

* core/authority-discovery: Move authorities priority group name to const

* node/runtime/src/lib.rs: Bump runtime spec version

* *: Fix lints and node/testing test failures

* *: Fix formatting

* core/authority-discovery: Box dht event channel in unit tests

* node/cli/src/service.rs: Fix future import

* node/cli/src/service.rs: Replace unwrap by expect with proof

* node/cli/src/chain_spec: Remove TODO for testnet key generation

* core/authority-discovery/src/lib: Remove scale encoding TODOs

* srml/authority-discovery: Make comment a doc comment

* core/authority-discovery: Remove unused StreamExt import

* node/runtime: Bump impl version to debug CI

* Test ci.

* Change the line width to 100.

* Revert "Change the line width to 100."

This reverts commit edff1f8.

* Fix a check for polkadot to work on forked repos.

* Revert "node/runtime: Bump impl version to debug CI"

This reverts commit 1a90903.

* Revert "Test ci."

This reverts commit a2c9df5.

* Cargo.lock: Fix wrong lock file merge

* srml/authority-discovery: Keep track of new validator set not upcoming

* core/authority-discovery: Document key retrieval functions
  • Loading branch information
mxinden authored and bkchr committed Nov 14, 2019
1 parent 5025e6a commit 6ae3b6c
Show file tree
Hide file tree
Showing 17 changed files with 326 additions and 302 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion core/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ client = { package = "substrate-client", path = "../../core/client" }
codec = { package = "parity-scale-codec", default-features = false, version = "1.0.3" }
derive_more = "0.15.0"
futures-preview = "0.3.0-alpha.19"
futures-timer = "0.4"
keystore = { package = "substrate-keystore", path = "../keystore" }
libp2p = { version = "0.13.0", default-features = false, features = ["secp256k1", "libp2p-websocket"] }
log = "0.4.8"
network = { package = "substrate-network", path = "../../core/network" }
primitives = { package = "substrate-primitives", path = "../primitives" }
prost = "0.5.0"
serde_json = "1.0.41"
sr-primitives = { path = "../../core/sr-primitives" }
futures-timer = "0.4"

[dev-dependencies]
parking_lot = "0.9.0"
Expand Down
4 changes: 3 additions & 1 deletion core/authority-discovery/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ description = "Authority discovery primitives"
edition = "2018"

[dependencies]
app-crypto = { package = "substrate-application-crypto", path = "../../application-crypto", default-features = false }
codec = { package = "parity-scale-codec", default-features = false, version = "1.0.3" }
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }
sr-api = { path = "../../sr-api", default-features = false }
sr-primitives = { path = "../../sr-primitives", default-features = false }
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }

[features]
default = ["std"]
std = [
"app-crypto/std",
"rstd/std",
"sr-api/std",
"codec/std",
Expand Down
33 changes: 15 additions & 18 deletions core/authority-discovery/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,28 @@
#![cfg_attr(not(feature = "std"), no_std)]

use rstd::vec::Vec;
use sr_primitives::RuntimeDebug;

#[derive(codec::Encode, codec::Decode, Eq, PartialEq, Clone, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Hash))]
pub struct Signature(pub Vec<u8>);
#[derive(codec::Encode, codec::Decode, Eq, PartialEq, Clone, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Hash))]
pub struct AuthorityId(pub Vec<u8>);
mod app {
use app_crypto::{app_crypto, key_types::AUTHORITY_DISCOVERY, sr25519};
app_crypto!(sr25519, AUTHORITY_DISCOVERY);
}

/// An authority discovery authority keypair.
#[cfg(feature = "std")]
pub type AuthorityPair = app::Pair;

/// An authority discovery authority identifier.
pub type AuthorityId = app::Public;

/// An authority discovery authority signature.
pub type AuthoritySignature = app::Signature;

sr_api::decl_runtime_apis! {
/// The authority discovery api.
///
/// This api is used by the `core/authority-discovery` module to retrieve our
/// own authority identifier, to retrieve identifiers of the current authority
/// set, as well as sign and verify Kademlia Dht external address payloads
/// from and to other authorities.
/// This api is used by the `core/authority-discovery` module to retrieve identifiers of the current authority set.
pub trait AuthorityDiscoveryApi {
/// Retrieve authority identifiers of the current authority set.
fn authorities() -> Vec<AuthorityId>;

/// Sign the given payload with the private key corresponding to the given authority id.
fn sign(payload: &Vec<u8>) -> Option<(Signature, AuthorityId)>;

/// Verify the given signature for the given payload with the given
/// authority identifier.
fn verify(payload: &Vec<u8>, signature: &Signature, authority_id: &AuthorityId) -> bool;
}
}
12 changes: 6 additions & 6 deletions core/authority-discovery/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ pub enum Error {
HashingAuthorityId(libp2p::core::multiaddr::multihash::EncodeError),
/// Failed calling into the Substrate runtime.
CallingRuntime(client::error::Error),
/// Failed signing the dht payload via the Substrate runtime.
SigningDhtPayload,
/// From the Dht we only get the hashed authority id. In order to retrieve the actual authority id and to ensure it
/// is actually an authority, we match the hash against the hash of the authority id of all other authorities. This
/// error is the result of the above failing.
MatchingHashedAuthorityIdWithAuthorityId,
/// Failed to set the authority discovery peerset priority group in the peerset module.
SettingPeersetPriorityGroup(String),
/// Failed to encode a dht payload.
Encoding(prost::EncodeError),
/// Failed to decode a dht payload.
Decoding(prost::DecodeError),
/// Failed to encode a protobuf payload.
EncodingProto(prost::EncodeError),
/// Failed to decode a protobuf payload.
DecodingProto(prost::DecodeError),
/// Failed to encode or decode scale payload
EncodingDecodingScale(codec::Error),
/// Failed to parse a libp2p multi address.
ParsingMultiaddress(libp2p::core::multiaddr::Error),
}
Loading

0 comments on commit 6ae3b6c

Please sign in to comment.