From d8ab19cd8d9e654de845f962ac5f6ba3ded7b1a5 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Fri, 20 Dec 2024 06:03:13 -0500 Subject: [PATCH] eventemitter3 --- package-lock.json | 4 +++- packages/portalnetwork/package.json | 3 ++- packages/portalnetwork/src/client/client.ts | 21 +++++++++++------ packages/portalnetwork/src/client/types.ts | 9 +++----- .../portalnetwork/src/networks/network.ts | 23 ++++++++++--------- .../src/transports/capacitorUdp.ts | 6 ++--- .../src/transports/websockets.ts | 9 ++------ .../src/wire/utp/Socket/congestionControl.ts | 2 +- 8 files changed, 40 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2b2b1d9d3..f7314b04c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5833,7 +5833,8 @@ }, "node_modules/eventemitter3": { "version": "5.0.1", - "license": "MIT" + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz", + "integrity": "sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==" }, "node_modules/eventsource": { "version": "2.0.2", @@ -10256,6 +10257,7 @@ "debug": "^4.3.2", "ethereum-cryptography": "^3.0.0", "ethers": "^6.8.0", + "eventemitter3": "^5.0.1", "heap-js": "^2.5.0", "isomorphic-ws": "^4.0.1", "level": "^8.0.0", diff --git a/packages/portalnetwork/package.json b/packages/portalnetwork/package.json index 31412c39c..37f209654 100644 --- a/packages/portalnetwork/package.json +++ b/packages/portalnetwork/package.json @@ -49,8 +49,9 @@ "debug": "^4.3.2", "ethereum-cryptography": "^3.0.0", "ethers": "^6.8.0", - "isomorphic-ws": "^4.0.1", + "eventemitter3": "^5.0.1", "heap-js": "^2.5.0", + "isomorphic-ws": "^4.0.1", "level": "^8.0.0", "lru-cache": "^10.0.0", "memory-level": "^1.0.0", diff --git a/packages/portalnetwork/src/client/client.ts b/packages/portalnetwork/src/client/client.ts index 2f5cd67c3..02a33ce73 100644 --- a/packages/portalnetwork/src/client/client.ts +++ b/packages/portalnetwork/src/client/client.ts @@ -1,6 +1,6 @@ -import { EventEmitter } from 'events' +import { EventEmitter } from 'eventemitter3' import { Discv5 } from '@chainsafe/discv5' -import { ENR, SignableENR } from '@chainsafe/enr'; +import { ENR, SignableENR } from '@chainsafe/enr' import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { keys } from '@libp2p/crypto' import { fromNodeAddress, multiaddr } from '@multiformats/multiaddr' @@ -25,10 +25,15 @@ import type { IDiscv5CreateOptions, SignableENRInput } from '@chainsafe/discv5' import type { ITalkReqMessage, ITalkRespMessage } from '@chainsafe/discv5/message' import type { Debugger } from 'debug' import type { BaseNetwork } from '../networks/network.js' -import type { INodeAddress, PortalNetworkEventEmitter, PortalNetworkMetrics , PortalNetworkOpts } from './types.js' -import { MessageCodes, PortalWireMessageType } from '../wire/types.js'; +import type { + INodeAddress, + PortalNetworkEvents, + PortalNetworkMetrics, + PortalNetworkOpts, +} from './types.js' +import { MessageCodes, PortalWireMessageType } from '../wire/types.js' -export class PortalNetwork extends (EventEmitter as { new (): PortalNetworkEventEmitter }) { +export class PortalNetwork extends EventEmitter { eventLog: boolean discv5: Discv5 networks: Map @@ -297,7 +302,7 @@ export class PortalNetwork extends (EventEmitter as { new (): PortalNetworkEvent public stop = async () => { await this.discv5.stop() this.discv5.removeAllListeners() - await this.removeAllListeners() + this.removeAllListeners() await this.db.close() for (const network of this.networks.values()) { network.stopRefresh() @@ -408,7 +413,9 @@ export class PortalNetwork extends (EventEmitter as { new (): PortalNetworkEvent try { await this.uTP.handleUtpPacket(packetBuffer, src.nodeId) } catch (err: any) { - this.logger.extend('error')(`handleUTP error: ${err.message}. SrcId: ${src.nodeId} MultiAddr: ${src.socketAddr.toString()}`) + this.logger.extend('error')( + `handleUTP error: ${err.message}. SrcId: ${src.nodeId} MultiAddr: ${src.socketAddr.toString()}`, + ) } } diff --git a/packages/portalnetwork/src/client/types.ts b/packages/portalnetwork/src/client/types.ts index 106889131..ecc992526 100644 --- a/packages/portalnetwork/src/client/types.ts +++ b/packages/portalnetwork/src/client/types.ts @@ -1,8 +1,7 @@ -import type EventEmitter from 'events' import type { IDiscv5CreateOptions } from '@chainsafe/discv5' import type { NodeId } from '@chainsafe/enr' import type { AbstractLevel } from 'abstract-level' -import type StrictEventEmitter from 'strict-event-emitter-types/types/src' + import type { NetworkId } from '../index.js' import type { PortalNetworkRoutingTable } from './routingTable.js' import type { Multiaddr } from '@multiformats/multiaddr' @@ -10,9 +9,9 @@ import type { Multiaddr } from '@multiformats/multiaddr' /** A representation of an unsigned contactable node. */ export interface INodeAddress { /** The destination socket address. */ - socketAddr: Multiaddr; + socketAddr: Multiaddr /** The destination Node Id. */ - nodeId: NodeId; + nodeId: NodeId } export interface PortalNetworkEvents { @@ -58,8 +57,6 @@ export interface PortalNetworkOpts { gossipCount?: number } -export type PortalNetworkEventEmitter = StrictEventEmitter - export type RoutingTable = PortalNetworkRoutingTable interface Gauge { inc(value?: number): void diff --git a/packages/portalnetwork/src/networks/network.ts b/packages/portalnetwork/src/networks/network.ts index f5eca9f1a..254966511 100644 --- a/packages/portalnetwork/src/networks/network.ts +++ b/packages/portalnetwork/src/networks/network.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from 'events' +import { EventEmitter } from 'eventemitter3' import { digest } from '@chainsafe/as-sha256' import { EntryStatus, MAX_NODES_PER_BUCKET, distance } from '@chainsafe/discv5' import { ENR } from '@chainsafe/enr' @@ -49,7 +49,8 @@ import type { OfferMessage, PingMessage, PongMessage, - PortalNetwork } from '../index.js' + PortalNetwork, +} from '../index.js' import { GossipManager } from './gossip.js' export abstract class BaseNetwork extends EventEmitter { @@ -640,14 +641,14 @@ export abstract class BaseNetwork extends EventEmitter { ) const _id = randUint16() const enr = this.findEnr(src.nodeId) ?? src - await this.handleNewRequest({ - networkId: this.networkId, - contentKeys: [decodedContentMessage.contentKey], - enr, - connectionId: _id, - requestCode: RequestCode.FOUNDCONTENT_WRITE, - contents: value, - }) + await this.handleNewRequest({ + networkId: this.networkId, + contentKeys: [decodedContentMessage.contentKey], + enr, + connectionId: _id, + requestCode: RequestCode.FOUNDCONTENT_WRITE, + contents: value, + }) const id = new Uint8Array(2) new DataView(id.buffer).setUint16(0, _id, false) @@ -827,7 +828,7 @@ export abstract class BaseNetwork extends EventEmitter { if (pong !== undefined) { for (let x = 239; x < 256; x++) { // Ask for nodes in all log2distances 239 - 256 - if (this.routingTable.valuesOfDistance(x).length < 16 ) { + if (this.routingTable.valuesOfDistance(x).length < 16) { await this.sendFindNodes(enr, [x]) } } diff --git a/packages/portalnetwork/src/transports/capacitorUdp.ts b/packages/portalnetwork/src/transports/capacitorUdp.ts index 3295b4fa8..e6335fb31 100644 --- a/packages/portalnetwork/src/transports/capacitorUdp.ts +++ b/packages/portalnetwork/src/transports/capacitorUdp.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from 'events' +import { EventEmitter } from 'eventemitter3' import { getSocketAddressOnENR } from '@chainsafe/discv5' import { decodePacket, encodePacket } from '@chainsafe/discv5/packet' import { UDP } from '@frontall/capacitor-udp' @@ -9,8 +9,8 @@ import type { IPacket } from '@chainsafe/discv5/packet' import type { IPMode, IRemoteInfo, + ITransportEvents, ITransportService, - TransportEventEmitter, } from '@chainsafe/discv5/transport' import type { ENR } from '@chainsafe/enr' import type { Multiaddr } from '@multiformats/multiaddr' @@ -19,7 +19,7 @@ import type { Multiaddr } from '@multiformats/multiaddr' * This class is responsible for encoding outgoing Packets and decoding incoming Packets over UDP */ export class CapacitorUDPTransportService - extends (EventEmitter as { new (): TransportEventEmitter }) + extends EventEmitter implements ITransportService { private socket!: { diff --git a/packages/portalnetwork/src/transports/websockets.ts b/packages/portalnetwork/src/transports/websockets.ts index 0bad5c75e..7623f2b5f 100644 --- a/packages/portalnetwork/src/transports/websockets.ts +++ b/packages/portalnetwork/src/transports/websockets.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from 'events' +import { EventEmitter } from 'eventemitter3' import { getSocketAddressOnENR } from '@chainsafe/discv5' import { decodePacket, encodePacket } from '@chainsafe/discv5/packet' import { multiaddr as ma } from '@multiformats/multiaddr' @@ -17,7 +17,6 @@ import type { IPacket } from '@chainsafe/discv5/packet' import type { ENR } from '@chainsafe/enr' import type { Multiaddr } from '@multiformats/multiaddr' import type { Debugger } from 'debug' -import type StrictEventEmitter from 'strict-event-emitter-types/types/src' const log = debug('discv5:transport') @@ -25,15 +24,11 @@ interface WebSocketTransportEvents extends ITransportEvents { multiAddr: (src: Multiaddr) => void } -export declare type WSTransportEventEmitter = StrictEventEmitter< - EventEmitter, - WebSocketTransportEvents -> /** * This class is responsible for encoding outgoing Packets and decoding incoming Packets over Websockets */ export class WebSocketTransportService - extends (EventEmitter as { new (): WSTransportEventEmitter }) + extends EventEmitter implements ITransportService { public multiaddr: Multiaddr diff --git a/packages/portalnetwork/src/wire/utp/Socket/congestionControl.ts b/packages/portalnetwork/src/wire/utp/Socket/congestionControl.ts index 9db3f1c5d..ca0074f96 100644 --- a/packages/portalnetwork/src/wire/utp/Socket/congestionControl.ts +++ b/packages/portalnetwork/src/wire/utp/Socket/congestionControl.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from 'events' +import { EventEmitter } from 'eventemitter3' import debug from 'debug' import { DEFAULT_PACKET_SIZE, MAX_CWND_INCREASE_PACKETS_PER_RTT } from '../index.js'