Skip to content

Commit

Permalink
eventemitter3
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Dec 20, 2024
1 parent e224662 commit d8ab19c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 37 deletions.
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
6 changes: 3 additions & 3 deletions packages/portalnetwork/src/transports/capacitorUdp.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand All @@ -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<ITransportEvents>
implements ITransportService
{
private socket!: {
Expand Down
9 changes: 2 additions & 7 deletions packages/portalnetwork/src/transports/websockets.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -17,23 +17,18 @@ 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')

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<WebSocketTransportEvents>
implements ITransportService
{
public multiaddr: Multiaddr
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

0 comments on commit d8ab19c

Please sign in to comment.