protocol_tx.rs 4.4 KB

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