node: Update routing table on fetch

Since a user can force a fetch of a private repository without any
announcement being involved, we update the routing table if the fetch
completes successfully, with the remote node.
This commit is contained in:
cloudhead 2024-04-18 10:51:23 +02:00
parent f17df88229
commit e5d8563cca
No known key found for this signature in database
1 changed files with 17 additions and 15 deletions

View File

@ -1042,6 +1042,9 @@ where
doc, doc,
}) => { }) => {
info!(target: "service", "Fetched {rid} from {remote} successfully"); info!(target: "service", "Fetched {rid} from {remote} successfully");
// Update our routing table in case this fetch was user-initiated and doesn't
// come from an announcement.
self.seed_discovered(rid, remote, self.time());
for update in &updated { for update in &updated {
if update.is_skipped() { if update.is_skipped() {
@ -1305,7 +1308,6 @@ where
/// and `false` if it should not. /// and `false` if it should not.
pub fn handle_announcement( pub fn handle_announcement(
&mut self, &mut self,
relayer: &NodeId,
relayer_addr: &Address, relayer_addr: &Address,
announcement: &Announcement, announcement: &Announcement,
) -> Result<bool, session::Error> { ) -> Result<bool, session::Error> {
@ -1442,19 +1444,7 @@ where
}; };
// We update inventories when receiving ref announcements, as these could come // We update inventories when receiving ref announcements, as these could come
// from a new repository being initialized. // from a new repository being initialized.
if let Ok(result) = self.seed_discovered(message.rid, *announcer, message.timestamp);
self.db
.routing_mut()
.insert([&message.rid], *announcer, message.timestamp)
{
if let &[(_, InsertResult::SeedAdded)] = result.as_slice() {
self.emitter.emit(Event::SeedDiscovered {
rid: message.rid,
nid: *relayer,
});
info!(target: "service", "Routing table updated for {} with seed {announcer}", message.rid);
}
}
// Update sync status of announcer for this repo. // Update sync status of announcer for this repo.
if let Some(refs) = refs.iter().find(|r| &r.remote == self.nid()) { if let Some(refs) = refs.iter().find(|r| &r.remote == self.nid()) {
@ -1555,6 +1545,8 @@ where
timestamp, timestamp,
addresses addresses
.iter() .iter()
// Ignore non-routable addresses unless received from a local network
// peer. This allows the node to function in a local network.
.filter(|a| a.is_routable() || relayer_addr.is_local()) .filter(|a| a.is_routable() || relayer_addr.is_local())
.map(|a| KnownAddress::new(a.clone(), address::Source::Peer)), .map(|a| KnownAddress::new(a.clone(), address::Source::Peer)),
) { ) {
@ -1628,7 +1620,7 @@ where
let announcer = ann.node; let announcer = ann.node;
// Returning true here means that the message should be relayed. // Returning true here means that the message should be relayed.
if self.handle_announcement(&relayer, &relayer_addr, &ann)? { if self.handle_announcement(&relayer_addr, &ann)? {
// Choose peers we should relay this message to. // Choose peers we should relay this message to.
// 1. Don't relay to the peer who sent us this message. // 1. Don't relay to the peer who sent us this message.
// 2. Don't relay to the peer who signed this announcement. // 2. Don't relay to the peer who signed this announcement.
@ -1749,6 +1741,16 @@ where
Ok(refs) Ok(refs)
} }
/// 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 &[(_, InsertResult::SeedAdded)] = result.as_slice() {
self.emitter.emit(Event::SeedDiscovered { rid, nid });
info!(target: "service", "Routing table updated for {} with seed {nid}", rid);
}
}
}
/// Set of initial messages to send to a peer. /// Set of initial messages to send to a peer.
fn initial(&self, _link: Link) -> Vec<Message> { fn initial(&self, _link: Link) -> Vec<Message> {
let filter = self.filter(); let filter = self.filter();