p2p.rs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 std::sync::{
  19. atomic::{AtomicBool, Ordering},
  20. Arc,
  21. };
  22. use futures::{stream::FuturesUnordered, TryFutureExt};
  23. use futures_rustls::rustls::crypto::{ring, CryptoProvider};
  24. use log::{debug, error, info, warn};
  25. use smol::{
  26. fs::{self, unix::PermissionsExt},
  27. lock::RwLock as AsyncRwLock,
  28. stream::StreamExt,
  29. };
  30. use url::Url;
  31. use super::{
  32. channel::ChannelPtr,
  33. dnet::DnetEvent,
  34. hosts::{Hosts, HostsPtr},
  35. message::Message,
  36. protocol::{protocol_registry::ProtocolRegistry, register_default_protocols},
  37. session::{
  38. InboundSession, InboundSessionPtr, ManualSession, ManualSessionPtr, OutboundSession,
  39. OutboundSessionPtr, RefineSession, RefineSessionPtr, SeedSyncSession, SeedSyncSessionPtr,
  40. },
  41. settings::Settings,
  42. };
  43. use crate::{
  44. system::{ExecutorPtr, Publisher, PublisherPtr, Subscription},
  45. util::path::expand_path,
  46. Result,
  47. };
  48. /// Atomic pointer to the p2p interface
  49. pub type P2pPtr = Arc<P2p>;
  50. /// Toplevel peer-to-peer networking interface
  51. pub struct P2p {
  52. /// Global multithreaded executor reference
  53. executor: ExecutorPtr,
  54. /// Known hosts (peers)
  55. hosts: HostsPtr,
  56. /// Protocol registry
  57. protocol_registry: ProtocolRegistry,
  58. /// P2P network settings
  59. settings: Arc<AsyncRwLock<Settings>>,
  60. /// Reference to configured [`ManualSession`]
  61. session_manual: ManualSessionPtr,
  62. /// Reference to configured [`InboundSession`]
  63. session_inbound: InboundSessionPtr,
  64. /// Reference to configured [`OutboundSession`]
  65. session_outbound: OutboundSessionPtr,
  66. /// Reference to configured [`RefineSession`]
  67. session_refine: RefineSessionPtr,
  68. /// Reference to configured [`SeedSyncSession`]
  69. session_seedsync: SeedSyncSessionPtr,
  70. /// Enable network debugging
  71. pub dnet_enabled: AtomicBool,
  72. /// The publisher for which we can give dnet info over
  73. dnet_publisher: PublisherPtr<DnetEvent>,
  74. }
  75. impl P2p {
  76. /// Initialize a new p2p network.
  77. ///
  78. /// Initializes all sessions and protocols. Adds the protocols to the protocol
  79. /// registry, along with a bitflag session selector that includes or excludes
  80. /// sessions from seed, version, and address protocols.
  81. ///
  82. /// Creates a weak pointer to self that is used by all sessions to access the
  83. /// p2p parent class.
  84. pub async fn new(settings: Settings, executor: ExecutorPtr) -> Result<P2pPtr> {
  85. // Create the datastore
  86. if let Some(ref datastore) = settings.datastore {
  87. let datastore = expand_path(datastore)?;
  88. fs::create_dir_all(&datastore).await?;
  89. fs::set_permissions(&datastore, PermissionsExt::from_mode(0o700)).await?;
  90. }
  91. // Register a CryptoProvider for rustls
  92. let _ = CryptoProvider::install_default(ring::default_provider());
  93. // Wrap the Settings into an Arc<RwLock>
  94. let settings = Arc::new(AsyncRwLock::new(settings));
  95. let self_ = Arc::new(Self {
  96. executor,
  97. hosts: Hosts::new(Arc::clone(&settings)),
  98. protocol_registry: ProtocolRegistry::new(),
  99. settings,
  100. session_manual: ManualSession::new(),
  101. session_inbound: InboundSession::new(),
  102. session_outbound: OutboundSession::new(),
  103. session_refine: RefineSession::new(),
  104. session_seedsync: SeedSyncSession::new(),
  105. dnet_enabled: AtomicBool::new(false),
  106. dnet_publisher: Publisher::new(),
  107. });
  108. self_.session_inbound.p2p.init(self_.clone());
  109. self_.session_manual.p2p.init(self_.clone());
  110. self_.session_seedsync.p2p.init(self_.clone());
  111. self_.session_outbound.p2p.init(self_.clone());
  112. self_.session_refine.p2p.init(self_.clone());
  113. register_default_protocols(self_.clone()).await;
  114. Ok(self_)
  115. }
  116. /// Starts inbound, outbound, and manual sessions.
  117. pub async fn start(self: Arc<Self>) -> Result<()> {
  118. debug!(target: "net::p2p::start", "P2P::start() [BEGIN]");
  119. info!(target: "net::p2p::start", "[P2P] Starting P2P subsystem");
  120. // Start the inbound session
  121. if let Err(err) = self.session_inbound().start().await {
  122. error!(target: "net::p2p::start", "Failed to start inbound session!: {}", err);
  123. return Err(err)
  124. }
  125. // Start the manual session
  126. self.session_manual().start().await;
  127. // Start the seedsync session. Seed connections will not
  128. // activate yet- they wait for a call to notify().
  129. self.session_seedsync().start().await;
  130. // Start the outbound session
  131. self.session_outbound().start().await;
  132. // Start the refine session
  133. self.session_refine().start().await;
  134. info!(target: "net::p2p::start", "[P2P] P2P subsystem started");
  135. Ok(())
  136. }
  137. /// Reseed the P2P network.
  138. pub async fn seed(self: Arc<Self>) {
  139. debug!(target: "net::p2p::seed()", "P2P::seed() [BEGIN]");
  140. info!(target: "net::p2p::seed()", "[P2P] Seeding P2P subsystem");
  141. // Activate the seed session.
  142. self.session_seedsync().notify().await;
  143. debug!(target: "net::p2p::seed()", "P2P::seed() [END]");
  144. }
  145. /// Stop the running P2P subsystem
  146. pub async fn stop(&self) {
  147. // Stop the sessions
  148. self.session_manual().stop().await;
  149. self.session_inbound().stop().await;
  150. self.session_seedsync().stop().await;
  151. self.session_outbound().stop().await;
  152. self.session_refine().stop().await;
  153. }
  154. /// Broadcasts a message concurrently across all active channels.
  155. pub async fn broadcast<M: Message>(&self, message: &M) {
  156. self.broadcast_with_exclude(message, &[]).await
  157. }
  158. /// Broadcasts a message concurrently across active channels, excluding
  159. /// the ones provided in `exclude_list`.
  160. pub async fn broadcast_with_exclude<M: Message>(&self, message: &M, exclude_list: &[Url]) {
  161. let mut channels = Vec::new();
  162. for channel in self.hosts().channels() {
  163. if exclude_list.contains(channel.address()) {
  164. continue
  165. }
  166. channels.push(channel);
  167. }
  168. self.broadcast_to(message, &channels).await
  169. }
  170. /// Broadcast a message concurrently to all given peers.
  171. pub async fn broadcast_to<M: Message>(&self, message: &M, channel_list: &[ChannelPtr]) {
  172. if channel_list.is_empty() {
  173. warn!(target: "net::p2p::broadcast()", "[P2P] No connected channels found for broadcast");
  174. return
  175. }
  176. let futures = FuturesUnordered::new();
  177. for channel in channel_list {
  178. futures.push(channel.send(message).map_err(|e| {
  179. error!(
  180. target: "net::p2p::broadcast()",
  181. "[P2P] Broadcasting message to {} failed: {}",
  182. channel.address(), e
  183. );
  184. // If the channel is stopped then it should automatically die
  185. // and the session will remove it from p2p.
  186. assert!(channel.is_stopped());
  187. }));
  188. }
  189. let _results: Vec<_> = futures.collect().await;
  190. }
  191. pub fn is_connected(&self) -> bool {
  192. !self.hosts().channels().is_empty()
  193. }
  194. /// Return an atomic pointer to the set network settings
  195. pub fn settings(&self) -> Arc<AsyncRwLock<Settings>> {
  196. Arc::clone(&self.settings)
  197. }
  198. /// Return an atomic pointer to the list of hosts
  199. pub fn hosts(&self) -> HostsPtr {
  200. self.hosts.clone()
  201. }
  202. /// Reference the global executor
  203. pub fn executor(&self) -> ExecutorPtr {
  204. self.executor.clone()
  205. }
  206. /// Return a reference to the internal protocol registry
  207. pub fn protocol_registry(&self) -> &ProtocolRegistry {
  208. &self.protocol_registry
  209. }
  210. /// Get pointer to manual session
  211. pub fn session_manual(&self) -> ManualSessionPtr {
  212. self.session_manual.clone()
  213. }
  214. /// Get pointer to inbound session
  215. pub fn session_inbound(&self) -> InboundSessionPtr {
  216. self.session_inbound.clone()
  217. }
  218. /// Get pointer to outbound session
  219. pub fn session_outbound(&self) -> OutboundSessionPtr {
  220. self.session_outbound.clone()
  221. }
  222. /// Get pointer to refine session
  223. pub fn session_refine(&self) -> RefineSessionPtr {
  224. self.session_refine.clone()
  225. }
  226. /// Get pointer to seedsync session
  227. pub fn session_seedsync(&self) -> SeedSyncSessionPtr {
  228. self.session_seedsync.clone()
  229. }
  230. /// Enable network debugging
  231. pub fn dnet_enable(&self) {
  232. self.dnet_enabled.store(true, Ordering::SeqCst);
  233. warn!("[P2P] Network debugging enabled!");
  234. }
  235. /// Disable network debugging
  236. pub fn dnet_disable(&self) {
  237. self.dnet_enabled.store(false, Ordering::SeqCst);
  238. warn!("[P2P] Network debugging disabled!");
  239. }
  240. /// Subscribe to dnet events
  241. pub async fn dnet_subscribe(&self) -> Subscription<DnetEvent> {
  242. self.dnet_publisher.clone().subscribe().await
  243. }
  244. /// Send a dnet notification over the publisher
  245. pub(super) async fn dnet_notify(&self, event: DnetEvent) {
  246. self.dnet_publisher.notify(event).await;
  247. }
  248. }