From 4760df1e3126d6280073d75014b71e7ec193bca7 Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Mon, 15 Mar 2021 01:42:47 +0000 Subject: [PATCH] fix build with libp2p_agents_metrics switch --- libp2p/protocols/pubsub/gossipsub/scoring.nim | 18 +++++++++--------- libp2p/stream/chronosstream.nim | 6 +++--- libp2p/utility.nim | 14 +++++++++++++- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/libp2p/protocols/pubsub/gossipsub/scoring.nim b/libp2p/protocols/pubsub/gossipsub/scoring.nim index d2db2c20b..3c82d5671 100644 --- a/libp2p/protocols/pubsub/gossipsub/scoring.nim +++ b/libp2p/protocols/pubsub/gossipsub/scoring.nim @@ -71,9 +71,9 @@ proc disconnectPeer(g: GossipSub, peer: PubSubPeer) {.async.} = peer.shortAgent else: if peer.sendConn != nil: - let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].toLowerAscii() - if KnownLibP2PAgentsSeq.contains(shortAgent): - peer.shortAgent = shortAgent + let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].safeToLowerAscii() + if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()): + peer.shortAgent = shortAgent.get() else: peer.shortAgent = "unknown" peer.shortAgent @@ -165,9 +165,9 @@ proc updateScores*(g: GossipSub) = # avoid async peer.shortAgent else: if peer.sendConn != nil: - let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].toLowerAscii() - if KnownLibP2PAgentsSeq.contains(shortAgent): - peer.shortAgent = shortAgent + let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].safeToLowerAscii() + if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()): + peer.shortAgent = shortAgent.get() else: peer.shortAgent = "unknown" peer.shortAgent @@ -221,9 +221,9 @@ proc updateScores*(g: GossipSub) = # avoid async peer.shortAgent else: if peer.sendConn != nil: - let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].toLowerAscii() - if KnownLibP2PAgentsSeq.contains(shortAgent): - peer.shortAgent = shortAgent + let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].safeToLowerAscii() + if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()): + peer.shortAgent = shortAgent.get() else: peer.shortAgent = "unknown" peer.shortAgent diff --git a/libp2p/stream/chronosstream.nim b/libp2p/stream/chronosstream.nim index 259587f92..bb8416b3e 100644 --- a/libp2p/stream/chronosstream.nim +++ b/libp2p/stream/chronosstream.nim @@ -79,9 +79,9 @@ when defined(libp2p_agents_metrics): if not s.tracked: if not isNil(s.peerInfo) and s.peerInfo.agentVersion.len > 0: # / seems a weak "standard" so for now it's reliable - let shortAgent = s.peerInfo.agentVersion.split("/")[0].toLowerAscii() - if KnownLibP2PAgentsSeq.contains(shortAgent): - s.shortAgent = shortAgent + let shortAgent = s.peerInfo.agentVersion.split("/")[0].safeToLowerAscii() + if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()): + s.shortAgent = shortAgent.get() else: s.shortAgent = "unknown" libp2p_peers_identity.inc(labelValues = [s.shortAgent]) diff --git a/libp2p/utility.nim b/libp2p/utility.nim index f866e9ecb..11c427d6c 100644 --- a/libp2p/utility.nim +++ b/libp2p/utility.nim @@ -39,6 +39,18 @@ func shortLog*(item: string): string = result &= item[(item.len - split)..item.high] when defined(libp2p_agents_metrics): + import strutils + export split + + import stew/results + export results + + proc safeToLowerAscii*(s: string): Result[string, cstring] = + try: + ok(s.toLowerAscii()) + except: + err("toLowerAscii failed") + const KnownLibP2PAgents* {.strdefine.} = "" - KnownLibP2PAgentsSeq* = KnownLibP2PAgents.toLowerAscii().split(",") + KnownLibP2PAgentsSeq* = KnownLibP2PAgents.safeToLowerAscii().tryGet().split(",")