darkfi/net/protocol/mod.rs
1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use super::{
20 p2p::P2pPtr,
21 session::{SESSION_DEFAULT, SESSION_SEED},
22};
23
24/// Manages the tasks for the network protocol.
25///
26/// Used by other connection protocols to handle asynchronous task execution
27/// across the network. Runs all tasks that are handed to it on an executor
28/// that has stopping functionality.
29pub mod protocol_jobs_manager;
30
31/// Protocol for version information handshake between nodes at the start
32/// of a connection. This is the first step when establishing a p2p conn.
33///
34/// The version protocol starts by instantiating the protocol and creating
35/// a new subscription to version and version acknowledgement messages.
36/// Then we run the protocol. Nodes send a version message and wait for a
37/// version acknowledgement, while asynchronously waiting for version info
38/// from the other node and sending the version acknowledgement.
39pub mod protocol_version;
40pub use protocol_version::ProtocolVersion;
41
42/// Protocol for ping-pong keepalive messages.
43///
44/// Implements ping message and pong response. These messages are like the
45/// network heartbeat - they are sent continually between nodes, to ensure
46/// each node is still alive and active. Ping-pong messages ensure that the
47/// network doesn't time out.
48pub mod protocol_ping;
49pub use protocol_ping::ProtocolPing;
50
51/// Protocol for address and get-address messages.
52///
53/// Implements how nodes exchange connection information about other nodes
54/// on the network. Address and get-address messages are exchanged continually
55/// alongside ping-pong messages as part of a network connection.
56///
57/// Protocol starts by creating a subscription to address and get-address
58/// messages. Then the protocol sends out a get-address message and waits
59/// for an address message. Upon receiving address messages, nodes validate
60/// and add the address information to their local store.
61pub mod protocol_address;
62pub use protocol_address::ProtocolAddress;
63
64/// Seed server protocol. Seed server is used when connecting to the network
65/// for the first time. Returns a list of peers that nodes can connect to.
66///
67/// To start the seed protocol, we create a subscription to the address
68/// message, and send our address to the seed server. Then we send a
69/// get-address message and receive an address message. We add these addresses
70/// to our internal store.
71pub mod protocol_seed;
72pub use protocol_seed::ProtocolSeed;
73
74/// Generic protocol to receive specified structure messages.
75///
76/// Acts as a simple message queue, where we listen for the specified
77/// structure message, and when one is received, we send it to the provided
78/// smol channel. Afterwards, we wait for an action signal, specifying whether
79/// or not we should propagate the message to rest nodes or skip it.
80pub mod protocol_generic;
81
82/// Base trait for implementing P2P protocols
83pub mod protocol_base;
84/// Interface for registering arbitrary P2P protocols
85pub mod protocol_registry;
86
87/// Register the default network protocols for a p2p instance.
88pub async fn register_default_protocols(p2p: P2pPtr) {
89 let registry = p2p.protocol_registry();
90 registry.register(SESSION_DEFAULT | SESSION_SEED, ProtocolPing::init).await;
91 registry.register(SESSION_DEFAULT, ProtocolAddress::init).await;
92 registry.register(SESSION_SEED, ProtocolSeed::init).await;
93}