Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch events to eventEmitter3 #698

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/portalnetwork/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 14 additions & 7 deletions packages/portalnetwork/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<PortalNetworkEvents> {
eventLog: boolean
discv5: Discv5
networks: Map<NetworkId, BaseNetwork>
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()}`,
)
}
}

Expand Down
9 changes: 3 additions & 6 deletions packages/portalnetwork/src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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'

/** 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 {
Expand Down Expand Up @@ -58,8 +57,6 @@ export interface PortalNetworkOpts {
gossipCount?: number
}

export type PortalNetworkEventEmitter = StrictEventEmitter<EventEmitter, PortalNetworkEvents>

export type RoutingTable = PortalNetworkRoutingTable
interface Gauge {
inc(value?: number): void
Expand Down
23 changes: 12 additions & 11 deletions packages/portalnetwork/src/networks/network.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Loading