From a70a6822e0e598bc33cedf747a22630c70bf8538 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Fri, 9 Sep 2022 14:49:00 +0200 Subject: [PATCH] node: Propagate refs update Signed-off-by: Alexis Sellier --- node/src/protocol.rs | 16 +++------------- node/src/protocol/peer.rs | 28 ++++++++++++++-------------- node/src/storage/refs.rs | 13 ++++++++++--- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/node/src/protocol.rs b/node/src/protocol.rs index 34008f1c..a0de6df5 100644 --- a/node/src/protocol.rs +++ b/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 { // 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. diff --git a/node/src/protocol/peer.rs b/node/src/protocol/peer.rs index 47dfc2ee..b1e24296 100644 --- a/node/src/protocol/peer.rs +++ b/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); diff --git a/node/src/storage/refs.rs b/node/src/storage/refs.rs index 54a39475..399c30e8 100644 --- a/node/src/storage/refs.rs +++ b/node/src/storage/refs.rs @@ -178,9 +178,7 @@ impl SignedRefs { } pub fn verified(self, signer: &PublicKey) -> Result, 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 { 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 {