node: Handle refs update announcement

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier
2022-09-13 10:16:05 +02:00
parent ff2bd185e7
commit 14395ad873
8 changed files with 90 additions and 24 deletions
+13 -6
View File
@@ -4,7 +4,7 @@ use git_url::Url;
use serde::{Deserialize, Serialize};
use crate::crypto;
use crate::identity::ProjId;
use crate::identity::{ProjId, UserId};
use crate::protocol::{Context, NodeId, Timestamp, PROTOCOL_VERSION};
use crate::storage;
@@ -86,9 +86,7 @@ pub enum Message {
announcement: NodeAnnouncement,
},
/// Get a peer's inventory.
GetInventory {
ids: Vec<ProjId>,
},
GetInventory { ids: Vec<ProjId> },
/// Send our inventory to a peer. Sent in response to [`Message::GetInventory`].
/// Nb. This should be the whole inventory, not a partial update.
Inventory {
@@ -98,8 +96,17 @@ pub enum Message {
/// are the originator, only when relaying.
origin: Option<NodeId>,
},
InventoryUpdate {
inv: Vec<ProjId>,
/// Project refs were updated. Includes the signature of the user who updated
/// their view of the project.
RefsUpdate {
/// Project under which the refs were updated.
proj: ProjId,
/// User signing.
user: UserId,
/// Updated refs.
refs: storage::Refs,
/// Signature over the refs.
signature: crypto::Signature,
},
}
+22 -3
View File
@@ -194,9 +194,28 @@ impl Peer {
}));
}
}
(PeerState::Negotiated { id, git, .. }, Message::InventoryUpdate { inv }) => {
// TODO: Buffer/throttle fetches.
ctx.process_inventory_update(&inv, *id, git);
(
PeerState::Negotiated { git, .. },
Message::RefsUpdate {
proj,
user,
refs,
signature,
},
) => {
let bytes = serde_json::to_vec(&refs).unwrap();
if user.verify(&signature, &bytes).is_ok() {
// 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, &user, git) {
// TODO: If refs were updated, propagate message to peers.
}
} else {
return Err(PeerError::Misbehavior);
}
}
(
PeerState::Negotiated { .. },