protocol_sync_consensus.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. debug!("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 bootstrap_slot = lock.consensus.bootstrap_slot;
  77. let offset = lock.consensus.offset;
  78. let mut forks = vec![];
  79. for fork in &lock.consensus.forks {
  80. forks.push(fork.clone().into());
  81. }
  82. let unconfirmed_txs = lock.unconfirmed_txs.clone();
  83. let slot_checkpoints = lock.consensus.slot_checkpoints.clone();
  84. let leaders_history = lock.consensus.leaders_history.clone();
  85. let nullifiers = lock.consensus.nullifiers.clone();
  86. let response = ConsensusResponse {
  87. bootstrap_slot,
  88. offset,
  89. forks,
  90. unconfirmed_txs,
  91. slot_checkpoints,
  92. leaders_history,
  93. nullifiers,
  94. };
  95. if let Err(e) = self.channel.send(response).await {
  96. error!("ProtocolSyncConsensus::handle_receive_request() channel send fail: {}", e);
  97. };
  98. }
  99. }
  100. async fn handle_receive_slot_checkpoints_request(self: Arc<Self>) -> Result<()> {
  101. debug!("ProtocolSyncConsensus::handle_receive_slot_checkpoints_request() [START]");
  102. loop {
  103. let req = match self.slot_checkpoints_request_sub.receive().await {
  104. Ok(v) => v,
  105. Err(e) => {
  106. debug!("ProtocolSyncConsensus::handle_receive_slot_checkpoints_request() recv fail: {}", e);
  107. continue
  108. }
  109. };
  110. debug!(
  111. "ProtocolSyncConsensuss::handle_receive_slot_checkpoints_request() received {:?}",
  112. req
  113. );
  114. // Extra validations can be added here.
  115. let lock = self.state.read().await;
  116. let bootstrap_slot = lock.consensus.bootstrap_slot;
  117. let is_empty = lock.consensus.slot_checkpoints.is_empty();
  118. let response = ConsensusSlotCheckpointsResponse { bootstrap_slot, is_empty };
  119. if let Err(e) = self.channel.send(response).await {
  120. error!("ProtocolSyncConsensus::handle_receive_slot_checkpoints_request() channel send fail: {}", e);
  121. };
  122. }
  123. }
  124. }
  125. #[async_trait]
  126. impl ProtocolBase for ProtocolSyncConsensus {
  127. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  128. debug!("ProtocolSyncConsensus::start() [START]");
  129. self.jobsman.clone().start(executor.clone());
  130. self.jobsman.clone().spawn(self.clone().handle_receive_request(), executor.clone()).await;
  131. self.jobsman
  132. .clone()
  133. .spawn(self.clone().handle_receive_slot_checkpoints_request(), executor.clone())
  134. .await;
  135. debug!("ProtocolSyncConsensus::start() [END]");
  136. Ok(())
  137. }
  138. fn name(&self) -> &'static str {
  139. "ProtocolSyncConsensus"
  140. }
  141. }