node: Propagate refs update

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-09-09 14:49:00 +02:00
parent db6932d58c
commit a70a6822e0
No known key found for this signature in database
3 changed files with 27 additions and 30 deletions

View File

@ -22,7 +22,7 @@ use crate::address_manager::AddressManager;
use crate::clock::RefClock; use crate::clock::RefClock;
use crate::collections::{HashMap, HashSet}; use crate::collections::{HashMap, HashSet};
use crate::crypto; use crate::crypto;
use crate::identity::{Id, Project, PublicKey}; use crate::identity::{Id, Project};
use crate::protocol::config::ProjectTracking; use crate::protocol::config::ProjectTracking;
use crate::protocol::message::Message; use crate::protocol::message::Message;
use crate::protocol::peer::{Peer, PeerError, PeerState}; use crate::protocol::peer::{Peer, PeerError, PeerState};
@ -729,17 +729,7 @@ where
} }
} }
/// Process a peer inventory update announcement by (maybe) fetching. fn fetch(&mut self, proj_id: &Id, remote: &Url) -> Vec<RefUpdate> {
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) {
// TODO: Verify refs before adding them to storage. // TODO: Verify refs before adding them to storage.
let mut repo = self.storage.repository(proj_id).unwrap(); let mut repo = self.storage.repository(proj_id).unwrap();
let mut path = remote.path.clone(); let mut path = remote.path.clone();
@ -751,7 +741,7 @@ where
path, path,
..remote.clone() ..remote.clone()
}) })
.unwrap(); .unwrap()
} }
/// Disconnect a peer. /// Disconnect a peer.

View File

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

View File

@ -178,9 +178,7 @@ impl SignedRefs<Unverified> {
} }
pub fn verified(self, signer: &PublicKey) -> Result<SignedRefs<Verified>, crypto::Error> { pub fn verified(self, signer: &PublicKey) -> Result<SignedRefs<Verified>, crypto::Error> {
let canonical = self.refs.canonical(); match self.verify(signer) {
match signer.verify(&self.signature, &canonical) {
Ok(()) => Ok(SignedRefs { Ok(()) => Ok(SignedRefs {
refs: self.refs, refs: self.refs,
signature: self.signature, signature: self.signature,
@ -189,6 +187,15 @@ impl SignedRefs<Unverified> {
Err(e) => Err(e), 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> { impl SignedRefs<Verified> {