Skip to main content

radicle_cli/commands/
sync.rs

1mod args;
2
3use std::cmp::Ordering;
4use std::collections::BTreeMap;
5use std::collections::HashSet;
6use std::time;
7
8use anyhow::{anyhow, Context as _};
9
10use radicle::node;
11use radicle::node::address::Store;
12use radicle::node::sync;
13use radicle::node::sync::fetch::SuccessfulOutcome;
14use radicle::node::SyncedAt;
15use radicle::node::{AliasStore, Handle as _, Node, Seed, SyncStatus};
16use radicle::prelude::{NodeId, Profile, RepoId};
17use radicle::storage::ReadRepository;
18use radicle::storage::RefUpdate;
19use radicle::storage::{ReadStorage, RemoteRepository};
20use radicle_term::Element;
21
22use crate::node::SyncReporting;
23use crate::node::SyncSettings;
24use crate::terminal as term;
25use crate::terminal::format::Author;
26use crate::terminal::{Table, TableOptions};
27
28pub use args::Args;
29use args::{Command, SortBy, SyncDirection, SyncMode};
30
31pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
32    let profile = ctx.profile()?;
33    let mut node = radicle::Node::new(profile.socket());
34    if !node.is_running() {
35        anyhow::bail!(
36            "to sync a repository, your node must be running. To start it, run `rad node start`"
37        );
38    }
39    let verbose = args.verbose;
40    let debug = args.verbose;
41
42    match args.command {
43        Some(Command::Status { rid, sort_by }) => {
44            let rid = match rid {
45                Some(rid) => rid,
46                None => {
47                    let (_, rid) = radicle::rad::cwd()
48                        .context("Current directory is not a Radicle repository")?;
49                    rid
50                }
51            };
52            sync_status(rid, &mut node, &profile, &sort_by, verbose)?;
53        }
54        None => match SyncMode::from(args.sync) {
55            SyncMode::Repo {
56                rid,
57                settings,
58                direction,
59            } => {
60                let rid = match rid {
61                    Some(rid) => rid,
62                    None => {
63                        let (_, rid) = radicle::rad::cwd()
64                            .context("Current directory is not a Radicle repository")?;
65                        rid
66                    }
67                };
68                let settings = settings.clone().with_profile(&profile);
69
70                if matches!(direction, SyncDirection::Fetch | SyncDirection::Both) {
71                    if !profile.policies()?.is_seeding(&rid)? {
72                        anyhow::bail!("repository {rid} is not seeded");
73                    }
74                    let result = fetch(rid, settings.clone(), &mut node, &profile)?;
75                    display_fetch_result(&result, verbose)
76                }
77                if matches!(direction, SyncDirection::Announce | SyncDirection::Both) {
78                    announce_refs(rid, settings, &mut node, &profile, verbose, debug)?;
79                }
80            }
81            SyncMode::Inventory => {
82                announce_inventory(node)?;
83            }
84        },
85    }
86
87    Ok(())
88}
89
90fn sync_status(
91    rid: RepoId,
92    node: &mut Node,
93    profile: &Profile,
94    sort_by: &SortBy,
95    verbose: bool,
96) -> anyhow::Result<()> {
97    const SYMBOL_STATE: &str = "?";
98    const SYMBOL_STATE_UNKNOWN: &str = "•";
99
100    let mut table = Table::<5, term::Label>::new(TableOptions::bordered());
101    let mut seeds: Vec<_> = node.seeds_for(rid, [*profile.did()])?.into();
102    let local_nid = node.nid()?;
103    let aliases = profile.aliases();
104
105    table.header([
106        term::format::bold("Node ID").into(),
107        term::format::bold("Alias").into(),
108        term::format::bold(SYMBOL_STATE).into(),
109        term::format::bold("SigRefs").into(),
110        term::format::bold("Timestamp").into(),
111    ]);
112    table.divider();
113
114    sort_seeds_by(local_nid, &mut seeds, &aliases, sort_by);
115
116    let seeds = seeds.into_iter().flat_map(|seed| {
117        let (status, head, time) = match seed.sync {
118            Some(SyncStatus::Synced {
119                at: SyncedAt { oid, timestamp },
120            }) => (
121                term::PREFIX_SUCCESS,
122                term::format::oid(oid),
123                term::format::timestamp(timestamp),
124            ),
125            Some(SyncStatus::OutOfSync {
126                remote: SyncedAt { timestamp, .. },
127                local,
128                ..
129            }) if seed.nid == local_nid => (
130                term::PREFIX_WARNING,
131                term::format::oid(local.oid),
132                term::format::timestamp(timestamp),
133            ),
134            Some(SyncStatus::OutOfSync {
135                remote: SyncedAt { oid, timestamp },
136                ..
137            }) => (
138                term::PREFIX_ERROR,
139                term::format::oid(oid),
140                term::format::timestamp(timestamp),
141            ),
142            None if verbose => (
143                term::format::dim(SYMBOL_STATE_UNKNOWN),
144                term::paint(String::new()),
145                term::paint(String::new()),
146            ),
147            None => return None,
148        };
149
150        let (alias, nid) = Author::new(&seed.nid, profile, verbose).labels();
151
152        Some([
153            nid,
154            alias,
155            status.into(),
156            term::format::secondary(head).into(),
157            time.dim().italic().into(),
158        ])
159    });
160
161    table.extend(seeds);
162    table.print();
163
164    if profile.hints() {
165        const COLUMN_WIDTH: usize = 16;
166        let status = format!(
167            "\n{:>4} … {}\n       {}   {}\n       {}   {}",
168            term::Paint::from(SYMBOL_STATE.to_string()).fg(radicle_term::Color::White),
169            term::format::dim("Status:"),
170            format_args!(
171                "{} {:width$}",
172                term::PREFIX_SUCCESS,
173                term::format::dim("… in sync"),
174                width = COLUMN_WIDTH,
175            ),
176            format_args!(
177                "{} {}",
178                term::PREFIX_ERROR,
179                term::format::dim("… out of sync")
180            ),
181            format_args!(
182                "{} {:width$}",
183                term::PREFIX_WARNING,
184                term::format::dim("… not announced"),
185                width = COLUMN_WIDTH,
186            ),
187            format_args!(
188                "{} {}",
189                term::format::dim(SYMBOL_STATE_UNKNOWN),
190                term::format::dim("… unknown")
191            ),
192        );
193        term::hint(status);
194    }
195
196    Ok(())
197}
198
199fn announce_refs(
200    rid: RepoId,
201    settings: SyncSettings,
202    node: &mut Node,
203    profile: &Profile,
204    verbose: bool,
205    debug: bool,
206) -> anyhow::Result<()> {
207    let Ok(repo) = profile.storage.repository(rid) else {
208        return Err(anyhow!(
209            "nothing to announce, repository {rid} is not available locally"
210        ));
211    };
212    if let Err(e) = repo.remote(&profile.public_key) {
213        if e.is_not_found() {
214            term::print(term::format::italic(
215                "Nothing to announce, you don't have a fork of this repository.",
216            ));
217            return Ok(());
218        } else {
219            return Err(anyhow!("failed to load local fork of {rid}: {e}"));
220        }
221    }
222
223    let result = crate::node::announce(
224        &repo,
225        settings,
226        SyncReporting {
227            debug,
228            ..SyncReporting::default()
229        },
230        node,
231        profile,
232    )?;
233    if let Some(result) = result {
234        print_announcer_result(&result, verbose)
235    }
236
237    Ok(())
238}
239
240pub fn announce_inventory(mut node: Node) -> anyhow::Result<()> {
241    let peers = node.sessions()?.iter().filter(|s| s.is_connected()).count();
242    let spinner = term::spinner(format!("Announcing inventory to {peers} peers.."));
243
244    node.announce_inventory()?;
245    spinner.finish();
246
247    Ok(())
248}
249
250#[derive(Debug, thiserror::Error)]
251pub enum FetchError {
252    #[error(transparent)]
253    Node(#[from] node::Error),
254    #[error(transparent)]
255    Db(#[from] node::db::Error),
256    #[error(transparent)]
257    Address(#[from] node::address::Error),
258    #[error(transparent)]
259    Fetcher(#[from] sync::FetcherError),
260}
261
262pub fn fetch(
263    rid: RepoId,
264    settings: SyncSettings,
265    node: &mut Node,
266    profile: &Profile,
267) -> Result<sync::FetcherResult, FetchError> {
268    let db = profile.database()?;
269    let local = profile.id();
270    let is_private = profile.storage.repository(rid).ok().and_then(|repo| {
271        let doc = repo.identity_doc().ok()?.doc;
272        sync::PrivateNetwork::private_repo(&doc)
273    });
274    let config = match is_private {
275        Some(private) => sync::FetcherConfig::private(private, settings.replicas, *local),
276        None => {
277            // We push nodes that are in our seed list in attempt to fulfill the
278            // replicas, if needed.
279            let seeds = node.seeds_for(rid, [*profile.did()])?;
280            let (connected, disconnected) = seeds.partition();
281            let candidates = connected
282                .into_iter()
283                .map(|seed| seed.nid)
284                .chain(disconnected.into_iter().filter_map(|seed| {
285                    // Only consider seeds that have at least one known address.
286                    (!seed.addrs.is_empty()).then_some(seed.nid)
287                }))
288                .map(sync::fetch::Candidate::new);
289            sync::FetcherConfig::public(settings.seeds.clone(), settings.replicas, *local)
290                .with_candidates(candidates)
291        }
292    };
293    let mut fetcher = sync::Fetcher::new(config)?;
294
295    let mut progress = fetcher.progress();
296    term::info!(
297        "Fetching {} from the network, found {} potential seed(s).",
298        term::format::tertiary(rid),
299        term::format::tertiary(progress.candidate())
300    );
301    let mut spinner = FetcherSpinner::new(fetcher.target(), &progress);
302
303    while let Some(nid) = fetcher.next_node() {
304        match node.session(nid)? {
305            Some(session) if session.is_connected() => fetcher.ready_to_fetch(nid, session.addr),
306            _ => {
307                let addrs = db.addresses_of(&nid)?;
308                if addrs.is_empty() {
309                    fetcher.fetch_failed(nid, "Could not connect. No addresses known.");
310                } else if let Some(addr) = connect(
311                    nid,
312                    addrs.into_iter().map(|ka| ka.addr),
313                    settings.timeout,
314                    node,
315                    &mut spinner,
316                    &fetcher.progress(),
317                ) {
318                    fetcher.ready_to_fetch(nid, addr)
319                } else {
320                    fetcher
321                        .fetch_failed(nid, "Could not connect. At least one address is known but all attempts timed out.");
322                }
323            }
324        }
325        if let Some((nid, addr)) = fetcher.next_fetch() {
326            spinner.emit_fetching(&nid, &addr, &progress);
327            let result = node.fetch(rid, nid, settings.timeout)?;
328            match fetcher.fetch_complete(nid, result) {
329                std::ops::ControlFlow::Continue(update) => {
330                    spinner.emit_progress(&update);
331                    progress = update
332                }
333                std::ops::ControlFlow::Break(success) => {
334                    spinner.finished(success.outcome());
335                    return Ok(sync::FetcherResult::TargetReached(success));
336                }
337            }
338        }
339    }
340    let result = fetcher.finish();
341    match &result {
342        sync::FetcherResult::TargetReached(success) => {
343            spinner.finished(success.outcome());
344        }
345        sync::FetcherResult::TargetError(missed) => spinner.failed(missed),
346    }
347    Ok(result)
348}
349
350// Try all addresses until one succeeds.
351// FIXME(fintohaps): I think this could return a `Result<node::Address,
352// Vec<AddressError>>` which could report back why each address failed
353fn connect(
354    nid: NodeId,
355    addrs: impl Iterator<Item = node::Address>,
356    timeout: time::Duration,
357    node: &mut Node,
358    spinner: &mut FetcherSpinner,
359    progress: &sync::fetch::Progress,
360) -> Option<node::Address> {
361    for addr in addrs {
362        spinner.emit_dialing(&nid, &addr, progress);
363        let cr = node.connect(
364            nid,
365            addr.clone(),
366            node::ConnectOptions {
367                persistent: false,
368                timeout,
369            },
370        );
371
372        match cr {
373            Ok(node::ConnectResult::Connected) => {
374                return Some(addr);
375            }
376            Ok(node::ConnectResult::Disconnected { .. }) => {
377                continue;
378            }
379            Err(e) => {
380                log::warn!(target: "cli", "Failed to connect to {nid}@{addr}: {e}");
381                continue;
382            }
383        }
384    }
385    None
386}
387
388fn sort_seeds_by(local: NodeId, seeds: &mut [Seed], aliases: &impl AliasStore, sort_by: &SortBy) {
389    let compare = |a: &Seed, b: &Seed| match sort_by {
390        SortBy::Nid => a.nid.cmp(&b.nid),
391        SortBy::Alias => {
392            let a = aliases.alias(&a.nid);
393            let b = aliases.alias(&b.nid);
394            a.cmp(&b)
395        }
396        SortBy::Status => match (&a.sync, &b.sync) {
397            (Some(_), None) => Ordering::Less,
398            (None, Some(_)) => Ordering::Greater,
399            (Some(a), Some(b)) => a.cmp(b).reverse(),
400            (None, None) => Ordering::Equal,
401        },
402    };
403
404    // Always show our local node first.
405    seeds.sort_by(|a, b| {
406        if a.nid == local {
407            Ordering::Less
408        } else if b.nid == local {
409            Ordering::Greater
410        } else {
411            compare(a, b)
412        }
413    });
414}
415
416struct FetcherSpinner {
417    preferred_seeds: usize,
418    replicas: sync::ReplicationFactor,
419    spinner: term::Spinner,
420}
421
422impl FetcherSpinner {
423    fn new(target: &sync::fetch::Target, progress: &sync::fetch::Progress) -> Self {
424        let preferred_seeds = target.preferred_seeds().len();
425        let replicas = target.replicas();
426        let spinner = term::spinner(format!(
427            "{} of {} preferred seeds, and {} of at least {} total seeds.",
428            term::format::secondary(progress.preferred()),
429            term::format::secondary(preferred_seeds),
430            term::format::secondary(progress.succeeded()),
431            term::format::secondary(replicas.lower_bound())
432        ));
433        Self {
434            preferred_seeds: target.preferred_seeds().len(),
435            replicas: *target.replicas(),
436            spinner,
437        }
438    }
439
440    fn emit_progress(&mut self, progress: &sync::fetch::Progress) {
441        self.spinner.message(format!(
442            "{} of {} preferred seeds, and {} of at least {} total seeds.",
443            term::format::secondary(progress.preferred()),
444            term::format::secondary(self.preferred_seeds),
445            term::format::secondary(progress.succeeded()),
446            term::format::secondary(self.replicas.lower_bound()),
447        ))
448    }
449
450    fn emit_fetching(
451        &mut self,
452        node: &NodeId,
453        addr: &node::Address,
454        progress: &sync::fetch::Progress,
455    ) {
456        self.spinner.message(format!(
457            "{} of {} preferred seeds, and {} of at least {} total seeds… [fetch {}@{}]",
458            term::format::secondary(progress.preferred()),
459            term::format::secondary(self.preferred_seeds),
460            term::format::secondary(progress.succeeded()),
461            term::format::secondary(self.replicas.lower_bound()),
462            term::format::tertiary(term::format::node_id_human_compact(node)),
463            term::format::tertiary(addr.display_compact()),
464        ))
465    }
466
467    fn emit_dialing(
468        &mut self,
469        node: &NodeId,
470        addr: &node::Address,
471        progress: &sync::fetch::Progress,
472    ) {
473        self.spinner.message(format!(
474            "{} of {} preferred seeds, and {} of at least {} total seeds… [dial {}@{}]",
475            term::format::secondary(progress.preferred()),
476            term::format::secondary(self.preferred_seeds),
477            term::format::secondary(progress.succeeded()),
478            term::format::secondary(self.replicas.lower_bound()),
479            term::format::tertiary(term::format::node_id_human_compact(node)),
480            term::format::tertiary(addr.display_compact()),
481        ))
482    }
483
484    fn finished(mut self, outcome: &SuccessfulOutcome) {
485        match outcome {
486            SuccessfulOutcome::PreferredNodes { preferred } => {
487                self.spinner.message(format!(
488                    "Target met: {} preferred seed(s).",
489                    term::format::positive(preferred),
490                ));
491            }
492            SuccessfulOutcome::MinReplicas { succeeded, .. } => {
493                self.spinner.message(format!(
494                    "Target met: {} seed(s)",
495                    term::format::positive(succeeded)
496                ));
497            }
498            SuccessfulOutcome::MaxReplicas {
499                succeeded,
500                min,
501                max,
502            } => {
503                self.spinner.message(format!(
504                    "Target met: {} of {} min and {} max seed(s)",
505                    succeeded,
506                    term::format::secondary(min),
507                    term::format::secondary(max)
508                ));
509            }
510        }
511        self.spinner.finish()
512    }
513
514    fn failed(mut self, missed: &sync::fetch::TargetMissed) {
515        let mut message = "Target not met: ".to_string();
516        let missing_preferred_seeds = missed
517            .missed_nodes()
518            .iter()
519            .map(|nid| term::format::node_id_human(nid).to_string())
520            .collect::<Vec<_>>();
521        let required = missed.required_nodes();
522        if !missing_preferred_seeds.is_empty() {
523            message.push_str(&format!(
524                "could not fetch from [{}], and required {} more seed(s)",
525                missing_preferred_seeds.join(", "),
526                required
527            ));
528        } else {
529            message.push_str(&format!("required {required} more seed(s)"));
530        }
531        self.spinner.message(message);
532        self.spinner.failed();
533    }
534}
535
536fn display_fetch_result(result: &sync::FetcherResult, verbose: bool) {
537    match result {
538        sync::FetcherResult::TargetReached(success) => {
539            let progress = success.progress();
540            let results = success.fetch_results();
541            display_success(results.success(), verbose);
542            let failed = progress.failed();
543            if failed > 0 && verbose {
544                term::warning(format!("Failed to fetch from {failed} seed(s)."));
545                for (node, reason) in results.failed() {
546                    term::warning(format!(
547                        "{}: {}",
548                        term::format::node_id_human(node),
549                        term::format::yellow(reason),
550                    ))
551                }
552            }
553        }
554        sync::FetcherResult::TargetError(failed) => {
555            let results = failed.fetch_results();
556            let progress = failed.progress();
557            let target = failed.target();
558            let succeeded = progress.succeeded();
559            let missed = failed.missed_nodes();
560            term::error(format!(
561                "Fetched from {} preferred seed(s), could not reach {} seed(s)",
562                succeeded,
563                target.replicas().lower_bound(),
564            ));
565            term::error(format!(
566                "Could not replicate from {} preferred seed(s)",
567                missed.len()
568            ));
569            for (node, reason) in results.failed() {
570                term::error(format!(
571                    "{}: {}",
572                    term::format::node_id_human(node),
573                    term::format::negative(reason),
574                ))
575            }
576            if succeeded > 0 {
577                term::info!("Successfully fetched from the following seeds:");
578                display_success(results.success(), verbose)
579            }
580        }
581    }
582}
583
584fn display_success<'a>(
585    results: impl Iterator<Item = (&'a NodeId, &'a [RefUpdate], HashSet<NodeId>)>,
586    verbose: bool,
587) {
588    for (node, updates, _) in results {
589        term::println(
590            "🌱 Fetched from",
591            term::format::secondary(term::format::node_id_human(node)),
592        );
593        if verbose {
594            let mut updates = updates
595                .iter()
596                .filter(|up| !matches!(up, RefUpdate::Skipped { .. }))
597                .peekable();
598            if updates.peek().is_none() {
599                term::indented(term::format::italic("no references were updated"));
600            } else {
601                for update in updates {
602                    term::indented(term::format::ref_update_verbose(update))
603                }
604            }
605        }
606    }
607}
608
609fn print_announcer_result(result: &sync::AnnouncerResult, verbose: bool) {
610    use sync::announce::SuccessfulOutcome::*;
611    match result {
612        sync::AnnouncerResult::Success(success) if verbose => {
613            // N.b. Printing how many seeds were synced with is printed
614            // elsewhere
615            match success.outcome() {
616                MinReplicationFactor { preferred, synced }
617                | MaxReplicationFactor { preferred, synced }
618                | PreferredNodes {
619                    preferred,
620                    total_nodes_synced: synced,
621                } => {
622                    if preferred == 0 {
623                        term::success!("Synced {} seed(s)", term::format::positive(synced));
624                    } else {
625                        term::success!(
626                            "Synced {} preferred seed(s) and {} total seed(s)",
627                            term::format::positive(preferred),
628                            term::format::positive(synced)
629                        );
630                    }
631                }
632            }
633            print_synced(success.synced());
634        }
635        sync::AnnouncerResult::Success(_) => {
636            // Successes are ignored when `!verbose`.
637        }
638        sync::AnnouncerResult::TimedOut(result) => {
639            if result.synced().is_empty() {
640                term::error("All seeds timed out, use `rad sync -v` to see the list of seeds");
641                return;
642            }
643            let timed_out = result.timed_out();
644            term::warning(format!(
645                "{} seed(s) timed out, use `rad sync -v` to see the list of seeds",
646                timed_out.len(),
647            ));
648            if verbose {
649                print_synced(result.synced());
650                for node in timed_out {
651                    term::warning(format!("{} timed out", term::format::node_id_human(node)));
652                }
653            }
654        }
655        sync::AnnouncerResult::NoNodes(result) => {
656            term::info!("Announcement could not sync with anymore seeds.");
657            if verbose {
658                print_synced(result.synced())
659            }
660        }
661    }
662}
663
664fn print_synced(synced: &BTreeMap<NodeId, sync::announce::SyncStatus>) {
665    for (node, status) in synced.iter() {
666        let mut message = format!("🌱 Synced with {}", term::format::node_id_human(node));
667
668        match status {
669            sync::announce::SyncStatus::AlreadySynced => {
670                message.push_str(&format!("{}", term::format::dim(" (already in sync)")));
671            }
672            sync::announce::SyncStatus::Synced { duration } => {
673                message.push_str(&format!(
674                    "{}",
675                    term::format::dim(format!(" in {}s", duration.as_secs()))
676                ));
677            }
678        }
679        term::info!("{}", message);
680    }
681}