protocol_tx.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use async_executor::Executor;
  2. use async_std::sync::Arc;
  3. use async_trait::async_trait;
  4. use log::{debug, error, warn};
  5. use crate::{
  6. consensus::{Tx, ValidatorState, ValidatorStatePtr},
  7. net::{
  8. ChannelPtr, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
  9. ProtocolJobsManager, ProtocolJobsManagerPtr,
  10. },
  11. node::MemoryState,
  12. util::serial::serialize,
  13. Result,
  14. };
  15. pub struct ProtocolTx {
  16. tx_sub: MessageSubscription<Tx>,
  17. jobsman: ProtocolJobsManagerPtr,
  18. state: ValidatorStatePtr,
  19. p2p: P2pPtr,
  20. }
  21. impl ProtocolTx {
  22. pub async fn init(
  23. channel: ChannelPtr,
  24. state: ValidatorStatePtr,
  25. p2p: P2pPtr,
  26. ) -> Result<ProtocolBasePtr> {
  27. debug!("Adding ProtocolTx to the protocol registry");
  28. let msg_subsystem = channel.get_message_subsystem();
  29. msg_subsystem.add_dispatch::<Tx>().await;
  30. let tx_sub = channel.subscribe_msg::<Tx>().await?;
  31. Ok(Arc::new(Self {
  32. tx_sub,
  33. jobsman: ProtocolJobsManager::new("TxProtocol", channel),
  34. state,
  35. p2p,
  36. }))
  37. }
  38. async fn handle_receive_tx(self: Arc<Self>) -> Result<()> {
  39. debug!("ProtocolTx::handle_receive_tx() [START]");
  40. loop {
  41. let tx = match self.tx_sub.receive().await {
  42. Ok(v) => v,
  43. Err(e) => {
  44. error!("ProtocolTx::handle_receive_tx(): recv fail: {}", e);
  45. continue
  46. }
  47. };
  48. debug!("ProtocolTx::handle_receive_tx() recv: {:?}", tx);
  49. let tx_copy = (*tx).clone();
  50. let tx_hash = blake3::hash(&serialize(&tx_copy));
  51. let tx_in_txstore =
  52. match self.state.read().await.blockchain.transactions.contains(tx_hash) {
  53. Ok(v) => v,
  54. Err(e) => {
  55. error!("handle_receive_tx(): Failed querying txstore: {}", e);
  56. continue
  57. }
  58. };
  59. if self.state.read().await.unconfirmed_txs.contains(&tx_copy) || tx_in_txstore {
  60. debug!("ProtocolTx::handle_receive_tx(): We have already seen this tx.");
  61. continue
  62. }
  63. debug!("ProtocolTx::handle_receive_tx(): Starting state transition validation");
  64. let canon_state_clone = self.state.read().await.state_machine.lock().await.clone();
  65. let mem_state = MemoryState::new(canon_state_clone);
  66. match ValidatorState::validate_state_transitions(mem_state, &[tx_copy.clone()]) {
  67. Ok(_) => debug!("ProtocolTx::handle_receive_tx(): State transition valid"),
  68. Err(e) => {
  69. warn!("ProtocolTx::handle_receive_tx(): State transition fail: {}", e);
  70. continue
  71. }
  72. }
  73. // Nodes use unconfirmed_txs vector as seen_txs pool.
  74. if self.state.write().await.append_tx(tx_copy.clone()) {
  75. match self.p2p.broadcast(tx_copy).await {
  76. Ok(()) => {}
  77. Err(e) => {
  78. error!("handle_receive_tx(): p2p broadcast fail: {}", e);
  79. continue
  80. }
  81. };
  82. }
  83. }
  84. }
  85. }
  86. #[async_trait]
  87. impl ProtocolBase for ProtocolTx {
  88. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  89. debug!("ProtocolTx::start() [START]");
  90. self.jobsman.clone().start(executor.clone());
  91. self.jobsman.clone().spawn(self.clone().handle_receive_tx(), executor.clone()).await;
  92. debug!("ProtocolTx::start() [END]");
  93. Ok(())
  94. }
  95. fn name(&self) -> &'static str {
  96. "ProtocolTx"
  97. }
  98. }