Skip to main content

rust_ipfs/p2p/
mod.rs

1//! P2P handling for IPFS nodes.
2
3use crate::repo::DefaultStorage;
4use crate::repo::Repo;
5use crate::IpfsOptions;
6use connexa::behaviour::peer_store::store::memory::MemoryStore;
7
8pub use behaviour::Behaviour;
9pub use behaviour::IdentifyConfiguration;
10pub use behaviour::{RateLimit, RelayConfig};
11use connexa::prelude::gossipsub::ValidationMode;
12use connexa::prelude::identify::Info as IdentifyInfo;
13use connexa::prelude::identity::Keypair;
14use connexa::prelude::identity::PublicKey;
15use connexa::prelude::swarm::NetworkBehaviour;
16use connexa::prelude::swarm::Swarm;
17use connexa::prelude::Multiaddr;
18use connexa::prelude::PeerId;
19use connexa::prelude::StreamProtocol;
20
21pub(crate) mod addr;
22pub(crate) mod addressbook;
23pub mod bitswap;
24pub(crate) mod peerbook;
25pub mod protocol;
26
27mod behaviour;
28pub use self::addressbook::Config as AddressBookConfig;
29pub use self::behaviour::BehaviourEvent;
30
31pub use addr::MultiaddrExt;
32pub use behaviour::KadResult;
33
34pub(crate) type TSwarm<C> =
35    Swarm<connexa::behaviour::Behaviour<behaviour::Behaviour<C>, MemoryStore>>;
36
37/// Abstraction of IdentifyInfo but includes PeerId
38#[derive(Clone, Debug, Eq)]
39pub struct PeerInfo {
40    /// The peer id of the user
41    pub peer_id: PeerId,
42
43    /// The public key of the local peer.
44    pub public_key: PublicKey,
45
46    /// Application-specific version of the protocol family used by the peer,
47    /// e.g. `ipfs/1.0.0` or `polkadot/1.0.0`.
48    pub protocol_version: String,
49
50    /// Name and version of the peer, similar to the `User-Agent` header in
51    /// the HTTP protocol.
52    pub agent_version: String,
53
54    /// The addresses that the peer is listening on.
55    pub listen_addrs: Vec<Multiaddr>,
56
57    /// The list of protocols supported by the peer, e.g. `/ipfs/ping/1.0.0`.
58    pub protocols: Vec<StreamProtocol>,
59
60    /// Address observed by or for the remote.
61    pub observed_addr: Option<Multiaddr>,
62}
63
64impl core::hash::Hash for PeerInfo {
65    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
66        self.peer_id.hash(state);
67        self.public_key.hash(state);
68    }
69}
70
71impl PartialEq for PeerInfo {
72    fn eq(&self, other: &Self) -> bool {
73        self.peer_id == other.peer_id && self.public_key == other.public_key
74    }
75}
76
77impl From<IdentifyInfo> for PeerInfo {
78    fn from(info: IdentifyInfo) -> Self {
79        let IdentifyInfo {
80            public_key,
81            protocol_version,
82            agent_version,
83            listen_addrs,
84            protocols,
85            observed_addr,
86            ..
87        } = info;
88        let peer_id = public_key.to_peer_id();
89        let observed_addr = Some(observed_addr);
90        Self {
91            peer_id,
92            public_key,
93            protocol_version,
94            agent_version,
95            listen_addrs,
96            protocols,
97            observed_addr,
98        }
99    }
100}
101
102#[derive(Debug, Clone, PartialEq, Eq)]
103pub struct PubsubConfig {
104    /// Custom protocol name
105    pub custom_protocol_id: Option<String>,
106
107    /// Max size that can be transmitted over gossipsub
108    pub max_transmit_size: usize,
109
110    /// Floodsub compatibility
111    pub floodsub_compat: bool,
112
113    /// Validation
114    pub validate: PubsubValidation,
115}
116
117#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
118pub enum PubsubValidation {
119    /// See [`ValidationMode::Strict`]
120    Strict,
121
122    /// See [`ValidationMode::Permissive`]
123    Permissive,
124
125    /// See [`ValidationMode::Anonymous`]
126    Anonymous,
127
128    /// See [`ValidationMode::None`]
129    Relaxed,
130}
131
132impl From<PubsubValidation> for ValidationMode {
133    fn from(validation: PubsubValidation) -> Self {
134        match validation {
135            PubsubValidation::Strict => ValidationMode::Strict,
136            PubsubValidation::Permissive => ValidationMode::Permissive,
137            PubsubValidation::Anonymous => ValidationMode::Anonymous,
138            PubsubValidation::Relaxed => ValidationMode::None,
139        }
140    }
141}
142
143impl Default for PubsubConfig {
144    fn default() -> Self {
145        Self {
146            custom_protocol_id: None,
147            max_transmit_size: 2 * 1024 * 1024,
148            validate: PubsubValidation::Strict,
149            floodsub_compat: false,
150        }
151    }
152}
153
154/// Construct new behaviour
155pub(crate) fn create_create_behaviour<C>(
156    keypair: &Keypair,
157    options: &IpfsOptions,
158    repo: &Repo<DefaultStorage>,
159    custom: Option<C>,
160) -> behaviour::Behaviour<C>
161where
162    C: NetworkBehaviour,
163    C: Send + Sync + 'static,
164    <C as NetworkBehaviour>::ToSwarm: std::fmt::Debug + Send + Sync + 'static,
165{
166    behaviour::Behaviour::new(&keypair, options, repo, custom)
167}