Generalize the Consensus Infrastructure (#883)

* Split out Consensus
* Supply ImportQueue through network-service
  - simplify ImportQueue.import_blocks
  - remove Deadlock on import_block
  - Adding Verifier-Trait
  - Implement import_queue provisioning in service; allow cli to import
* Allow to actually customize import queue
* Consensus Gossip: Cache Message hash per Topic
This commit is contained in:
Benjamin Kampmann
2018-10-16 13:40:33 +02:00
committed by GitHub
parent a24e61cb29
commit ac4bcf879f
61 changed files with 1937 additions and 3306 deletions
-1
View File
@@ -15,7 +15,6 @@ parking_lot = "0.4"
rhododendron = "0.3"
sr-primitives = { path = "../../core/sr-primitives" }
srml-system = { path = "../../srml/system" }
substrate-bft = { path = "../../core/bft" }
substrate-client = { path = "../../core/client" }
substrate-primitives = { path = "../../core/primitives" }
substrate-transaction-pool = { path = "../../core/transaction-pool" }
+2 -1
View File
@@ -16,6 +16,8 @@
//! This service uses BFT consensus provided by the substrate.
#![cfg(feature="rhd")]
extern crate node_runtime;
extern crate node_primitives;
@@ -68,7 +70,6 @@ pub use service::Service;
mod evaluation;
mod error;
mod offline_tracker;
mod service;
/// Shared offline validator tracker.
@@ -1,137 +0,0 @@
// Copyright 2018 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/>.
//! Tracks offline validators.
use node_primitives::AccountId;
use std::collections::HashMap;
use std::time::{Instant, Duration};
// time before we report a validator.
const REPORT_TIME: Duration = Duration::from_secs(60 * 5);
struct Observed {
last_round_end: Instant,
offline_since: Instant,
}
impl Observed {
fn new() -> Observed {
let now = Instant::now();
Observed {
last_round_end: now,
offline_since: now,
}
}
fn note_round_end(&mut self, was_online: bool) {
let now = Instant::now();
self.last_round_end = now;
if was_online {
self.offline_since = now;
}
}
fn is_active(&self) -> bool {
// can happen if clocks are not monotonic
if self.offline_since > self.last_round_end { return true }
self.last_round_end.duration_since(self.offline_since) < REPORT_TIME
}
}
/// Tracks offline validators and can issue a report for those offline.
pub struct OfflineTracker {
observed: HashMap<AccountId, Observed>,
}
impl OfflineTracker {
/// Create a new tracker.
pub fn new() -> Self {
OfflineTracker { observed: HashMap::new() }
}
/// Note new consensus is starting with the given set of validators.
pub fn note_new_block(&mut self, validators: &[AccountId]) {
use std::collections::HashSet;
let set: HashSet<_> = validators.iter().cloned().collect();
self.observed.retain(|k, _| set.contains(k));
}
/// Note that a round has ended.
pub fn note_round_end(&mut self, validator: AccountId, was_online: bool) {
self.observed.entry(validator)
.or_insert_with(Observed::new)
.note_round_end(was_online);
}
/// Generate a vector of indices for offline account IDs.
pub fn reports(&self, validators: &[AccountId]) -> Vec<u32> {
validators.iter()
.enumerate()
.filter_map(|(i, v)| if self.is_online(v) {
None
} else {
Some(i as u32)
})
.collect()
}
/// Whether reports on a validator set are consistent with our view of things.
pub fn check_consistency(&self, validators: &[AccountId], reports: &[u32]) -> bool {
reports.iter().cloned().all(|r| {
let v = match validators.get(r as usize) {
Some(v) => v,
None => return false,
};
// we must think all validators reported externally are offline.
let thinks_online = self.is_online(v);
!thinks_online
})
}
fn is_online(&self, v: &AccountId) -> bool {
self.observed.get(v).map(Observed::is_active).unwrap_or(true)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validator_offline() {
let mut tracker = OfflineTracker::new();
let v = [0; 32].into();
let v2 = [1; 32].into();
let v3 = [2; 32].into();
tracker.note_round_end(v, true);
tracker.note_round_end(v2, true);
tracker.note_round_end(v3, true);
let slash_time = REPORT_TIME + Duration::from_secs(5);
tracker.observed.get_mut(&v).unwrap().offline_since -= slash_time;
tracker.observed.get_mut(&v2).unwrap().offline_since -= slash_time;
assert_eq!(tracker.reports(&[v, v2, v3]), vec![0, 1]);
tracker.note_new_block(&[v, v3]);
assert_eq!(tracker.reports(&[v, v2, v3]), vec![0]);
}
}
+5 -5
View File
@@ -22,7 +22,7 @@ use std::thread;
use std::time::{Duration, Instant};
use std::sync::Arc;
use bft::{self, BftService};
use rhd::{self, BftService};
use client::{BlockchainEvents, ChainHead, BlockBody};
use ed25519;
use futures::prelude::*;
@@ -47,11 +47,11 @@ fn start_bft<F, C, Block>(
header: <Block as BlockT>::Header,
bft_service: Arc<BftService<Block, F, C>>,
) where
F: bft::Environment<Block> + 'static,
C: bft::BlockImport<Block> + bft::Authorities<Block> + 'static,
F: rhd::Environment<Block> + 'static,
C: rhd::BlockImport<Block> + rhd::Authorities<Block> + 'static,
F::Error: ::std::fmt::Debug,
<F::Proposer as bft::Proposer<Block>>::Error: ::std::fmt::Display + Into<error::Error>,
<F as bft::Environment<Block>>::Error: ::std::fmt::Display,
<F::Proposer as rhd::Proposer<Block>>::Error: ::std::fmt::Display + Into<error::Error>,
<F as rhd::Environment<Block>>::Error: ::std::fmt::Display,
Block: BlockT,
{
let mut handle = LocalThreadHandle::current();
+1 -1
View File
@@ -7,7 +7,7 @@ description = "Substrate node networking protocol"
[dependencies]
node-consensus = { path = "../consensus" }
node-primitives = { path = "../primitives" }
substrate-bft = { path = "../../core/bft" }
substrate-consensus-rhd = { path = "../../core/consensus/rhd" }
substrate-network = { path = "../../core/network" }
substrate-primitives = { path = "../../core/primitives" }
futures = "0.1"
-13
View File
@@ -20,23 +20,10 @@
#![warn(unused_extern_crates)]
extern crate substrate_bft as bft;
#[macro_use]
extern crate substrate_network;
extern crate substrate_primitives;
extern crate node_consensus;
extern crate node_primitives;
extern crate futures;
extern crate tokio;
extern crate rhododendron;
#[macro_use]
extern crate log;
pub mod consensus;
use node_primitives::{Block, Hash};
use substrate_network::consensus_gossip::ConsensusGossip;
+2
View File
@@ -239,6 +239,8 @@ pub type Address = balances::Address<Runtime>;
pub type Header = generic::Header<BlockNumber, BlakeTwo256, Log>;
/// Block type as expected by this runtime.
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// A Block signed with a Justification
pub type SignedBlock = generic::SignedBlock<Header, UncheckedExtrinsic>;
/// BlockId type as expected by this runtime.
pub type BlockId = generic::BlockId<Block>;
/// Unchecked extrinsic type as expected by this runtime.
File diff suppressed because it is too large Load Diff
-1
View File
@@ -28,7 +28,6 @@ tokio = "0.1.7"
[dev-dependencies]
substrate-service-test = { path = "../../core/service/test" }
substrate-bft = { path = "../../core/bft" }
substrate-test-client = { path = "../../core/test-client" }
substrate-keyring = { path = "../../core/keyring" }
rhododendron = "0.3"
+4 -3
View File
@@ -132,6 +132,7 @@ pub fn staging_testnet_config() -> ChainSpec<GenesisConfig> {
boot_nodes,
Some(STAGING_TELEMETRY_URL.into()),
None,
None,
)
}
@@ -227,7 +228,7 @@ fn development_config_genesis() -> GenesisConfig {
/// Development config (single validator Alice)
pub fn development_config() -> ChainSpec<GenesisConfig> {
ChainSpec::from_genesis("Development", "development", development_config_genesis, vec![], None, None)
ChainSpec::from_genesis("Development", "development", development_config_genesis, vec![], None, None, None)
}
fn local_testnet_genesis() -> GenesisConfig {
@@ -245,10 +246,10 @@ fn local_testnet_genesis_instant() -> GenesisConfig {
/// Local testnet config (multivalidator Alice + Bob)
pub fn local_testnet_config() -> ChainSpec<GenesisConfig> {
ChainSpec::from_genesis("Local Testnet", "local_testnet", local_testnet_genesis, vec![], None, None)
ChainSpec::from_genesis("Local Testnet", "local_testnet", local_testnet_genesis, vec![], None, None, None)
}
/// Local testnet config (multivalidator Alice + Bob)
pub fn integration_test_config() -> ChainSpec<GenesisConfig> {
ChainSpec::from_genesis("Integration Test", "test", local_testnet_genesis_instant, vec![], None, None)
ChainSpec::from_genesis("Integration Test", "test", local_testnet_genesis_instant, vec![], None, None, None)
}
+72 -86
View File
@@ -22,44 +22,33 @@ extern crate node_primitives;
extern crate node_runtime;
extern crate node_executor;
extern crate node_network;
extern crate node_consensus as consensus;
extern crate substrate_client as client;
extern crate substrate_network as network;
extern crate substrate_primitives as primitives;
extern crate substrate_service as service;
extern crate substrate_transaction_pool as transaction_pool;
extern crate parity_codec as codec;
extern crate tokio;
#[cfg(test)]
extern crate substrate_service_test as service_test;
#[macro_use]
extern crate log;
#[macro_use]
extern crate hex_literal;
#[cfg(test)]
extern crate parking_lot;
#[cfg(test)]
extern crate substrate_bft as bft;
#[cfg(test)]
extern crate substrate_test_client;
#[cfg(test)]
extern crate substrate_keyring as keyring;
#[cfg(test)]
#[cfg(all(test, feature="rhd"))]
extern crate rhododendron as rhd;
extern crate sr_primitives as runtime_primitives;
pub mod chain_spec;
use std::sync::Arc;
use codec::Decode;
use transaction_pool::txpool::{Pool as TransactionPool};
use node_primitives::{Block, Hash, Timestamp, BlockId};
use node_runtime::{GenesisConfig, BlockPeriod, StorageValue, Runtime};
use node_primitives::{Block, Hash};
use node_runtime::GenesisConfig;
use client::Client;
use consensus::AuthoringApi;
use node_network::{Protocol as DemoProtocol, consensus::ConsensusNetwork};
use node_network::Protocol as DemoProtocol;
use tokio::runtime::TaskExecutor;
use service::FactoryFullConfiguration;
use primitives::{Blake2Hasher, storage::StorageKey, twox_128};
use network::import_queue::{BasicQueue, BlockOrigin, ImportBlock, Verifier};
use runtime_primitives::{traits::Block as BlockT};
use primitives::{Blake2Hasher, AuthorityId};
pub use service::{Roles, PruningMode, TransactionPoolOptions, ServiceFactory,
ErrorKind, Error, ComponentBlock, LightComponents, FullComponents};
@@ -71,10 +60,33 @@ pub type ChainSpec = service::ChainSpec<GenesisConfig>;
pub type ComponentClient<C> = Client<<C as Components>::Backend, <C as Components>::Executor, Block>;
pub type NetworkService = network::Service<Block, <Factory as service::ServiceFactory>::NetworkProtocol, Hash>;
/// A verifier that doesn't actually do any checks
pub struct NoneVerifier;
/// This Verifiyer accepts all data as valid
impl<B: BlockT> Verifier<B> for NoneVerifier {
fn verify(
&self,
origin: BlockOrigin,
header: B::Header,
justification: Vec<u8>,
body: Option<Vec<B::Extrinsic>>
) -> Result<(ImportBlock<B>, Option<Vec<AuthorityId>>), String> {
Ok((ImportBlock {
origin,
header,
body,
finalized: true,
external_justification: justification,
internal_justification: vec![],
auxiliary: Vec::new(),
}, None))
}
}
/// A collection of type to generalise specific components over full / light client.
pub trait Components: service::Components {
/// Demo API.
type Api: 'static + AuthoringApi + Send + Sync;
type Api: 'static + Send + Sync;
/// Client backend.
type Backend: 'static + client::backend::Backend<Block, Blake2Hasher>;
/// Client executor.
@@ -114,6 +126,8 @@ impl service::ServiceFactory for Factory {
type Configuration = CustomConfiguration;
type FullService = Service<service::FullComponents<Self>>;
type LightService = Service<service::LightComponents<Self>>;
/// instance of import queue for clients
type ImportQueue = BasicQueue<Block, NoneVerifier>;
fn build_full_transaction_pool(config: TransactionPoolOptions, client: Arc<service::FullClient<Self>>)
-> Result<TransactionPool<Self::FullTransactionPoolApi>, Error>
@@ -133,6 +147,20 @@ impl service::ServiceFactory for Factory {
Ok(DemoProtocol::new())
}
fn build_full_import_queue(
_config: &FactoryFullConfiguration<Self>,
_client: Arc<service::FullClient<Self>>,
) -> Result<BasicQueue<Block, NoneVerifier>, service::Error> {
Ok(BasicQueue::new(Arc::new(NoneVerifier {})))
}
fn build_light_import_queue(
_config: &FactoryFullConfiguration<Self>,
_client: Arc<service::LightClient<Self>>,
) -> Result<BasicQueue<Block, NoneVerifier>, service::Error> {
Ok(BasicQueue::new(Arc::new(NoneVerifier {})))
}
fn new_light(config: Configuration, executor: TaskExecutor)
-> Result<Service<LightComponents<Factory>>, Error>
{
@@ -146,85 +174,39 @@ impl service::ServiceFactory for Factory {
fn new_full(config: Configuration, executor: TaskExecutor)
-> Result<Service<FullComponents<Factory>>, Error>
{
let is_validator = (config.roles & Roles::AUTHORITY) == Roles::AUTHORITY;
let service = service::Service::<FullComponents<Factory>>::new(config, executor.clone())?;
// Spin consensus service if configured
let consensus = if is_validator {
// Load the first available key
let key = service.keystore().load(&service.keystore().contents()?[0], "")?;
info!("Using authority key {}", key.public());
let client = service.client();
let consensus_net = ConsensusNetwork::new(service.network(), client.clone());
let block_id = BlockId::number(client.info().unwrap().chain.best_number);
// TODO: this needs to be dynamically adjustable
let block_delay = client.storage(&block_id, &StorageKey(twox_128(BlockPeriod::<Runtime>::key()).to_vec()))?
.and_then(|data| Timestamp::decode(&mut data.0.as_slice()))
.unwrap_or_else(|| {
warn!("Block period is missing in the storage.");
5
});
Some(consensus::Service::new(
client.clone(),
client.clone(),
consensus_net,
service.transaction_pool(),
executor,
key,
block_delay,
))
} else {
None
};
// FIXME: Spin consensus service if configured
let consensus = None;
Ok(Service {
inner: service,
_consensus: consensus,
})
}
}
/// Demo service.
pub struct Service<C: Components> {
inner: service::Service<C>,
_consensus: Option<consensus::Service>,
}
/// Creates bare client without any networking.
pub fn new_client(config: Configuration)
-> Result<Arc<service::ComponentClient<FullComponents<Factory>>>, Error>
{
service::new_client::<Factory>(config)
_consensus: Option<bool>, // FIXME: add actual consensus engine
}
impl<C: Components> ::std::ops::Deref for Service<C> {
type Target = service::Service<C>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
/// Creates bare client without any networking.
pub fn new_client(config: Configuration)
-> Result<Arc<service::ComponentClient<FullComponents<Factory>>>, Error>
{
service::new_client::<Factory>(&config)
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use parking_lot::RwLock;
use {service, service_test, Factory, chain_spec};
use consensus::{self, OfflineTracker};
use primitives::ed25519;
use runtime_primitives::traits::BlockNumberToHash;
use runtime_primitives::generic::Era;
use node_primitives::Block;
use bft::{Proposer, Environment};
use node_network::consensus::ConsensusNetwork;
use substrate_test_client::fake_justify;
use node_primitives::BlockId;
use keyring::Keyring;
use node_runtime::{UncheckedExtrinsic, Call, BalancesCall};
use node_primitives::UncheckedExtrinsic as OpaqueExtrinsic;
use codec::{Decode, Encode};
use node_runtime::RawAddress;
use {service_test, Factory, chain_spec};
#[test]
fn test_connectivity() {
@@ -232,7 +214,10 @@ mod tests {
}
#[test]
#[cfg(feature = "rhd")]
fn test_sync() {
use client::{ImportBlock, BlockOrigin};
let alice: Arc<ed25519::Pair> = Arc::new(Keyring::Alice.into());
let bob: Arc<ed25519::Pair> = Arc::new(Keyring::Bob.into());
let validators = vec![alice.public().0.into(), bob.public().0.into()];
@@ -253,9 +238,15 @@ mod tests {
};
let (proposer, _, _) = proposer_factory.init(&parent_header, &validators, alice.clone()).unwrap();
let block = proposer.propose().expect("Error making test block");
let justification = fake_justify::<Block>(&block.header, &keys);
let justification = service.client().check_justification(block.header, justification).unwrap();
(justification, Some(block.extrinsics))
ImportBlock {
origin: BlockOrigin::File,
external_justification: Vec::new(),
internal_justification: Vec::new(),
finalized: true,
body: Some(block.extrinsics),
header: block.header,
auxiliary: Vec::new(),
}
};
let extrinsic_factory = |service: &<Factory as service::ServiceFactory>::FullService| {
let payload = (0, Call::Balances(BalancesCall::transfer(RawAddress::Id(bob.public().0.into()), 69)), Era::immortal(), service.client().genesis_hash());
@@ -271,9 +262,4 @@ mod tests {
service_test::sync::<Factory, _, _>(chain_spec::integration_test_config(), block_factory, extrinsic_factory);
}
#[test]
fn test_consensus() {
service_test::consensus::<Factory>(chain_spec::integration_test_config(), vec!["Alice".into(), "Bob".into()]);
}
}