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
@@ -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()]);
}
}