Browse Source

net: add peer to the anchorlist with an updated last_seen when we call p2p.store() on a connected channel

lunar-mining 2 years ago
parent
commit
03ae65956a
2 changed files with 16 additions and 6 deletions
  1. 9 3
      src/net/p2p.rs
  2. 7 3
      src/net/session/mod.rs

+ 9 - 3
src/net/p2p.rs

@@ -232,10 +232,16 @@ impl P2p {
     }
 
     /// Add a channel to the set of connected channels
-    pub(super) async fn store(&self, channel: ChannelPtr) {
-        // TODO: Check the code path for this, and potentially also insert the remote
-        // into the hosts list?
+    pub(super) async fn store(&self, channel: ChannelPtr, last_seen: u64) {
         self.channels.lock().await.insert(channel.address().clone(), channel.clone());
+
+        // TODO: check for duplicates.
+        // TODO: FIXME: unwrap
+        self.hosts()
+            .anchorlist_store_or_update(&[(channel.address().clone(), last_seen)])
+            .await
+            .unwrap();
+
         self.channel_subscriber.notify(Ok(channel)).await;
     }
 

+ 7 - 3
src/net/session/mod.rs

@@ -16,7 +16,10 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use std::sync::{Arc, Weak};
+use std::{
+    sync::{Arc, Weak},
+    time::UNIX_EPOCH,
+};
 
 use async_trait::async_trait;
 use log::debug;
@@ -132,10 +135,11 @@ pub trait Session: Sync {
         // Perform handshake
         protocol_version.run(executor.clone()).await?;
 
-        // Channel is now initialized
+        // Channel is now initialized. Timestamp this.
+        let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
 
         // Add channel to p2p
-        self.p2p().store(channel.clone()).await;
+        self.p2p().store(channel.clone(), last_seen).await;
 
         // Subscribe to stop, so we can remove from p2p
         executor.spawn(remove_sub_on_stop(self.p2p(), channel)).detach();