channel.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 async_std::sync::{Arc, Mutex};
  19. use futures::{
  20. io::{ReadHalf, WriteHalf},
  21. AsyncReadExt,
  22. };
  23. use log::{debug, error, info};
  24. use rand::Rng;
  25. use serde_json::json;
  26. use smol::Executor;
  27. use url::Url;
  28. use super::{
  29. message,
  30. message_subscriber::{MessageSubscription, MessageSubsystem},
  31. transport::TransportStream,
  32. Session, SessionBitflag, SessionWeakPtr,
  33. };
  34. use crate::{
  35. system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription},
  36. util::time::NanoTimestamp,
  37. Error, Result,
  38. };
  39. /// Atomic pointer to async channel.
  40. pub type ChannelPtr = Arc<Channel>;
  41. struct ChannelInfo {
  42. random_id: u32,
  43. remote_node_id: String,
  44. last_msg: String,
  45. last_status: String,
  46. // Message log which is cleared on querying get_info
  47. log: Option<Mutex<Vec<(NanoTimestamp, String, String)>>>,
  48. }
  49. impl ChannelInfo {
  50. fn new(channel_log: bool) -> Self {
  51. let log = match channel_log {
  52. true => Some(Mutex::new(Vec::new())),
  53. false => None,
  54. };
  55. Self {
  56. random_id: rand::thread_rng().gen(),
  57. remote_node_id: String::new(),
  58. last_msg: String::new(),
  59. last_status: String::new(),
  60. log,
  61. }
  62. }
  63. // ANCHOR: get_info
  64. async fn get_info(&self) -> serde_json::Value {
  65. let log = match &self.log {
  66. Some(l) => {
  67. let mut lock = l.lock().await;
  68. let ret = lock.clone();
  69. *lock = Vec::new();
  70. ret
  71. }
  72. None => vec![],
  73. };
  74. json!({
  75. "random_id": self.random_id,
  76. "remote_node_id": self.remote_node_id,
  77. "last_msg": self.last_msg,
  78. "last_status": self.last_status,
  79. "log": log,
  80. })
  81. }
  82. // ANCHOR_END: get_info
  83. }
  84. /// Async channel for communication between nodes.
  85. pub struct Channel {
  86. reader: Mutex<ReadHalf<Box<dyn TransportStream>>>,
  87. writer: Mutex<WriteHalf<Box<dyn TransportStream>>>,
  88. address: Url,
  89. message_subsystem: MessageSubsystem,
  90. stop_subscriber: SubscriberPtr<Error>,
  91. receive_task: StoppableTaskPtr,
  92. stopped: Mutex<bool>,
  93. info: Mutex<ChannelInfo>,
  94. session: SessionWeakPtr,
  95. }
  96. impl Channel {
  97. /// Sets up a new channel. Creates a reader and writer TCP stream and
  98. /// summons the message subscriber subsystem. Performs a network
  99. /// handshake on the subsystem dispatchers.
  100. pub async fn new(
  101. stream: Box<dyn TransportStream>,
  102. address: Url,
  103. session: SessionWeakPtr,
  104. ) -> Arc<Self> {
  105. let (reader, writer) = stream.split();
  106. let reader = Mutex::new(reader);
  107. let writer = Mutex::new(writer);
  108. let message_subsystem = MessageSubsystem::new();
  109. Self::setup_dispatchers(&message_subsystem).await;
  110. let channel_log = session.upgrade().unwrap().p2p().settings().channel_log;
  111. Arc::new(Self {
  112. reader,
  113. writer,
  114. address,
  115. message_subsystem,
  116. stop_subscriber: Subscriber::new(),
  117. receive_task: StoppableTask::new(),
  118. stopped: Mutex::new(false),
  119. info: Mutex::new(ChannelInfo::new(channel_log)),
  120. session,
  121. })
  122. }
  123. pub async fn get_info(&self) -> serde_json::Value {
  124. self.info.lock().await.get_info().await
  125. }
  126. /// Starts the channel. Runs a receive loop to start receiving messages or
  127. /// handles a network failure.
  128. pub fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) {
  129. debug!(target: "net::channel::start()", "START, address={}", self.address());
  130. let self2 = self.clone();
  131. self.receive_task.clone().start(
  132. self.clone().main_receive_loop(),
  133. |result| self2.handle_stop(result),
  134. Error::NetworkServiceStopped,
  135. executor,
  136. );
  137. debug!(target: "net::channel::start()", "END, address={}", self.address());
  138. }
  139. /// Stops the channel. Steps through each component of the channel
  140. /// connection and sends a stop signal. Notifies all subscribers that
  141. /// the channel has been closed.
  142. pub async fn stop(&self) {
  143. debug!(target: "net::channel::stop()", "START, address={}", self.address());
  144. if !(*self.stopped.lock().await) {
  145. *self.stopped.lock().await = true;
  146. self.stop_subscriber.notify(Error::ChannelStopped).await;
  147. self.receive_task.stop().await;
  148. self.message_subsystem.trigger_error(Error::ChannelStopped).await;
  149. debug!(target: "net::channel::stop()", "END, address={}", self.address());
  150. }
  151. }
  152. /// Creates a subscription to a stopped signal.
  153. /// If the channel is stopped then this will return a ChannelStopped error.
  154. pub async fn subscribe_stop(&self) -> Result<Subscription<Error>> {
  155. debug!(target: "net::channel::subscribe_stop()", "START, address={}", self.address());
  156. {
  157. let stopped = *self.stopped.lock().await;
  158. if stopped {
  159. return Err(Error::ChannelStopped)
  160. }
  161. }
  162. let sub = self.stop_subscriber.clone().subscribe().await;
  163. debug!(target: "net::channel::subscribe_stop()", "END, address={}", self.address());
  164. Ok(sub)
  165. }
  166. /// Sends a message across a channel. Calls function 'send_message' that
  167. /// creates a new payload and sends it over the TCP connection as a
  168. /// packet. Returns an error if something goes wrong.
  169. pub async fn send<M: message::Message>(&self, message: M) -> Result<()> {
  170. debug!(
  171. target: "net::channel::send()",
  172. "START, command={:?}, address={}",
  173. M::name(),
  174. self.address()
  175. );
  176. {
  177. let stopped = *self.stopped.lock().await;
  178. if stopped {
  179. return Err(Error::ChannelStopped)
  180. }
  181. }
  182. // Catch failure and stop channel, return a net error
  183. let result = match self.send_message(message).await {
  184. Ok(()) => Ok(()),
  185. Err(err) => {
  186. error!(target: "net::channel::send()", "Channel send error for [{}]: {}", self.address(), err);
  187. self.stop().await;
  188. Err(Error::ChannelStopped)
  189. }
  190. };
  191. debug!(
  192. target: "net::channel::send()",
  193. "END, command={:?}, address={}",
  194. M::name(),
  195. self.address()
  196. );
  197. {
  198. let info = &mut *self.info.lock().await;
  199. info.last_msg = M::name().to_string();
  200. info.last_status = "sent".to_string();
  201. }
  202. result
  203. }
  204. /// Implements send message functionality. Creates a new payload and encodes
  205. /// it. Then creates a message packet- the base type of the network- and
  206. /// copies the payload into it. Then we send the packet over the TCP
  207. /// stream.
  208. async fn send_message<M: message::Message>(&self, message: M) -> Result<()> {
  209. let mut payload = Vec::new();
  210. message.encode(&mut payload)?;
  211. let packet = message::Packet { command: String::from(M::name()), payload };
  212. let time = NanoTimestamp::current_time();
  213. //let time = time::unix_timestamp()?;
  214. {
  215. let info = &mut *self.info.lock().await;
  216. if let Some(l) = &info.log {
  217. l.lock().await.push((time, "send".to_string(), packet.command.clone()));
  218. };
  219. }
  220. let stream = &mut *self.writer.lock().await;
  221. message::send_packet(stream, packet).await
  222. }
  223. /// Subscribe to a messages on the message subsystem.
  224. pub async fn subscribe_msg<M: message::Message>(&self) -> Result<MessageSubscription<M>> {
  225. debug!(
  226. target: "net::channel::subscribe_msg()",
  227. "START, command={:?}, address={}",
  228. M::name(),
  229. self.address()
  230. );
  231. let sub = self.message_subsystem.subscribe::<M>().await;
  232. debug!(
  233. target: "net::channel::subscribe_msg()",
  234. "END, command={:?}, address={}",
  235. M::name(),
  236. self.address()
  237. );
  238. sub
  239. }
  240. /// Return the local socket address.
  241. pub fn address(&self) -> Url {
  242. self.address.clone()
  243. }
  244. pub async fn remote_node_id(&self) -> String {
  245. self.info.lock().await.remote_node_id.clone()
  246. }
  247. pub async fn set_remote_node_id(&self, remote_node_id: String) {
  248. self.info.lock().await.remote_node_id = remote_node_id;
  249. }
  250. /// End of file error. Triggered when unexpected end of file occurs.
  251. fn is_eof_error(err: Error) -> bool {
  252. match err {
  253. Error::Io(io_err) => io_err == std::io::ErrorKind::UnexpectedEof,
  254. _ => false,
  255. }
  256. }
  257. /// Perform network handshake for message subsystem dispatchers.
  258. async fn setup_dispatchers(message_subsystem: &MessageSubsystem) {
  259. message_subsystem.add_dispatch::<message::VersionMessage>().await;
  260. message_subsystem.add_dispatch::<message::VerackMessage>().await;
  261. message_subsystem.add_dispatch::<message::PingMessage>().await;
  262. message_subsystem.add_dispatch::<message::PongMessage>().await;
  263. message_subsystem.add_dispatch::<message::GetAddrsMessage>().await;
  264. message_subsystem.add_dispatch::<message::AddrsMessage>().await;
  265. message_subsystem.add_dispatch::<message::ExtAddrsMessage>().await;
  266. }
  267. /// Convenience function that returns the Message Subsystem.
  268. pub fn get_message_subsystem(&self) -> &MessageSubsystem {
  269. &self.message_subsystem
  270. }
  271. /// Run the receive loop. Start receiving messages or handle network
  272. /// failure.
  273. async fn main_receive_loop(self: Arc<Self>) -> Result<()> {
  274. debug!(target: "net::channel::main_receive_loop()", "START, address={}", self.address());
  275. let reader = &mut *self.reader.lock().await;
  276. loop {
  277. let packet = match message::read_packet(reader).await {
  278. Ok(packet) => packet,
  279. Err(err) => {
  280. if Self::is_eof_error(err.clone()) {
  281. info!(
  282. target: "net::channel::main_receive_loop()",
  283. "Inbound connection {} disconnected",
  284. self.address()
  285. );
  286. } else {
  287. error!(
  288. target: "net::channel::main_receive_loop()",
  289. "Read error on channel {}: {}",
  290. self.address(),
  291. err
  292. );
  293. }
  294. debug!(
  295. target: "net::channel::main_receive_loop()",
  296. "Channel::receive_loop() stopping channel {}",
  297. self.address()
  298. );
  299. self.stop().await;
  300. return Err(Error::ChannelStopped)
  301. }
  302. };
  303. {
  304. let info = &mut *self.info.lock().await;
  305. info.last_msg = packet.command.clone();
  306. info.last_status = "recv".to_string();
  307. let time = NanoTimestamp::current_time();
  308. //let time = time::unix_timestamp()?;
  309. if let Some(l) = &info.log {
  310. l.lock().await.push((time, "recv".to_string(), packet.command.clone()));
  311. };
  312. }
  313. // Send result to our subscribers
  314. self.message_subsystem.notify(&packet.command, packet.payload).await;
  315. }
  316. }
  317. /// Handle network errors. Panic if error passes silently, otherwise
  318. /// broadcast the error.
  319. async fn handle_stop(self: Arc<Self>, result: Result<()>) {
  320. debug!(
  321. target: "net::channel::handle_stop()",
  322. "START, address={}",
  323. self.address()
  324. );
  325. match result {
  326. Ok(()) => panic!("Channel task should never complete without error status"),
  327. Err(err) => {
  328. // Send this error to all channel subscribers
  329. self.message_subsystem.trigger_error(err).await;
  330. }
  331. }
  332. debug!(
  333. target: "net::channel::handle_stop()",
  334. "END, address={}",
  335. self.address()
  336. );
  337. }
  338. fn session(&self) -> Arc<dyn Session> {
  339. self.session.upgrade().unwrap()
  340. }
  341. pub fn session_type_id(&self) -> SessionBitflag {
  342. let session = self.session();
  343. session.type_id()
  344. }
  345. }