From 671c1692442170429cb7bdd2ddcc6480ee4e71ad Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 18 Apr 2023 20:43:43 +0200 Subject: [PATCH] node: Implement `rad sync` command This command announces refs to peers and waits for them to be in sync. Adds a subscribe method to `Handle` so that we can get the events from the seed to allow us to confirm that the configured seeds fetched from us. --- radicle-cli/examples/rad-sync.md | 27 ++++ radicle-cli/src/commands.rs | 2 + radicle-cli/src/commands/help.rs | 1 + radicle-cli/src/commands/sync.rs | 146 ++++++++++++++++++ radicle-cli/src/main.rs | 8 + radicle-cli/tests/commands.rs | 44 ++++++ radicle-node/src/control.rs | 35 ++++- radicle-node/src/runtime/handle.rs | 9 +- radicle-node/src/service.rs | 3 +- radicle-node/src/test/handle.rs | 22 ++- radicle-term/src/io.rs | 12 ++ radicle/src/node.rs | 93 ++++++++--- .../service => radicle/src/node}/events.rs | 16 +- 13 files changed, 375 insertions(+), 43 deletions(-) create mode 100644 radicle-cli/examples/rad-sync.md create mode 100644 radicle-cli/src/commands/sync.rs rename {radicle-node/src/service => radicle/src/node}/events.rs (84%) diff --git a/radicle-cli/examples/rad-sync.md b/radicle-cli/examples/rad-sync.md new file mode 100644 index 00000000..eaa85eb1 --- /dev/null +++ b/radicle-cli/examples/rad-sync.md @@ -0,0 +1,27 @@ +The `rad sync` command announces changes to the network and waits for other +nodes to be synchronized with those changes. + +For instance let's create an issue and sync it with the network: + +``` +$ rad issue open --title "Test `rad sync`" --description "Check that the command works" -q --no-announce +``` + +Now let's run `rad sync`. This will announce the issue refs to the network and +wait for nodes to announce that they have fetched those refs. + +``` +$ rad sync +✓ Synced with 2 node(s) +``` + +If we try to sync again after the nodes have synced, we will get a timeout +after one second, since the nodes will not emit any message: + +``` +$ rad sync --timeout 1 +✗ Syncing with 2 node(s).. +! Seed z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk timed out.. +! Seed z6Mkux1aUQD2voWWukVb5nNUR7thrHveQG4pDQua8nVhib7Z timed out.. +✗ Sync failed: all seeds timed out +``` diff --git a/radicle-cli/src/commands.rs b/radicle-cli/src/commands.rs index 621ae673..1e86d3c2 100644 --- a/radicle-cli/src/commands.rs +++ b/radicle-cli/src/commands.rs @@ -46,6 +46,8 @@ pub mod rad_review; pub mod rad_rm; #[path = "commands/self.rs"] pub mod rad_self; +#[path = "commands/sync.rs"] +pub mod rad_sync; #[path = "commands/tag.rs"] pub mod rad_tag; #[path = "commands/track.rs"] diff --git a/radicle-cli/src/commands/help.rs b/radicle-cli/src/commands/help.rs index 7d0e5d47..659d484a 100644 --- a/radicle-cli/src/commands/help.rs +++ b/radicle-cli/src/commands/help.rs @@ -40,6 +40,7 @@ const COMMANDS: &[Help] = &[ rad_untag::HELP, rad_untrack::HELP, rad_remote::HELP, + rad_sync::HELP, ]; #[derive(Default)] diff --git a/radicle-cli/src/commands/sync.rs b/radicle-cli/src/commands/sync.rs new file mode 100644 index 00000000..30894c9b --- /dev/null +++ b/radicle-cli/src/commands/sync.rs @@ -0,0 +1,146 @@ +use std::collections::BTreeSet; +use std::ffi::OsString; +use std::path::Path; +use std::{io, time}; + +use anyhow::{anyhow, Context as _}; + +use radicle::node::Event; +use radicle::node::Handle as _; +use radicle::prelude::{Id, NodeId}; + +use crate::terminal as term; +use crate::terminal::args::{Args, Error, Help}; + +pub const HELP: Help = Help { + name: "sync", + description: "Sync repositories to the network", + version: env!("CARGO_PKG_VERSION"), + usage: r#" +Usage + + rad sync [] [