Skip to content

Commit

Permalink
Handle unknown topic without try catch (#3881)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion authored Apr 4, 2022
1 parent 5bcadf8 commit 111b194
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/lodestar/src/network/gossip/gossipsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,19 +442,18 @@ 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) {
peersByBeaconSyncSubnetByFork.set(topic.fork, topic.subnet, peers.size);
} 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
Expand Down
6 changes: 6 additions & 0 deletions packages/lodestar/src/network/gossip/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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});
Expand Down

0 comments on commit 111b194

Please sign in to comment.