darkfid/task/
sync.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 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
19use std::collections::HashMap;
20
21use darkfi::{
22    blockchain::HeaderHash, net::ChannelPtr, rpc::jsonrpc::JsonSubscriber, system::sleep,
23    util::encoding::base64, validator::consensus::Proposal, Error, Result,
24};
25use darkfi_serial::serialize_async;
26use rand::{prelude::SliceRandom, rngs::OsRng};
27use tinyjson::JsonValue;
28use tracing::{debug, info, warn};
29
30use crate::{
31    proto::{
32        ForkSyncRequest, ForkSyncResponse, HeaderSyncRequest, HeaderSyncResponse, SyncRequest,
33        SyncResponse, TipRequest, TipResponse, BATCH,
34    },
35    DarkfiNodePtr,
36};
37
38// TODO: Parallelize independent requests.
39//       We can also make them be like torrents, where we retrieve chunks not in order.
40/// async task used for block syncing.
41/// A checkpoint can be provided to ensure node syncs the correct sequence.
42pub async fn sync_task(node: &DarkfiNodePtr, checkpoint: Option<(u32, HeaderHash)>) -> Result<()> {
43    info!(target: "darkfid::task::sync_task", "Starting blockchain sync...");
44
45    // Grab blocks subscriber
46    let block_sub = node.subscribers.get("blocks").unwrap();
47
48    // Grab last known block header, including existing pending sync ones
49    let mut last = node.validator.blockchain.last()?;
50
51    // If checkpoint is not reached, purge headers and start syncing from scratch
52    if let Some(checkpoint) = checkpoint {
53        if checkpoint.0 > last.0 {
54            node.validator.blockchain.headers.remove_all_sync()?;
55        }
56    }
57
58    // Check sync headers first record is the next one
59    if let Some(next) = node.validator.blockchain.headers.get_first_sync()? {
60        if next.height == last.0 + 1 {
61            // Grab last sync header to continue syncing from
62            if let Some(last_sync) = node.validator.blockchain.headers.get_last_sync()? {
63                last = (last_sync.height, last_sync.hash());
64            }
65        } else {
66            // Purge headers and start syncing from scratch
67            node.validator.blockchain.headers.remove_all_sync()?;
68        }
69    }
70    info!(target: "darkfid::task::sync_task", "Last known block: {} - {}", last.0, last.1);
71
72    // Grab the most common tip and the corresponding peers
73    let (mut common_tip_height, common_tip_hash, mut common_tip_peers) =
74        most_common_tip(node, &last.1, checkpoint).await;
75
76    // If the most common tip is the empty tip, we skip syncing
77    // further and will reorg if needed when a new proposal arrives.
78    if common_tip_hash == [0u8; 32] {
79        *node.validator.synced.write().await = true;
80        info!(target: "darkfid::task::sync_task", "Blockchain synced!");
81        return Ok(())
82    }
83
84    // If last known block header is before the checkpoint, we sync until that first.
85    if let Some(checkpoint) = checkpoint {
86        if checkpoint.0 > last.0 {
87            info!(target: "darkfid::task::sync_task", "Syncing until configured checkpoint: {} - {}", checkpoint.0, checkpoint.1);
88            // Retrieve all the headers backwards until our last known one and verify them.
89            // We use the next height, in order to also retrieve the checkpoint header.
90            retrieve_headers(node, &common_tip_peers, last.0, checkpoint.0 + 1).await?;
91
92            // Retrieve all the blocks for those headers and apply them to canonical
93            last = retrieve_blocks(node, &common_tip_peers, last, block_sub, true).await?;
94            info!(target: "darkfid::task::sync_task", "Last received block: {} - {}", last.0, last.1);
95
96            // Grab synced peers most common tip again
97            (common_tip_height, _, common_tip_peers) = most_common_tip(node, &last.1, None).await;
98        }
99    }
100
101    // Sync headers and blocks
102    loop {
103        // Retrieve all the headers backwards until our last known one and verify them.
104        // We use the next height, in order to also retrieve the peers tip header.
105        retrieve_headers(node, &common_tip_peers, last.0, common_tip_height + 1).await?;
106
107        // Retrieve all the blocks for those headers and apply them to canonical
108        let last_received =
109            retrieve_blocks(node, &common_tip_peers, last, block_sub, false).await?;
110        info!(target: "darkfid::task::sync_task", "Last received block: {} - {}", last_received.0, last_received.1);
111
112        if last == last_received {
113            break
114        }
115
116        last = last_received;
117
118        // Grab synced peers most common tip again
119        (common_tip_height, _, common_tip_peers) = most_common_tip(node, &last.1, None).await;
120    }
121
122    // Sync best fork
123    sync_best_fork(node, &common_tip_peers, &last.1).await;
124
125    // Perform confirmation
126    let confirmed = node.validator.confirmation().await?;
127    if !confirmed.is_empty() {
128        // Notify subscriber
129        let mut notif_blocks = Vec::with_capacity(confirmed.len());
130        for block in confirmed {
131            notif_blocks.push(JsonValue::String(base64::encode(&serialize_async(&block).await)));
132        }
133        block_sub.notify(JsonValue::Array(notif_blocks)).await;
134    }
135
136    *node.validator.synced.write().await = true;
137    info!(target: "darkfid::task::sync_task", "Blockchain synced!");
138    Ok(())
139}
140
141/// Auxiliary function to block until node is connected to at least one synced peer,
142/// and retrieve the synced peers tips.
143async fn synced_peers(
144    node: &DarkfiNodePtr,
145    last_tip: &HeaderHash,
146    checkpoint: Option<(u32, HeaderHash)>,
147) -> HashMap<(u32, [u8; 32]), Vec<ChannelPtr>> {
148    info!(target: "darkfid::task::sync::synced_peers", "Receiving tip from peers...");
149    let mut tips = HashMap::new();
150    loop {
151        // Grab channels
152        let peers = node.p2p_handler.p2p.hosts().channels();
153
154        // Ask each peer(if we got any) if they are synced
155        for peer in peers {
156            let comms_timeout = node
157                .p2p_handler
158                .p2p
159                .settings()
160                .read_arc()
161                .await
162                .outbound_connect_timeout(peer.address().scheme());
163
164            // If a checkpoint was provider, we check that the peer follows that sequence
165            if let Some(c) = checkpoint {
166                // Communication setup
167                let Ok(response_sub) = peer.subscribe_msg::<HeaderSyncResponse>().await else {
168                    debug!(target: "darkfid::task::sync::synced_peers", "Failure during `HeaderSyncResponse` communication setup with peer: {peer:?}");
169                    continue
170                };
171
172                // Node creates a `HeaderSyncRequest` and sends it
173                let request = HeaderSyncRequest { height: c.0 + 1 };
174                if let Err(e) = peer.send(&request).await {
175                    debug!(target: "darkfid::task::sync::synced_peers", "Failure during `HeaderSyncRequest` send to peer {peer:?}: {e}");
176                    continue
177                };
178
179                // Node waits for response
180                let Ok(response) = response_sub.receive_with_timeout(comms_timeout).await else {
181                    debug!(target: "darkfid::task::sync::synced_peers", "Timeout while waiting for `HeaderSyncResponse` from peer: {peer:?}");
182                    continue
183                };
184
185                // Handle response
186                if response.headers.is_empty() || response.headers.last().unwrap().hash() != c.1 {
187                    debug!(target: "darkfid::task::sync::synced_peers", "Invalid `HeaderSyncResponse` from peer: {peer:?}");
188                    continue
189                }
190            }
191
192            // Communication setup
193            let Ok(response_sub) = peer.subscribe_msg::<TipResponse>().await else {
194                debug!(target: "darkfid::task::sync::synced_peers", "Failure during `TipResponse` communication setup with peer: {peer:?}");
195                continue
196            };
197
198            // Node creates a `TipRequest` and sends it
199            let request = TipRequest { tip: *last_tip };
200            if let Err(e) = peer.send(&request).await {
201                debug!(target: "darkfid::task::sync::synced_peers", "Failure during `TipRequest` send to peer {peer:?}: {e}");
202                continue
203            };
204
205            // Node waits for response
206            let Ok(response) = response_sub.receive_with_timeout(comms_timeout).await else {
207                debug!(target: "darkfid::task::sync::synced_peers", "Timeout while waiting for `TipResponse` from peer: {peer:?}");
208                continue
209            };
210
211            // Handle response
212            if response.synced {
213                // Grab response tip
214                let tip = if response.height.is_some() && response.hash.is_some() {
215                    (response.height.unwrap(), *response.hash.unwrap().inner())
216                } else {
217                    // Empty response while synced means the peer is on an
218                    // entirely different chain/fork, so we keep track of
219                    // them in the empty tip reference.
220                    (0, [0u8; 32])
221                };
222                let Some(tip_peers) = tips.get_mut(&tip) else {
223                    tips.insert(tip, vec![peer.clone()]);
224                    continue
225                };
226                tip_peers.push(peer.clone());
227            }
228        }
229
230        // Check if we got any tips
231        if !tips.is_empty() {
232            break
233        }
234
235        warn!(target: "darkfid::task::sync::synced_peers", "Node is not connected to other synced nodes, waiting to retry...");
236        let subscription = node.p2p_handler.p2p.hosts().subscribe_channel().await;
237        let _ = subscription.receive().await;
238        subscription.unsubscribe().await;
239
240        let comms_timeout =
241            node.p2p_handler.p2p.settings().read_arc().await.outbound_connect_timeout_max();
242
243        info!(target: "darkfid::task::sync::synced_peers", "Sleeping for {comms_timeout} to allow for more nodes to connect...");
244        sleep(comms_timeout).await;
245    }
246
247    tips
248}
249
250/// Auxiliary function to ask all peers for their current tip and find the most common one.
251async fn most_common_tip(
252    node: &DarkfiNodePtr,
253    last_tip: &HeaderHash,
254    checkpoint: Option<(u32, HeaderHash)>,
255) -> (u32, [u8; 32], Vec<ChannelPtr>) {
256    // Grab synced peers tips
257    let tips = synced_peers(node, last_tip, checkpoint).await;
258
259    // Grab the most common highest tip peers
260    info!(target: "darkfid::task::sync::most_common_tip", "Finding most common tip...");
261    let mut common_tip = (0, [0u8; 32], vec![]);
262    for (tip, peers) in tips {
263        // Check if tip peers is less than the most common tip peers
264        if peers.len() < common_tip.2.len() {
265            continue;
266        }
267        // If peers are the same length, skip if tip height is less than
268        // the most common tip height.
269        if peers.len() == common_tip.2.len() || tip.0 < common_tip.0 {
270            continue;
271        }
272        // Keep the heighest tip with the most peers
273        common_tip = (tip.0, tip.1, peers);
274    }
275
276    info!(target: "darkfid::task::sync::most_common_tip", "Most common tip: {} - {}", common_tip.0, HeaderHash::new(common_tip.1));
277    common_tip
278}
279
280/// Auxiliary function to retrieve headers backwards until our last known one and verify them.
281async fn retrieve_headers(
282    node: &DarkfiNodePtr,
283    peers: &[ChannelPtr],
284    last_known: u32,
285    tip_height: u32,
286) -> Result<()> {
287    info!(target: "darkfid::task::sync::retrieve_headers", "Retrieving missing headers from peers...");
288    // Communication setup
289    let mut peer_subs = vec![];
290    for peer in peers {
291        match peer.subscribe_msg::<HeaderSyncResponse>().await {
292            Ok(response_sub) => peer_subs.push((Some(response_sub), false)),
293            Err(e) => {
294                debug!(target: "darkfid::task::sync::retrieve_headers", "Failure during `HeaderSyncResponse` communication setup with peer {peer:?}: {e}");
295                peer_subs.push((None, true))
296            }
297        }
298    }
299
300    // We subtract 1 since tip_height is increased by one
301    let total = tip_height - last_known - 1;
302    let mut last_tip_height = tip_height;
303    'headers_loop: loop {
304        // Check if all our peers are failing
305        let mut count = 0;
306        for (peer_sub, failed) in &peer_subs {
307            if peer_sub.is_none() || *failed {
308                count += 1;
309            }
310        }
311        if count == peer_subs.len() {
312            debug!(target: "darkfid::task::sync::retrieve_headers", "All peer connections failed.");
313            break
314        }
315
316        for (index, peer) in peers.iter().enumerate() {
317            // Grab the response sub reference
318            let (peer_sub, failed) = &mut peer_subs[index];
319            if *failed {
320                continue;
321            }
322            let Some(ref response_sub) = peer_sub else {
323                continue;
324            };
325
326            // Node creates a `HeaderSyncRequest` and sends it
327            let request = HeaderSyncRequest { height: last_tip_height };
328            if let Err(e) = peer.send(&request).await {
329                debug!(target: "darkfid::task::sync::retrieve_headers", "Failure during `HeaderSyncRequest` send to peer {peer:?}: {e}");
330                *failed = true;
331                continue
332            };
333
334            let comms_timeout = node
335                .p2p_handler
336                .p2p
337                .settings()
338                .read_arc()
339                .await
340                .outbound_connect_timeout(peer.address().scheme());
341
342            // Node waits for response
343            let Ok(response) = response_sub.receive_with_timeout(comms_timeout).await else {
344                debug!(target: "darkfid::task::sync::retrieve_headers", "Timeout while waiting for `HeaderSyncResponse` from peer: {peer:?}");
345                *failed = true;
346                continue
347            };
348
349            // Retain only the headers after our last known
350            let mut response_headers = response.headers.to_vec();
351            response_headers.retain(|h| h.height > last_known);
352
353            if response_headers.is_empty() {
354                break 'headers_loop
355            }
356
357            // Store the headers
358            node.validator.blockchain.headers.insert_sync(&response_headers)?;
359            last_tip_height = response_headers[0].height;
360            info!(target: "darkfid::task::sync::retrieve_headers", "Headers received: {}/{total}", node.validator.blockchain.headers.len_sync());
361        }
362    }
363
364    // Check if we retrieved any new headers
365    if node.validator.blockchain.headers.is_empty_sync() {
366        return Ok(());
367    }
368
369    // Verify headers sequence. Here we do a quick and dirty verification
370    // of just the hashes and heights sequence. We will formaly verify
371    // the blocks when we retrieve them. We verify them in batches,
372    // to not load them all in memory.
373    info!(target: "darkfid::task::sync::retrieve_headers", "Verifying headers sequence...");
374    let mut verified_headers = 0;
375    let total = node.validator.blockchain.headers.len_sync();
376    // First we verify the first `BATCH` sequence, using the last known header
377    // as the first sync header previous.
378    let last_known = node.validator.consensus.best_fork_last_header().await?;
379    let mut headers = node.validator.blockchain.headers.get_after_sync(0, BATCH)?;
380    if headers[0].previous != last_known.1 || headers[0].height != last_known.0 + 1 {
381        node.validator.blockchain.headers.remove_all_sync()?;
382        return Err(Error::BlockIsInvalid(headers[0].hash().as_string()))
383    }
384    verified_headers += 1;
385    for (index, header) in headers[1..].iter().enumerate() {
386        if header.previous != headers[index].hash() || header.height != headers[index].height + 1 {
387            node.validator.blockchain.headers.remove_all_sync()?;
388            return Err(Error::BlockIsInvalid(header.hash().as_string()))
389        }
390        verified_headers += 1;
391    }
392    info!(target: "darkfid::task::sync::retrieve_headers", "Headers verified: {verified_headers}/{total}");
393
394    // Now we verify the rest sequences
395    let mut last_checked = headers.last().unwrap().clone();
396    headers = node.validator.blockchain.headers.get_after_sync(last_checked.height, BATCH)?;
397    while !headers.is_empty() {
398        if headers[0].previous != last_checked.hash() ||
399            headers[0].height != last_checked.height + 1
400        {
401            node.validator.blockchain.headers.remove_all_sync()?;
402            return Err(Error::BlockIsInvalid(headers[0].hash().as_string()))
403        }
404        verified_headers += 1;
405        for (index, header) in headers[1..].iter().enumerate() {
406            if header.previous != headers[index].hash() ||
407                header.height != headers[index].height + 1
408            {
409                node.validator.blockchain.headers.remove_all_sync()?;
410                return Err(Error::BlockIsInvalid(header.hash().as_string()))
411            }
412            verified_headers += 1;
413        }
414        last_checked = headers.last().unwrap().clone();
415        headers = node.validator.blockchain.headers.get_after_sync(last_checked.height, BATCH)?;
416        info!(target: "darkfid::task::sync::retrieve_headers", "Headers verified: {verified_headers}/{total}");
417    }
418
419    info!(target: "darkfid::task::sync::retrieve_headers", "Headers sequence verified!");
420    Ok(())
421}
422
423/// Auxiliary function to retrieve blocks of provided headers and apply them to canonical.
424async fn retrieve_blocks(
425    node: &DarkfiNodePtr,
426    peers: &[ChannelPtr],
427    last_known: (u32, HeaderHash),
428    block_sub: &JsonSubscriber,
429    checkpoint_blocks: bool,
430) -> Result<(u32, HeaderHash)> {
431    info!(target: "darkfid::task::sync::retrieve_blocks", "Retrieving missing blocks from peers...");
432    let mut last_received = last_known;
433    // Communication setup
434    let mut peer_subs = vec![];
435    for peer in peers {
436        match peer.subscribe_msg::<SyncResponse>().await {
437            Ok(response_sub) => peer_subs.push((Some(response_sub), false)),
438            Err(e) => {
439                debug!(target: "darkfid::task::sync::retrieve_blocks", "Failure during `SyncResponse` communication setup with peer {peer:?}: {e}");
440                peer_subs.push((None, true))
441            }
442        }
443    }
444
445    let mut received_blocks = 0;
446    let total = node.validator.blockchain.headers.len_sync();
447    'blocks_loop: loop {
448        // Check if all our peers are failing
449        let mut count = 0;
450        for (peer_sub, failed) in &peer_subs {
451            if peer_sub.is_none() || *failed {
452                count += 1;
453            }
454        }
455        if count == peer_subs.len() {
456            debug!(target: "darkfid::task::sync::retrieve_blocks", "All peer connections failed.");
457            break
458        }
459
460        'peers_loop: for (index, peer) in peers.iter().enumerate() {
461            // Grab the response sub reference
462            let (peer_sub, failed) = &mut peer_subs[index];
463            if *failed {
464                continue;
465            }
466            let Some(ref response_sub) = peer_sub else {
467                continue;
468            };
469
470            // Grab first `BATCH` headers
471            let headers = node.validator.blockchain.headers.get_after_sync(0, BATCH)?;
472            if headers.is_empty() {
473                break 'blocks_loop
474            }
475            let mut headers_hashes = Vec::with_capacity(headers.len());
476            let mut synced_headers = Vec::with_capacity(headers.len());
477            for header in &headers {
478                headers_hashes.push(header.hash());
479                synced_headers.push(header.height);
480            }
481
482            // Node creates a `SyncRequest` and sends it
483            let request = SyncRequest { headers: headers_hashes.clone() };
484            if let Err(e) = peer.send(&request).await {
485                debug!(target: "darkfid::task::sync::retrieve_blocks", "Failure during `SyncRequest` send to peer {peer:?}: {e}");
486                *failed = true;
487                continue
488            };
489
490            let comms_timeout = node
491                .p2p_handler
492                .p2p
493                .settings()
494                .read_arc()
495                .await
496                .outbound_connect_timeout(peer.address().scheme());
497
498            // Node waits for response
499            let Ok(response) = response_sub.receive_with_timeout(comms_timeout).await else {
500                debug!(target: "darkfid::task::sync::retrieve_blocks", "Timeout while waiting for `SyncResponse` from peer: {peer:?}");
501                *failed = true;
502                continue
503            };
504
505            // Verify and store retrieved blocks
506            debug!(target: "darkfid::task::sync::retrieve_blocks", "Processing received blocks");
507            received_blocks += response.blocks.len();
508            if checkpoint_blocks {
509                if let Err(e) =
510                    node.validator.add_checkpoint_blocks(&response.blocks, &headers_hashes).await
511                {
512                    debug!(target: "darkfid::task::sync::retrieve_blocks", "Error while adding checkpoint blocks: {e}");
513                    continue
514                };
515            } else {
516                for block in &response.blocks {
517                    if let Err(e) =
518                        node.validator.append_proposal(&Proposal::new(block.clone())).await
519                    {
520                        debug!(target: "darkfid::task::sync::retrieve_blocks", "Error while appending proposal: {e}");
521                        continue 'peers_loop
522                    };
523                }
524            }
525            last_received = (*synced_headers.last().unwrap(), *headers_hashes.last().unwrap());
526
527            // Remove synced headers
528            node.validator.blockchain.headers.remove_sync(&synced_headers)?;
529
530            if checkpoint_blocks {
531                // Notify subscriber
532                let mut notif_blocks = Vec::with_capacity(response.blocks.len());
533                info!(target: "darkfid::task::sync::retrieve_blocks", "Blocks added:");
534                for (index, block) in response.blocks.iter().enumerate() {
535                    info!(target: "darkfid::task::sync::retrieve_blocks", "\t{} - {}", headers_hashes[index], headers[index].height);
536                    notif_blocks
537                        .push(JsonValue::String(base64::encode(&serialize_async(block).await)));
538                }
539                block_sub.notify(JsonValue::Array(notif_blocks)).await;
540            } else {
541                // Perform confirmation for received blocks
542                let confirmed = node.validator.confirmation().await?;
543                if !confirmed.is_empty() {
544                    // Notify subscriber
545                    let mut notif_blocks = Vec::with_capacity(confirmed.len());
546                    for block in confirmed {
547                        notif_blocks.push(JsonValue::String(base64::encode(
548                            &serialize_async(&block).await,
549                        )));
550                    }
551                    block_sub.notify(JsonValue::Array(notif_blocks)).await;
552                }
553            }
554
555            info!(target: "darkfid::task::sync::retrieve_blocks", "Blocks received: {received_blocks}/{total}");
556        }
557    }
558
559    Ok(last_received)
560}
561
562/// Auxiliary function to retrieve best fork state from a random peer.
563async fn sync_best_fork(node: &DarkfiNodePtr, peers: &[ChannelPtr], last_tip: &HeaderHash) {
564    info!(target: "darkfid::task::sync::sync_best_fork", "Syncing fork states from peers...");
565    // Getting a random peer to ask for blocks
566    let peer = &peers.choose(&mut OsRng).unwrap();
567
568    // Communication setup
569    let Ok(response_sub) = peer.subscribe_msg::<ForkSyncResponse>().await else {
570        debug!(target: "darkfid::task::sync::sync_best_fork", "Failure during `ForkSyncResponse` communication setup with peer: {peer:?}");
571        return
572    };
573    let notif_sub = node.subscribers.get("proposals").unwrap();
574
575    // Node creates a `ForkSyncRequest` and sends it
576    let request = ForkSyncRequest { tip: *last_tip, fork_tip: None };
577    if let Err(e) = peer.send(&request).await {
578        debug!(target: "darkfid::task::sync::sync_best_fork", "Failure during `ForkSyncRequest` send to peer {peer:?}: {e}");
579        return
580    };
581
582    let comms_timeout = node
583        .p2p_handler
584        .p2p
585        .settings()
586        .read_arc()
587        .await
588        .outbound_connect_timeout(peer.address().scheme());
589
590    // Node waits for response
591    let Ok(response) = response_sub.receive_with_timeout(comms_timeout).await else {
592        debug!(target: "darkfid::task::sync::sync_best_fork", "Timeout while waiting for `ForkSyncResponse` from peer: {peer:?}");
593        return
594    };
595
596    // Verify and store retrieved proposals
597    debug!(target: "darkfid::task::sync::sync_best_fork", "Processing received proposals");
598    for proposal in &response.proposals {
599        if let Err(e) = node.validator.append_proposal(proposal).await {
600            debug!(target: "darkfid::task::sync::sync_best_fork", "Error while appending proposal: {e}");
601            return
602        };
603        // Notify subscriber
604        let enc_prop = JsonValue::String(base64::encode(&serialize_async(proposal).await));
605        notif_sub.notify(vec![enc_prop].into()).await;
606    }
607}