swarm_nl/
prelude.rs

1// Copyright 2024 Algorealm, Inc.
2// Apache 2.0 License
3
4//! Types and traits that are used throughout SwarmNL.
5
6use libp2p_identity::KeyType;
7use std::{collections::HashMap, net::Ipv4Addr};
8use thiserror::Error;
9
10/// Default IP address when no address is specified.
11pub static DEFAULT_IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(0, 0, 0, 0);
12
13/// Default amount of time to keep a connection alive.
14pub static DEFAULT_KEEP_ALIVE_DURATION: Seconds = 60;
15
16/// Library error type containing all custom errors that could be encountered.
17#[derive(Error, Debug)]
18pub enum SwarmNlError {
19	#[error("could not read bootstrap config file")]
20	BoostrapFileReadError(String),
21	#[error("could not parse data read from bootstrap config file")]
22	BoostrapDataParseError(String),
23	#[error("could not configure transport. It is likely not supported on machine")]
24	TransportConfigError(TransportOpts),
25	#[error("could not configure DNS resolution into transport")]
26	DNSConfigError,
27	#[error("could not configure the selected protocols")]
28	ProtocolConfigError,
29	#[error("could not listen on specified address")]
30	MultiaddressListenError(String),
31	#[error("could not dial remote peer")]
32	RemotePeerDialError(String),
33	#[error("could not parse provided network id")]
34	NetworkIdParseError(String),
35	#[error("could not configure node for gossiping")]
36	GossipConfigError,
37}
38
39/// Generic SwarmNl result type.
40pub type SwarmNlResult<T> = Result<T, SwarmNlError>;
41/// Port type.
42pub type Port = u16;
43/// Seconds type.
44pub type Seconds = u64;
45/// The stringified `PeerId` type.
46pub type PeerIdString = String;
47/// The stringified `Multiaddr` type.
48pub type MultiaddrString = String;
49/// A collection of nodes described purely by their addresses.
50pub type Nodes = HashMap<PeerIdString, MultiaddrString>;
51
52/// Lower bound port range.
53pub const MIN_PORT: u16 = 1000;
54/// Upper bound port range (u16::MAX).
55pub const MAX_PORT: u16 = 65535;
56
57/// Default network ID.
58pub static DEFAULT_NETWORK_ID: &str = "/swarmnl/1.0";
59/// This constant sets the shortest acceptable length for a network ID.
60/// The network ID identifies a network and ensures it's distinct from others.
61pub static MIN_NETWORK_ID_LENGTH: u8 = 4;
62
63/// An implementation of [`From<&str>`] for [`KeyType`] to read a key type from a bootstrap config
64/// file.
65///
66/// We define a custom trait because of the Rust visibility rule.
67pub trait CustomFrom {
68	fn from(string: &str) -> Option<Self>
69	where
70		Self: Sized;
71}
72
73impl CustomFrom for KeyType {
74	fn from(s: &str) -> Option<Self> {
75		match s.to_lowercase().as_str() {
76			"ed25519" => Some(KeyType::Ed25519),
77			"rsa" => Some(KeyType::RSA),
78			"secp256k1" => Some(KeyType::Secp256k1),
79			"ecdsa" => Some(KeyType::Ecdsa),
80			_ => None,
81		}
82	}
83}
84
85/// Supported transport protocols.
86#[derive(Hash, Eq, PartialEq, Debug, Clone)]
87pub enum TransportOpts {
88	/// QUIC transport protocol enabled with TCP/IP as fallback. DNS lookup is also configured by
89	/// default.
90	TcpQuic { tcp_config: TcpConfig },
91}
92
93/// TCP setup configuration.
94#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy)]
95pub enum TcpConfig {
96	/// Default configuration specified in the [libp2p docs](https://docs.rs/libp2p/latest/libp2p/tcp/struct.Config.html#method.new).
97	Default,
98	Custom {
99		/// Configures the IP_TTL option for new sockets.
100		ttl: u32,
101		/// Configures the TCP_NODELAY option for new sockets.
102		nodelay: bool,
103		/// Configures the listen backlog for new listen sockets.
104		backlog: u32,
105		// false by default, we're not dealing with NAT traversal for now.
106		// port_resuse: bool
107	},
108}