1pub use crate::{
25 discovery::DEFAULT_KADEMLIA_REPLICATION_FACTOR,
26 peer_store::PeerStoreProvider,
27 protocol::{notification_service, NotificationsSink, ProtocolHandlePair},
28 request_responses::{
29 IncomingRequest, OutgoingResponse, ProtocolConfig as RequestResponseConfig,
30 },
31 service::{
32 metrics::NotificationMetrics,
33 traits::{NotificationConfig, NotificationService, PeerStore},
34 },
35 types::ProtocolName,
36};
37
38pub use sc_network_types::{build_multiaddr, ed25519};
39use sc_network_types::{
40 multiaddr::{self, Multiaddr},
41 PeerId,
42};
43
44use crate::service::{ensure_addresses_consistent_with_transport, traits::NetworkBackend};
45use codec::Encode;
46use prometheus_endpoint::Registry;
47use zeroize::Zeroize;
48
49pub use sc_network_common::{
50 role::{Role, Roles},
51 sync::SyncMode,
52 ExHashT,
53};
54
55use sp_runtime::traits::Block as BlockT;
56
57use std::{
58 error::Error,
59 fmt, fs,
60 future::Future,
61 io::{self, Write},
62 iter,
63 net::Ipv4Addr,
64 num::NonZeroUsize,
65 path::{Path, PathBuf},
66 pin::Pin,
67 str::{self, FromStr},
68 sync::Arc,
69};
70
71#[derive(Clone, PartialEq, Eq, Hash)]
75pub struct ProtocolId(smallvec::SmallVec<[u8; 6]>);
76
77impl<'a> From<&'a str> for ProtocolId {
78 fn from(bytes: &'a str) -> ProtocolId {
79 Self(bytes.as_bytes().into())
80 }
81}
82
83impl AsRef<str> for ProtocolId {
84 fn as_ref(&self) -> &str {
85 str::from_utf8(&self.0[..])
86 .expect("the only way to build a ProtocolId is through a UTF-8 String; qed")
87 }
88}
89
90impl fmt::Debug for ProtocolId {
91 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92 fmt::Debug::fmt(self.as_ref(), f)
93 }
94}
95
96pub fn parse_str_addr(addr_str: &str) -> Result<(PeerId, Multiaddr), ParseErr> {
111 let addr: Multiaddr = addr_str.parse()?;
112 parse_addr(addr)
113}
114
115pub fn parse_addr(mut addr: Multiaddr) -> Result<(PeerId, Multiaddr), ParseErr> {
117 let multihash = match addr.pop() {
118 Some(multiaddr::Protocol::P2p(multihash)) => multihash,
119 _ => return Err(ParseErr::PeerIdMissing),
120 };
121 let peer_id = PeerId::from_multihash(multihash).map_err(|_| ParseErr::InvalidPeerId)?;
122
123 Ok((peer_id, addr))
124}
125
126#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
141#[serde(try_from = "String", into = "String")]
142pub struct MultiaddrWithPeerId {
143 pub multiaddr: Multiaddr,
145 pub peer_id: PeerId,
147}
148
149impl MultiaddrWithPeerId {
150 pub fn concat(&self) -> Multiaddr {
152 let proto = multiaddr::Protocol::P2p(From::from(self.peer_id));
153 self.multiaddr.clone().with(proto)
154 }
155}
156
157impl fmt::Display for MultiaddrWithPeerId {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159 fmt::Display::fmt(&self.concat(), f)
160 }
161}
162
163impl FromStr for MultiaddrWithPeerId {
164 type Err = ParseErr;
165
166 fn from_str(s: &str) -> Result<Self, Self::Err> {
167 let (peer_id, multiaddr) = parse_str_addr(s)?;
168 Ok(Self { peer_id, multiaddr })
169 }
170}
171
172impl From<MultiaddrWithPeerId> for String {
173 fn from(ma: MultiaddrWithPeerId) -> String {
174 format!("{}", ma)
175 }
176}
177
178impl TryFrom<String> for MultiaddrWithPeerId {
179 type Error = ParseErr;
180 fn try_from(string: String) -> Result<Self, Self::Error> {
181 string.parse()
182 }
183}
184
185#[derive(Debug)]
187pub enum ParseErr {
188 MultiaddrParse(multiaddr::ParseError),
190 InvalidPeerId,
192 PeerIdMissing,
194}
195
196impl fmt::Display for ParseErr {
197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198 match self {
199 Self::MultiaddrParse(err) => write!(f, "{}", err),
200 Self::InvalidPeerId => write!(f, "Peer id at the end of the address is invalid"),
201 Self::PeerIdMissing => write!(f, "Peer id is missing from the address"),
202 }
203 }
204}
205
206impl std::error::Error for ParseErr {
207 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
208 match self {
209 Self::MultiaddrParse(err) => Some(err),
210 Self::InvalidPeerId => None,
211 Self::PeerIdMissing => None,
212 }
213 }
214}
215
216impl From<multiaddr::ParseError> for ParseErr {
217 fn from(err: multiaddr::ParseError) -> ParseErr {
218 Self::MultiaddrParse(err)
219 }
220}
221
222#[derive(Debug, Clone)]
224pub struct NotificationHandshake(Vec<u8>);
225
226impl NotificationHandshake {
227 pub fn new<H: Encode>(handshake: H) -> Self {
229 Self(handshake.encode())
230 }
231
232 pub fn from_bytes(bytes: Vec<u8>) -> Self {
234 Self(bytes)
235 }
236}
237
238impl std::ops::Deref for NotificationHandshake {
239 type Target = Vec<u8>;
240
241 fn deref(&self) -> &Self::Target {
242 &self.0
243 }
244}
245
246#[derive(Clone, Debug)]
248pub enum TransportConfig {
249 Normal {
251 enable_mdns: bool,
254
255 allow_private_ip: bool,
259 },
260
261 MemoryOnly,
264}
265
266#[derive(Clone, Debug, PartialEq, Eq)]
268pub enum NonReservedPeerMode {
269 Accept,
271 Deny,
273}
274
275impl NonReservedPeerMode {
276 pub fn parse(s: &str) -> Option<Self> {
278 match s {
279 "accept" => Some(Self::Accept),
280 "deny" => Some(Self::Deny),
281 _ => None,
282 }
283 }
284
285 pub fn is_reserved_only(&self) -> bool {
287 matches!(self, NonReservedPeerMode::Deny)
288 }
289}
290
291#[derive(Clone, Debug)]
295pub enum NodeKeyConfig {
296 Ed25519(Secret<ed25519::SecretKey>),
298}
299
300impl Default for NodeKeyConfig {
301 fn default() -> NodeKeyConfig {
302 Self::Ed25519(Secret::New)
303 }
304}
305
306pub type Ed25519Secret = Secret<ed25519::SecretKey>;
308
309#[derive(Clone)]
311pub enum Secret<K> {
312 Input(K),
314 File(PathBuf),
320 New,
322}
323
324impl<K> fmt::Debug for Secret<K> {
325 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
326 match self {
327 Self::Input(_) => f.debug_tuple("Secret::Input").finish(),
328 Self::File(path) => f.debug_tuple("Secret::File").field(path).finish(),
329 Self::New => f.debug_tuple("Secret::New").finish(),
330 }
331 }
332}
333
334impl NodeKeyConfig {
335 pub fn into_keypair(self) -> io::Result<ed25519::Keypair> {
346 use NodeKeyConfig::*;
347 match self {
348 Ed25519(Secret::New) => Ok(ed25519::Keypair::generate()),
349
350 Ed25519(Secret::Input(k)) => Ok(ed25519::Keypair::from(k).into()),
351
352 Ed25519(Secret::File(f)) => get_secret(
353 f,
354 |mut b| match String::from_utf8(b.to_vec()).ok().and_then(|s| {
355 if s.len() == 64 {
356 array_bytes::hex2bytes(&s).ok()
357 } else {
358 None
359 }
360 }) {
361 Some(s) => ed25519::SecretKey::try_from_bytes(s),
362 _ => ed25519::SecretKey::try_from_bytes(&mut b),
363 },
364 ed25519::SecretKey::generate,
365 |b| b.as_ref().to_vec(),
366 )
367 .map(ed25519::Keypair::from),
368 }
369 }
370}
371
372fn get_secret<P, F, G, E, W, K>(file: P, parse: F, generate: G, serialize: W) -> io::Result<K>
376where
377 P: AsRef<Path>,
378 F: for<'r> FnOnce(&'r mut [u8]) -> Result<K, E>,
379 G: FnOnce() -> K,
380 E: Error + Send + Sync + 'static,
381 W: Fn(&K) -> Vec<u8>,
382{
383 std::fs::read(&file)
384 .and_then(|mut sk_bytes| {
385 parse(&mut sk_bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
386 })
387 .or_else(|e| {
388 if e.kind() == io::ErrorKind::NotFound {
389 file.as_ref().parent().map_or(Ok(()), fs::create_dir_all)?;
390 let sk = generate();
391 let mut sk_vec = serialize(&sk);
392 write_secret_file(file, &sk_vec)?;
393 sk_vec.zeroize();
394 Ok(sk)
395 } else {
396 Err(e)
397 }
398 })
399}
400
401fn write_secret_file<P>(path: P, sk_bytes: &[u8]) -> io::Result<()>
403where
404 P: AsRef<Path>,
405{
406 let mut file = open_secret_file(&path)?;
407 file.write_all(sk_bytes)
408}
409
410#[cfg(unix)]
412fn open_secret_file<P>(path: P) -> io::Result<fs::File>
413where
414 P: AsRef<Path>,
415{
416 use std::os::unix::fs::OpenOptionsExt;
417 fs::OpenOptions::new().write(true).create_new(true).mode(0o600).open(path)
418}
419
420#[cfg(not(unix))]
422fn open_secret_file<P>(path: P) -> Result<fs::File, io::Error>
423where
424 P: AsRef<Path>,
425{
426 fs::OpenOptions::new().write(true).create_new(true).open(path)
427}
428
429#[derive(Clone, Debug)]
431pub struct SetConfig {
432 pub in_peers: u32,
434
435 pub out_peers: u32,
437
438 pub reserved_nodes: Vec<MultiaddrWithPeerId>,
440
441 pub non_reserved_mode: NonReservedPeerMode,
444}
445
446impl Default for SetConfig {
447 fn default() -> Self {
448 Self {
449 in_peers: 25,
450 out_peers: 75,
451 reserved_nodes: Vec::new(),
452 non_reserved_mode: NonReservedPeerMode::Accept,
453 }
454 }
455}
456
457#[derive(Debug)]
462pub struct NonDefaultSetConfig {
463 protocol_name: ProtocolName,
469
470 fallback_names: Vec<ProtocolName>,
477
478 handshake: Option<NotificationHandshake>,
484
485 max_notification_size: u64,
487
488 set_config: SetConfig,
490
491 protocol_handle_pair: ProtocolHandlePair,
499}
500
501impl NonDefaultSetConfig {
502 pub fn new(
505 protocol_name: ProtocolName,
506 fallback_names: Vec<ProtocolName>,
507 max_notification_size: u64,
508 handshake: Option<NotificationHandshake>,
509 set_config: SetConfig,
510 ) -> (Self, Box<dyn NotificationService>) {
511 let (protocol_handle_pair, notification_service) =
512 notification_service(protocol_name.clone());
513 (
514 Self {
515 protocol_name,
516 max_notification_size,
517 fallback_names,
518 handshake,
519 set_config,
520 protocol_handle_pair,
521 },
522 notification_service,
523 )
524 }
525
526 pub fn protocol_name(&self) -> &ProtocolName {
528 &self.protocol_name
529 }
530
531 pub fn fallback_names(&self) -> impl Iterator<Item = &ProtocolName> {
533 self.fallback_names.iter()
534 }
535
536 pub fn handshake(&self) -> &Option<NotificationHandshake> {
538 &self.handshake
539 }
540
541 pub fn max_notification_size(&self) -> u64 {
543 self.max_notification_size
544 }
545
546 pub fn set_config(&self) -> &SetConfig {
548 &self.set_config
549 }
550
551 pub fn take_protocol_handle(self) -> ProtocolHandlePair {
553 self.protocol_handle_pair
554 }
555
556 pub fn allow_non_reserved(&mut self, in_peers: u32, out_peers: u32) {
558 self.set_config.in_peers = in_peers;
559 self.set_config.out_peers = out_peers;
560 self.set_config.non_reserved_mode = NonReservedPeerMode::Accept;
561 }
562
563 pub fn add_reserved(&mut self, peer: MultiaddrWithPeerId) {
565 self.set_config.reserved_nodes.push(peer);
566 }
567
568 pub fn add_fallback_names(&mut self, fallback_names: Vec<ProtocolName>) {
572 self.fallback_names.extend(fallback_names);
573 }
574}
575
576impl NotificationConfig for NonDefaultSetConfig {
577 fn set_config(&self) -> &SetConfig {
578 &self.set_config
579 }
580
581 fn protocol_name(&self) -> &ProtocolName {
583 &self.protocol_name
584 }
585}
586
587#[derive(Clone, Debug)]
589pub struct NetworkConfiguration {
590 pub net_config_path: Option<PathBuf>,
592
593 pub listen_addresses: Vec<Multiaddr>,
595
596 pub public_addresses: Vec<Multiaddr>,
598
599 pub boot_nodes: Vec<MultiaddrWithPeerId>,
601
602 pub node_key: NodeKeyConfig,
604
605 pub default_peers_set: SetConfig,
607
608 pub default_peers_set_num_full: u32,
613
614 pub client_version: String,
616
617 pub node_name: String,
619
620 pub transport: TransportConfig,
622
623 pub max_parallel_downloads: u32,
625
626 pub max_blocks_per_request: u32,
628
629 pub sync_mode: SyncMode,
631
632 pub enable_dht_random_walk: bool,
636
637 pub allow_non_globals_in_dht: bool,
639
640 pub kademlia_disjoint_query_paths: bool,
643
644 pub kademlia_replication_factor: NonZeroUsize,
649
650 pub ipfs_server: bool,
652
653 pub network_backend: NetworkBackendType,
655}
656
657impl NetworkConfiguration {
658 pub fn new<SN: Into<String>, SV: Into<String>>(
660 node_name: SN,
661 client_version: SV,
662 node_key: NodeKeyConfig,
663 net_config_path: Option<PathBuf>,
664 ) -> Self {
665 let default_peers_set = SetConfig::default();
666 Self {
667 net_config_path,
668 listen_addresses: Vec::new(),
669 public_addresses: Vec::new(),
670 boot_nodes: Vec::new(),
671 node_key,
672 default_peers_set_num_full: default_peers_set.in_peers + default_peers_set.out_peers,
673 default_peers_set,
674 client_version: client_version.into(),
675 node_name: node_name.into(),
676 transport: TransportConfig::Normal { enable_mdns: false, allow_private_ip: true },
677 max_parallel_downloads: 5,
678 max_blocks_per_request: 64,
679 sync_mode: SyncMode::Full,
680 enable_dht_random_walk: true,
681 allow_non_globals_in_dht: false,
682 kademlia_disjoint_query_paths: false,
683 kademlia_replication_factor: NonZeroUsize::new(DEFAULT_KADEMLIA_REPLICATION_FACTOR)
684 .expect("value is a constant; constant is non-zero; qed."),
685 ipfs_server: false,
686 network_backend: NetworkBackendType::Litep2p,
687 }
688 }
689
690 pub fn new_local() -> NetworkConfiguration {
693 let mut config =
694 NetworkConfiguration::new("test-node", "test-client", Default::default(), None);
695
696 config.listen_addresses =
697 vec![iter::once(multiaddr::Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1)))
698 .chain(iter::once(multiaddr::Protocol::Tcp(0)))
699 .collect()];
700
701 config.allow_non_globals_in_dht = true;
702 config
703 }
704
705 pub fn new_memory() -> NetworkConfiguration {
708 let mut config =
709 NetworkConfiguration::new("test-node", "test-client", Default::default(), None);
710
711 config.listen_addresses =
712 vec![iter::once(multiaddr::Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1)))
713 .chain(iter::once(multiaddr::Protocol::Tcp(0)))
714 .collect()];
715
716 config.allow_non_globals_in_dht = true;
717 config
718 }
719}
720
721pub struct Params<Block: BlockT, H: ExHashT, N: NetworkBackend<Block, H>> {
723 pub role: Role,
725
726 pub executor: Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send + Sync>,
728
729 pub network_config: FullNetworkConfiguration<Block, H, N>,
731
732 pub protocol_id: ProtocolId,
734
735 pub genesis_hash: Block::Hash,
737
738 pub fork_id: Option<String>,
741
742 pub metrics_registry: Option<Registry>,
744
745 pub block_announce_config: N::NotificationProtocolConfig,
747
748 pub bitswap_config: Option<N::BitswapConfig>,
750
751 pub notification_metrics: NotificationMetrics,
753}
754
755pub struct FullNetworkConfiguration<B: BlockT + 'static, H: ExHashT, N: NetworkBackend<B, H>> {
757 pub(crate) notification_protocols: Vec<N::NotificationProtocolConfig>,
759
760 pub(crate) request_response_protocols: Vec<N::RequestResponseProtocolConfig>,
762
763 pub network_config: NetworkConfiguration,
765
766 peer_store: Option<N::PeerStore>,
768
769 peer_store_handle: Arc<dyn PeerStoreProvider>,
771
772 pub metrics_registry: Option<Registry>,
774}
775
776impl<B: BlockT + 'static, H: ExHashT, N: NetworkBackend<B, H>> FullNetworkConfiguration<B, H, N> {
777 pub fn new(network_config: &NetworkConfiguration, metrics_registry: Option<Registry>) -> Self {
779 let bootnodes = network_config.boot_nodes.iter().map(|bootnode| bootnode.peer_id).collect();
780 let peer_store = N::peer_store(bootnodes, metrics_registry.clone());
781 let peer_store_handle = peer_store.handle();
782
783 Self {
784 peer_store: Some(peer_store),
785 peer_store_handle,
786 notification_protocols: Vec::new(),
787 request_response_protocols: Vec::new(),
788 network_config: network_config.clone(),
789 metrics_registry,
790 }
791 }
792
793 pub fn add_notification_protocol(&mut self, config: N::NotificationProtocolConfig) {
795 self.notification_protocols.push(config);
796 }
797
798 pub fn notification_protocols(&self) -> &Vec<N::NotificationProtocolConfig> {
800 &self.notification_protocols
801 }
802
803 pub fn add_request_response_protocol(&mut self, config: N::RequestResponseProtocolConfig) {
805 self.request_response_protocols.push(config);
806 }
807
808 pub fn peer_store_handle(&self) -> Arc<dyn PeerStoreProvider> {
810 Arc::clone(&self.peer_store_handle)
811 }
812
813 pub fn take_peer_store(&mut self) -> N::PeerStore {
821 self.peer_store
822 .take()
823 .expect("`PeerStore` can only be taken once when it's started; qed")
824 }
825
826 pub fn sanity_check_addresses(&self) -> Result<(), crate::error::Error> {
828 ensure_addresses_consistent_with_transport(
829 self.network_config.listen_addresses.iter(),
830 &self.network_config.transport,
831 )?;
832 ensure_addresses_consistent_with_transport(
833 self.network_config.boot_nodes.iter().map(|x| &x.multiaddr),
834 &self.network_config.transport,
835 )?;
836 ensure_addresses_consistent_with_transport(
837 self.network_config
838 .default_peers_set
839 .reserved_nodes
840 .iter()
841 .map(|x| &x.multiaddr),
842 &self.network_config.transport,
843 )?;
844
845 for notification_protocol in &self.notification_protocols {
846 ensure_addresses_consistent_with_transport(
847 notification_protocol.set_config().reserved_nodes.iter().map(|x| &x.multiaddr),
848 &self.network_config.transport,
849 )?;
850 }
851 ensure_addresses_consistent_with_transport(
852 self.network_config.public_addresses.iter(),
853 &self.network_config.transport,
854 )?;
855
856 Ok(())
857 }
858
859 pub fn sanity_check_bootnodes(&self) -> Result<(), crate::error::Error> {
861 self.network_config.boot_nodes.iter().try_for_each(|bootnode| {
862 if let Some(other) = self
863 .network_config
864 .boot_nodes
865 .iter()
866 .filter(|o| o.multiaddr == bootnode.multiaddr)
867 .find(|o| o.peer_id != bootnode.peer_id)
868 {
869 Err(crate::error::Error::DuplicateBootnode {
870 address: bootnode.multiaddr.clone().into(),
871 first_id: bootnode.peer_id.into(),
872 second_id: other.peer_id.into(),
873 })
874 } else {
875 Ok(())
876 }
877 })
878 }
879
880 pub fn known_addresses(&self) -> Vec<(PeerId, Multiaddr)> {
882 let mut addresses: Vec<_> = self
883 .network_config
884 .default_peers_set
885 .reserved_nodes
886 .iter()
887 .map(|reserved| (reserved.peer_id, reserved.multiaddr.clone()))
888 .chain(self.notification_protocols.iter().flat_map(|protocol| {
889 protocol
890 .set_config()
891 .reserved_nodes
892 .iter()
893 .map(|reserved| (reserved.peer_id, reserved.multiaddr.clone()))
894 }))
895 .chain(
896 self.network_config
897 .boot_nodes
898 .iter()
899 .map(|bootnode| (bootnode.peer_id, bootnode.multiaddr.clone())),
900 )
901 .collect();
902
903 addresses.sort();
905 addresses.dedup();
906
907 addresses
908 }
909}
910
911#[derive(Debug, Clone, Default, Copy)]
913pub enum NetworkBackendType {
914 #[default]
918 Litep2p,
919
920 Libp2p,
927}
928
929#[cfg(test)]
930mod tests {
931 use super::*;
932 use tempfile::TempDir;
933
934 fn tempdir_with_prefix(prefix: &str) -> TempDir {
935 tempfile::Builder::new().prefix(prefix).tempdir().unwrap()
936 }
937
938 fn secret_bytes(kp: ed25519::Keypair) -> Vec<u8> {
939 kp.secret().to_bytes().into()
940 }
941
942 #[test]
943 fn test_secret_file() {
944 let tmp = tempdir_with_prefix("x");
945 std::fs::remove_dir(tmp.path()).unwrap(); let file = tmp.path().join("x").to_path_buf();
947 let kp1 = NodeKeyConfig::Ed25519(Secret::File(file.clone())).into_keypair().unwrap();
948 let kp2 = NodeKeyConfig::Ed25519(Secret::File(file.clone())).into_keypair().unwrap();
949 assert!(file.is_file() && secret_bytes(kp1) == secret_bytes(kp2))
950 }
951
952 #[test]
953 fn test_secret_input() {
954 let sk = ed25519::SecretKey::generate();
955 let kp1 = NodeKeyConfig::Ed25519(Secret::Input(sk.clone())).into_keypair().unwrap();
956 let kp2 = NodeKeyConfig::Ed25519(Secret::Input(sk)).into_keypair().unwrap();
957 assert!(secret_bytes(kp1) == secret_bytes(kp2));
958 }
959
960 #[test]
961 fn test_secret_new() {
962 let kp1 = NodeKeyConfig::Ed25519(Secret::New).into_keypair().unwrap();
963 let kp2 = NodeKeyConfig::Ed25519(Secret::New).into_keypair().unwrap();
964 assert!(secret_bytes(kp1) != secret_bytes(kp2));
965 }
966}