sc_network/litep2p/shim/notification/
config.rs1use crate::{
22 config::{MultiaddrWithPeerId, NonReservedPeerMode, NotificationHandshake, SetConfig},
23 litep2p::shim::notification::{
24 peerset::{Peerset, PeersetCommand},
25 NotificationProtocol,
26 },
27 peer_store::PeerStoreProvider,
28 service::{metrics::NotificationMetrics, traits::NotificationConfig},
29 NotificationService, ProtocolName,
30};
31
32use litep2p::protocol::notification::{Config, ConfigBuilder};
33
34use sc_utils::mpsc::TracingUnboundedSender;
35
36use std::sync::{atomic::AtomicUsize, Arc};
37
38#[derive(Debug, Clone)]
40pub struct ProtocolControlHandle {
41 pub tx: TracingUnboundedSender<PeersetCommand>,
43
44 pub connected_peers: Arc<AtomicUsize>,
46}
47
48impl ProtocolControlHandle {
49 pub fn new(
51 tx: TracingUnboundedSender<PeersetCommand>,
52 connected_peers: Arc<AtomicUsize>,
53 ) -> Self {
54 Self { tx, connected_peers }
55 }
56}
57
58#[derive(Debug)]
60pub struct NotificationProtocolConfig {
61 pub protocol_name: ProtocolName,
64
65 max_notification_size: usize,
67
68 set_config: SetConfig,
70
71 pub config: Config,
73
74 pub handle: ProtocolControlHandle,
76}
77
78impl NotificationProtocolConfig {
79 pub fn new(
81 protocol_name: ProtocolName,
82 fallback_names: Vec<ProtocolName>,
83 max_notification_size: usize,
84 handshake: Option<NotificationHandshake>,
85 set_config: SetConfig,
86 metrics: NotificationMetrics,
87 peerstore_handle: Arc<dyn PeerStoreProvider>,
88 ) -> (Self, Box<dyn NotificationService>) {
89 let connected_peers = Arc::new(Default::default());
91 let (peerset, peerset_tx) = Peerset::new(
92 protocol_name.clone(),
93 set_config.out_peers as usize,
94 set_config.in_peers as usize,
95 set_config.non_reserved_mode == NonReservedPeerMode::Deny,
96 set_config.reserved_nodes.iter().map(|address| address.peer_id).collect(),
97 Arc::clone(&connected_peers),
98 peerstore_handle,
99 );
100
101 let (config, handle) = ConfigBuilder::new(protocol_name.clone().into())
108 .with_handshake(handshake.map_or(vec![1], |handshake| (*handshake).to_vec()))
109 .with_max_size(max_notification_size as usize)
110 .with_auto_accept_inbound(true)
111 .with_fallback_names(fallback_names.into_iter().map(From::from).collect())
112 .build();
113
114 let protocol = NotificationProtocol::new(protocol_name.clone(), handle, peerset, metrics);
118
119 (
120 Self {
121 protocol_name,
122 max_notification_size,
123 set_config,
124 config,
125 handle: ProtocolControlHandle::new(peerset_tx, connected_peers),
126 },
127 Box::new(protocol),
128 )
129 }
130
131 pub fn protocol_name(&self) -> &ProtocolName {
133 &self.protocol_name
134 }
135
136 pub fn set_config(&self) -> &SetConfig {
138 &self.set_config
139 }
140
141 pub fn allow_non_reserved(&mut self, in_peers: u32, out_peers: u32) {
143 self.set_config.in_peers = in_peers;
144 self.set_config.out_peers = out_peers;
145 self.set_config.non_reserved_mode = NonReservedPeerMode::Accept;
146 }
147
148 pub fn add_reserved(&mut self, peer: MultiaddrWithPeerId) {
150 self.set_config.reserved_nodes.push(peer);
151 }
152
153 pub fn max_notification_size(&self) -> usize {
155 self.max_notification_size
156 }
157}
158
159impl NotificationConfig for NotificationProtocolConfig {
160 fn set_config(&self) -> &SetConfig {
161 &self.set_config
162 }
163
164 fn protocol_name(&self) -> &ProtocolName {
166 &self.protocol_name
167 }
168}