Skip to content

Commit 1c29d22

Browse files
authored
Merge branch 'main' into obrok/contract-support-redux
2 parents 3df5c2f + d59c6d4 commit 1c29d22

File tree

8 files changed

+212
-74
lines changed

8 files changed

+212
-74
lines changed

bin/node/src/service.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use aleph_primitives::AlephSessionApi;
99
use aleph_runtime::{self, opaque::Block, RuntimeApi, MAX_BLOCK_SIZE};
1010
use finality_aleph::{
1111
run_nonvalidator_node, run_validator_node, AlephBlockImport, AlephConfig,
12-
JustificationNotification, Metrics, MillisecsPerBlock, Protocol, SessionPeriod,
12+
JustificationNotification, Metrics, MillisecsPerBlock, Protocol, ProtocolNaming, SessionPeriod,
1313
};
1414
use futures::channel::mpsc;
1515
use log::warn;
16-
use sc_client_api::{Backend, HeaderBackend};
16+
use sc_client_api::{Backend, BlockBackend, HeaderBackend};
1717
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
1818
use sc_consensus_slots::BackoffAuthoringBlocksStrategy;
1919
use sc_network::NetworkService;
@@ -212,14 +212,35 @@ fn setup(
212212
(
213213
RpcHandlers,
214214
Arc<NetworkService<Block, <Block as BlockT>::Hash>>,
215+
ProtocolNaming,
215216
NetworkStarter,
216217
),
217218
ServiceError,
218219
> {
220+
let genesis_hash = client
221+
.block_hash(0)
222+
.ok()
223+
.flatten()
224+
.expect("we should have a hash");
225+
let chain_prefix = match config.chain_spec.fork_id() {
226+
Some(fork_id) => format!("/{}/{}", genesis_hash, fork_id),
227+
None => format!("/{}", genesis_hash),
228+
};
229+
let protocol_naming = ProtocolNaming::new(chain_prefix);
230+
config
231+
.network
232+
.extra_sets
233+
.push(finality_aleph::peers_set_config(
234+
protocol_naming.clone(),
235+
Protocol::Authentication,
236+
));
219237
config
220238
.network
221239
.extra_sets
222-
.push(finality_aleph::peers_set_config(Protocol::Authentication));
240+
.push(finality_aleph::peers_set_config(
241+
protocol_naming.clone(),
242+
Protocol::BlockSync,
243+
));
223244

224245
let (network, system_rpc_tx, tx_handler_controller, network_starter) =
225246
sc_service::build_network(sc_service::BuildNetworkParams {
@@ -262,7 +283,7 @@ fn setup(
262283
telemetry: telemetry.as_mut(),
263284
})?;
264285

265-
Ok((rpc_handlers, network, network_starter))
286+
Ok((rpc_handlers, network, protocol_naming, network_starter))
266287
}
267288

268289
/// Builds a new service for a full client.
@@ -308,7 +329,7 @@ pub fn new_authority(
308329
let backoff_authoring_blocks = Some(LimitNonfinalized(aleph_config.max_nonfinalized_blocks()));
309330
let prometheus_registry = config.prometheus_registry().cloned();
310331

311-
let (_rpc_handlers, network, network_starter) = setup(
332+
let (_rpc_handlers, network, protocol_naming, network_starter) = setup(
312333
config,
313334
backend.clone(),
314335
&keystore_container,
@@ -383,6 +404,7 @@ pub fn new_authority(
383404
backup_saving_path: backup_path,
384405
external_addresses: aleph_config.external_addresses(),
385406
validator_port: aleph_config.validator_port(),
407+
protocol_naming,
386408
};
387409
task_manager.spawn_essential_handle().spawn_blocking(
388410
"aleph",
@@ -418,7 +440,7 @@ pub fn new_full(
418440
.path(),
419441
);
420442

421-
let (_rpc_handlers, network, network_starter) = setup(
443+
let (_rpc_handlers, network, protocol_naming, network_starter) = setup(
422444
config,
423445
backend.clone(),
424446
&keystore_container,
@@ -460,6 +482,7 @@ pub fn new_full(
460482
backup_saving_path: backup_path,
461483
external_addresses: aleph_config.external_addresses(),
462484
validator_port: aleph_config.validator_port(),
485+
protocol_naming,
463486
};
464487

465488
task_manager.spawn_essential_handle().spawn_blocking(

finality-aleph/src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use tokio::time::Duration;
2222
use crate::{
2323
abft::{CurrentNetworkData, LegacyNetworkData, CURRENT_VERSION, LEGACY_VERSION},
2424
aggregation::{CurrentRmcNetworkData, LegacyRmcNetworkData},
25-
network::{data::split::Split, protocol_name},
25+
network::data::split::Split,
2626
session::{
2727
first_block_of_session, last_block_of_session, session_id_from_block_num,
2828
SessionBoundaries, SessionId,
@@ -51,7 +51,7 @@ pub use abft::{Keychain, NodeCount, NodeIndex, Recipient, SignatureSet, SpawnHan
5151
pub use aleph_primitives::{AuthorityId, AuthorityPair, AuthoritySignature};
5252
pub use import::AlephBlockImport;
5353
pub use justification::{AlephJustification, JustificationNotification};
54-
pub use network::Protocol;
54+
pub use network::{Protocol, ProtocolNaming};
5555
pub use nodes::{run_nonvalidator_node, run_validator_node};
5656
pub use session::SessionPeriod;
5757

@@ -67,21 +67,21 @@ enum Error {
6767
}
6868

6969
/// Returns a NonDefaultSetConfig for the specified protocol.
70-
pub fn peers_set_config(protocol: Protocol) -> sc_network_common::config::NonDefaultSetConfig {
71-
let name = protocol_name(&protocol);
72-
70+
pub fn peers_set_config(
71+
naming: ProtocolNaming,
72+
protocol: Protocol,
73+
) -> sc_network_common::config::NonDefaultSetConfig {
7374
let mut config = sc_network_common::config::NonDefaultSetConfig::new(
74-
name,
75+
naming.protocol_name(&protocol),
7576
// max_notification_size should be larger than the maximum possible honest message size (in bytes).
7677
// Max size of alert is UNIT_SIZE * MAX_UNITS_IN_ALERT ~ 100 * 5000 = 50000 bytes
7778
// Max size of parents response UNIT_SIZE * N_MEMBERS ~ 100 * N_MEMBERS
7879
// When adding other (large) message types we need to make sure this limit is fine.
7980
1024 * 1024,
8081
);
8182

82-
config.set_config = match protocol {
83-
Protocol::Authentication => sc_network_common::config::SetConfig::default(),
84-
};
83+
config.set_config = sc_network_common::config::SetConfig::default();
84+
config.add_fallback_names(naming.fallback_protocol_names(&protocol));
8585
config
8686
}
8787

@@ -254,6 +254,7 @@ pub struct AlephConfig<B: Block, H: ExHashT, C, SC, BB> {
254254
pub backup_saving_path: Option<PathBuf>,
255255
pub external_addresses: Vec<String>,
256256
pub validator_port: u16,
257+
pub protocol_naming: ProtocolNaming,
257258
}
258259

259260
pub trait BlockchainBackend<B: Block> {

finality-aleph/src/network/gossip/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ pub trait Network<D: Data>: Send + 'static {
4646
async fn next(&mut self) -> Result<(D, Self::PeerId), Self::Error>;
4747
}
4848

49-
/// The Authentication protocol is used for validator discovery.
49+
/// Protocols used by the network.
5050
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
5151
pub enum Protocol {
52+
/// The authentication protocol is used for validator discovery.
5253
Authentication,
54+
/// The block synchronization protocol.
55+
BlockSync,
5356
}
5457

5558
/// Abstraction over a sender to the raw network.

finality-aleph/src/network/gossip/service.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ enum Command<D: Data, P: Clone + Debug + Eq + Hash + Send + 'static> {
3535
pub struct Service<N: RawNetwork, D: Data> {
3636
network: N,
3737
messages_from_user: mpsc::UnboundedReceiver<Command<D, N::PeerId>>,
38-
messages_for_user: mpsc::UnboundedSender<(D, N::PeerId)>,
38+
messages_for_authentication_user: mpsc::UnboundedSender<(D, N::PeerId)>,
39+
messages_for_block_sync_user: mpsc::UnboundedSender<(D, N::PeerId)>,
3940
authentication_connected_peers: HashSet<N::PeerId>,
4041
authentication_peer_senders: HashMap<N::PeerId, TracingUnboundedSender<D>>,
42+
block_sync_connected_peers: HashSet<N::PeerId>,
43+
block_sync_peer_senders: HashMap<N::PeerId, TracingUnboundedSender<D>>,
4144
spawn_handle: SpawnTaskHandle,
4245
}
4346

4447
struct ServiceInterface<D: Data, P: Clone + Debug + Eq + Hash + Send + 'static> {
48+
protocol: Protocol,
4549
messages_from_service: mpsc::UnboundedReceiver<(D, P)>,
4650
messages_for_service: mpsc::UnboundedSender<Command<D, P>>,
4751
}
@@ -70,7 +74,7 @@ impl<D: Data, P: Clone + Debug + Eq + Hash + Send + 'static> Network<D> for Serv
7074

7175
fn send_to(&mut self, data: D, peer_id: Self::PeerId) -> Result<(), Self::Error> {
7276
self.messages_for_service
73-
.unbounded_send(Command::Send(data, peer_id, Protocol::Authentication))
77+
.unbounded_send(Command::Send(data, peer_id, self.protocol))
7478
.map_err(|_| Error::ServiceStopped)
7579
}
7680

@@ -80,17 +84,13 @@ impl<D: Data, P: Clone + Debug + Eq + Hash + Send + 'static> Network<D> for Serv
8084
peer_ids: HashSet<Self::PeerId>,
8185
) -> Result<(), Self::Error> {
8286
self.messages_for_service
83-
.unbounded_send(Command::SendToRandom(
84-
data,
85-
peer_ids,
86-
Protocol::Authentication,
87-
))
87+
.unbounded_send(Command::SendToRandom(data, peer_ids, self.protocol))
8888
.map_err(|_| Error::ServiceStopped)
8989
}
9090

9191
fn broadcast(&mut self, data: D) -> Result<(), Self::Error> {
9292
self.messages_for_service
93-
.unbounded_send(Command::Broadcast(data, Protocol::Authentication))
93+
.unbounded_send(Command::Broadcast(data, self.protocol))
9494
.map_err(|_| Error::ServiceStopped)
9595
}
9696

@@ -115,20 +115,32 @@ impl<N: RawNetwork, D: Data> Service<N, D> {
115115
) -> (
116116
Service<N, D>,
117117
impl Network<D, Error = Error, PeerId = N::PeerId>,
118+
impl Network<D, Error = Error, PeerId = N::PeerId>,
118119
) {
119-
let (messages_for_user, messages_from_service) = mpsc::unbounded();
120+
let (messages_for_authentication_user, messages_from_authentication_service) =
121+
mpsc::unbounded();
122+
let (messages_for_block_sync_user, messages_from_block_sync_service) = mpsc::unbounded();
120123
let (messages_for_service, messages_from_user) = mpsc::unbounded();
121124
(
122125
Service {
123126
network,
124127
messages_from_user,
125-
messages_for_user,
128+
messages_for_authentication_user,
129+
messages_for_block_sync_user,
126130
spawn_handle,
127131
authentication_connected_peers: HashSet::new(),
128132
authentication_peer_senders: HashMap::new(),
133+
block_sync_connected_peers: HashSet::new(),
134+
block_sync_peer_senders: HashMap::new(),
135+
},
136+
ServiceInterface {
137+
protocol: Protocol::Authentication,
138+
messages_from_service: messages_from_authentication_service,
139+
messages_for_service: messages_for_service.clone(),
129140
},
130141
ServiceInterface {
131-
messages_from_service,
142+
protocol: Protocol::BlockSync,
143+
messages_from_service: messages_from_block_sync_service,
132144
messages_for_service,
133145
},
134146
)
@@ -141,6 +153,7 @@ impl<N: RawNetwork, D: Data> Service<N, D> {
141153
) -> Option<&mut TracingUnboundedSender<D>> {
142154
match protocol {
143155
Protocol::Authentication => self.authentication_peer_senders.get_mut(peer),
156+
Protocol::BlockSync => self.block_sync_peer_senders.get_mut(peer),
144157
}
145158
}
146159

@@ -211,6 +224,7 @@ impl<N: RawNetwork, D: Data> Service<N, D> {
211224
fn protocol_peers(&self, protocol: Protocol) -> &HashSet<N::PeerId> {
212225
match protocol {
213226
Protocol::Authentication => &self.authentication_connected_peers,
227+
Protocol::BlockSync => &self.block_sync_connected_peers,
214228
}
215229
}
216230

@@ -262,6 +276,12 @@ impl<N: RawNetwork, D: Data> Service<N, D> {
262276
self.authentication_peer_senders.insert(peer.clone(), tx);
263277
rx
264278
}
279+
Protocol::BlockSync => {
280+
let (tx, rx) = tracing_unbounded("mpsc_notification_stream_block_sync");
281+
self.block_sync_connected_peers.insert(peer.clone());
282+
self.block_sync_peer_senders.insert(peer.clone(), tx);
283+
rx
284+
}
265285
};
266286
self.spawn_handle.spawn(
267287
"aleph/network/peer_sender",
@@ -276,19 +296,33 @@ impl<N: RawNetwork, D: Data> Service<N, D> {
276296
self.authentication_connected_peers.remove(&peer);
277297
self.authentication_peer_senders.remove(&peer);
278298
}
299+
Protocol::BlockSync => {
300+
self.block_sync_connected_peers.remove(&peer);
301+
self.block_sync_peer_senders.remove(&peer);
302+
}
279303
}
280304
}
281305
Messages(peer_id, messages) => {
282306
for (protocol, data) in messages.into_iter() {
283307
match protocol {
284308
Protocol::Authentication => match D::decode(&mut &data[..]) {
285309
Ok(data) => self
286-
.messages_for_user
310+
.messages_for_authentication_user
287311
.unbounded_send((data, peer_id.clone()))?,
288312
Err(e) => {
289313
warn!(target: "aleph-network", "Error decoding authentication protocol message: {}", e)
290314
}
291315
},
316+
// This is a bit of a placeholder for now, as we are not yet using this
317+
// protocol. In the future we will not be using the same D as above.
318+
Protocol::BlockSync => match D::decode(&mut &data[..]) {
319+
Ok(data) => self
320+
.messages_for_block_sync_user
321+
.unbounded_send((data, peer_id.clone()))?,
322+
Err(e) => {
323+
warn!(target: "aleph-network", "Error decoding block sync protocol message: {}", e)
324+
}
325+
},
292326
};
293327
}
294328
}
@@ -303,6 +337,10 @@ impl<N: RawNetwork, D: Data> Service<N, D> {
303337
"authentication connected peers - {:?}; ",
304338
self.authentication_connected_peers.len()
305339
));
340+
status.push_str(&format!(
341+
"block sync connected peers - {:?}; ",
342+
self.block_sync_connected_peers.len()
343+
));
306344

307345
info!(target: "aleph-network", "{}", status);
308346
}
@@ -379,7 +417,7 @@ mod tests {
379417

380418
// Prepare service
381419
let network = MockRawNetwork::new(event_stream_oneshot_tx);
382-
let (service, gossip_network) =
420+
let (service, gossip_network, _) =
383421
Service::new(network.clone(), task_manager.spawn_handle());
384422
let gossip_network = Box::new(gossip_network);
385423

finality-aleph/src/network/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod tcp;
1919
#[cfg(test)]
2020
pub use gossip::mock::{MockEvent, MockRawNetwork};
2121
pub use gossip::{Network as GossipNetwork, Protocol, Service as GossipService};
22-
pub use substrate::protocol_name;
22+
pub use substrate::{ProtocolNaming, SubstrateNetwork};
2323

2424
/// Represents the id of an arbitrary node.
2525
pub trait PeerId: PartialEq + Eq + Clone + Debug + Display + Hash + Codec + Send {

0 commit comments

Comments
 (0)