protocol_proposal.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 darkfi::{
  24. impl_p2p_message,
  25. net::{
  26. ChannelPtr, Message, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
  27. ProtocolJobsManager, ProtocolJobsManagerPtr,
  28. },
  29. rpc::jsonrpc::JsonSubscriber,
  30. validator::{consensus::Proposal, ValidatorPtr},
  31. Result,
  32. };
  33. use darkfi_serial::{SerialDecodable, SerialEncodable};
  34. /// Auxiliary [`Proposal`] wrapper structure used for messaging.
  35. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  36. struct ProposalMessage(Proposal);
  37. impl_p2p_message!(ProposalMessage, "proposal");
  38. pub struct ProtocolProposal {
  39. proposal_sub: MessageSubscription<ProposalMessage>,
  40. jobsman: ProtocolJobsManagerPtr,
  41. validator: ValidatorPtr,
  42. p2p: P2pPtr,
  43. channel_address: Url,
  44. subscriber: JsonSubscriber,
  45. }
  46. impl ProtocolProposal {
  47. pub async fn init(
  48. channel: ChannelPtr,
  49. validator: ValidatorPtr,
  50. p2p: P2pPtr,
  51. subscriber: JsonSubscriber,
  52. ) -> Result<ProtocolBasePtr> {
  53. debug!(
  54. target: "validator::protocol_proposal::init",
  55. "Adding ProtocolProposal to the protocol registry"
  56. );
  57. let msg_subsystem = channel.message_subsystem();
  58. msg_subsystem.add_dispatch::<ProposalMessage>().await;
  59. let proposal_sub = channel.subscribe_msg::<ProposalMessage>().await?;
  60. Ok(Arc::new(Self {
  61. proposal_sub,
  62. jobsman: ProtocolJobsManager::new("ProposalProtocol", channel.clone()),
  63. validator,
  64. p2p,
  65. channel_address: channel.address().clone(),
  66. subscriber,
  67. }))
  68. }
  69. async fn handle_receive_proposal(self: Arc<Self>) -> Result<()> {
  70. debug!(target: "consensus::protocol_proposal::handle_receive_proposal", "START");
  71. let exclude_list = vec![self.channel_address.clone()];
  72. loop {
  73. let proposal = match self.proposal_sub.receive().await {
  74. Ok(v) => v,
  75. Err(e) => {
  76. debug!(
  77. target: "validator::protocol_proposal::handle_receive_proposal",
  78. "recv fail: {}",
  79. e
  80. );
  81. continue
  82. }
  83. };
  84. // Check if node has finished syncing its blockchain
  85. if !self.validator.read().await.synced {
  86. debug!(
  87. target: "validator::protocol_proposal::handle_receive_proposal",
  88. "Node still syncing blockchain, skipping..."
  89. );
  90. continue
  91. }
  92. // Check if node started participating in consensus.
  93. if !self.validator.read().await.consensus.participating {
  94. debug!(
  95. target: "validator::protocol_proposal::handle_receive_proposal",
  96. "Node is not participating in consensus, skipping..."
  97. );
  98. continue
  99. }
  100. let proposal_copy = (*proposal).clone();
  101. match self.validator.write().await.consensus.append_proposal(&proposal_copy.0).await {
  102. Ok(()) => {
  103. self.p2p.broadcast_with_exclude(&proposal_copy, &exclude_list).await;
  104. self.subscriber.notify(&[proposal_copy]).await;
  105. }
  106. Err(e) => {
  107. debug!(
  108. target: "validator::protocol_proposal::handle_receive_proposal",
  109. "append_proposal fail: {}",
  110. e
  111. );
  112. }
  113. };
  114. }
  115. }
  116. }
  117. #[async_trait]
  118. impl ProtocolBase for ProtocolProposal {
  119. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  120. debug!(target: "validator::protocol_proposal::start", "START");
  121. self.jobsman.clone().start(executor.clone());
  122. self.jobsman.clone().spawn(self.clone().handle_receive_proposal(), executor.clone()).await;
  123. debug!(target: "validator::protocol_proposal::start", "END");
  124. Ok(())
  125. }
  126. fn name(&self) -> &'static str {
  127. "ProtocolProposal"
  128. }
  129. }