From 7c5ce7d5baf3cab01dc87c0c85c936c87ea508e0 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Sat, 17 Jun 2023 15:05:31 +0700 Subject: [PATCH 1/2] fix: add setTimeout to onGossipsubMessage and onValidationResult --- .../src/network/gossip/gossipsub.ts | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/beacon-node/src/network/gossip/gossipsub.ts b/packages/beacon-node/src/network/gossip/gossipsub.ts index fbafabcf1a4..019a9861a84 100644 --- a/packages/beacon-node/src/network/gossip/gossipsub.ts +++ b/packages/beacon-node/src/network/gossip/gossipsub.ts @@ -280,20 +280,25 @@ export class Eth2Gossipsub extends GossipSub { const seenTimestampSec = Date.now() / 1000; // Emit message to network processor - this.events.emit(NetworkEvent.pendingGossipsubMessage, { - topic, - msg, - msgId, - // Hot path, use cached .toString() version - propagationSource: propagationSource.toString(), - seenTimestampSec, - startProcessUnixSec: null, - }); + setTimeout(() => { + this.events.emit(NetworkEvent.pendingGossipsubMessage, { + topic, + msg, + msgId, + // Hot path, use cached .toString() version + propagationSource: propagationSource.toString(), + seenTimestampSec, + startProcessUnixSec: null, + }); + }, 0); } private onValidationResult(data: NetworkEventData[NetworkEvent.gossipMessageValidationResult]): void { - // TODO: reportMessageValidationResult should take PeerIdStr since it only uses string version - this.reportMessageValidationResult(data.msgId, peerIdFromString(data.propagationSource), data.acceptance); + // test sending validation results on the next event loop + setTimeout(() => { + // TODO: reportMessageValidationResult should take PeerIdStr since it only uses string version + this.reportMessageValidationResult(data.msgId, peerIdFromString(data.propagationSource), data.acceptance); + }, 0); } } From 2a265aa805d37f0f3e54508dfad6353188556e62 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Sun, 18 Jun 2023 15:23:16 +0700 Subject: [PATCH 2/2] chore: more comments --- packages/beacon-node/src/network/gossip/gossipsub.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/beacon-node/src/network/gossip/gossipsub.ts b/packages/beacon-node/src/network/gossip/gossipsub.ts index 019a9861a84..ef159e01907 100644 --- a/packages/beacon-node/src/network/gossip/gossipsub.ts +++ b/packages/beacon-node/src/network/gossip/gossipsub.ts @@ -279,7 +279,9 @@ export class Eth2Gossipsub extends GossipSub { // Get seenTimestamp before adding the message to the queue or add async delays const seenTimestampSec = Date.now() / 1000; - // Emit message to network processor + // Use setTimeout to yield to the macro queue + // Without this we'll have huge event loop lag + // See https://github.com/ChainSafe/lodestar/issues/5604 setTimeout(() => { this.events.emit(NetworkEvent.pendingGossipsubMessage, { topic, @@ -294,7 +296,9 @@ export class Eth2Gossipsub extends GossipSub { } private onValidationResult(data: NetworkEventData[NetworkEvent.gossipMessageValidationResult]): void { - // test sending validation results on the next event loop + // Use setTimeout to yield to the macro queue + // Without this we'll have huge event loop lag + // See https://github.com/ChainSafe/lodestar/issues/5604 setTimeout(() => { // TODO: reportMessageValidationResult should take PeerIdStr since it only uses string version this.reportMessageValidationResult(data.msgId, peerIdFromString(data.propagationSource), data.acceptance);