Просмотр исходного кода

net: allow configuring a node_id. this is sent to all nodes with the version message for debugging purposes.

narodnik 4 лет назад
Родитель
Сommit
5cac77a67a
4 измененных файлов с 30 добавлено и 8 удалено
  1. 10 0
      src/net/channel.rs
  2. 9 5
      src/net/message.rs
  3. 4 3
      src/net/protocol/protocol_version.rs
  4. 7 0
      src/net/settings.rs

+ 10 - 0
src/net/channel.rs

@@ -27,6 +27,7 @@ pub type ChannelPtr = Arc<Channel>;
 
 struct ChannelInfo {
     random_id: u32,
+    remote_node_id: String,
     last_msg: String,
     last_status: String,
     // Message log which is cleared on querying get_info
@@ -37,6 +38,7 @@ impl ChannelInfo {
     fn new() -> Self {
         Self {
             random_id: rand::thread_rng().gen(),
+            remote_node_id: String::new(),
             last_msg: String::new(),
             last_status: String::new(),
             log: Mutex::new(Vec::new()),
@@ -46,6 +48,7 @@ impl ChannelInfo {
     async fn get_info(&self) -> serde_json::Value {
         let result = json!({
             "random_id": self.random_id,
+            "remote_node_id": self.remote_node_id,
             "last_msg": self.last_msg,
             "last_status": self.last_status,
             "log": self.log.lock().await.clone(),
@@ -232,6 +235,13 @@ impl Channel {
         self.address.clone()
     }
 
+    pub async fn remote_node_id(&self) -> String {
+        self.info.lock().await.remote_node_id.clone()
+    }
+    pub async fn set_remote_node_id(&self, remote_node_id: String) {
+        self.info.lock().await.remote_node_id = remote_node_id;
+    }
+
     /// End of file error. Triggered when unexpected end of file occurs.
     fn is_eof_error(err: Error) -> bool {
         match err {

+ 9 - 5
src/net/message.rs

@@ -36,7 +36,9 @@ pub struct AddrsMessage {
 }
 
 /// Requests version information of outbound connection.
-pub struct VersionMessage {}
+pub struct VersionMessage {
+    pub node_id: String,
+}
 
 /// Sends version information to inbound connection. Response to VersionMessage.
 pub struct VerackMessage {}
@@ -133,14 +135,16 @@ impl Decodable for AddrsMessage {
 }
 
 impl Encodable for VersionMessage {
-    fn encode<S: io::Write>(&self, _s: S) -> Result<usize> {
-        Ok(0)
+    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+        let mut len = 0;
+        len += self.node_id.encode(&mut s)?;
+        Ok(len)
     }
 }
 
 impl Decodable for VersionMessage {
-    fn decode<D: io::Read>(_d: D) -> Result<Self> {
-        Ok(Self {})
+    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
+        Ok(Self { node_id: Decodable::decode(&mut d)? })
     }
 }
 

+ 4 - 3
src/net/protocol/protocol_version.rs

@@ -75,7 +75,7 @@ impl ProtocolVersion {
     /// Send version info and wait for version acknowledgement.
     async fn send_version(self: Arc<Self>) -> Result<()> {
         debug!(target: "net", "ProtocolVersion::send_version() [START]");
-        let version = message::VersionMessage {};
+        let version = message::VersionMessage { node_id: self.settings.node_id.clone() };
         self.channel.clone().send(version).await?;
 
         // Wait for version acknowledgement
@@ -88,8 +88,9 @@ impl ProtocolVersion {
     /// acknowledgement.
     async fn recv_version(self: Arc<Self>) -> Result<()> {
         debug!(target: "net", "ProtocolVersion::recv_version() [START]");
-        // Rec
-        let _version_msg = self.version_sub.receive().await?;
+        // Receive version message
+        let version = self.version_sub.receive().await?;
+        self.channel.set_remote_node_id(version.node_id.clone()).await;
 
         // Check the message is OK
 

+ 7 - 0
src/net/settings.rs

@@ -21,6 +21,7 @@ pub struct Settings {
     pub external_addr: Option<Url>,
     pub peers: Vec<Url>,
     pub seeds: Vec<Url>,
+    pub node_id: String,
 }
 
 impl Default for Settings {
@@ -36,6 +37,7 @@ impl Default for Settings {
             external_addr: None,
             peers: Vec::new(),
             seeds: Vec::new(),
+            node_id: String::new(),
         }
     }
 }
@@ -76,6 +78,10 @@ pub struct SettingsOpt {
     pub channel_handshake_seconds: Option<u32>,
     #[structopt(skip)]
     pub channel_heartbeat_seconds: Option<u32>,
+
+    #[serde(default)]
+    #[structopt(skip)]
+    pub node_id: String,
 }
 
 impl From<SettingsOpt> for Settings {
@@ -91,6 +97,7 @@ impl From<SettingsOpt> for Settings {
             external_addr: settings_opt.external_addr,
             peers: settings_opt.peers,
             seeds: settings_opt.seeds,
+            node_id: settings_opt.node_id,
         }
     }
 }