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

Devp2p EventEmitter refactor #2893

Merged
merged 44 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
3437b24
Change access specifiers for RLPx _privateKey, _id, _debug, _timeout
scorbajio Jul 13, 2023
d6e994e
Renaming accessed id property in client tests
scorbajio Jul 13, 2023
edd692a
Change access specifiers for RLPx _maxPeers, _clientId, _remoteClient…
scorbajio Jul 13, 2023
640f52b
Ignore error message from reassigning readonly property in tests
scorbajio Jul 13, 2023
1b9f734
Change access specifiers for _common, _listenPort, and _dpt
scorbajio Jul 13, 2023
be2d41d
Change access specifiers for _peersLRU, _peersQueue, _server, _peers,…
scorbajio Jul 13, 2023
abcd71b
Ignore accessibility errors in examples
scorbajio Jul 16, 2023
666e077
Update names and access specifiers of Peer fields
scorbajio Jul 16, 2023
29849e9
Ignore access errors for _eciesSession
scorbajio Jul 16, 2023
991397c
Make common field public for Peer class
scorbajio Jul 16, 2023
b623ef0
Update names and access specifiers of Peer fields
scorbajio Jul 16, 2023
7fcc607
Make id public readonly
scorbajio Jul 16, 2023
584f5ef
Update names and access specifiers of Mac class fields
scorbajio Jul 16, 2023
8c73139
Update names and access specifiers of ECIES class fields
scorbajio Jul 16, 2023
12d266d
Update names and access specifiers of Peer class fields
scorbajio Jul 16, 2023
00c2cac
Ignore accessibility errors in examples
scorbajio Jul 16, 2023
aa10a2e
Update example
scorbajio Jul 16, 2023
40b15a1
Update names and access specifiers of class fields in protocol subpac…
scorbajio Jul 16, 2023
6c84f17
Update names and access specifiers of class fields in ext subpackage
scorbajio Jul 16, 2023
e8e7f22
Update names and access specifiers of class fields in dpt subpackage
scorbajio Jul 16, 2023
2250869
Update names and access specifiers of class fields in dns subpackage
scorbajio Jul 16, 2023
cea1c28
Merge branch 'master' into devp2p-fields-access-and-naming-cleanup
scorbajio Jul 16, 2023
34a1ab9
Fix name of accessed field
scorbajio Jul 16, 2023
cc708e2
Update tests
scorbajio Jul 16, 2023
9cfb5b8
Merge branch 'devp2p-fields-access-and-naming-cleanup' of github.com:…
scorbajio Jul 16, 2023
12534b6
Don't extend EventEmitter in RLPx class
scorbajio Jul 16, 2023
7b6103d
Merge branch 'master' of github.com:ethereumjs/ethereumjs-monorepo in…
scorbajio Jul 17, 2023
1d176e2
Update tests
scorbajio Jul 17, 2023
bf5694a
Update tests
scorbajio Jul 17, 2023
6ac217e
Update examples
scorbajio Jul 17, 2023
367e6fb
Update tests
scorbajio Jul 17, 2023
00f0e6e
Don't exten EventEmitter in Protocol class
scorbajio Jul 17, 2023
4690132
Update tests
scorbajio Jul 17, 2023
6cb7d15
Update examples
scorbajio Jul 17, 2023
3bcd4cf
Update tests
scorbajio Jul 17, 2023
9aff8b0
Don't extend EventEmitter in dpt class
scorbajio Jul 17, 2023
21afc32
Don't extend EventEmitter in kbucket class
scorbajio Jul 17, 2023
5993820
Update tests
scorbajio Jul 17, 2023
dd0d13d
Update tests
scorbajio Jul 17, 2023
02f2db7
Merge branch 'master' into devp2p-eventemitter-refactor
scorbajio Jul 17, 2023
f8d1025
Merge remote-tracking branch 'origin/master' into devp2p-eventemitter…
acolytec3 Jul 18, 2023
f39f6b5
Fix rlpxserver test
acolytec3 Jul 18, 2023
9924ac6
Fix tests
acolytec3 Jul 18, 2023
8866867
Merge remote-tracking branch 'origin/master' into devp2p-eventemitter…
acolytec3 Jul 19, 2023
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
12 changes: 6 additions & 6 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions packages/client/src/net/peer/rlpxpeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ export class RlpxPeer extends Peer {
this.rlpxPeer = null
this.connected = false
this.config.events.emit(Event.PEER_DISCONNECTED, this)
this.rlpx?.removeListener('peer:error', peerErrorHandlerBound)
this.rlpx?.events.removeListener('peer:error', peerErrorHandlerBound)
}
this.rlpx.on('peer:error', peerErrorHandlerBound)
this.rlpx.once('peer:added', peerAddedHandler.bind(this))
this.rlpx.once('peer:removed', peerRemovedHandler.bind(this))
this.rlpx.events.on('peer:error', peerErrorHandlerBound)
this.rlpx.events.once('peer:added', peerAddedHandler.bind(this))
this.rlpx.events.once('peer:removed', peerRemovedHandler.bind(this))
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/net/protocol/rlpxsender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export class RlpxSender extends Sender {
super()

this.sender = rlpxProtocol
this.sender.on('status', (status: any) => {
this.sender.events.on('status', (status: any) => {
this.status = status
})
this.sender.on('message', (code: number, payload: any) => {
this.sender.events.on('message', (code: number, payload: any) => {
this.emit('message', { code, payload })
})
}
Expand Down
16 changes: 9 additions & 7 deletions packages/client/src/net/server/rlpxserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ export class RlpxServer extends Server {
dnsAddr: this.config.dnsAddr,
})

this.dpt.on('error', (e: Error) => this.error(e))
this.dpt.events.on('error', (e: Error) => this.error(e))

this.dpt.on('listening', () => {
this.dpt.events.on('listening', () => {
resolve()
})

Expand All @@ -250,7 +250,7 @@ export class RlpxServer extends Server {
common: this.config.chainCommon,
})

this.rlpx.on('peer:added', async (rlpxPeer: Devp2pRLPxPeer) => {
this.rlpx.events.on('peer:added', async (rlpxPeer: Devp2pRLPxPeer) => {
let peer: RlpxPeer | null = new RlpxPeer({
config: this.config,
id: bytesToUnprefixedHex(rlpxPeer.getId()!),
Expand All @@ -276,7 +276,7 @@ export class RlpxServer extends Server {
}
})

this.rlpx.on('peer:removed', (rlpxPeer: Devp2pRLPxPeer, reason: any) => {
this.rlpx.events.on('peer:removed', (rlpxPeer: Devp2pRLPxPeer, reason: any) => {
const id = bytesToUnprefixedHex(rlpxPeer.getId() as Uint8Array)
const peer = this.peers.get(id)
if (peer) {
Expand All @@ -288,11 +288,13 @@ export class RlpxServer extends Server {
}
})

this.rlpx.on('peer:error', (rlpxPeer: Devp2pRLPxPeer, error: Error) => this.error(error))
this.rlpx.events.on('peer:error', (rlpxPeer: Devp2pRLPxPeer, error: Error) =>
this.error(error)
)

this.rlpx.on('error', (e: Error) => this.error(e))
this.rlpx.events.on('error', (e: Error) => this.error(e))

this.rlpx.on('listening', () => {
this.rlpx.events.on('listening', () => {
this.config.events.emit(Event.SERVER_LISTENING, {
transport: this.name,
url: this.getRlpxInfo().enode ?? '',
Expand Down
46 changes: 35 additions & 11 deletions packages/client/test/net/peer/rlpxpeer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { RLPx } from '@ethereumjs/devp2p'
import { EventEmitter } from 'events'
import { assert, describe, expect, it, vi } from 'vitest'

import { Config } from '../../../src/config'
import { Event } from '../../../src/types'

describe('[RlpxPeer]', async () => {
vi.mock('@ethereumjs/devp2p')
vi.mock('@ethereumjs/devp2p', async () => {
const devp2p = await vi.importActual<any>('@ethereumjs/devp2p')
const RLPx = vi.fn().mockImplementation(() => {
return {
events: new EventEmitter(),
connect: vi.fn(),
}
})

return {
...devp2p,
RLPx,
}
})

const { RlpxPeer } = await import('../../../src/net/peer/rlpxpeer')

Expand All @@ -22,7 +34,7 @@ describe('[RlpxPeer]', async () => {
assert.notOk(peer.connected, 'not connected')
})

it('should compute capabilities', () => {
it('should compute capabilities', async () => {
const protocols: any = [
{ name: 'eth', versions: [66] },
{ name: 'les', versions: [4] },
Expand Down Expand Up @@ -57,7 +69,7 @@ describe('[RlpxPeer]', async () => {
proto0.open = vi.fn().mockResolvedValue(null)
await peer.connect()
assert.ok('connected successfully')
expect(RLPx.prototype.connect).toBeCalled()
expect(peer.rlpx!.connect).toBeCalled()
})

it('should handle peer events', async () => {
Expand All @@ -83,9 +95,9 @@ describe('[RlpxPeer]', async () => {
peer.config.events.on(Event.PEER_DISCONNECTED, (rlpxPeer) =>
assert.equal(rlpxPeer.pooled, false, 'got disconnected')
)
peer.rlpx!.emit('peer:error', rlpxPeer, new Error('err0'))
peer.rlpx!.emit('peer:added', rlpxPeer)
peer.rlpx!.emit('peer:removed', rlpxPeer, 'reason')
peer.rlpx!.events.emit('peer:error', rlpxPeer, new Error('err0'))
scorbajio marked this conversation as resolved.
Show resolved Hide resolved
peer.rlpx!.events.emit('peer:added', rlpxPeer)
peer.rlpx!.events.emit('peer:removed', rlpxPeer, 'reason')
;(peer as any).bindProtocols = vi.fn().mockRejectedValue(new Error('err1'))
rlpxPeer.getDisconnectPrefix = vi.fn().mockImplementation((param: string) => {
if (param === 'reason') throw new Error('err2')
Expand All @@ -98,8 +110,8 @@ describe('[RlpxPeer]', async () => {
if (err.message === 'err1') assert.ok(true, 'got err1')
if (err.message === 'err2') assert.ok(true, 'got err2')
})
peer.rlpx!.emit('peer:added', rlpxPeer)
peer.rlpx!.emit('peer:removed', rlpxPeer, 'reason')
peer.rlpx!.events.emit('peer:added', rlpxPeer)
peer.rlpx!.events.emit('peer:removed', rlpxPeer, 'reason')
})

it('should accept peer connection', async () => {
Expand All @@ -114,8 +126,20 @@ describe('[RlpxPeer]', async () => {
it('should bind protocols', async () => {
const config = new Config({ transports: [], accountCache: 10000, storageCache: 1000 })
const protocols = [{ name: 'proto0' }] as any
const peer = new RlpxPeer({ config, id: 'abcdef0123', protocols, host: '10.0.0.1', port: 1234 })
const proto0 = new (class Proto0 extends EventEmitter {})()
const peer = new RlpxPeer({
config,
id: 'abcdef0123',
protocols,
host: '10.0.0.1',
port: 1234,
})
class Proto0 {
events: EventEmitter
constructor() {
this.events = new EventEmitter()
}
}
const proto0 = new Proto0()

const rlpxPeer = {
getProtocols: vi.fn().mockReturnValue([proto0]),
Expand Down
10 changes: 5 additions & 5 deletions packages/client/test/net/protocol/rlpxsender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ describe('[RlpxSender]', () => {
})

it('should receive status', () => {
const rlpxProtocol = new EventEmitter()
const rlpxProtocol = { events: new EventEmitter() }
const sender = new RlpxSender(rlpxProtocol as Devp2pETH)
sender.on('status', (status: any) => {
assert.equal(status.id, 5, 'status received')
assert.equal(sender.status.id, 5, 'status getter')
})
rlpxProtocol.emit('status', { id: 5 })
rlpxProtocol.events.emit('status', { id: 5 })
})

it('should receive message', () => {
const rlpxProtocol = new EventEmitter()
const rlpxProtocol = { events: new EventEmitter() }
const sender = new RlpxSender(rlpxProtocol as Devp2pETH)
sender.on('message', (message: any) => {
assert.equal(message.code, 1, 'message received (code)')
assert.equal(message.payload, 5, 'message received (payload)')
})
rlpxProtocol.emit('message', 1, 5)
rlpxProtocol.events.emit('message', 1, 5)
})

it('should catch errors', () => {
const rlpxProtocol = new EventEmitter()
const rlpxProtocol = { events: new EventEmitter() }
const sender = new RlpxSender(rlpxProtocol as Devp2pETH)
assert.throws(() => sender.sendStatus({ id: 5 }), /not a function/, 'sendStatus error')
assert.throws(() => sender.sendMessage(1, 5), /not a function/, 'sendMessage error')
Expand Down
14 changes: 7 additions & 7 deletions packages/client/test/net/server/rlpxserver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('[RlpxServer]', async () => {
server.rlpx = td.object()
td.when(
server.dpt!.bootstrap({ address: '10.0.0.1', udpPort: 1234, tcpPort: 1234 })
).thenResolve()
).thenResolve(undefined)
td.when(
(server.dpt! as any).bootstrap({ address: '10.0.0.2', udpPort: '1234', tcpPort: '1234' })
).thenReject(new Error('err0'))
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('[RlpxServer]', async () => {
assert.equal(err.message, 'err0', 'got error')
})
)
;(server.dpt as any).emit('error', new Error('err0'))
server['dpt'].events.emit('error', new Error('err0'))
})

it('should init rlpx', async () => {
Expand Down Expand Up @@ -269,14 +269,14 @@ describe('[RlpxServer]', async () => {
assert.deepEqual(info, { transport: 'rlpx', url: 'enode://ff@0.0.0.0:30303' }, 'listening')
})
)
server.rlpx!.emit('peer:added', rlpxPeer)
server.rlpx!.events.emit('peer:added', rlpxPeer)
;(server as any).peers.set('01', { id: '01' } as any)
server.rlpx!.emit('peer:removed', rlpxPeer)
server.rlpx!.emit('peer:error', rlpxPeer, new Error('err0'))
server.rlpx!.events.emit('peer:removed', rlpxPeer)
server.rlpx!.events.emit('peer:error', rlpxPeer, new Error('err0'))

// @ts-ignore
server.rlpx!.id = hexToBytes('0xff')
server.rlpx!.emit('listening')
server.rlpx!.events.emit('listening')
})

it('should handles errors from id-less peers', async () => {
Expand All @@ -293,7 +293,7 @@ describe('[RlpxServer]', async () => {
assert.equal(err.message, 'err0', 'got error')
})
)
server.rlpx!.emit('peer:error', rlpxPeer, new Error('err0'))
server.rlpx!.events.emit('peer:error', rlpxPeer, new Error('err0'))
})

it('should reset td', () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/devp2p/examples/peer-communication-les.cts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const dpt = new devp2p.DPT(PRIVATE_KEY, {
})

/* eslint-disable no-console */
dpt.on('error', (err) => console.error(chalk.red(`DPT error: ${err}`)))
dpt.events.on('error', (err) => console.error(chalk.red(`DPT error: ${err}`)))

/* eslint-disable @typescript-eslint/no-use-before-define */

Expand All @@ -61,9 +61,9 @@ const rlpx = new devp2p.RLPx(PRIVATE_KEY, {
remoteClientIdFilter: REMOTE_CLIENTID_FILTER,
})

rlpx.on('error', (err) => console.error(chalk.red(`RLPx error: ${err.stack ?? err}`)))
rlpx.events.on('error', (err) => console.error(chalk.red(`RLPx error: ${err.stack ?? err}`)))

rlpx.on('peer:added', (peer) => {
rlpx.events.on('peer:added', (peer) => {
const addr = getPeerAddr(peer)
const les = peer.getProtocols()[0]
const requests: { headers: BlockHeader[]; bodies: any[] } = { headers: [], bodies: [] }
Expand All @@ -85,7 +85,7 @@ rlpx.on('peer:added', (peer) => {
forkID: [hexToBytes('0x3b8e0691'), intToBytes(1)],
})

les.once('status', (status: LES.Status) => {
les.events.once('status', (status: LES.Status) => {
const msg = [
Uint8Array.from([]),
[
Expand All @@ -98,7 +98,7 @@ rlpx.on('peer:added', (peer) => {
les.sendMessage(devp2p.LES.MESSAGE_CODES.GET_BLOCK_HEADERS, msg)
})

les.on('message', async (code: LES.MESSAGE_CODES, payload: any) => {
les.events.on('message', async (code: LES.MESSAGE_CODES, payload: any) => {
switch (code) {
case devp2p.LES.MESSAGE_CODES.BLOCK_HEADERS: {
if (payload[2].length > 1) {
Expand Down Expand Up @@ -148,7 +148,7 @@ rlpx.on('peer:added', (peer) => {
})
})

rlpx.on('peer:removed', (peer, reasonCode, disconnectWe) => {
rlpx.events.on('peer:removed', (peer, reasonCode, disconnectWe) => {
const who = disconnectWe === true ? 'we disconnect' : 'peer disconnect'
const total = rlpx.getPeers().length
console.log(
Expand All @@ -160,7 +160,7 @@ rlpx.on('peer:removed', (peer, reasonCode, disconnectWe) => {
)
})

rlpx.on('peer:error', (peer, err) => {
rlpx.events.on('peer:error', (peer, err) => {
if (err.code === 'ECONNRESET') return

if (err instanceof Error) {
Expand Down
Loading