Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
node: Propagate refs update
Alexis Sellier committed 3 years ago
commit a70a6822e0e598bc33cedf747a22630c70bf8538
parent db6932d58ca22165fe8a438da519f1eee897b530
3 files changed +27 -30
modified node/src/protocol.rs
@@ -22,7 +22,7 @@ use crate::address_manager::AddressManager;
use crate::clock::RefClock;
use crate::collections::{HashMap, HashSet};
use crate::crypto;
-
use crate::identity::{Id, Project, PublicKey};
+
use crate::identity::{Id, Project};
use crate::protocol::config::ProjectTracking;
use crate::protocol::message::Message;
use crate::protocol::peer::{Peer, PeerError, PeerState};
@@ -729,17 +729,7 @@ where
        }
    }

-
    /// Process a peer inventory update announcement by (maybe) fetching.
-
    fn process_refs_update(&mut self, id: &Id, _user: &PublicKey, remote: &Url) -> bool {
-
        // TODO: Check that we're tracking this user as well.
-
        if self.config.is_tracking(id) {
-
            self.fetch(id, remote);
-
        }
-
        // TODO: If refs were updated, return `true`.
-
        false
-
    }
-

-
    fn fetch(&mut self, proj_id: &Id, remote: &Url) {
+
    fn fetch(&mut self, proj_id: &Id, remote: &Url) -> Vec<RefUpdate> {
        // TODO: Verify refs before adding them to storage.
        let mut repo = self.storage.repository(proj_id).unwrap();
        let mut path = remote.path.clone();
@@ -751,7 +741,7 @@ where
            path,
            ..remote.clone()
        })
-
        .unwrap();
+
        .unwrap()
    }

    /// Disconnect a peer.
modified node/src/protocol/peer.rs
@@ -194,21 +194,21 @@ impl Peer {
                    }));
                }
            }
-
            (
-
                PeerState::Negotiated { git, .. },
-
                Message::RefsUpdate {
-
                    id: proj,
-
                    signer,
-
                    refs,
-
                },
-
            ) => {
-
                if refs.verified(&signer).is_ok() {
+
            // Process a peer inventory update announcement by (maybe) fetching.
+
            (PeerState::Negotiated { git, .. }, Message::RefsUpdate { id, signer, refs }) => {
+
                if let Ok(refs) = refs.verified(&signer) {
                    // TODO: Buffer/throttle fetches.
-
                    // TODO: Also pass the updated refs so that we can check whether
-
                    // we need to fetch or not.
-
                    // TODO: Check that the refs are valid.
-
                    if ctx.process_refs_update(&proj, &signer, git) {
-
                        // TODO: If refs were updated, propagate message to peers.
+
                    // TODO: Check that we're tracking this user as well.
+
                    if ctx.config.is_tracking(&id) {
+
                        // TODO: Check refs to see if we should try to fetch or not.
+
                        let updated = ctx.fetch(&id, git);
+
                        if !updated.is_empty() {
+
                            return Ok(Some(Message::RefsUpdate {
+
                                id,
+
                                signer,
+
                                refs: refs.unverified(),
+
                            }));
+
                        }
                    }
                } else {
                    return Err(PeerError::Misbehavior);
modified node/src/storage/refs.rs
@@ -178,9 +178,7 @@ impl SignedRefs<Unverified> {
    }

    pub fn verified(self, signer: &PublicKey) -> Result<SignedRefs<Verified>, crypto::Error> {
-
        let canonical = self.refs.canonical();
-

-
        match signer.verify(&self.signature, &canonical) {
+
        match self.verify(signer) {
            Ok(()) => Ok(SignedRefs {
                refs: self.refs,
                signature: self.signature,
@@ -189,6 +187,15 @@ impl SignedRefs<Unverified> {
            Err(e) => Err(e),
        }
    }
+

+
    pub fn verify(&self, signer: &PublicKey) -> Result<(), crypto::Error> {
+
        let canonical = self.refs.canonical();
+

+
        match signer.verify(&self.signature, &canonical) {
+
            Ok(()) => Ok(()),
+
            Err(e) => Err(e),
+
        }
+
    }
}

impl SignedRefs<Verified> {