node: Implement auto-fetching of projects

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier
2022-09-13 10:16:05 +02:00
parent a5a7af57b5
commit 92f17d71c3
6 changed files with 148 additions and 64 deletions
+14 -9
View File
@@ -63,7 +63,10 @@ where
pub fn new(name: &'static str, ip: impl Into<net::IpAddr>, storage: S) -> Self {
Self::config(
name,
Config::default(),
Config {
git_url: storage.url(),
..Config::default()
},
ip,
vec![],
storage,
@@ -143,20 +146,22 @@ where
.expect("`get-inventory` is sent");
}
pub fn connect_to(&mut self, remote: &net::SocketAddr) {
self.initialize();
self.protocol.attempted(remote);
self.protocol
.connected(*remote, &self.local_addr, Link::Outbound);
pub fn connect_to(&mut self, peer: &Self) {
let remote = simulator::Peer::<Protocol<S>>::addr(peer);
let mut msgs = self.messages(remote);
self.initialize();
self.protocol.attempted(&remote);
self.protocol
.connected(remote, &self.local_addr, Link::Outbound);
let mut msgs = self.messages(&remote);
msgs.find(|m| matches!(m, Message::Hello { .. }))
.expect("`hello` is sent");
msgs.find(|m| matches!(m, Message::GetInventory { .. }))
.expect("`get-inventory` is sent");
let git = self.config().git_url.clone();
self.receive(remote, Message::hello(self.local_time().as_secs(), git));
let git = peer.config().git_url.clone();
self.receive(&remote, Message::hello(self.local_time().as_secs(), git));
}
/// Drain outgoing messages sent from this peer to the remote address.
+10 -2
View File
@@ -23,6 +23,14 @@ impl MockStorage {
}
impl ReadStorage for MockStorage {
fn url(&self) -> Url {
Url {
scheme: git_url::Scheme::Radicle,
host: Some("mock".to_string()),
..Url::default()
}
}
fn get(&self, proj: &ProjId) -> Result<Option<Remotes<Unverified>>, Error> {
if let Some((_, refs)) = self.inventory.iter().find(|(id, _)| id == proj) {
return Ok(Some(refs.clone()));
@@ -45,7 +53,7 @@ impl WriteStorage for MockStorage {
type Repository = MockRepository;
fn repository(&self, _proj: &ProjId) -> Result<Self::Repository, Error> {
todo!()
Ok(MockRepository {})
}
}
@@ -53,7 +61,7 @@ pub struct MockRepository {}
impl WriteRepository for MockRepository {
fn fetch(&mut self, _url: &Url) -> Result<(), git2::Error> {
todo!()
Ok(())
}
fn namespace(
+31 -9
View File
@@ -31,8 +31,8 @@ fn test_outbound_connection() {
let bob = Peer::new("bob", [9, 9, 9, 9], MockStorage::empty());
let eve = Peer::new("eve", [7, 7, 7, 7], MockStorage::empty());
alice.connect_to(&bob.addr());
alice.connect_to(&eve.addr());
alice.connect_to(&bob);
alice.connect_to(&eve);
let peers = alice
.protocol
@@ -120,20 +120,42 @@ fn test_wrong_peer_magic() {
}
#[test]
fn test_inventory_fetch() {
let mut alice = Peer::new("alice", [7, 7, 7, 7], MockStorage::empty());
let bob = Peer::new("bob", [8, 8, 8, 8], MockStorage::empty());
fn test_inventory_sync() {
let tmp = tempfile::tempdir().unwrap();
let mut alice = Peer::new(
"alice",
[7, 7, 7, 7],
Storage::open(tmp.path().join("alice")).unwrap(),
);
let bob_storage = fixtures::storage(tmp.path().join("bob"));
let bob = Peer::new("bob", [8, 8, 8, 8], bob_storage);
let now = LocalTime::now().as_secs();
let projs = bob.storage().inventory().unwrap();
alice.connect_to(&bob.addr());
alice.connect_to(&bob);
alice.receive(
&bob.addr(),
Message::Inventory {
timestamp: now,
inv: vec![],
inv: projs.clone(),
origin: None,
},
);
for proj in &projs {
let providers = alice.routing().get(proj).unwrap();
assert!(providers.contains(&bob.ip));
}
let a = alice
.storage()
.inventory()
.unwrap()
.into_iter()
.collect::<HashSet<_>>();
let b = projs.into_iter().collect::<HashSet<_>>();
assert_eq!(a, b);
}
#[test]
@@ -143,7 +165,7 @@ fn test_inventory_relay_bad_timestamp() {
let two_hours = 3600 * 2;
let timestamp = alice.local_time.as_secs() - two_hours;
alice.connect_to(&bob.addr());
alice.connect_to(&bob);
alice.receive(
&bob.addr(),
Message::Inventory {
@@ -169,7 +191,7 @@ fn test_inventory_relay() {
let now = LocalTime::now().as_secs();
// Inventory from Bob relayed to Eve.
alice.connect_to(&bob.addr());
alice.connect_to(&bob);
alice.connect_from(&eve.addr());
alice.receive(
&bob.addr(),