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

net: implement DEP-0001

implement the upgrade specified at: https://darkrenaissance.github.io/darkfi/dep/0001.html

The `(services, version)` field is only partially implemented- we simply
send an empty vector in the Version message. Later this can be used to
enable features in protocols.
draoi пре 2 година
родитељ
комит
b7c11c2bed
3 измењених фајлова са 40 додато и 7 уклоњено
  1. 8 5
      src/net/channel.rs
  2. 16 0
      src/net/message.rs
  3. 16 2
      src/net/protocol/protocol_version.rs

+ 8 - 5
src/net/channel.rs

@@ -332,17 +332,20 @@ impl Channel {
     /// to connect_addr.
     pub fn address(&self) -> &Url {
         if self.info.resolve_addr.is_some() {
-            self.resolve_addr()
+            self.info.resolve_addr.as_ref().unwrap()
         } else {
-            self.connect_addr()
+            &self.info.connect_addr
         }
     }
 
-    fn resolve_addr(&self) -> &Url {
-        &self.info.resolve_addr.as_ref().unwrap()
+    /// Returns the socket address that has undergone transport
+    /// processing, if it exists. Returns None otherwise.
+    pub fn resolve_addr(&self) -> Option<Url> {
+        self.info.resolve_addr.clone()
     }
 
-    fn connect_addr(&self) -> &Url {
+    /// Return the socket address without transport processing.
+    pub fn connect_addr(&self) -> &Url {
         &self.info.connect_addr
     }
 

+ 16 - 0
src/net/message.rs

@@ -82,6 +82,22 @@ impl_p2p_message!(AddrsMessage, "addr");
 pub struct VersionMessage {
     /// Only used for debugging. Compromises privacy when set.
     pub node_id: String,
+    /// Identifies protocol version being used by the node
+    pub version: semver::Version,
+    /// UNIX timestamp of when the VersionMessage was created.
+    pub timestamp: u64,
+    /// Network address of the node receiving this message (before
+    /// resolving).
+    pub connect_recv_addr: Url,
+    /// Network address of the node receiving this message (after
+    /// resolving). Optional because only used by outbound connections.
+    pub resolve_recv_addr: Option<Url>,
+    /// External address of the sender node, if it exists (empty
+    /// otherwise).
+    pub ext_send_addr: Vec<Url>,
+    /// List of features consisting of a tuple of (services, version)
+    /// to be enabled for this connection
+    pub features: Vec<(String, u32)>,
 }
 impl_p2p_message!(VersionMessage, "version");
 

+ 16 - 2
src/net/protocol/protocol_version.rs

@@ -16,7 +16,10 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use std::{sync::Arc, time::Duration};
+use std::{
+    sync::Arc,
+    time::{Duration, UNIX_EPOCH},
+};
 
 use futures::future::join_all;
 use log::{debug, error};
@@ -126,7 +129,18 @@ impl ProtocolVersion {
             "START => address={}", self.channel.address(),
         );
 
-        let version = VersionMessage { node_id: self.settings.node_id.clone() };
+        let version = VersionMessage {
+            node_id: self.settings.node_id.clone(),
+            version: self.settings.app_version.clone(),
+            timestamp: UNIX_EPOCH.elapsed().unwrap().as_secs(),
+            connect_recv_addr: self.channel.connect_addr().clone(),
+            resolve_recv_addr: self.channel.resolve_addr().clone(),
+            ext_send_addr: self.settings.external_addrs.clone(),
+            /* TODO: `features` is a list of enabled features in the
+            format Vec<(service, version)>.  Protocols will add their
+            own data to this field when they are attached.*/
+            features: vec![],
+        };
         self.channel.send(&version).await?;
 
         // Wait for verack