protocol_tx.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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;
  19. use async_trait::async_trait;
  20. use log::debug;
  21. use smol::Executor;
  22. use url::Url;
  23. use crate::{
  24. impl_p2p_message,
  25. net::{
  26. ChannelPtr, Message, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
  27. ProtocolJobsManager, ProtocolJobsManagerPtr,
  28. },
  29. tx::Transaction,
  30. validator::ValidatorPtr,
  31. Result,
  32. };
  33. impl_p2p_message!(Transaction, "tx");
  34. pub struct ProtocolTx {
  35. tx_sub: MessageSubscription<Transaction>,
  36. jobsman: ProtocolJobsManagerPtr,
  37. validator: ValidatorPtr,
  38. p2p: P2pPtr,
  39. channel_address: Url,
  40. }
  41. impl ProtocolTx {
  42. pub async fn init(
  43. channel: ChannelPtr,
  44. validator: ValidatorPtr,
  45. p2p: P2pPtr,
  46. ) -> Result<ProtocolBasePtr> {
  47. debug!(
  48. target: "validator::protocol_tx::init",
  49. "Adding ProtocolTx to the protocol registry"
  50. );
  51. let msg_subsystem = channel.message_subsystem();
  52. msg_subsystem.add_dispatch::<Transaction>().await;
  53. let tx_sub = channel.subscribe_msg::<Transaction>().await?;
  54. Ok(Arc::new(Self {
  55. tx_sub,
  56. jobsman: ProtocolJobsManager::new("TxProtocol", channel.clone()),
  57. validator,
  58. p2p,
  59. channel_address: channel.address().clone(),
  60. }))
  61. }
  62. async fn handle_receive_tx(self: Arc<Self>) -> Result<()> {
  63. debug!(
  64. target: "validator::protocol_tx::handle_receive_tx",
  65. "START"
  66. );
  67. let exclude_list = vec![self.channel_address.clone()];
  68. loop {
  69. let tx = match self.tx_sub.receive().await {
  70. Ok(v) => v,
  71. Err(e) => {
  72. debug!(
  73. target: "validator::protocol_tx::handle_receive_tx",
  74. "recv fail: {}",
  75. e
  76. );
  77. continue
  78. }
  79. };
  80. // Check if node has finished syncing its blockchain
  81. if !self.validator.read().await.synced {
  82. debug!(
  83. target: "validator::protocol_tx::handle_receive_tx",
  84. "Node still syncing blockchain, skipping..."
  85. );
  86. continue
  87. }
  88. let tx_copy = (*tx).clone();
  89. // Nodes use unconfirmed_txs vector as seen_txs pool.
  90. match self.validator.write().await.append_tx(tx_copy.clone()).await {
  91. Ok(()) => self.p2p.broadcast_with_exclude(&tx_copy, &exclude_list).await,
  92. Err(e) => {
  93. debug!(
  94. target: "validator::protocol_tx::handle_receive_tx",
  95. "append_tx fail: {}",
  96. e
  97. );
  98. }
  99. }
  100. }
  101. }
  102. }
  103. #[async_trait]
  104. impl ProtocolBase for ProtocolTx {
  105. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  106. debug!(target: "validator::protocol_tx::start", "START");
  107. self.jobsman.clone().start(executor.clone());
  108. self.jobsman.clone().spawn(self.clone().handle_receive_tx(), executor.clone()).await;
  109. debug!(target: "validator::protocol_tx::start", "END");
  110. Ok(())
  111. }
  112. fn name(&self) -> &'static str {
  113. "ProtocolTx"
  114. }
  115. }