block_sync.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 crate::{
  19. consensus::{
  20. block::{BlockOrder, BlockResponse},
  21. state::{SlotRequest, SlotResponse},
  22. ValidatorStatePtr,
  23. },
  24. net, Result,
  25. };
  26. use log::{debug, info, warn};
  27. /// async task used for block syncing.
  28. pub async fn block_sync_task(p2p: net::P2pPtr, state: ValidatorStatePtr) -> Result<()> {
  29. info!(target: "consensus::block_sync", "Starting blockchain sync...");
  30. // Getting a random connected channel to ask from peers
  31. match p2p.random_channel().await {
  32. Some(channel) => {
  33. let msg_subsystem = channel.message_subsystem();
  34. // Communication setup for slots
  35. msg_subsystem.add_dispatch::<SlotResponse>().await;
  36. let slot_response_sub = channel.subscribe_msg::<SlotResponse>().await?;
  37. // Communication setup for blocks
  38. msg_subsystem.add_dispatch::<BlockResponse>().await;
  39. let block_response_sub = channel.subscribe_msg::<BlockResponse>().await?;
  40. // Node loops until both slots and blocks have been synced
  41. let mut slots_synced = false;
  42. let mut blocks_synced = false;
  43. loop {
  44. // Node sends the last known slot of the canonical blockchain
  45. // and loops until the response is the same slot (used to utilize batch requests).
  46. let mut last = state.read().await.blockchain.last_slot()?;
  47. info!(target: "consensus::block_sync", "Last known slot: {:?}", last.id);
  48. loop {
  49. // Node creates a `SlotRequest` and sends it
  50. let request = SlotRequest { slot: last.id };
  51. channel.send(&request).await?;
  52. // Node stores response data.
  53. let resp = slot_response_sub.receive().await?;
  54. // Verify and store retrieveds
  55. debug!(target: "consensus::block_sync", "block_sync_task(): Processing received slots");
  56. state.write().await.receive_slots(&resp.slots).await?;
  57. let last_received = state.read().await.blockchain.last_slot()?;
  58. info!(target: "consensus::block_sync", "Last received slot: {:?}", last_received.id);
  59. if last.id == last_received.id {
  60. break
  61. }
  62. blocks_synced = false;
  63. last = last_received;
  64. }
  65. // We force a recheck of slots after blocks have been synced
  66. if blocks_synced {
  67. slots_synced = true;
  68. }
  69. // Node sends the last known block hash of the canonical blockchain
  70. // and loops until the response is the same block (used to utilize
  71. // batch requests).
  72. let mut last = state.read().await.blockchain.last()?;
  73. info!(target: "consensus::block_sync", "Last known block: {:?} - {:?}", last.0, last.1);
  74. loop {
  75. // Node creates a `BlockOrder` and sends it
  76. let order = BlockOrder { slot: last.0, block: last.1 };
  77. channel.send(&order).await?;
  78. // Node stores response data.
  79. let _resp = block_response_sub.receive().await?;
  80. // Verify and store retrieved blocks
  81. debug!(target: "consensus::block_sync", "block_sync_task(): Processing received blocks");
  82. //state.write().await.receive_sync_blocks(&resp.blocks).await?;
  83. let last_received = state.read().await.blockchain.last()?;
  84. info!(target: "consensus::block_sync", "Last received block: {:?} - {:?}", last_received.0, last_received.1);
  85. if last == last_received {
  86. blocks_synced = true;
  87. break
  88. }
  89. slots_synced = false;
  90. last = last_received;
  91. }
  92. if slots_synced && blocks_synced {
  93. break
  94. }
  95. }
  96. }
  97. None => warn!(target: "consensus::block_sync", "Node is not connected to other nodes"),
  98. };
  99. state.write().await.synced = true;
  100. info!(target: "consensus::block_sync", "Blockchain synced!");
  101. Ok(())
  102. }