Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: Moving `RepoSync` from `radicle-cli` to `radicle::node`
Sebastian Martinez committed 2 years ago
commit 936c69c9df5bfd5e1354cf8acd7fb72af54a95ba
parent 0908c65f504ec56bcb368b3a020ddee5f251eb56
5 files changed +62 -59
modified radicle-cli/src/commands/clone.rs
@@ -61,7 +61,7 @@ pub struct Options {
    /// The seeding scope of the repository.
    scope: Scope,
    /// Sync settings.
-
    sync: sync::RepoSync,
+
    sync: node::seed::RepoSync,
    /// Fetch timeout.
    timeout: time::Duration,
}
@@ -73,7 +73,7 @@ impl Args for Options {
        let mut parser = lexopt::Parser::from_args(args);
        let mut id: Option<RepoId> = None;
        let mut scope = Scope::All;
-
        let mut sync = sync::RepoSync::default();
+
        let mut sync = node::seed::RepoSync::default();
        let mut timeout = time::Duration::from_secs(9);
        let mut directory = None;

@@ -235,7 +235,7 @@ pub fn clone<G: Signer>(
    id: RepoId,
    directory: Option<PathBuf>,
    scope: Scope,
-
    settings: sync::RepoSync,
+
    settings: node::seed::RepoSync,
    timeout: time::Duration,
    node: &mut Node,
    signer: &G,
modified radicle-cli/src/commands/remote/add.rs
@@ -3,6 +3,7 @@ use std::time;

use radicle::git;
use radicle::git::RefString;
+
use radicle::node;
use radicle::prelude::*;
use radicle::Profile;
use radicle_crypto::PublicKey;
@@ -31,7 +32,7 @@ pub fn run(
            follow::follow(*nid, alias, &mut node, profile)?;
            sync::fetch(
                rid,
-
                sync::RepoSync::default().with_profile(profile),
+
                node::seed::RepoSync::default().with_profile(profile),
                time::Duration::from_secs(9),
                &mut node,
            )?;
modified radicle-cli/src/commands/seed.rs
@@ -3,6 +3,7 @@ use std::time;

use anyhow::anyhow;

+
use radicle::node;
use radicle::node::policy;
use radicle::node::policy::Scope;
use radicle::node::Handle;
@@ -137,7 +138,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
            if fetch && node.is_running() {
                sync::fetch(
                    rid,
-
                    sync::RepoSync::default().with_profile(&profile),
+
                    node::seed::RepoSync::default().with_profile(&profile),
                    time::Duration::from_secs(6),
                    &mut node,
                )?;
modified radicle-cli/src/commands/sync.rs
@@ -8,9 +8,8 @@ use std::time;
use anyhow::{anyhow, Context as _};

use radicle::node;
-
use radicle::node::AliasStore;
-
use radicle::node::Seed;
-
use radicle::node::{FetchResult, FetchResults, Handle as _, Node, SyncStatus};
+
use radicle::node::seed::RepoSync;
+
use radicle::node::{AliasStore, FetchResult, FetchResults, Handle as _, Node, Seed, SyncStatus};
use radicle::prelude::{NodeId, Profile, RepoId};
use radicle::storage::{ReadRepository, ReadStorage};
use radicle_term::Element;
@@ -114,52 +113,6 @@ impl Default for SyncMode {
    }
}

-
/// Repository sync settings.
-
#[derive(Debug, Clone, PartialEq, Eq)]
-
pub struct RepoSync {
-
    /// Sync with at least N replicas.
-
    pub replicas: usize,
-
    /// Sync with the given list of seeds.
-
    pub seeds: BTreeSet<NodeId>,
-
}
-

-
impl RepoSync {
-
    pub fn from_seeds(seeds: impl IntoIterator<Item = NodeId>) -> Self {
-
        let seeds = BTreeSet::from_iter(seeds);
-
        Self {
-
            replicas: seeds.len(),
-
            seeds,
-
        }
-
    }
-

-
    /// Use profile to populate sync settings, by adding preferred seeds if no seeds are specified,
-
    /// and removing the local node from the set.
-
    pub fn with_profile(mut self, profile: &Profile) -> Self {
-
        // If no seeds were specified, add up to `replica` seeds from the preferred seeds.
-
        if self.seeds.is_empty() {
-
            self.seeds = profile
-
                .config
-
                .preferred_seeds
-
                .iter()
-
                .map(|p| p.id)
-
                .take(self.replicas)
-
                .collect();
-
        }
-
        // Remove our local node from the seed set just in case it was added by mistake.
-
        self.seeds.remove(profile.id());
-
        self
-
    }
-
}
-

-
impl Default for RepoSync {
-
    fn default() -> Self {
-
        Self {
-
            replicas: 3,
-
            seeds: BTreeSet::new(),
-
        }
-
    }
-
}
-

#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub enum SyncDirection {
    Fetch,
@@ -547,14 +500,14 @@ pub fn fetch(

    // Fetch from connected seeds.
    let connected = connected
-
        .into_iter()
+
        .iter()
        .filter(|c| !results.contains(&c.nid))
-
        .map(|c| c.nid)
+
        .map(|c| &c.nid)
        .take(replicas)
        .collect::<Vec<_>>();
    for nid in connected {
-
        let result = fetch_from(rid, &nid, timeout, node)?;
-
        results.push(nid, result);
+
        let result = fetch_from(rid, nid, timeout, node)?;
+
        results.push(*nid, result);
    }

    // Try to connect to disconnected seeds and fetch from them.
modified radicle/src/node/seed.rs
@@ -1,12 +1,14 @@
pub mod store;
+
use std::collections::BTreeSet;
+

pub use store::{Error, Store};

use localtime::LocalTime;

-
use crate::git;
use crate::node::KnownAddress;
use crate::prelude::NodeId;
use crate::storage::{refs::RefsAt, ReadRepository, RemoteId};
+
use crate::{git, Profile};

/// Holds an oid and timestamp.
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -60,3 +62,49 @@ pub struct SyncedSeed {
    /// Sync information for a given repo.
    pub synced_at: SyncedAt,
}
+

+
/// Repository sync settings.
+
#[derive(Debug, Clone, PartialEq, Eq)]
+
pub struct RepoSync {
+
    /// Sync with at least N replicas.
+
    pub replicas: usize,
+
    /// Sync with the given list of seeds.
+
    pub seeds: BTreeSet<NodeId>,
+
}
+

+
impl RepoSync {
+
    pub fn from_seeds(seeds: impl IntoIterator<Item = NodeId>) -> Self {
+
        let seeds = BTreeSet::from_iter(seeds);
+
        Self {
+
            replicas: seeds.len(),
+
            seeds,
+
        }
+
    }
+

+
    /// Use profile to populate sync settings, by adding preferred seeds if no seeds are specified,
+
    /// and removing the local node from the set.
+
    pub fn with_profile(mut self, profile: &Profile) -> Self {
+
        // If no seeds were specified, add up to `replica` seeds from the preferred seeds.
+
        if self.seeds.is_empty() {
+
            self.seeds = profile
+
                .config
+
                .preferred_seeds
+
                .iter()
+
                .map(|p| p.id)
+
                .take(self.replicas)
+
                .collect();
+
        }
+
        // Remove our local node from the seed set just in case it was added by mistake.
+
        self.seeds.remove(profile.id());
+
        self
+
    }
+
}
+

+
impl Default for RepoSync {
+
    fn default() -> Self {
+
        Self {
+
            replicas: 3,
+
            seeds: BTreeSet::new(),
+
        }
+
    }
+
}