srml/authority-discovery: Introduce srml module to sign and verify (#3385)

In order to have authorities (validators) discover each other, they need
to publish their public addresses by their ip address on the Kademlia
Dht indexed by their public key. This payload needs to be signed by a
key identifying them as a valid authority.

Code inside `/core` does not know the current set of authorities nor
can it assume what kind of cryptography primitives are currently in use.
Instead it can retrieve its public key and the current set of
authorities from the runtime and have it sign and verify Dht payloads.

This commit enables code in `/core` to do so by introducing a srml
module and runtime api to:

1. Retrieve own public key.

2. Retrieve public keys of current authority set.

3. Sign a Dht payload.

4. Verify a Dht payload.

This commit makes the logic from the previous commit
(`core/consensus/common/primitives.ConsensusApi`)
cf80af9255 obsolete and thus removes it.
This commit is contained in:
Max Inden
2019-08-20 17:39:14 +02:00
committed by GitHub
parent 5ff10aece8
commit 2c0e73b78c
15 changed files with 509 additions and 69 deletions
@@ -1,5 +1,5 @@
[package]
name = "substrate-consensus-common-primitives"
name = "substrate-authority-discovery-primitives"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Common consensus primitives"
@@ -7,9 +7,9 @@ edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", default-features = false, version = "1.0.3" }
client = { package = "substrate-client", path = "../../../client", default-features = false }
sr-primitives = { path = "../../../sr-primitives", default-features = false }
rstd = { package = "sr-std", path = "../../../sr-std", default-features = false }
client = { package = "substrate-client", path = "../../client", default-features = false }
sr-primitives = { path = "../../sr-primitives", default-features = false }
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }
[features]
default = ["std"]
@@ -0,0 +1,49 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Runtime Api to help discover authorities.
#![cfg_attr(not(feature = "std"), no_std)]
use client::decl_runtime_apis;
use codec::Codec;
use rstd::vec::Vec;
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.
pub trait AuthorityDiscoveryApi<AuthorityId: Codec> {
/// Returns own authority identifier iff it is part of the current authority
/// set, otherwise this function returns None. The restriction might be
/// softened in the future in case a consumer needs to learn own authority
/// identifier.
fn authority_id() -> Option<AuthorityId>;
/// 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>, authority_id: AuthorityId) -> Option<Vec<u8>>;
/// Verify the given signature for the given payload with the given
/// authority identifier.
fn verify(payload: Vec<u8>, signature: Vec<u8>, authority_id: AuthorityId) -> bool;
}
}
@@ -1,31 +0,0 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Common consensus primitives.
#![cfg_attr(not(feature = "std"), no_std)]
use codec::Codec;
use client::decl_runtime_apis;
use rstd::vec::Vec;
decl_runtime_apis! {
/// Common consensus runtime api.
pub trait ConsensusApi<AuthorityId: Codec> {
/// Returns the set of authorities of the currently active consensus mechanism.
fn authorities() -> Vec<AuthorityId>;
}
}
@@ -89,6 +89,11 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> TestExternalities<H, N> {
self.offchain = Some(Box::new(offchain));
}
/// Set keystore.
pub fn set_keystore(&mut self, keystore: BareCryptoStorePtr) {
self.keystore = Some(keystore);
}
/// Get mutable reference to changes trie storage.
pub fn changes_trie_storage(&mut self) -> &mut ChangesTrieInMemoryStorage<H, N> {
&mut self.changes_trie_storage