Przeglądaj źródła

error: more descriptive error handling for state transitions

draoi 2 lat temu
rodzic
commit
c2941c4726

+ 2 - 2
src/error.rs

@@ -188,8 +188,8 @@ pub enum Error {
     #[error("No matching hostlist entry")]
     #[error("No matching hostlist entry")]
     HostDoesNotExist,
     HostDoesNotExist,
 
 
-    #[error("Attempted state change was blocked: {0}")]
-    StateBlocked(String),
+    #[error("Invalid state transition: current_state={0}, end_state={0}")]
+    HostStateBlocked(String, String),
 
 
     // =============
     // =============
     // Crypto errors
     // Crypto errors

+ 3 - 1
src/net/hosts/refinery.rs

@@ -134,7 +134,9 @@ impl GreylistRefinery {
                 Some((entry, position)) => {
                 Some((entry, position)) => {
                     let url = &entry.0;
                     let url = &entry.0;
 
 
-                    if hosts.try_register(url.clone(), HostState::Refine).await.is_err() {
+                    if let Err(e) = hosts.try_register(url.clone(), HostState::Refine).await {
+                        debug!(target: "net::refinery", "Unable to refine addr={}, err={}",
+                               url.clone(), e);
                         continue
                         continue
                     }
                     }
 
 

+ 52 - 38
src/net/hosts/store.rs

@@ -119,13 +119,15 @@ impl HostState {
     // Try to change state to Insert. Only possible if we are not yet
     // Try to change state to Insert. Only possible if we are not yet
     // tracking this host in the HostRegistry.
     // tracking this host in the HostRegistry.
     fn try_insert(&self) -> Result<Self> {
     fn try_insert(&self) -> Result<Self> {
+        let start = self.to_string();
+        let end = HostState::Insert.to_string();
         match self {
         match self {
-            HostState::Insert => Err(Error::StateBlocked(self.to_string())),
-            HostState::Refine => Err(Error::StateBlocked(self.to_string())),
-            HostState::Connect => Err(Error::StateBlocked(self.to_string())),
-            HostState::Suspend => Err(Error::StateBlocked(self.to_string())),
-            HostState::Connected(_) => Err(Error::StateBlocked(self.to_string())),
-            HostState::Move(_) => Err(Error::StateBlocked(self.to_string())),
+            HostState::Insert => Err(Error::HostStateBlocked(start, end)),
+            HostState::Refine => Err(Error::HostStateBlocked(start, end)),
+            HostState::Connect => Err(Error::HostStateBlocked(start, end)),
+            HostState::Suspend => Err(Error::HostStateBlocked(start, end)),
+            HostState::Connected(_) => Err(Error::HostStateBlocked(start, end)),
+            HostState::Move(_) => Err(Error::HostStateBlocked(start, end)),
         }
         }
     }
     }
 
 
@@ -133,26 +135,30 @@ impl HostState {
     // tracking this host in the HostRegistry or if the host is marked as
     // tracking this host in the HostRegistry or if the host is marked as
     // Suspend i.e. we have failed to connect to it.
     // Suspend i.e. we have failed to connect to it.
     fn try_refine(&self) -> Result<Self> {
     fn try_refine(&self) -> Result<Self> {
+        let start = self.to_string();
+        let end = HostState::Refine.to_string();
         match self {
         match self {
-            HostState::Insert => Err(Error::StateBlocked(self.to_string())),
-            HostState::Refine => Err(Error::StateBlocked(self.to_string())),
-            HostState::Connect => Err(Error::StateBlocked(self.to_string())),
+            HostState::Insert => Err(Error::HostStateBlocked(start, end)),
+            HostState::Refine => Err(Error::HostStateBlocked(start, end)),
+            HostState::Connect => Err(Error::HostStateBlocked(start, end)),
             HostState::Suspend => Ok(HostState::Refine),
             HostState::Suspend => Ok(HostState::Refine),
-            HostState::Connected(_) => Err(Error::StateBlocked(self.to_string())),
-            HostState::Move(_) => Err(Error::StateBlocked(self.to_string())),
+            HostState::Connected(_) => Err(Error::HostStateBlocked(start, end)),
+            HostState::Move(_) => Err(Error::HostStateBlocked(start, end)),
         }
         }
     }
     }
 
 
     // Try to change state to Connect. Only possible if we are not yet
     // Try to change state to Connect. Only possible if we are not yet
     // tracking this host in the HostRegistry.
     // tracking this host in the HostRegistry.
     fn try_connect(&self) -> Result<Self> {
     fn try_connect(&self) -> Result<Self> {
+        let start = self.to_string();
+        let end = HostState::Connect.to_string();
         match self {
         match self {
-            HostState::Insert => Err(Error::StateBlocked(self.to_string())),
-            HostState::Refine => Err(Error::StateBlocked(self.to_string())),
-            HostState::Connect => Err(Error::StateBlocked(self.to_string())),
-            HostState::Suspend => Err(Error::StateBlocked(self.to_string())),
-            HostState::Connected(_) => Err(Error::StateBlocked(self.to_string())),
-            HostState::Move(_) => Err(Error::StateBlocked(self.to_string())),
+            HostState::Insert => Err(Error::HostStateBlocked(start, end)),
+            HostState::Refine => Err(Error::HostStateBlocked(start, end)),
+            HostState::Connect => Err(Error::HostStateBlocked(start, end)),
+            HostState::Suspend => Err(Error::HostStateBlocked(start, end)),
+            HostState::Connected(_) => Err(Error::HostStateBlocked(start, end)),
+            HostState::Move(_) => Err(Error::HostStateBlocked(start, end)),
         }
         }
     }
     }
 
 
@@ -163,12 +169,14 @@ impl HostState {
     // and must be re-added to the Connected() state after the promotion
     // and must be re-added to the Connected() state after the promotion
     // has completed.
     // has completed.
     fn try_connected(&self, channel: ChannelPtr) -> Result<Self> {
     fn try_connected(&self, channel: ChannelPtr) -> Result<Self> {
+        let start = self.to_string();
+        let end = HostState::Connected(channel.clone()).to_string();
         match self {
         match self {
-            HostState::Insert => Err(Error::StateBlocked(self.to_string())),
+            HostState::Insert => Err(Error::HostStateBlocked(start, end)),
             HostState::Refine => Ok(HostState::Connected(channel)),
             HostState::Refine => Ok(HostState::Connected(channel)),
             HostState::Connect => Ok(HostState::Connected(channel)),
             HostState::Connect => Ok(HostState::Connected(channel)),
-            HostState::Suspend => Err(Error::StateBlocked(self.to_string())),
-            HostState::Connected(_) => Err(Error::StateBlocked(self.to_string())),
+            HostState::Suspend => Err(Error::HostStateBlocked(start, end)),
+            HostState::Connected(_) => Err(Error::HostStateBlocked(start, end)),
             HostState::Move(_) => Ok(HostState::Connected(channel)),
             HostState::Move(_) => Ok(HostState::Connected(channel)),
         }
         }
     }
     }
@@ -177,13 +185,15 @@ impl HostState {
     // Connect i.e. it is being connected to, or if we are currently Connected
     // Connect i.e. it is being connected to, or if we are currently Connected
     // to this peer (necessary due to Gold list promotion sequence).
     // to this peer (necessary due to Gold list promotion sequence).
     fn try_move(&self, channel: Option<ChannelPtr>) -> Result<Self> {
     fn try_move(&self, channel: Option<ChannelPtr>) -> Result<Self> {
+        let start = self.to_string();
+        let end = HostState::Move(channel.clone()).to_string();
         match self {
         match self {
-            HostState::Insert => Err(Error::StateBlocked(self.to_string())),
-            HostState::Refine => Err(Error::StateBlocked(self.to_string())),
+            HostState::Insert => Err(Error::HostStateBlocked(start, end)),
+            HostState::Refine => Err(Error::HostStateBlocked(start, end)),
             HostState::Connect => Ok(HostState::Move(channel)),
             HostState::Connect => Ok(HostState::Move(channel)),
-            HostState::Suspend => Err(Error::StateBlocked(self.to_string())),
+            HostState::Suspend => Err(Error::HostStateBlocked(start, end)),
             HostState::Connected(_) => Ok(HostState::Move(channel)),
             HostState::Connected(_) => Ok(HostState::Move(channel)),
-            HostState::Move(_) => Err(Error::StateBlocked(self.to_string())),
+            HostState::Move(_) => Err(Error::HostStateBlocked(start, end)),
         }
         }
     }
     }
 
 
@@ -191,12 +201,14 @@ impl HostState {
     // currently moving this host, since we suspend a host after failing
     // currently moving this host, since we suspend a host after failing
     // to connect to it and then downgrading in move_host.
     // to connect to it and then downgrading in move_host.
     fn try_suspend(&self) -> Result<Self> {
     fn try_suspend(&self) -> Result<Self> {
+        let start = self.to_string();
+        let end = HostState::Suspend.to_string();
         match self {
         match self {
-            HostState::Insert => Err(Error::StateBlocked(self.to_string())),
-            HostState::Refine => Err(Error::StateBlocked(self.to_string())),
-            HostState::Connect => Err(Error::StateBlocked(self.to_string())),
-            HostState::Suspend => Err(Error::StateBlocked(self.to_string())),
-            HostState::Connected(_) => Err(Error::StateBlocked(self.to_string())),
+            HostState::Insert => Err(Error::HostStateBlocked(start, end)),
+            HostState::Refine => Err(Error::HostStateBlocked(start, end)),
+            HostState::Connect => Err(Error::HostStateBlocked(start, end)),
+            HostState::Suspend => Err(Error::HostStateBlocked(start, end)),
+            HostState::Connected(_) => Err(Error::HostStateBlocked(start, end)),
             HostState::Move(_) => Ok(HostState::Suspend),
             HostState::Move(_) => Ok(HostState::Suspend),
         }
         }
     }
     }
@@ -786,9 +798,10 @@ impl Hosts {
 
 
         // Then ensure we aren't currently trying to add this peer to the hostlist.
         // Then ensure we aren't currently trying to add this peer to the hostlist.
         for (i, (addr, last_seen)) in filtered_addrs.iter().enumerate() {
         for (i, (addr, last_seen)) in filtered_addrs.iter().enumerate() {
-            if self.try_register(addr.clone(), HostState::Insert).await.is_err() {
-                debug!(target: "net::hosts::store_or_update()",
-                "{} is already registered. Skipping...", addr);
+            if let Err(e) = self.try_register(addr.clone(), HostState::Insert).await {
+                debug!(target: "net::hosts::store_or_update", "Cannot insert addr={}, err={}",
+                       addr.clone(), e);
+
                 continue
                 continue
             }
             }
 
 
@@ -809,10 +822,6 @@ impl Hosts {
         if registry.contains_key(&addr) {
         if registry.contains_key(&addr) {
             let current_state = registry.get(&addr).unwrap().clone();
             let current_state = registry.get(&addr).unwrap().clone();
 
 
-            debug!(target: "net::hosts::try_update_registry()",
-            "Attempting to update addr={} current_state={}, new_state={}",
-            addr, current_state, new_state.to_string());
-
             let result: Result<HostState> = match new_state {
             let result: Result<HostState> = match new_state {
                 HostState::Insert => current_state.try_insert(),
                 HostState::Insert => current_state.try_insert(),
                 HostState::Refine => current_state.try_refine(),
                 HostState::Refine => current_state.try_refine(),
@@ -843,7 +852,10 @@ impl Hosts {
         for (host, last_seen) in hosts {
         for (host, last_seen) in hosts {
             debug!(target: "net::hosts::check_addrs()", "Starting checks");
             debug!(target: "net::hosts::check_addrs()", "Starting checks");
 
 
-            if self.try_register(host.clone(), HostState::Connect).await.is_err() {
+            if let Err(e) = self.try_register(host.clone(), HostState::Connect).await {
+                debug!(target: "net::hosts::check_addrs", "Skipping addr={}, err={}",
+                       host.clone(), e);
+
                 continue
                 continue
             }
             }
 
 
@@ -986,7 +998,9 @@ impl Hosts {
                 continue
                 continue
             }
             }
 
 
-            // Reject this peer if it's already stored on the hostlist.
+            // Reject this peer if it's already stored on the Gold or White list.
+            // If it exists on the Grey list, we will simply update its last_seen
+            // field.
             if self.container.contains(HostColor::Gold as usize, addr_).await ||
             if self.container.contains(HostColor::Gold as usize, addr_).await ||
                 self.container.contains(HostColor::White as usize, addr_).await
                 self.container.contains(HostColor::White as usize, addr_).await
             {
             {

+ 2 - 1
src/net/session/manual_session.rs

@@ -172,7 +172,8 @@ impl ManualSession {
                 }
                 }
                 // This address is currently unavailable.
                 // This address is currently unavailable.
                 Err(e) => {
                 Err(e) => {
-                    debug!(target: "net::manual_session", "{} addr={}", e, addr.clone());
+                    debug!(target: "net::manual_session", "[P2P] Unable to connect to manual
+                           outbound [{}]: {}", addr.clone(), e);
                 }
                 }
             }
             }
             // Wait and try again.
             // Wait and try again.

+ 1 - 1
src/net/session/outbound_session.rs

@@ -551,7 +551,7 @@ impl PeerDiscoveryBase for PeerDiscovery {
     /// connected P2P channels and send out a `GetAddrs` message to request more
     /// connected P2P channels and send out a `GetAddrs` message to request more
     /// peers. Other parts of the P2P stack will then handle the incoming addresses
     /// peers. Other parts of the P2P stack will then handle the incoming addresses
     /// and place them in the hosts list.
     /// and place them in the hosts list.
-    /// This function will also sleep `Settings::outbound_connect_timeout` seconds
+    /// This function will also sleep `Settings::outbound_peer_discovery_attempt_time` seconds
     /// after broadcasting in order to let the P2P stack receive and work through
     /// after broadcasting in order to let the P2P stack receive and work through
     /// the addresses it is expecting.
     /// the addresses it is expecting.
     async fn run(self: Arc<Self>) {
     async fn run(self: Arc<Self>) {