Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A0-4289: Rewrite Gossip #1705

Merged
merged 8 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 28 additions & 47 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use std::{

use finality_aleph::{
run_validator_node, AlephBlockImport, AlephConfig, AllBlockMetrics, BlockImporter,
ChannelProvider, Justification, JustificationTranslator, MillisecsPerBlock,
NotificationServices, Protocol, ProtocolNaming, RateLimiterConfig, RedirectingBlockImport,
SessionPeriod, SubstrateChainStatus, SubstrateNetworkEventStream, SyncOracle,
TracingBlockImport, ValidatorAddressCache,
ChannelProvider, Justification, JustificationTranslator, MillisecsPerBlock, NetConfig,
RateLimiterConfig, RedirectingBlockImport, SessionPeriod, SubstrateChainStatus,
SyncNetworkService, SyncOracle, TracingBlockImport, ValidatorAddressCache,
};
use log::warn;
use primitives::{
Expand All @@ -21,7 +20,6 @@ use sc_client_api::{BlockBackend, HeaderBackend};
use sc_consensus::ImportQueue;
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
use sc_consensus_slots::BackoffAuthoringBlocksStrategy;
use sc_network::config::FullNetworkConfiguration;
use sc_service::{error::Error as ServiceError, Configuration, TFullClient, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sp_api::ProvideRuntimeApi;
Expand Down Expand Up @@ -222,43 +220,6 @@ fn get_validator_address_cache(aleph_config: &AlephCli) -> Option<ValidatorAddre
.then(ValidatorAddressCache::new)
}

fn get_net_config(
config: &Configuration,
client: &Arc<FullClient>,
) -> (
FullNetworkConfiguration,
ProtocolNaming,
NotificationServices,
) {
let genesis_hash = client
.block_hash(0)
.ok()
.flatten()
.expect("we should have a hash");
let chain_prefix = match config.chain_spec.fork_id() {
Some(fork_id) => format!("/{genesis_hash}/{fork_id}"),
None => format!("/{genesis_hash}"),
};
let protocol_naming = ProtocolNaming::new(chain_prefix);
let mut net_config = FullNetworkConfiguration::new(&config.network);

let (config, authentication_notification_service) =
finality_aleph::peers_set_config(protocol_naming.clone(), Protocol::Authentication);
net_config.add_notification_protocol(config);
let (config, sync_notification_service) =
finality_aleph::peers_set_config(protocol_naming.clone(), Protocol::BlockSync);
net_config.add_notification_protocol(config);

(
net_config,
protocol_naming,
NotificationServices {
authentication: authentication_notification_service,
sync: sync_notification_service,
},
)
}

fn get_proposer_factory(
service_components: &ServiceComponents,
config: &Configuration,
Expand All @@ -284,6 +245,18 @@ fn get_rate_limit_config(aleph_config: &AlephCli) -> RateLimiterConfig {
}
}

fn chain_prefix(config: &Configuration, client: &Arc<FullClient>) -> String {
let genesis_hash = client
.block_hash(0)
.ok()
.flatten()
.expect("we should have a hash");
match config.chain_spec.fork_id() {
Some(fork_id) => format!("/{genesis_hash}/{fork_id}"),
None => format!("/{genesis_hash}"),
}
}

/// Builds a new service for a full client.
pub fn new_authority(
config: Configuration,
Expand Down Expand Up @@ -336,8 +309,11 @@ pub fn new_authority(

let import_queue_handle = BlockImporter::new(service_components.import_queue.service());

let (net_config, protocol_naming, notifications) =
get_net_config(&config, &service_components.client);
let NetConfig {
net_config,
authentication_network,
block_sync_network,
} = NetConfig::new(&config, &chain_prefix(&config, &service_components.client));
let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_network) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: &config,
Expand Down Expand Up @@ -400,16 +376,21 @@ pub fn new_authority(

// Network event stream needs to be created before starting the network,
// otherwise some events might be missed.
let network_event_stream =
SubstrateNetworkEventStream::new(network, sync_network, protocol_naming, notifications);
let sync_network_service = SyncNetworkService::new(
network,
sync_network,
vec![authentication_network.name(), block_sync_network.name()],
);

let AlephRuntimeVars {
millisecs_per_block,
session_period,
} = get_aleph_runtime_vars(&service_components.client);

let aleph_config = AlephConfig {
network_event_stream,
authentication_network,
block_sync_network,
sync_network_service,
client: service_components.client,
chain_status,
import_queue_handle,
Expand Down
84 changes: 60 additions & 24 deletions finality-aleph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use sc_client_api::{
};
use sc_consensus::BlockImport;
use sc_keystore::LocalKeystore;
use sc_network::config::FullNetworkConfiguration;
use sc_service::Configuration;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{HeaderBackend, HeaderMetadata};
use sp_runtime::traits::{BlakeTwo256, Block};
Expand Down Expand Up @@ -74,8 +76,7 @@ pub use crate::{
metrics::{AllBlockMetrics, DefaultClock, FinalityRateMetrics, TimingBlockMetrics},
network::{
address_cache::{ValidatorAddressCache, ValidatorAddressingInfo},
NotificationServices, Protocol, ProtocolNaming, SubstrateNetworkEventStream,
SubstratePeerId,
ProtocolNetwork, SubstratePeerId, SyncNetworkService,
},
nodes::run_validator_node,
session::SessionPeriod,
Expand All @@ -85,29 +86,62 @@ pub use crate::{
/// Constant defining how often components of finality-aleph should report their state
const STATUS_REPORT_INTERVAL: Duration = Duration::from_secs(20);

fn max_message_size(protocol: Protocol) -> u64 {
match protocol {
Protocol::Authentication => MAX_AUTHENTICATION_MESSAGE_SIZE,
Protocol::BlockSync => MAX_BLOCK_SYNC_MESSAGE_SIZE,
}
/// Name of the network protocol used by Aleph Zero to disseminate validator
/// authentications.
const AUTHENTICATION_PROTOCOL_NAME: &str = "/auth/0";

/// Name of the network protocol used by Aleph Zero to synchronize the block state.
const BLOCK_SYNC_PROTOCOL_NAME: &str = "/sync/0";

pub struct NetConfig {
timorleph marked this conversation as resolved.
Show resolved Hide resolved
pub net_config: FullNetworkConfiguration,
pub authentication_network: ProtocolNetwork,
pub block_sync_network: ProtocolNetwork,
}

/// Returns a NonDefaultSetConfig for the specified protocol.
pub fn peers_set_config(
naming: ProtocolNaming,
protocol: Protocol,
) -> (
sc_network::config::NonDefaultSetConfig,
Box<dyn sc_network::config::NotificationService>,
) {
sc_network::config::NonDefaultSetConfig::new(
naming.protocol_name(&protocol),
naming.fallback_protocol_names(&protocol),
max_message_size(protocol),
// we do not use custom handshake
None,
sc_network::config::SetConfig::default(),
)
impl NetConfig {
fn add_protocol(
chain_prefix: &str,
protocol_name: &str,
max_message_size: u64,
net_config: &mut FullNetworkConfiguration,
) -> ProtocolNetwork {
let (config, notifications) = sc_network::config::NonDefaultSetConfig::new(
// full protocol name
format!("{chain_prefix}{protocol_name}").into(),
// no fallback names
vec![],
max_message_size,
// we do not use custom handshake
None,
sc_network::config::SetConfig::default(),
);
net_config.add_notification_protocol(config);
ProtocolNetwork::new(notifications)
}

pub fn new(config: &Configuration, chain_prefix: &str) -> Self {
let mut net_config = FullNetworkConfiguration::new(&config.network);

let authentication_network = Self::add_protocol(
chain_prefix,
AUTHENTICATION_PROTOCOL_NAME,
MAX_AUTHENTICATION_MESSAGE_SIZE,
&mut net_config,
);
let block_sync_network = Self::add_protocol(
chain_prefix,
BLOCK_SYNC_PROTOCOL_NAME,
MAX_BLOCK_SYNC_MESSAGE_SIZE,
&mut net_config,
);

Self {
net_config,
authentication_network,
block_sync_network,
}
}
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)]
Expand Down Expand Up @@ -283,7 +317,9 @@ pub struct RateLimiterConfig {
}

pub struct AlephConfig<C, SC, T> {
pub network_event_stream: SubstrateNetworkEventStream<AlephBlock, AlephHash>,
pub authentication_network: ProtocolNetwork,
pub block_sync_network: ProtocolNetwork,
pub sync_network_service: SyncNetworkService<AlephBlock, AlephHash>,
pub client: Arc<C>,
pub chain_status: SubstrateChainStatus,
pub import_queue_handle: BlockImporter,
Expand Down
111 changes: 0 additions & 111 deletions finality-aleph/src/network/gossip/metrics.rs

This file was deleted.

Loading
Loading