From 111b194e3e6e9b6249149dd6d716c42d36915644 Mon Sep 17 00:00:00 2001 From: Lion - dapplion <35266934+dapplion@users.noreply.github.com> Date: Mon, 4 Apr 2022 20:45:08 +0200 Subject: [PATCH] Handle unknown topic without try catch (#3881) --- packages/lodestar/src/network/gossip/gossipsub.ts | 9 ++++----- packages/lodestar/src/network/gossip/topic.ts | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/lodestar/src/network/gossip/gossipsub.ts b/packages/lodestar/src/network/gossip/gossipsub.ts index 1e57573a796..bab213dcecc 100644 --- a/packages/lodestar/src/network/gossip/gossipsub.ts +++ b/packages/lodestar/src/network/gossip/gossipsub.ts @@ -442,10 +442,10 @@ export class Eth2Gossipsub extends Gossipsub { // Ignore topics with 0 peers. May prevent overriding after a fork if (peers.size === 0) continue; - // there are some new topics in the network so we have to try/catch + // there are some new topics in the network so `getKnownTopic()` returns undefined // for example in prater: /eth2/82f4a72b/optimistic_light_client_update_v0/ssz_snappy - try { - const topic = this.gossipTopicCache.getTopic(topicString); + const topic = this.gossipTopicCache.getKnownTopic(topicString); + if (topic !== undefined) { if (topic.type === GossipType.beacon_attestation) { peersByBeaconAttSubnetByFork.set(topic.fork, topic.subnet, peers.size); } else if (topic.type === GossipType.sync_committee) { @@ -453,8 +453,7 @@ export class Eth2Gossipsub extends Gossipsub { } else { peersByTypeByFork.set(topic.fork, topic.type, peers.size); } - // eslint-disable-next-line no-empty - } catch {} + } } // beacon attestation mesh gets counted separately so we can track mesh peers by subnet diff --git a/packages/lodestar/src/network/gossip/topic.ts b/packages/lodestar/src/network/gossip/topic.ts index 90621384c87..6c14b86e814 100644 --- a/packages/lodestar/src/network/gossip/topic.ts +++ b/packages/lodestar/src/network/gossip/topic.ts @@ -16,6 +16,7 @@ export class GossipTopicCache implements IGossipTopicCache { constructor(private readonly forkDigestContext: IForkDigestContext) {} + /** Returns cached GossipTopic, otherwise attempts to parse it from the str */ getTopic(topicStr: string): GossipTopic { let topic = this.topicsByTopicStr.get(topicStr); if (topic === undefined) { @@ -26,6 +27,11 @@ export class GossipTopicCache implements IGossipTopicCache { return topic; } + /** Returns cached GossipTopic, otherwise returns undefined */ + getKnownTopic(topicStr: string): GossipTopic | undefined { + return this.topicsByTopicStr.get(topicStr); + } + setTopic(topicStr: string, topic: GossipTopic): void { if (!this.topicsByTopicStr.has(topicStr)) { this.topicsByTopicStr.set(topicStr, {encoding: DEFAULT_ENCODING, ...topic});