Split the Roles in three types (#5520)

* Split the Roles bitfield in three

* Forgot to include some changes

* Fix cli test

* More test fixes

* Oh God, merging master broke other tests

* Didn't run the doctests

* Address review

* I'm trying to fix the build blindly because it's taking a good hour to compile on my machine

* Address some review

* Also update the peerset's API to make sense

* Fix peerset tests

* Fix browser node

* client: distinguish between local and network authority

Co-authored-by: André Silva <andre.beat@gmail.com>
This commit is contained in:
Pierre Krieger
2020-04-03 19:08:14 +02:00
committed by GitHub
parent 9dbcb11f66
commit 8c03a4fcef
44 changed files with 591 additions and 432 deletions
+15 -8
View File
@@ -35,7 +35,7 @@ use futures::{
};
use sc_keystore::{Store as Keystore};
use log::{info, warn, error};
use sc_network::config::{FinalityProofProvider, OnDemand, BoxFinalityProofRequestBuilder};
use sc_network::config::{Role, FinalityProofProvider, OnDemand, BoxFinalityProofRequestBuilder};
use sc_network::{NetworkService, NetworkStateInfo};
use parking_lot::{Mutex, RwLock};
use sp_runtime::generic::BlockId;
@@ -840,7 +840,7 @@ ServiceBuilder<
.register_transaction_pool(Arc::downgrade(&transaction_pool) as _);
let transaction_pool_adapter = Arc::new(TransactionPoolAdapter {
imports_external_transactions: !config.roles.is_light(),
imports_external_transactions: !matches!(config.role, Role::Light),
pool: transaction_pool.clone(),
client: client.clone(),
executor: tasks_builder.spawn_handle(),
@@ -863,7 +863,7 @@ ServiceBuilder<
Box::new(sp_consensus::block_validation::DefaultBlockAnnounceValidator::new(client.clone()));
let network_params = sc_network::config::Params {
roles: config.roles,
role: config.role.clone(),
executor: {
let spawn_handle = tasks_builder.spawn_handle();
Some(Box::new(move |fut| {
@@ -913,7 +913,7 @@ ServiceBuilder<
let offchain = offchain_workers.as_ref().map(Arc::downgrade);
let notifications_spawn_handle = tasks_builder.spawn_handle();
let network_state_info: Arc<dyn NetworkStateInfo + Send + Sync> = network.clone();
let is_validator = config.roles.is_authority();
let is_validator = config.role.is_authority();
let (import_stream, finality_stream) = (
client.import_notification_stream().map(|n| ChainEvent::NewBlock {
@@ -1003,9 +1003,16 @@ ServiceBuilder<
.const_label("version", config.impl_version)
.const_label("commit", config.impl_commit),
)?, &registry)?.set(1);
let role_bits = match config.role {
Role::Full => 1,
Role::Light => 2,
Role::Sentry { .. } => 3,
Role::Authority { .. } => 4,
};
register(Gauge::<U64>::new(
"node_roles", "The roles the node is running as",
)?, &registry)?.set(u64::from(config.roles.bits()));
"node_role", "The role the node is running as",
)?, &registry)?.set(role_bits);
let metrics = ServiceMetrics::register(&registry)?;
@@ -1198,7 +1205,7 @@ ServiceBuilder<
spawn_handle.spawn(
"network-worker",
build_network_future(
config.roles,
config.role.clone(),
network_mut,
client.clone(),
network_status_sinks.clone(),
@@ -1212,7 +1219,7 @@ ServiceBuilder<
// Telemetry
let telemetry = config.telemetry_endpoints.clone().map(|endpoints| {
let is_authority = config.roles.is_authority();
let is_authority = config.role.is_authority();
let network_id = network.local_peer_id().to_base58();
let name = config.name.clone();
let impl_name = config.impl_name.to_owned();
+6 -17
View File
@@ -18,7 +18,7 @@
pub use sc_client::ExecutionStrategies;
pub use sc_client_db::{kvdb::KeyValueDB, PruningMode};
pub use sc_network::config::{ExtTransport, NetworkConfiguration, Roles};
pub use sc_network::{Multiaddr, config::{MultiaddrWithPeerId, ExtTransport, NetworkConfiguration, Role}};
pub use sc_executor::WasmExecutionMethod;
use std::{future::Future, path::{PathBuf, Path}, pin::Pin, net::SocketAddr, sync::Arc};
@@ -58,8 +58,8 @@ pub struct Configuration {
pub impl_version: &'static str,
/// Git commit if any.
pub impl_commit: &'static str,
/// Node roles.
pub roles: Roles,
/// Node role.
pub role: Role,
/// How to spawn background tasks. Mandatory, otherwise creating a `Service` will error.
pub task_executor: Option<Arc<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send + Sync>>,
/// Extrinsic pool configuration.
@@ -105,10 +105,6 @@ pub struct Configuration {
pub default_heap_pages: Option<u64>,
/// Should offchain workers be executed.
pub offchain_worker: bool,
/// Sentry mode is enabled, the node's role is AUTHORITY but it should not
/// actively participate in consensus (i.e. no keystores should be passed to
/// consensus modules).
pub sentry_mode: bool,
/// Enable authoring even when offline.
pub force_authoring: bool,
/// Disable GRANDPA when running in validator mode
@@ -204,7 +200,7 @@ impl Default for Configuration {
chain_spec: None,
config_dir: None,
name: Default::default(),
roles: Roles::FULL,
role: Role::Full,
task_executor: None,
transaction_pool: Default::default(),
network: Default::default(),
@@ -224,7 +220,6 @@ impl Default for Configuration {
telemetry_external_transport: None,
default_heap_pages: None,
offchain_worker: Default::default(),
sentry_mode: false,
force_authoring: false,
disable_grandpa: false,
dev_key_seed: None,
@@ -286,15 +281,9 @@ impl Configuration {
self.database.as_ref().expect("database must be specified")
}
/// Returns a string displaying the node role, special casing the sentry mode
/// (returning `SENTRY`), since the node technically has an `AUTHORITY` role but
/// doesn't participate.
/// Returns a string displaying the node role.
pub fn display_role(&self) -> String {
if self.sentry_mode {
"SENTRY".to_string()
} else {
self.roles.to_string()
}
self.role.to_string()
}
/// Use in memory keystore config when it is not required at all.
+9 -12
View File
@@ -58,7 +58,7 @@ pub use self::builder::{
ServiceBuilder, ServiceBuilderCommand, TFullClient, TLightClient, TFullBackend, TLightBackend,
TFullCallExecutor, TLightCallExecutor,
};
pub use config::{Configuration, Roles, PruningMode, DatabaseConfig};
pub use config::{Configuration, Role, PruningMode, DatabaseConfig};
pub use sc_chain_spec::{
ChainSpec, GenericChainSpec, Properties, RuntimeGenesis, Extension as ChainSpecExtension
};
@@ -322,7 +322,7 @@ fn build_network_future<
C: sc_client::BlockchainEvents<B>,
H: sc_network::ExHashT
> (
roles: Roles,
role: Role,
mut network: sc_network::NetworkWorker<B, H>,
client: Arc<C>,
status_sinks: Arc<Mutex<status_sinks::StatusSinks<(NetworkStatus<B>, NetworkState)>>>,
@@ -399,17 +399,14 @@ fn build_network_future<
sc_rpc::system::Request::NodeRoles(sender) => {
use sc_rpc::system::NodeRole;
let node_roles = (0 .. 8)
.filter(|&bit_number| (roles.bits() >> bit_number) & 1 == 1)
.map(|bit_number| match Roles::from_bits(1 << bit_number) {
Some(Roles::AUTHORITY) => NodeRole::Authority,
Some(Roles::LIGHT) => NodeRole::LightClient,
Some(Roles::FULL) => NodeRole::Full,
_ => NodeRole::UnknownRole(bit_number),
})
.collect();
let node_role = match role {
Role::Authority { .. } => NodeRole::Authority,
Role::Light => NodeRole::LightClient,
Role::Full => NodeRole::Full,
Role::Sentry { .. } => NodeRole::Sentry,
};
let _ = sender.send(node_roles);
let _ = sender.send(vec![node_role]);
}
};
}