mod.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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. use super::{
  19. p2p::P2pPtr,
  20. session::{SESSION_ALL, SESSION_SEED},
  21. };
  22. /// Manages the tasks for the network protocol. Used by other connection
  23. /// protocols to handle asynchronous task execution across the network.
  24. /// Runs all tasks that are handed to it on an executor that has stopping
  25. /// functionality.
  26. pub mod protocol_jobs_manager;
  27. /// Protocol for version information handshake between nodes at the start
  28. /// of a connection. This is the first step when establishing a p2p conn.
  29. ///
  30. /// The version protocol starts by instantiating the protocol and creating
  31. /// a new subscription to version and version acknowledgement messages.
  32. /// Then we run the protocol. Nodes send a version message and wait for a
  33. /// version acknowledgement, while asynchronously waiting for version info
  34. /// from the other node and sending the version acknowledgement.
  35. pub mod protocol_version;
  36. pub use protocol_version::ProtocolVersion;
  37. /// Protocol for ping-pong keepalive messages. Implements ping message and
  38. /// pong response. These messages are like the network heartbeat - they are
  39. /// sent continually between nodes, to ensure each node is still alive and
  40. /// active. Ping-pong messages ensure that the network doesn't time out.
  41. pub mod protocol_ping;
  42. pub use protocol_ping::ProtocolPing;
  43. /// Protocol for address and get-address messages. Implements how nodes
  44. /// exchange connection information about other nodes on the network.
  45. /// Address and get-address messages are exchanged continually alongside
  46. /// ping-pong messages as part of a network connection.
  47. ///
  48. /// Protocol starts by creating a subscription to address and get-address
  49. /// messages. Then the protocol sends out a get-address message and waits
  50. /// for an address message. Upon receiving address messages, nodes validate
  51. /// and add the address information to their local store.
  52. pub mod protocol_address;
  53. pub use protocol_address::ProtocolAddress;
  54. /// Seed server protocol. Seed server is used when connecting to the network
  55. /// for the first time. Returns a list of peers that nodes can connect to.
  56. ///
  57. /// To start the seed protocol, we create a subscription to the address
  58. /// message, and send our address to the seed server. Then we send a
  59. /// get-address message and receive an address message. We add these addresses
  60. /// to our internal store.
  61. pub mod protocol_seed;
  62. pub use protocol_seed::ProtocolSeed;
  63. /// Base trait for implementing P2P protocols
  64. pub mod protocol_base;
  65. /// Interface for registering arbitrary P2P protocols
  66. pub mod protocol_registry;
  67. /// Register the default network protocols for a p2p instance.
  68. pub async fn register_default_protocols(p2p: P2pPtr) {
  69. let registry = p2p.protocol_registry();
  70. registry.register(SESSION_ALL, ProtocolPing::init).await;
  71. registry.register(!SESSION_SEED, ProtocolAddress::init).await;
  72. registry.register(SESSION_SEED, ProtocolSeed::init).await;
  73. }