From f05b9b31d6fee26ef21f298b556fb31be1cd964e Mon Sep 17 00:00:00 2001
From: Andrei Sandu <54316454+sandreim@users.noreply.github.com>
Date: Wed, 22 Jun 2022 12:10:24 +0300
Subject: [PATCH] Update metric before bailing out (#5706)

* Update authority metrics before bailing out

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* remove redundant call

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* doc

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

* review feedback

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>
---
 .../node/network/gossip-support/src/lib.rs    | 71 ++++++++++---------
 1 file changed, 39 insertions(+), 32 deletions(-)

diff --git a/polkadot/node/network/gossip-support/src/lib.rs b/polkadot/node/network/gossip-support/src/lib.rs
index 5cb81bde25c..1cebcc64f78 100644
--- a/polkadot/node/network/gossip-support/src/lib.rs
+++ b/polkadot/node/network/gossip-support/src/lib.rs
@@ -256,12 +256,9 @@ where
 					}
 				}
 
-				// Gossip topology is only relevant for authorities in the current session.
-				let our_index =
-					ensure_i_am_an_authority(&self.keystore, &session_info.discovery_keys).await?;
-
 				if is_new_session {
-					self.update_authority_status_metrics(&session_info).await;
+					// Gossip topology is only relevant for authorities in the current session.
+					let our_index = self.get_key_index_and_update_metrics(&session_info).await?;
 
 					update_gossip_topology(
 						sender,
@@ -277,35 +274,45 @@ where
 		Ok(())
 	}
 
-	async fn update_authority_status_metrics(&mut self, session_info: &SessionInfo) {
-		let maybe_index =
-			match ensure_i_am_an_authority(&self.keystore, &session_info.discovery_keys).await {
-				Ok(index) => {
-					self.metrics.on_is_authority();
-					Some(index)
-				},
-				Err(util::Error::NotAValidator) => {
-					self.metrics.on_is_not_authority();
+	// Checks if the node is an authority and also updates `polkadot_node_is_authority` and
+	// `polkadot_node_is_parachain_validator` metrics accordingly.
+	// On success, returns the index of our keys in `session_info.discovery_keys`.
+	async fn get_key_index_and_update_metrics(
+		&mut self,
+		session_info: &SessionInfo,
+	) -> Result<usize, util::Error> {
+		let authority_check_result =
+			ensure_i_am_an_authority(&self.keystore, &session_info.discovery_keys).await;
+
+		match authority_check_result.as_ref() {
+			Ok(index) => {
+				gum::trace!(target: LOG_TARGET, "We are now an authority",);
+				self.metrics.on_is_authority();
+
+				// The subset of authorities participating in parachain consensus.
+				let parachain_validators_this_session = session_info.validators.len();
+
+				// First `maxValidators` entries are the parachain validators. We'll check
+				// if our index is in this set to avoid searching for the keys.
+				// https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148
+				if *index < parachain_validators_this_session {
+					gum::trace!(target: LOG_TARGET, "We are now a parachain validator",);
+					self.metrics.on_is_parachain_validator();
+				} else {
+					gum::trace!(target: LOG_TARGET, "We are no longer a parachain validator",);
 					self.metrics.on_is_not_parachain_validator();
-					None
-				},
-				// Don't update on runtime errors.
-				Err(_) => None,
-			};
-
-		if let Some(validator_index) = maybe_index {
-			// The subset of authorities participating in parachain consensus.
-			let parachain_validators_this_session = session_info.validators.len();
-
-			// First `maxValidators` entries are the parachain validators. We'll check
-			// if our index is in this set to avoid searching for the keys.
-			// https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148
-			if validator_index < parachain_validators_this_session {
-				self.metrics.on_is_parachain_validator();
-			} else {
+				}
+			},
+			Err(util::Error::NotAValidator) => {
+				gum::trace!(target: LOG_TARGET, "We are no longer an authority",);
+				self.metrics.on_is_not_authority();
 				self.metrics.on_is_not_parachain_validator();
-			}
-		}
+			},
+			// Don't update on runtime errors.
+			Err(_) => {},
+		};
+
+		authority_check_result
 	}
 
 	async fn issue_connection_request<Sender>(
-- 
GitLab