protocol_proposal.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. use async_std::sync::Arc;
  2. use async_executor::Executor;
  3. use async_trait::async_trait;
  4. use log::{debug, error, info};
  5. use url::Url;
  6. use crate::{
  7. consensus::{BlockProposal, ValidatorStatePtr},
  8. net::{
  9. ChannelPtr, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
  10. ProtocolJobsManager, ProtocolJobsManagerPtr,
  11. },
  12. Result,
  13. };
  14. pub struct ProtocolProposal {
  15. proposal_sub: MessageSubscription<BlockProposal>,
  16. jobsman: ProtocolJobsManagerPtr,
  17. state: ValidatorStatePtr,
  18. p2p: P2pPtr,
  19. channel_address: Url,
  20. }
  21. impl ProtocolProposal {
  22. pub async fn init(
  23. channel: ChannelPtr,
  24. state: ValidatorStatePtr,
  25. p2p: P2pPtr,
  26. ) -> Result<ProtocolBasePtr> {
  27. debug!("Adding ProtocolProposal to the protocol registry");
  28. let msg_subsystem = channel.get_message_subsystem();
  29. msg_subsystem.add_dispatch::<BlockProposal>().await;
  30. let proposal_sub = channel.subscribe_msg::<BlockProposal>().await?;
  31. let channel_address = channel.address();
  32. Ok(Arc::new(Self {
  33. proposal_sub,
  34. jobsman: ProtocolJobsManager::new("ProposalProtocol", channel),
  35. state,
  36. p2p,
  37. channel_address,
  38. }))
  39. }
  40. async fn handle_receive_proposal(self: Arc<Self>) -> Result<()> {
  41. debug!("ProtocolProposal::handle_receive_proposal() [START]");
  42. let exclude_list = vec![self.channel_address.clone()];
  43. loop {
  44. let proposal = match self.proposal_sub.receive().await {
  45. Ok(v) => v,
  46. Err(e) => {
  47. error!("ProtocolProposal::handle_receive_proposal(): recv fail: {}", e);
  48. continue
  49. }
  50. };
  51. info!("ProtocolProposal::handle_receive_proposal(): recv: {}", proposal);
  52. debug!("ProtocolProposal::handle_receive_proposal(): Full proposal: {:?}", proposal);
  53. let proposal_copy = (*proposal).clone();
  54. let vote = match self.state.write().await.receive_proposal(&proposal_copy).await {
  55. Ok(v) => {
  56. if v.is_none() {
  57. debug!("ProtocolProposal::handle_receive_proposal(): Node didn't vote for proposed block.");
  58. continue
  59. }
  60. v.unwrap()
  61. }
  62. Err(e) => {
  63. debug!("ProtocolProposal::handle_receive_proposal(): error processing proposal: {}", e);
  64. continue
  65. }
  66. };
  67. if let Err(e) = self.state.write().await.receive_vote(&vote).await {
  68. error!("ProtocolProposal::handle_receive_proposal(): receive_vote error: {}", e);
  69. continue
  70. }
  71. // Broadcast block to rest of nodes
  72. if let Err(e) = self.p2p.broadcast_with_exclude(proposal_copy, &exclude_list).await {
  73. error!(
  74. "ProtocolProposal::handle_receive_proposal(): proposal broadcast fail: {}",
  75. e
  76. );
  77. };
  78. // Broadcast vote
  79. if let Err(e) = self.p2p.broadcast(vote).await {
  80. error!("ProtocolProposal::handle_receive_proposal(): vote broadcast fail: {}", e);
  81. }
  82. }
  83. }
  84. }
  85. #[async_trait]
  86. impl ProtocolBase for ProtocolProposal {
  87. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  88. debug!("ProtocolProposal::start() [START]");
  89. self.jobsman.clone().start(executor.clone());
  90. self.jobsman.clone().spawn(self.clone().handle_receive_proposal(), executor.clone()).await;
  91. debug!("ProtocolProposal::start() [END]");
  92. Ok(())
  93. }
  94. fn name(&self) -> &'static str {
  95. "ProtocolProposal"
  96. }
  97. }