diff --git a/radicle-cli/src/project.rs b/radicle-cli/src/project.rs index 8d6e2cc4..1d472c72 100644 --- a/radicle-cli/src/project.rs +++ b/radicle-cli/src/project.rs @@ -59,12 +59,13 @@ pub fn add_inventory( node: &mut Node, profile: &Profile, ) -> Result { - match node.update_inventory(rid) { + match node.add_inventory(rid) { Ok(updated) => Ok(updated), Err(e) if e.is_connection_err() => { let now = LocalTime::now(); let mut db = profile.database_mut()?; - let updates = routing::Store::insert(&mut db, [&rid], *profile.id(), now.into())?; + let updates = + routing::Store::add_inventory(&mut db, [&rid], *profile.id(), now.into())?; Ok(!updates.is_empty()) } @@ -91,7 +92,7 @@ pub fn seed( let now = LocalTime::now(); let mut db = profile.database_mut()?; - routing::Store::insert(&mut db, [&rid], *profile.id(), now.into())?; + routing::Store::add_inventory(&mut db, [&rid], *profile.id(), now.into())?; } Ok(result) } @@ -109,7 +110,7 @@ pub fn unseed(rid: RepoId, node: &mut Node, profile: &Profile) -> Result match handle.update_inventory(rid) { + Command::AddInventory { rid } => match handle.add_inventory(rid) { Ok(result) => { CommandResult::updated(result).to_writer(writer)?; } diff --git a/radicle-node/src/runtime/handle.rs b/radicle-node/src/runtime/handle.rs index 5e60e40e..f8a5e545 100644 --- a/radicle-node/src/runtime/handle.rs +++ b/radicle-node/src/runtime/handle.rs @@ -260,9 +260,9 @@ impl radicle::node::Handle for Handle { .map_err(Error::from) } - fn update_inventory(&mut self, rid: RepoId) -> Result { + fn add_inventory(&mut self, rid: RepoId) -> Result { let (sender, receiver) = chan::bounded(1); - self.command(service::Command::UpdateInventory(rid, sender))?; + self.command(service::Command::AddInventory(rid, sender))?; receiver.recv().map_err(Error::from) } diff --git a/radicle-node/src/service.rs b/radicle-node/src/service.rs index 30089657..98d7957f 100644 --- a/radicle-node/src/service.rs +++ b/radicle-node/src/service.rs @@ -216,8 +216,8 @@ pub enum Command { AnnounceRefs(RepoId, chan::Sender), /// Announce local repositories to peers. AnnounceInventory, - /// Update local inventory. - UpdateInventory(RepoId, chan::Sender), + /// Add repository to local inventory. + AddInventory(RepoId, chan::Sender), /// Connect to node with the given address. Connect(NodeId, Address, ConnectOptions), /// Disconnect from node. @@ -247,7 +247,7 @@ impl fmt::Debug for Command { match self { Self::AnnounceRefs(id, _) => write!(f, "AnnounceRefs({id})"), Self::AnnounceInventory => write!(f, "AnnounceInventory"), - Self::UpdateInventory(rid, _) => write!(f, "UpdateInventory({rid})"), + Self::AddInventory(rid, _) => write!(f, "AddInventory({rid})"), Self::Connect(id, addr, opts) => write!(f, "Connect({id}, {addr}, {opts:?})"), Self::Disconnect(id) => write!(f, "Disconnect({id})"), Self::Config(_) => write!(f, "Config"), @@ -734,7 +734,7 @@ where // user creates a new repository while the node is stopped. self.db .routing_mut() - .insert(inventory.iter(), nid, time.into())?; + .add_inventory(inventory.iter(), nid, time.into())?; self.inventory = gossip::inventory(self.timestamp(), inventory); } @@ -943,7 +943,7 @@ where Command::AnnounceInventory => { self.announce_inventory(); } - Command::UpdateInventory(rid, resp) => match self.add_inventory(rid) { + Command::AddInventory(rid, resp) => match self.add_inventory(rid) { Ok(updated) => { resp.send(updated).ok(); } @@ -1890,7 +1890,7 @@ where /// Add a seed to our routing table. fn seed_discovered(&mut self, rid: RepoId, nid: NodeId, time: Timestamp) { - if let Ok(result) = self.db.routing_mut().insert([&rid], nid, time) { + if let Ok(result) = self.db.routing_mut().add_inventory([&rid], nid, time) { if let &[(_, InsertResult::SeedAdded)] = result.as_slice() { self.emitter.emit(Event::SeedDiscovered { rid, nid }); info!(target: "service", "Routing table updated for {} with seed {nid}", rid); @@ -1940,8 +1940,7 @@ where let node = self.node_id(); let now = self.timestamp(); - // Remove inventory. - let removed = self.db.routing_mut().remove(rid, &node)?; + let removed = self.db.routing_mut().remove_inventory(rid, &node)?; if removed { // Update cached inventory message. let inventory = self.inventory()?; @@ -1960,8 +1959,8 @@ where error!(target: "service", "Attempt to add non-existing inventory {rid}: repository not found in storage"); return Ok(false); } - // Add to our inventory. - let updates = self.db.routing_mut().insert([&rid], node, now)?; + // Add to our local inventory. + let updates = self.db.routing_mut().add_inventory([&rid], node, now)?; let updated = !updates.is_empty(); if updated { @@ -1986,7 +1985,7 @@ where fn inventory(&self) -> Result, Error> { self.db .routing() - .get_resources(self.nid()) + .get_inventory(self.nid()) .map_err(Error::from) } @@ -2002,10 +2001,10 @@ where let mut synced = SyncedRouting::default(); let included = inventory.into_iter().collect::>(); - for (rid, result) in self - .db - .routing_mut() - .insert(included.iter(), from, timestamp)? + for (rid, result) in + self.db + .routing_mut() + .add_inventory(included.iter(), from, timestamp)? { match result { InsertResult::SeedAdded => { @@ -2028,9 +2027,9 @@ where InsertResult::NotUpdated => {} } } - for rid in self.db.routing().get_resources(&from)?.into_iter() { + for rid in self.db.routing().get_inventory(&from)?.into_iter() { if !included.contains(&rid) { - if self.db.routing_mut().remove(&rid, &from)? { + if self.db.routing_mut().remove_inventory(&rid, &from)? { synced.removed.push(rid); self.emitter.emit(Event::SeedDropped { rid, nid: from }); } diff --git a/radicle-node/src/test/handle.rs b/radicle-node/src/test/handle.rs index 1a3cb9b2..7b3247f1 100644 --- a/radicle-node/src/test/handle.rs +++ b/radicle-node/src/test/handle.rs @@ -104,7 +104,7 @@ impl radicle::node::Handle for Handle { Ok(()) } - fn update_inventory(&mut self, _rid: RepoId) -> Result { + fn add_inventory(&mut self, _rid: RepoId) -> Result { unimplemented!() } diff --git a/radicle-node/src/test/peer.rs b/radicle-node/src/test/peer.rs index 2e6f1671..9ff14016 100644 --- a/radicle-node/src/test/peer.rs +++ b/radicle-node/src/test/peer.rs @@ -269,7 +269,7 @@ where self.service .database() .routing() - .get_resources(self.nid()) + .get_inventory(self.nid()) .unwrap() } diff --git a/radicle-node/src/tests.rs b/radicle-node/src/tests.rs index 1da0a0bd..e09b5e9e 100644 --- a/radicle-node/src/tests.rs +++ b/radicle-node/src/tests.rs @@ -1680,7 +1680,7 @@ fn test_init_and_seed() { // We now expect Eve to fetch Alice's project from Alice. // Then we expect Bob to fetch Alice's project from Eve. alice.elapse(LocalDuration::from_secs(1)); // Make sure our announcement is fresh. - alice.command(service::Command::UpdateInventory(proj_id, send)); + alice.command(service::Command::AddInventory(proj_id, send)); sim.run_while([&mut alice, &mut bob, &mut eve], |s| !s.is_settled()); @@ -1925,7 +1925,7 @@ fn test_announcement_message_amplification() { .storage_mut() .repos .insert(rid, gen::(1)); - alice.command(Command::UpdateInventory(rid, tx)); + alice.command(Command::AddInventory(rid, tx)); sim.run_while([&mut alice, &mut bob, &mut eve, &mut zod, &mut tom], |s| { s.elapsed() < LocalDuration::from_mins(3) diff --git a/radicle/src/node.rs b/radicle/src/node.rs index 443564b2..8ba21249 100644 --- a/radicle/src/node.rs +++ b/radicle/src/node.rs @@ -463,7 +463,7 @@ pub enum Command { AnnounceInventory, /// Update node's inventory. - UpdateInventory { rid: RepoId }, + AddInventory { rid: RepoId }, /// Get the current node condiguration. Config, @@ -924,7 +924,7 @@ pub trait Handle: Clone + Sync + Send { /// Announce local inventory. fn announce_inventory(&mut self) -> Result<(), Self::Error>; /// Notify the service that our inventory was updated with the given repository. - fn update_inventory(&mut self, rid: RepoId) -> Result; + fn add_inventory(&mut self, rid: RepoId) -> Result; /// Ask the service to shutdown. fn shutdown(self) -> Result<(), Self::Error>; /// Query the peer session state. @@ -1224,8 +1224,8 @@ impl Handle for Node { Ok(()) } - fn update_inventory(&mut self, rid: RepoId) -> Result { - let mut lines = self.call::(Command::UpdateInventory { rid }, DEFAULT_TIMEOUT)?; + fn add_inventory(&mut self, rid: RepoId) -> Result { + let mut lines = self.call::(Command::AddInventory { rid }, DEFAULT_TIMEOUT)?; let response = lines.next().ok_or(Error::EmptyResponse)??; Ok(response.updated) diff --git a/radicle/src/node/routing.rs b/radicle/src/node/routing.rs index 0b222431..625661f8 100644 --- a/radicle/src/node/routing.rs +++ b/radicle/src/node/routing.rs @@ -36,8 +36,8 @@ pub enum Error { pub trait Store { /// Get the nodes seeding the given id. fn get(&self, id: &RepoId) -> Result, Error>; - /// Get the resources seeded by the given node. - fn get_resources(&self, node_id: &NodeId) -> Result, Error>; + /// Get the inventory seeded by the given node. + fn get_inventory(&self, node_id: &NodeId) -> Result, Error>; /// Get a specific entry. fn entry(&self, id: &RepoId, node: &NodeId) -> Result, Error>; /// Checks if any entries are available. @@ -45,14 +45,14 @@ pub trait Store { Ok(self.len()? == 0) } /// Add a new node seeding the given id. - fn insert<'a>( + fn add_inventory<'a>( &mut self, ids: impl IntoIterator, node: NodeId, time: Timestamp, ) -> Result, Error>; /// Remove a node for the given id. - fn remove(&mut self, id: &RepoId, node: &NodeId) -> Result; + fn remove_inventory(&mut self, id: &RepoId, node: &NodeId) -> Result; /// Iterate over all entries in the routing table. fn entries(&self) -> Result>, Error>; /// Get the total number of routing entries. @@ -77,15 +77,15 @@ impl Store for Database { Ok(nodes) } - fn get_resources(&self, node: &NodeId) -> Result, Error> { + fn get_inventory(&self, node: &NodeId) -> Result, Error> { let mut stmt = self.db.prepare("SELECT repo FROM routing WHERE node = ?")?; stmt.bind((1, node))?; - let mut resources = HashSet::new(); + let mut inventory = HashSet::new(); for row in stmt.into_iter() { - resources.insert(row?.read::("repo")); + inventory.insert(row?.read::("repo")); } - Ok(resources) + Ok(inventory) } fn entry(&self, id: &RepoId, node: &NodeId) -> Result, Error> { @@ -102,7 +102,7 @@ impl Store for Database { Ok(None) } - fn insert<'a>( + fn add_inventory<'a>( &mut self, ids: impl IntoIterator, node: NodeId, @@ -160,7 +160,7 @@ impl Store for Database { Ok(Box::new(entries.into_iter())) } - fn remove(&mut self, id: &RepoId, node: &NodeId) -> Result { + fn remove_inventory(&mut self, id: &RepoId, node: &NodeId) -> Result { let mut stmt = self .db .prepare("DELETE FROM routing WHERE repo = ? AND node = ?")?; @@ -242,7 +242,7 @@ mod test { for node in &nodes { assert_eq!( - db.insert(&ids, *node, Timestamp::EPOCH).unwrap(), + db.add_inventory(&ids, *node, Timestamp::EPOCH).unwrap(), ids.iter() .map(|id| (*id, InsertResult::SeedAdded)) .collect::>() @@ -264,11 +264,11 @@ mod test { let mut db = database(":memory:"); for node in &nodes { - db.insert(&ids, *node, Timestamp::EPOCH).unwrap(); + db.add_inventory(&ids, *node, Timestamp::EPOCH).unwrap(); } for node in &nodes { - let projects = db.get_resources(node).unwrap(); + let projects = db.get_inventory(node).unwrap(); for id in &ids { assert!(projects.contains(id)); } @@ -283,7 +283,7 @@ mod test { for node in &nodes { assert!(db - .insert(&ids, *node, Timestamp::EPOCH) + .add_inventory(&ids, *node, Timestamp::EPOCH) .unwrap() .iter() .all(|(_, r)| *r == InsertResult::SeedAdded)); @@ -305,11 +305,11 @@ mod test { let mut db = database(":memory:"); for node in &nodes { - db.insert(&ids, *node, Timestamp::EPOCH).unwrap(); + db.add_inventory(&ids, *node, Timestamp::EPOCH).unwrap(); } for id in &ids { for node in &nodes { - assert!(db.remove(id, node).unwrap()); + assert!(db.remove_inventory(id, node).unwrap()); } } for id in &ids { @@ -324,15 +324,15 @@ mod test { let mut db = database(":memory:"); assert_eq!( - db.insert([&id], node, Timestamp::EPOCH).unwrap(), + db.add_inventory([&id], node, Timestamp::EPOCH).unwrap(), vec![(id, InsertResult::SeedAdded)] ); assert_eq!( - db.insert([&id], node, Timestamp::EPOCH).unwrap(), + db.add_inventory([&id], node, Timestamp::EPOCH).unwrap(), vec![(id, InsertResult::NotUpdated)] ); assert_eq!( - db.insert([&id], node, Timestamp::EPOCH).unwrap(), + db.add_inventory([&id], node, Timestamp::EPOCH).unwrap(), vec![(id, InsertResult::NotUpdated)] ); } @@ -344,11 +344,11 @@ mod test { let mut db = database(":memory:"); assert_eq!( - db.insert([&id], node, Timestamp::EPOCH).unwrap(), + db.add_inventory([&id], node, Timestamp::EPOCH).unwrap(), vec![(id, InsertResult::SeedAdded)] ); assert_eq!( - db.insert([&id], node, Timestamp::try_from(1u64).unwrap()) + db.add_inventory([&id], node, Timestamp::try_from(1u64).unwrap()) .unwrap(), vec![(id, InsertResult::TimeUpdated)] ); @@ -366,18 +366,19 @@ mod test { let mut db = database(":memory:"); assert_eq!( - db.insert([&id1], node, Timestamp::EPOCH).unwrap(), + db.add_inventory([&id1], node, Timestamp::EPOCH).unwrap(), vec![(id1, InsertResult::SeedAdded)] ); assert_eq!( - db.insert([&id1, &id2], node, Timestamp::EPOCH).unwrap(), + db.add_inventory([&id1, &id2], node, Timestamp::EPOCH) + .unwrap(), vec![ (id1, InsertResult::NotUpdated), (id2, InsertResult::SeedAdded) ] ); assert_eq!( - db.insert([&id1, &id2], node, Timestamp::try_from(1u64).unwrap()) + db.add_inventory([&id1, &id2], node, Timestamp::try_from(1u64).unwrap()) .unwrap(), vec![ (id1, InsertResult::TimeUpdated), @@ -393,11 +394,11 @@ mod test { let mut db = database(":memory:"); assert_eq!( - db.insert([&id], node, Timestamp::EPOCH).unwrap(), + db.add_inventory([&id], node, Timestamp::EPOCH).unwrap(), vec![(id, InsertResult::SeedAdded)] ); - assert!(db.remove(&id, &node).unwrap()); - assert!(!db.remove(&id, &node).unwrap()); + assert!(db.remove_inventory(&id, &node).unwrap()); + assert!(!db.remove_inventory(&id, &node).unwrap()); } #[test] @@ -406,7 +407,8 @@ mod test { let ids = arbitrary::vec::(10); let node = arbitrary::gen(1); - db.insert(&ids, node, LocalTime::now().into()).unwrap(); + db.add_inventory(&ids, node, LocalTime::now().into()) + .unwrap(); assert_eq!(10, db.len().unwrap(), "correct number of rows in table"); } @@ -421,7 +423,7 @@ mod test { for node in &nodes { let time = rng.u64(..now.as_millis()); - db.insert(&ids, *node, Timestamp::try_from(time).unwrap()) + db.add_inventory(&ids, *node, Timestamp::try_from(time).unwrap()) .unwrap(); } @@ -430,7 +432,7 @@ mod test { for node in &nodes { let time = rng.u64(now.as_millis()..i64::MAX as u64); - db.insert(&ids, *node, Timestamp::try_from(time).unwrap()) + db.add_inventory(&ids, *node, Timestamp::try_from(time).unwrap()) .unwrap(); } @@ -452,7 +454,7 @@ mod test { let mut db = database(":memory:"); for node in &nodes { - db.insert([&id], *node, Timestamp::EPOCH).unwrap(); + db.add_inventory([&id], *node, Timestamp::EPOCH).unwrap(); } assert_eq!(db.count(&id).unwrap(), nodes.len()); }