Skip to main content

radicle/node/
seed.rs

1pub mod store;
2pub use store::{Error, Store};
3
4use localtime::LocalTime;
5
6use crate::node::KnownAddress;
7use crate::prelude::NodeId;
8use crate::storage::{refs::RefsAt, ReadRepository, RemoteId};
9
10/// Holds an oid and timestamp.
11#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
12#[serde(rename_all = "camelCase")]
13#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
14pub struct SyncedAt {
15    /// Head of `rad/sigrefs`.
16    pub oid: crate::git::Oid,
17    /// When these refs were synced.
18    pub timestamp: LocalTime,
19}
20
21impl SyncedAt {
22    /// Load a new [`SyncedAt`] for the given remote.
23    pub fn load<S: ReadRepository>(
24        repo: &S,
25        remote: RemoteId,
26    ) -> Result<Self, crate::git::raw::Error> {
27        let refs = RefsAt::new(repo, remote)?;
28        let oid = refs.at;
29
30        Self::new(oid, repo)
31    }
32
33    /// Create a new [`SyncedAt`] given an OID, by looking up the timestamp in the repo.
34    pub fn new<S: ReadRepository>(
35        oid: crate::git::Oid,
36        repo: &S,
37    ) -> Result<Self, crate::git::raw::Error> {
38        let timestamp = repo.commit(oid)?.time();
39        let timestamp = LocalTime::from_secs(timestamp.seconds() as u64);
40
41        Ok(Self { oid, timestamp })
42    }
43}
44
45impl Ord for SyncedAt {
46    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
47        self.timestamp.cmp(&other.timestamp)
48    }
49}
50
51impl PartialOrd for SyncedAt {
52    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
53        Some(self.cmp(other))
54    }
55}
56
57/// Seed of a specific repository that has been synced at least once.
58#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
59#[serde(rename_all = "camelCase")]
60pub struct SyncedSeed {
61    /// The Node ID.
62    pub nid: NodeId,
63    /// Known addresses for this node.
64    pub addresses: Vec<KnownAddress>,
65    /// Sync information for a given repo.
66    pub synced_at: SyncedAt,
67}