From cf80af9255eae8ed4538df04d555702073d6acfe Mon Sep 17 00:00:00 2001 From: Max Inden Date: Mon, 29 Jul 2019 14:39:47 +0200 Subject: [PATCH] core/consensus,node/runtime: Declare and implement authorities endpoint (#3207) The goal of the commit is to be able to retrieve the current set of authorities without needing to know the concrete consensus mechanism in place. In order to achieve the above this commit introduces the `core/consensus/common/primitives` crate, declaring the `ConsensusApi` runtime API. In addition it implements the above mentioned trait definition in `node/runtime` by returning the current authorities of the BABE consensus mechanism. --- substrate/Cargo.lock | 11 +++++++ .../consensus/common/primitives/Cargo.toml | 21 +++++++++++++ .../consensus/common/primitives/src/lib.rs | 31 +++++++++++++++++++ substrate/node/runtime/Cargo.toml | 2 ++ substrate/node/runtime/src/lib.rs | 6 ++++ 5 files changed, 71 insertions(+) create mode 100644 substrate/core/consensus/common/primitives/Cargo.toml create mode 100644 substrate/core/consensus/common/primitives/src/lib.rs diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index e1d9a49793..7e77f3003b 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -2365,6 +2365,7 @@ dependencies = [ "srml-treasury 2.0.0", "substrate-client 2.0.0", "substrate-consensus-babe-primitives 2.0.0", + "substrate-consensus-common-primitives 2.0.0", "substrate-keyring 2.0.0", "substrate-offchain-primitives 2.0.0", "substrate-primitives 2.0.0", @@ -4385,6 +4386,16 @@ dependencies = [ "substrate-test-runtime-client 2.0.0", ] +[[package]] +name = "substrate-consensus-common-primitives" +version = "2.0.0" +dependencies = [ + "parity-codec 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "sr-primitives 2.0.0", + "sr-std 2.0.0", + "substrate-client 2.0.0", +] + [[package]] name = "substrate-consensus-rhd" version = "2.0.0" diff --git a/substrate/core/consensus/common/primitives/Cargo.toml b/substrate/core/consensus/common/primitives/Cargo.toml new file mode 100644 index 0000000000..154863eed5 --- /dev/null +++ b/substrate/core/consensus/common/primitives/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "substrate-consensus-common-primitives" +version = "2.0.0" +authors = ["Parity Technologies "] +description = "Common consensus primitives" +edition = "2018" + +[dependencies] +parity-codec = { version = "4.1.1", default-features = false } +client = { package = "substrate-client", path = "../../../client", default-features = false } +sr-primitives = { package = "sr-primitives", path = "../../../sr-primitives", default-features = false } +rstd = { package = "sr-std", path = "../../../sr-std", default-features = false } + +[features] +default = ["std"] +std = [ + "rstd/std", + "client/std", + "parity-codec/std", + "sr-primitives/std" +] diff --git a/substrate/core/consensus/common/primitives/src/lib.rs b/substrate/core/consensus/common/primitives/src/lib.rs new file mode 100644 index 0000000000..47c4371721 --- /dev/null +++ b/substrate/core/consensus/common/primitives/src/lib.rs @@ -0,0 +1,31 @@ +// 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 . + +//! Common consensus primitives. + +#![cfg_attr(not(feature = "std"), no_std)] + +use parity_codec::Codec; +use client::decl_runtime_apis; +use rstd::vec::Vec; + +decl_runtime_apis! { + /// Common consensus runtime api. + pub trait ConsensusApi { + /// Returns the set of authorities of the currently active consensus mechanism. + fn authorities() -> Vec; + } +} diff --git a/substrate/node/runtime/Cargo.toml b/substrate/node/runtime/Cargo.toml index eca7c9ebb7..9c52125fe8 100644 --- a/substrate/node/runtime/Cargo.toml +++ b/substrate/node/runtime/Cargo.toml @@ -19,6 +19,7 @@ support = { package = "srml-support", path = "../../srml/support", default-featu authorship = { package = "srml-authorship", path = "../../srml/authorship", default-features = false } babe = { package = "srml-babe", path = "../../srml/babe", default-features = false } babe-primitives = { package = "substrate-consensus-babe-primitives", path = "../../core/consensus/babe/primitives", default-features = false } +consensus-primitives = { package = "substrate-consensus-common-primitives", path = "../../core/consensus/common/primitives", default-features = false } balances = { package = "srml-balances", path = "../../srml/balances", default-features = false } contracts = { package = "srml-contracts", path = "../../srml/contracts", default-features = false } collective = { package = "srml-collective", path = "../../srml/collective", default-features = false } @@ -57,6 +58,7 @@ std = [ "authorship/std", "babe/std", "babe-primitives/std", + "consensus-primitives/std", "balances/std", "contracts/std", "collective/std", diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index e657feebe9..f89b121afa 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -538,4 +538,10 @@ impl_runtime_apis! { } } } + + impl consensus_primitives::ConsensusApi for Runtime { + fn authorities() -> Vec { + Babe::authorities().into_iter().map(|(a, _)| a).collect() + } + } }