sync.rs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 darkfi::{util::async_util::sleep, Result};
  19. use log::{debug, info, warn};
  20. use crate::{
  21. proto::{SyncRequest, SyncResponse},
  22. Darkfid,
  23. };
  24. /// async task used for block syncing
  25. pub async fn sync_task(node: &Darkfid) -> Result<()> {
  26. info!(target: "darkfid::task::sync_task", "Starting blockchain sync...");
  27. // Block until at least node is connected to at least one peer
  28. loop {
  29. if !node.sync_p2p.channels().lock().await.is_empty() {
  30. break
  31. }
  32. warn!(target: "darkfid::task::sync_task", "Node is not connected to other nodes, waiting to retry...");
  33. sleep(10).await;
  34. }
  35. // Getting a random connected channel to ask from peers
  36. let channel = node.sync_p2p.random_channel().await.unwrap();
  37. // Communication setup
  38. let msg_subsystem = channel.message_subsystem();
  39. msg_subsystem.add_dispatch::<SyncResponse>().await;
  40. let block_response_sub = channel.subscribe_msg::<SyncResponse>().await?;
  41. let notif_sub = node.subscribers.get("blocks").unwrap();
  42. // TODO: make this parallel and use a head selection method,
  43. // for example use a manual known head and only connect to nodes
  44. // that follow that. Also use a random peer on every block range
  45. // we sync.
  46. // Node sends the last known block hash of the canonical blockchain
  47. // and loops until the response is the same block (used to utilize
  48. // batch requests).
  49. let mut last = node.validator.read().await.blockchain.last()?;
  50. info!(target: "darkfid::task::sync_task", "Last known block: {:?} - {:?}", last.0, last.1);
  51. loop {
  52. // Node creates a `SyncRequest` and sends it
  53. let request = SyncRequest { slot: last.0, block: last.1 };
  54. channel.send(&request).await?;
  55. // TODO: add a timeout here to retry
  56. // Node waits for response
  57. let response = block_response_sub.receive().await?;
  58. // Verify and store retrieved blocks
  59. debug!(target: "darkfid::task::sync_task", "Processing received blocks");
  60. node.validator.write().await.add_blocks(&response.blocks).await?;
  61. // Notify subscriber
  62. for block in &response.blocks {
  63. notif_sub.notify(&[block.clone()]).await;
  64. }
  65. let last_received = node.validator.read().await.blockchain.last()?;
  66. info!(target: "darkfid::task::sync_task", "Last received block: {:?} - {:?}", last_received.0, last_received.1);
  67. if last == last_received {
  68. break
  69. }
  70. last = last_received;
  71. }
  72. node.validator.write().await.synced = true;
  73. info!(target: "darkfid::task::sync_task", "Blockchain synced!");
  74. Ok(())
  75. }