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

Commit 6ae3b6c

Browse files
mxindenbkchr
authored andcommitted
*: Refactor authority discovery (key mngmt, runtime API) (#3955)
* {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
1 parent 5025e6a commit 6ae3b6c

File tree

17 files changed

+326
-302
lines changed

17 files changed

+326
-302
lines changed

Cargo.lock

+8-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/authority-discovery/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ client = { package = "substrate-client", path = "../../core/client" }
1515
codec = { package = "parity-scale-codec", default-features = false, version = "1.0.3" }
1616
derive_more = "0.15.0"
1717
futures-preview = "0.3.0-alpha.19"
18+
futures-timer = "0.4"
19+
keystore = { package = "substrate-keystore", path = "../keystore" }
1820
libp2p = { version = "0.13.0", default-features = false, features = ["secp256k1", "libp2p-websocket"] }
1921
log = "0.4.8"
2022
network = { package = "substrate-network", path = "../../core/network" }
2123
primitives = { package = "substrate-primitives", path = "../primitives" }
2224
prost = "0.5.0"
2325
serde_json = "1.0.41"
2426
sr-primitives = { path = "../../core/sr-primitives" }
25-
futures-timer = "0.4"
2627

2728
[dev-dependencies]
2829
parking_lot = "0.9.0"

core/authority-discovery/primitives/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ description = "Authority discovery primitives"
66
edition = "2018"
77

88
[dependencies]
9+
app-crypto = { package = "substrate-application-crypto", path = "../../application-crypto", default-features = false }
910
codec = { package = "parity-scale-codec", default-features = false, version = "1.0.3" }
11+
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }
1012
sr-api = { path = "../../sr-api", default-features = false }
1113
sr-primitives = { path = "../../sr-primitives", default-features = false }
12-
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }
1314

1415
[features]
1516
default = ["std"]
1617
std = [
18+
"app-crypto/std",
1719
"rstd/std",
1820
"sr-api/std",
1921
"codec/std",

core/authority-discovery/primitives/src/lib.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,28 @@
1919
#![cfg_attr(not(feature = "std"), no_std)]
2020

2121
use rstd::vec::Vec;
22-
use sr_primitives::RuntimeDebug;
2322

24-
#[derive(codec::Encode, codec::Decode, Eq, PartialEq, Clone, RuntimeDebug)]
25-
#[cfg_attr(feature = "std", derive(Hash))]
26-
pub struct Signature(pub Vec<u8>);
27-
#[derive(codec::Encode, codec::Decode, Eq, PartialEq, Clone, RuntimeDebug)]
28-
#[cfg_attr(feature = "std", derive(Hash))]
29-
pub struct AuthorityId(pub Vec<u8>);
23+
mod app {
24+
use app_crypto::{app_crypto, key_types::AUTHORITY_DISCOVERY, sr25519};
25+
app_crypto!(sr25519, AUTHORITY_DISCOVERY);
26+
}
27+
28+
/// An authority discovery authority keypair.
29+
#[cfg(feature = "std")]
30+
pub type AuthorityPair = app::Pair;
31+
32+
/// An authority discovery authority identifier.
33+
pub type AuthorityId = app::Public;
34+
35+
/// An authority discovery authority signature.
36+
pub type AuthoritySignature = app::Signature;
3037

3138
sr_api::decl_runtime_apis! {
3239
/// The authority discovery api.
3340
///
34-
/// This api is used by the `core/authority-discovery` module to retrieve our
35-
/// own authority identifier, to retrieve identifiers of the current authority
36-
/// set, as well as sign and verify Kademlia Dht external address payloads
37-
/// from and to other authorities.
41+
/// This api is used by the `core/authority-discovery` module to retrieve identifiers of the current authority set.
3842
pub trait AuthorityDiscoveryApi {
3943
/// Retrieve authority identifiers of the current authority set.
4044
fn authorities() -> Vec<AuthorityId>;
41-
42-
/// Sign the given payload with the private key corresponding to the given authority id.
43-
fn sign(payload: &Vec<u8>) -> Option<(Signature, AuthorityId)>;
44-
45-
/// Verify the given signature for the given payload with the given
46-
/// authority identifier.
47-
fn verify(payload: &Vec<u8>, signature: &Signature, authority_id: &AuthorityId) -> bool;
4845
}
4946
}

core/authority-discovery/src/error.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ pub enum Error {
2828
HashingAuthorityId(libp2p::core::multiaddr::multihash::EncodeError),
2929
/// Failed calling into the Substrate runtime.
3030
CallingRuntime(client::error::Error),
31-
/// Failed signing the dht payload via the Substrate runtime.
32-
SigningDhtPayload,
3331
/// From the Dht we only get the hashed authority id. In order to retrieve the actual authority id and to ensure it
3432
/// is actually an authority, we match the hash against the hash of the authority id of all other authorities. This
3533
/// error is the result of the above failing.
3634
MatchingHashedAuthorityIdWithAuthorityId,
3735
/// Failed to set the authority discovery peerset priority group in the peerset module.
3836
SettingPeersetPriorityGroup(String),
39-
/// Failed to encode a dht payload.
40-
Encoding(prost::EncodeError),
41-
/// Failed to decode a dht payload.
42-
Decoding(prost::DecodeError),
37+
/// Failed to encode a protobuf payload.
38+
EncodingProto(prost::EncodeError),
39+
/// Failed to decode a protobuf payload.
40+
DecodingProto(prost::DecodeError),
41+
/// Failed to encode or decode scale payload
42+
EncodingDecodingScale(codec::Error),
4343
/// Failed to parse a libp2p multi address.
4444
ParsingMultiaddress(libp2p::core::multiaddr::Error),
4545
}

0 commit comments

Comments
 (0)