node: Propagate refs update
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
parent
db6932d58c
commit
a70a6822e0
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue