From 7258584153c91eed517d86c5ce47129ce2e52b31 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Tue, 30 May 2023 19:35:56 +0300 Subject: [PATCH] Drop start stop pattern from Network where possible --- packages/beacon-node/src/network/core/networkCore.ts | 10 ++++------ packages/beacon-node/src/network/peers/peerManager.ts | 9 +++++---- .../beacon-node/src/network/subnets/attnetsService.ts | 4 +--- packages/beacon-node/src/network/subnets/interface.ts | 3 +-- .../beacon-node/src/network/subnets/syncnetsService.ts | 4 +--- .../test/e2e/network/peers/peerManager.test.ts | 4 +--- .../test/unit/network/subnets/attnetsService.test.ts | 3 +-- 7 files changed, 14 insertions(+), 23 deletions(-) diff --git a/packages/beacon-node/src/network/core/networkCore.ts b/packages/beacon-node/src/network/core/networkCore.ts index 9505752bd366..57fffcc98f05 100644 --- a/packages/beacon-node/src/network/core/networkCore.ts +++ b/packages/beacon-node/src/network/core/networkCore.ts @@ -213,15 +213,13 @@ export class NetworkCore implements INetworkCore { await reqResp.start(); await gossip.start(); - attnetsService.start(); - syncnetsService.start(); // Network spec decides version changes based on clock fork, not head fork const forkCurrentSlot = config.getForkName(clock.currentSlot); // Register only ReqResp protocols relevant to clock's fork reqResp.registerProtocolsAtFork(forkCurrentSlot); - await peerManager.start(); + await peerManager.startDiscovery(); // Bind discv5's ENR to local metadata discv5 = peerManager["discovery"]?.discv5; @@ -255,14 +253,14 @@ export class NetworkCore implements INetworkCore { // Must goodbye and disconnect before stopping libp2p await this.peerManager.goodbyeAndDisconnectAllPeers(); - await this.peerManager.stop(); + await this.peerManager.close(); await this.gossip.stop(); await this.reqResp.stop(); await this.reqResp.unregisterAllProtocols(); - this.attnetsService.stop(); - this.syncnetsService.stop(); + this.attnetsService.close(); + this.syncnetsService.close(); await this.libp2p.stop(); this.closed = true; diff --git a/packages/beacon-node/src/network/peers/peerManager.ts b/packages/beacon-node/src/network/peers/peerManager.ts index 62d0da61d91f..4ce7658121b8 100644 --- a/packages/beacon-node/src/network/peers/peerManager.ts +++ b/packages/beacon-node/src/network/peers/peerManager.ts @@ -175,10 +175,7 @@ export class PeerManager { if (metrics) { metrics.peers.addCollect(() => this.runPeerCountMetrics(metrics)); } - } - async start(): Promise { - await this.discovery?.start(); this.libp2p.connectionManager.addEventListener(Libp2pEvent.peerConnect, this.onLibp2pPeerConnect); this.libp2p.connectionManager.addEventListener(Libp2pEvent.peerDisconnect, this.onLibp2pPeerDisconnect); this.networkEventBus.on(NetworkEvent.reqRespRequest, this.onRequest); @@ -195,7 +192,11 @@ export class PeerManager { ]; } - async stop(): Promise { + async startDiscovery(): Promise { + await this.discovery?.start(); + } + + async close(): Promise { await this.discovery?.stop(); this.libp2p.connectionManager.removeEventListener(Libp2pEvent.peerConnect, this.onLibp2pPeerConnect); this.libp2p.connectionManager.removeEventListener(Libp2pEvent.peerDisconnect, this.onLibp2pPeerDisconnect); diff --git a/packages/beacon-node/src/network/subnets/attnetsService.ts b/packages/beacon-node/src/network/subnets/attnetsService.ts index a5cdf4c1b7c9..a95834316c38 100644 --- a/packages/beacon-node/src/network/subnets/attnetsService.ts +++ b/packages/beacon-node/src/network/subnets/attnetsService.ts @@ -93,14 +93,12 @@ export class AttnetsService implements IAttnetsService { if (metrics) { metrics.attnetsService.subscriptionsRandom.addCollect(() => this.onScrapeLodestarMetrics(metrics)); } - } - start(): void { this.clock.on(ClockEvent.slot, this.onSlot); this.clock.on(ClockEvent.epoch, this.onEpoch); } - stop(): void { + close(): void { this.clock.off(ClockEvent.slot, this.onSlot); this.clock.off(ClockEvent.epoch, this.onEpoch); } diff --git a/packages/beacon-node/src/network/subnets/interface.ts b/packages/beacon-node/src/network/subnets/interface.ts index c62a63bf2915..8dc56fc54c54 100644 --- a/packages/beacon-node/src/network/subnets/interface.ts +++ b/packages/beacon-node/src/network/subnets/interface.ts @@ -12,8 +12,7 @@ export type CommitteeSubscription = { }; export type SubnetsService = { - start(): void; - stop(): void; + close(): void; addCommitteeSubscriptions(subscriptions: CommitteeSubscription[]): void; getActiveSubnets(): RequestedSubnet[]; subscribeSubnetsToNextFork(nextFork: ForkName): void; diff --git a/packages/beacon-node/src/network/subnets/syncnetsService.ts b/packages/beacon-node/src/network/subnets/syncnetsService.ts index 7a45fb5de75f..147c4d0b5fb8 100644 --- a/packages/beacon-node/src/network/subnets/syncnetsService.ts +++ b/packages/beacon-node/src/network/subnets/syncnetsService.ts @@ -39,13 +39,11 @@ export class SyncnetsService implements SubnetsService { if (metrics) { metrics.syncnetsService.subscriptionsCommittee.addCollect(() => this.onScrapeLodestarMetrics(metrics)); } - } - start(): void { this.clock.on(ClockEvent.epoch, this.onEpoch); } - stop(): void { + close(): void { this.clock.off(ClockEvent.epoch, this.onEpoch); } diff --git a/packages/beacon-node/test/e2e/network/peers/peerManager.test.ts b/packages/beacon-node/test/e2e/network/peers/peerManager.test.ts index ceace6bdebfa..9eb78bd5574a 100644 --- a/packages/beacon-node/test/e2e/network/peers/peerManager.test.ts +++ b/packages/beacon-node/test/e2e/network/peers/peerManager.test.ts @@ -64,8 +64,7 @@ describe("network / peers / PeerManager", function () { getActiveSubnets: () => [], shouldProcess: () => true, addCommitteeSubscriptions: () => {}, - start: () => {}, - stop: () => {}, + close: () => {}, subscribeSubnetsToNextFork: () => {}, unsubscribeSubnetsFromPrevFork: () => {}, }; @@ -93,7 +92,6 @@ describe("network / peers / PeerManager", function () { discv5FirstQueryDelayMs: 0, } ); - await peerManager.start(); return {statusCache, clock, libp2p, reqResp, peerManager, networkEventBus}; } diff --git a/packages/beacon-node/test/unit/network/subnets/attnetsService.test.ts b/packages/beacon-node/test/unit/network/subnets/attnetsService.test.ts index 81addabb943e..56239d37f889 100644 --- a/packages/beacon-node/test/unit/network/subnets/attnetsService.test.ts +++ b/packages/beacon-node/test/unit/network/subnets/attnetsService.test.ts @@ -77,11 +77,10 @@ describe("AttnetsService", function () { randBetweenFn, shuffleFn: shuffleFn as ShuffleFn, }); - service.start(); }); afterEach(() => { - service.stop(); + service.close(); sandbox.restore(); randomSubnet = 0; });