diff --git a/yarn-project/p2p/src/services/libp2p/libp2p_service.ts b/yarn-project/p2p/src/services/libp2p/libp2p_service.ts index d130c0b96097..f25e2fb59bed 100644 --- a/yarn-project/p2p/src/services/libp2p/libp2p_service.ts +++ b/yarn-project/p2p/src/services/libp2p/libp2p_service.ts @@ -102,6 +102,8 @@ export class LibP2PService extends WithTracer implement */ private blockReceivedCallback: (block: BlockProposal) => Promise; + private gossipSubEventHandler: (e: CustomEvent) => void; + constructor( private clientType: T, private config: P2PConfig, @@ -139,6 +141,8 @@ export class LibP2PService extends WithTracer implement this.attestationValidator = new AttestationValidator(epochCache); this.blockProposalValidator = new BlockProposalValidator(epochCache); + this.gossipSubEventHandler = this.handleGossipSubEvent.bind(this); + this.blockReceivedCallback = async (block: BlockProposal): Promise => { this.logger.debug( `Handler not yet registered: Block received callback not set. Received block for slot ${block.slotNumber.toNumber()} from peer.`, @@ -329,7 +333,7 @@ export class LibP2PService extends WithTracer implement }; // add GossipSub listener - this.node.services.pubsub.addEventListener(GossipSubEvent.MESSAGE, this.handleGossipSubEvent.bind(this)); + this.node.services.pubsub.addEventListener(GossipSubEvent.MESSAGE, this.gossipSubEventHandler); // Start running promise for peer discovery this.discoveryRunningPromise = new RunningPromise( @@ -360,7 +364,7 @@ export class LibP2PService extends WithTracer implement */ public async stop() { // Remove gossip sub listener - this.node.services.pubsub.removeEventListener(GossipSubEvent.MESSAGE, this.handleGossipSubEvent.bind(this)); + this.node.services.pubsub.removeEventListener(GossipSubEvent.MESSAGE, this.gossipSubEventHandler); // Stop peer manager this.logger.debug('Stopping peer manager...');