Skip to main content

soil_network/mixnet/
protocol.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use super::config::Config;
8use mixnet::core::PACKET_SIZE;
9use soil_network::{
10	config::{NonReservedPeerMode, SetConfig},
11	peer_store::PeerStoreProvider,
12	service::NotificationMetrics,
13	NetworkBackend, NotificationService, ProtocolName,
14};
15use subsoil::runtime::traits::Block as BlockT;
16
17/// Returns the protocol name to use for the mixnet controlled by the given chain.
18pub fn protocol_name(genesis_hash: &[u8], fork_id: Option<&str>) -> ProtocolName {
19	let name = if let Some(fork_id) = fork_id {
20		format!("/{}/{}/mixnet/1", array_bytes::bytes2hex("", genesis_hash), fork_id)
21	} else {
22		format!("/{}/mixnet/1", array_bytes::bytes2hex("", genesis_hash))
23	};
24	name.into()
25}
26
27/// Returns the peers set configuration for the mixnet protocol.
28pub fn peers_set_config<Block: BlockT, Network: NetworkBackend<Block, <Block as BlockT>::Hash>>(
29	name: ProtocolName,
30	config: &Config,
31	metrics: NotificationMetrics,
32	peerstore_handle: std::sync::Arc<dyn PeerStoreProvider>,
33) -> (Network::NotificationProtocolConfig, Box<dyn NotificationService>) {
34	let set_config = if config.substrate.num_gateway_slots != 0 {
35		// out_peers is always 0; we are only interested in connecting to mixnodes, which we do by
36		// setting them as reserved nodes
37		SetConfig {
38			in_peers: config.substrate.num_gateway_slots,
39			out_peers: 0,
40			reserved_nodes: Vec::new(),
41			non_reserved_mode: NonReservedPeerMode::Accept,
42		}
43	} else {
44		SetConfig {
45			in_peers: 0,
46			out_peers: 0,
47			reserved_nodes: Vec::new(),
48			non_reserved_mode: NonReservedPeerMode::Deny,
49		}
50	};
51
52	Network::notification_config(
53		name,
54		Vec::new(),
55		PACKET_SIZE as u64,
56		None,
57		set_config,
58		metrics,
59		peerstore_handle,
60	)
61}