From 9dcf6f12e87c479f7d32ff6ac7869b375ca06de3 Mon Sep 17 00:00:00 2001
From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Date: Sat, 20 Jul 2024 17:37:14 +0300
Subject: [PATCH] beefy: Increment metric and add extra log details (#5075)

This PR increments the beefy metric wrt no peers to query justification
from.
The metric is incremented when we submit a request to a known peer,
however that peer failed to provide a valid response, and there are no
further peers to query.

While at it, add a few extra details to identify the number of active
peers and cached peers, together with the request error

Part of:
- https://github.com/paritytech/polkadot-sdk/issues/4985
- https://github.com/paritytech/polkadot-sdk/issues/4925

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
---
 .../client/consensus/beefy/src/communication/peers.rs |  5 +++++
 .../request_response/outgoing_requests_engine.rs      | 11 +++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/substrate/client/consensus/beefy/src/communication/peers.rs b/substrate/client/consensus/beefy/src/communication/peers.rs
index 2d801aceaa8..929cc0c650b 100644
--- a/substrate/client/consensus/beefy/src/communication/peers.rs
+++ b/substrate/client/consensus/beefy/src/communication/peers.rs
@@ -75,6 +75,11 @@ impl<B: Block> KnownPeers<B> {
 	pub fn contains(&self, peer: &PeerId) -> bool {
 		self.live.contains_key(peer)
 	}
+
+	/// Number of peers in the set.
+	pub fn len(&self) -> usize {
+		self.live.len()
+	}
 }
 
 #[cfg(test)]
diff --git a/substrate/client/consensus/beefy/src/communication/request_response/outgoing_requests_engine.rs b/substrate/client/consensus/beefy/src/communication/request_response/outgoing_requests_engine.rs
index e127e5a8959..95ecf35557a 100644
--- a/substrate/client/consensus/beefy/src/communication/request_response/outgoing_requests_engine.rs
+++ b/substrate/client/consensus/beefy/src/communication/request_response/outgoing_requests_engine.rs
@@ -249,9 +249,16 @@ impl<B: Block, AuthorityId: AuthorityIdBound> OnDemandJustificationsEngine<B, Au
 				if let Some(peer) = self.try_next_peer() {
 					self.request_from_peer(peer, req_info);
 				} else {
+					metric_inc!(
+						self.metrics,
+						beefy_on_demand_justification_no_peer_to_request_from
+					);
+
+					let num_cache = self.peers_cache.len();
+					let num_live = self.live_peers.lock().len();
 					warn!(
 						target: BEEFY_SYNC_LOG_TARGET,
-						"🥩 ran out of peers to request justif #{:?} from", block
+						"🥩 ran out of peers to request justif #{block:?} from num_cache={num_cache} num_live={num_live} err={err:?}",
 					);
 				}
 				// Report peer based on error type.
@@ -265,7 +272,7 @@ impl<B: Block, AuthorityId: AuthorityIdBound> OnDemandJustificationsEngine<B, Au
 				metric_inc!(self.metrics, beefy_on_demand_justification_good_proof);
 				debug!(
 					target: BEEFY_SYNC_LOG_TARGET,
-					"🥩 received valid on-demand justif #{:?} from {:?}", block, peer
+					"🥩 received valid on-demand justif #{block:?} from {peer:?}",
 				);
 				let peer_report = PeerReport { who: peer, cost_benefit: benefit::VALIDATED_PROOF };
 				ResponseInfo::ValidProof(proof, peer_report)
-- 
GitLab