protocol_proposal.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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, error, trace};
  21. use smol::Executor;
  22. use url::Url;
  23. use crate::{
  24. consensus::{BlockProposal, ValidatorStatePtr},
  25. net::{
  26. ChannelPtr, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
  27. ProtocolJobsManager, ProtocolJobsManagerPtr,
  28. },
  29. Result,
  30. };
  31. pub struct ProtocolProposal {
  32. proposal_sub: MessageSubscription<BlockProposal>,
  33. jobsman: ProtocolJobsManagerPtr,
  34. state: ValidatorStatePtr,
  35. p2p: P2pPtr,
  36. channel_address: Url,
  37. }
  38. impl ProtocolProposal {
  39. pub async fn init(
  40. channel: ChannelPtr,
  41. state: ValidatorStatePtr,
  42. p2p: P2pPtr,
  43. ) -> Result<ProtocolBasePtr> {
  44. debug!("Adding ProtocolProposal to the protocol registry");
  45. let msg_subsystem = channel.get_message_subsystem();
  46. msg_subsystem.add_dispatch::<BlockProposal>().await;
  47. let proposal_sub = channel.subscribe_msg::<BlockProposal>().await?;
  48. let channel_address = channel.address();
  49. Ok(Arc::new(Self {
  50. proposal_sub,
  51. jobsman: ProtocolJobsManager::new("ProposalProtocol", channel),
  52. state,
  53. p2p,
  54. channel_address,
  55. }))
  56. }
  57. async fn handle_receive_proposal(self: Arc<Self>) -> Result<()> {
  58. debug!("ProtocolProposal::handle_receive_proposal() [START]");
  59. let exclude_list = vec![self.channel_address.clone()];
  60. loop {
  61. let proposal = match self.proposal_sub.receive().await {
  62. Ok(v) => v,
  63. Err(e) => {
  64. error!("ProtocolProposal::handle_receive_proposal(): recv fail: {}", e);
  65. continue
  66. }
  67. };
  68. debug!("ProtocolProposal::handle_receive_proposal(): recv: {}", proposal);
  69. trace!("ProtocolProposal::handle_receive_proposal(): Full proposal: {:?}", proposal);
  70. let proposal_copy = (*proposal).clone();
  71. // Verify we have the proposal already
  72. let mut lock = self.state.write().await;
  73. if lock.consensus.proposal_exists(&proposal_copy.hash) {
  74. debug!("ProtocolProposal::handle_receive_proposal(): Proposal already received.");
  75. continue
  76. }
  77. if let Err(e) = lock.receive_proposal(&proposal_copy, None).await {
  78. error!(
  79. "ProtocolProposal::handle_receive_proposal(): receive_proposal error: {}",
  80. e
  81. );
  82. continue
  83. }
  84. // Broadcast block to rest of nodes
  85. if let Err(e) = self.p2p.broadcast_with_exclude(proposal_copy, &exclude_list).await {
  86. error!(
  87. "ProtocolProposal::handle_receive_proposal(): proposal broadcast fail: {}",
  88. e
  89. );
  90. };
  91. }
  92. }
  93. }
  94. #[async_trait]
  95. impl ProtocolBase for ProtocolProposal {
  96. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  97. debug!("ProtocolProposal::start() [START]");
  98. self.jobsman.clone().start(executor.clone());
  99. self.jobsman.clone().spawn(self.clone().handle_receive_proposal(), executor.clone()).await;
  100. debug!("ProtocolProposal::start() [END]");
  101. Ok(())
  102. }
  103. fn name(&self) -> &'static str {
  104. "ProtocolProposal"
  105. }
  106. }