Преглед изворни кода

channel: expand ChannelInfo to include resolve_addr and connect_addr

This paves the way for DEP 0001: https://darkrenaissance.github.io/darkfi/dep/0001.html

We retain the same method for returning the channel's address, channel.address() but modify it so that it adapts depending on whether this channel is inbound or outbound. The usage remains the same and the return value is equivalent.
draoi пре 2 година
родитељ
комит
b9edcc6077
5 измењених фајлова са 38 додато и 13 уклоњено
  1. 1 1
      src/net/acceptor.rs
  2. 29 8
      src/net/channel.rs
  3. 2 1
      src/net/connector.rs
  4. 2 2
      src/net/session/inbound_session.rs
  5. 4 1
      src/rpc/from_impl.rs

+ 1 - 1
src/net/acceptor.rs

@@ -134,7 +134,7 @@ impl Acceptor {
 
 
                     // Create the new Channel.
                     // Create the new Channel.
                     let session = self.session.clone();
                     let session = self.session.clone();
-                    let channel = Channel::new(stream, url, session).await;
+                    let channel = Channel::new(stream, None, url, session).await;
 
 
                     // Increment the connection counter
                     // Increment the connection counter
                     self.conn_count.fetch_add(1, SeqCst);
                     self.conn_count.fetch_add(1, SeqCst);

+ 29 - 8
src/net/channel.rs

@@ -57,13 +57,14 @@ pub type ChannelPtr = Arc<Channel>;
 /// Channel debug info
 /// Channel debug info
 #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
 #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
 pub struct ChannelInfo {
 pub struct ChannelInfo {
-    pub addr: Url,
+    pub resolve_addr: Option<Url>,
+    pub connect_addr: Url,
     pub id: u32,
     pub id: u32,
 }
 }
 
 
 impl ChannelInfo {
 impl ChannelInfo {
-    fn new(addr: Url) -> Self {
-        Self { addr, id: OsRng.gen() }
+    fn new(resolve_addr: Option<Url>, connect_addr: Url) -> Self {
+        Self { resolve_addr, connect_addr, id: OsRng.gen() }
     }
     }
 }
 }
 
 
@@ -91,7 +92,12 @@ impl Channel {
     /// Sets up a new channel. Creates a reader and writer [`PtStream`] and
     /// Sets up a new channel. Creates a reader and writer [`PtStream`] and
     /// the message subscriber subsystem. Performs a network handshake on the
     /// the message subscriber subsystem. Performs a network handshake on the
     /// subsystem dispatchers.
     /// subsystem dispatchers.
-    pub async fn new(stream: Box<dyn PtStream>, addr: Url, session: SessionWeakPtr) -> Arc<Self> {
+    pub async fn new(
+        stream: Box<dyn PtStream>,
+        resolve_addr: Option<Url>,
+        connect_addr: Url,
+        session: SessionWeakPtr,
+    ) -> Arc<Self> {
         let (reader, writer) = io::split(stream);
         let (reader, writer) = io::split(stream);
         let reader = Mutex::new(reader);
         let reader = Mutex::new(reader);
         let writer = Mutex::new(writer);
         let writer = Mutex::new(writer);
@@ -99,7 +105,7 @@ impl Channel {
         let message_subsystem = MessageSubsystem::new();
         let message_subsystem = MessageSubsystem::new();
         Self::setup_dispatchers(&message_subsystem).await;
         Self::setup_dispatchers(&message_subsystem).await;
 
 
-        let info = ChannelInfo::new(addr.clone());
+        let info = ChannelInfo::new(resolve_addr, connect_addr.clone());
 
 
         Arc::new(Self {
         Arc::new(Self {
             reader,
             reader,
@@ -320,9 +326,24 @@ impl Channel {
         debug!(target: "net::channel::ban()", "STOP {:?}", self);
         debug!(target: "net::channel::ban()", "STOP {:?}", self);
     }
     }
 
 
-    /// Returns the local socket address
+    /// Returns the relevant socket address for this connection.  If this is
+    /// an outbound connection, the transport-processed resolve_addr will
+    /// be returned.  Otherwise for inbound connections it will default
+    /// to connect_addr.
     pub fn address(&self) -> &Url {
     pub fn address(&self) -> &Url {
-        &self.info.addr
+        if self.info.resolve_addr.is_some() {
+            self.resolve_addr()
+        } else {
+            self.connect_addr()
+        }
+    }
+
+    fn resolve_addr(&self) -> &Url {
+        &self.info.resolve_addr.as_ref().unwrap()
+    }
+
+    fn connect_addr(&self) -> &Url {
+        &self.info.connect_addr
     }
     }
 
 
     /// Returns the inner [`MessageSubsystem`] reference
     /// Returns the inner [`MessageSubsystem`] reference
@@ -353,6 +374,6 @@ impl Channel {
 
 
 impl fmt::Debug for Channel {
 impl fmt::Debug for Channel {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "<Channel addr='{}' id={}>", self.info.addr, self.info.id)
+        write!(f, "<Channel addr='{}' id={}>", self.address(), self.info.id)
     }
     }
 }
 }

+ 2 - 1
src/net/connector.rs

@@ -80,7 +80,8 @@ impl Connector {
         let timeout = Duration::from_secs(self.settings.outbound_connect_timeout);
         let timeout = Duration::from_secs(self.settings.outbound_connect_timeout);
         let ptstream = dialer.dial(Some(timeout)).await?;
         let ptstream = dialer.dial(Some(timeout)).await?;
 
 
-        let channel = Channel::new(ptstream, endpoint.clone(), self.session.clone()).await;
+        let channel =
+            Channel::new(ptstream, Some(endpoint.clone()), url.clone(), self.session.clone()).await;
         Ok((endpoint, channel))
         Ok((endpoint, channel))
     }
     }
 }
 }

+ 2 - 2
src/net/session/inbound_session.rs

@@ -189,7 +189,7 @@ impl InboundSession {
         );
         );
 
 
         dnetev!(self, InboundConnected, {
         dnetev!(self, InboundConnected, {
-            addr: channel.info.addr.clone(),
+            addr: channel.info.connect_addr.clone(),
             channel_id: channel.info.id,
             channel_id: channel.info.id,
         });
         });
 
 
@@ -207,7 +207,7 @@ impl InboundSession {
         );
         );
 
 
         dnetev!(self, InboundDisconnected, {
         dnetev!(self, InboundDisconnected, {
-            addr: channel.info.addr.clone(),
+            addr: channel.info.connect_addr.clone(),
             channel_id: channel.info.id,
             channel_id: channel.info.id,
         });
         });
 
 

+ 4 - 1
src/rpc/from_impl.rs

@@ -25,7 +25,10 @@ use crate::event_graph;
 #[cfg(feature = "net")]
 #[cfg(feature = "net")]
 impl From<net::channel::ChannelInfo> for JsonValue {
 impl From<net::channel::ChannelInfo> for JsonValue {
     fn from(info: net::channel::ChannelInfo) -> JsonValue {
     fn from(info: net::channel::ChannelInfo) -> JsonValue {
-        json_map([("addr", JsonStr(info.addr.to_string())), ("id", JsonNum(info.id.into()))])
+        json_map([
+            ("addr", JsonStr(info.connect_addr.to_string())),
+            ("id", JsonNum(info.id.into())),
+        ])
     }
     }
 }
 }