protocol_sync_consensus.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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};
  21. use smol::Executor;
  22. use crate::{
  23. consensus::{
  24. state::{
  25. ConsensusRequest, ConsensusResponse, ConsensusSlotCheckpointsRequest,
  26. ConsensusSlotCheckpointsResponse,
  27. },
  28. ValidatorStatePtr,
  29. },
  30. net::{
  31. ChannelPtr, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
  32. ProtocolJobsManager, ProtocolJobsManagerPtr,
  33. },
  34. Result,
  35. };
  36. pub struct ProtocolSyncConsensus {
  37. channel: ChannelPtr,
  38. request_sub: MessageSubscription<ConsensusRequest>,
  39. slot_checkpoints_request_sub: MessageSubscription<ConsensusSlotCheckpointsRequest>,
  40. jobsman: ProtocolJobsManagerPtr,
  41. state: ValidatorStatePtr,
  42. }
  43. impl ProtocolSyncConsensus {
  44. pub async fn init(
  45. channel: ChannelPtr,
  46. state: ValidatorStatePtr,
  47. _p2p: P2pPtr,
  48. ) -> Result<ProtocolBasePtr> {
  49. let msg_subsystem = channel.get_message_subsystem();
  50. msg_subsystem.add_dispatch::<ConsensusRequest>().await;
  51. msg_subsystem.add_dispatch::<ConsensusSlotCheckpointsRequest>().await;
  52. let request_sub = channel.subscribe_msg::<ConsensusRequest>().await?;
  53. let slot_checkpoints_request_sub =
  54. channel.subscribe_msg::<ConsensusSlotCheckpointsRequest>().await?;
  55. Ok(Arc::new(Self {
  56. channel: channel.clone(),
  57. request_sub,
  58. slot_checkpoints_request_sub,
  59. jobsman: ProtocolJobsManager::new("SyncConsensusProtocol", channel),
  60. state,
  61. }))
  62. }
  63. async fn handle_receive_request(self: Arc<Self>) -> Result<()> {
  64. debug!("ProtocolSyncConsensus::handle_receive_request() [START]");
  65. loop {
  66. let req = match self.request_sub.receive().await {
  67. Ok(v) => v,
  68. Err(e) => {
  69. error!("ProtocolSyncConsensus::handle_receive_request() recv fail: {}", e);
  70. continue
  71. }
  72. };
  73. debug!("ProtocolSyncConsensuss::handle_receive_request() received {:?}", req);
  74. // Extra validations can be added here.
  75. let lock = self.state.read().await;
  76. let offset = lock.consensus.offset;
  77. let mut forks = vec![];
  78. for fork in &lock.consensus.forks {
  79. forks.push(fork.clone().into());
  80. }
  81. let unconfirmed_txs = lock.unconfirmed_txs.clone();
  82. let slot_checkpoints = lock.consensus.slot_checkpoints.clone();
  83. let leaders_history = lock.consensus.leaders_history.clone();
  84. let nullifiers = lock.consensus.nullifiers.clone();
  85. let response = ConsensusResponse {
  86. offset,
  87. forks,
  88. unconfirmed_txs,
  89. slot_checkpoints,
  90. leaders_history,
  91. nullifiers,
  92. };
  93. if let Err(e) = self.channel.send(response).await {
  94. error!("ProtocolSyncConsensus::handle_receive_request() channel send fail: {}", e);
  95. };
  96. }
  97. }
  98. async fn handle_receive_slot_checkpoints_request(self: Arc<Self>) -> Result<()> {
  99. debug!("ProtocolSyncConsensus::handle_receive_slot_checkpoints_request() [START]");
  100. loop {
  101. let req = match self.slot_checkpoints_request_sub.receive().await {
  102. Ok(v) => v,
  103. Err(e) => {
  104. error!("ProtocolSyncConsensus::handle_receive_slot_checkpoints_request() recv fail: {}", e);
  105. continue
  106. }
  107. };
  108. debug!(
  109. "ProtocolSyncConsensuss::handle_receive_slot_checkpoints_request() received {:?}",
  110. req
  111. );
  112. // Extra validations can be added here.
  113. let slot_checkpoints = !self.state.read().await.consensus.slot_checkpoints.is_empty();
  114. let response = ConsensusSlotCheckpointsResponse { slot_checkpoints };
  115. if let Err(e) = self.channel.send(response).await {
  116. error!("ProtocolSyncConsensus::handle_receive_slot_checkpoints_request() channel send fail: {}", e);
  117. };
  118. }
  119. }
  120. }
  121. #[async_trait]
  122. impl ProtocolBase for ProtocolSyncConsensus {
  123. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  124. debug!("ProtocolSyncConsensus::start() [START]");
  125. self.jobsman.clone().start(executor.clone());
  126. self.jobsman.clone().spawn(self.clone().handle_receive_request(), executor.clone()).await;
  127. self.jobsman
  128. .clone()
  129. .spawn(self.clone().handle_receive_slot_checkpoints_request(), executor.clone())
  130. .await;
  131. debug!("ProtocolSyncConsensus::start() [END]");
  132. Ok(())
  133. }
  134. fn name(&self) -> &'static str {
  135. "ProtocolSyncConsensus"
  136. }
  137. }