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
+22 -8
View File
@@ -80,8 +80,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to equal spec_version. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 147,
impl_version: 150,
spec_version: 148,
impl_version: 148,
apis: RUNTIME_API_VERSIONS,
};
@@ -407,6 +407,8 @@ impl offences::Trait for Runtime {
type OnOffenceHandler = Staking;
}
impl authority_discovery::Trait for Runtime {}
impl grandpa::Trait for Runtime {
type Event = Event;
}
@@ -447,6 +449,7 @@ construct_runtime!(
Contracts: contracts,
Sudo: sudo,
ImOnline: im_online::{Module, Call, Storage, Event, ValidateUnsigned, Config},
AuthorityDiscovery: authority_discovery::{Module, Call, Config},
Offences: offences::{Module, Call, Storage, Event},
}
);
@@ -576,15 +579,26 @@ impl_runtime_apis! {
}
}
impl node_primitives::AccountNonceApi<Block> for Runtime {
fn account_nonce(account: AccountId) -> Index {
System::account_nonce(account)
impl authority_discovery_primitives::AuthorityDiscoveryApi<Block, im_online::AuthorityId> for Runtime {
fn authority_id() -> Option<im_online::AuthorityId> {
AuthorityDiscovery::authority_id()
}
fn authorities() -> Vec<im_online::AuthorityId> {
AuthorityDiscovery::authorities()
}
fn sign(payload: Vec<u8>, authority_id: im_online::AuthorityId) -> Option<Vec<u8>> {
AuthorityDiscovery::sign(payload, authority_id)
}
fn verify(payload: Vec<u8>, signature: Vec<u8>, public_key: im_online::AuthorityId) -> bool {
AuthorityDiscovery::verify(payload, signature, public_key)
}
}
impl consensus_primitives::ConsensusApi<Block, babe_primitives::AuthorityId> for Runtime {
fn authorities() -> Vec<babe_primitives::AuthorityId> {
Babe::authorities().into_iter().map(|(a, _)| a).collect()
impl node_primitives::AccountNonceApi<Block> for Runtime {
fn account_nonce(account: AccountId) -> Index {
System::account_nonce(account)
}
}