mod.rs 4.0 KB

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