protocol_tx.rs 3.7 KB

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