Skip to content

Commit ba916e5

Browse files
authored
devp2p: fix per-message debug logging (#1776)
* devp2p: fix debug logging * move _version to protocol class * move _send to protocol class * simplify types, set null defaults in class rather than in constructor
1 parent 2771a8b commit ba916e5

File tree

3 files changed

+38
-48
lines changed

3 files changed

+38
-48
lines changed

packages/devp2p/src/protocol/eth.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
11
import assert from 'assert'
22
import snappy from 'snappyjs'
3-
import { devp2pDebug } from '../util'
43
import { BN, rlp } from 'ethereumjs-util'
54
import { int2buffer, buffer2int, assertEq, formatLogId, formatLogData } from '../util'
65
import { Peer } from '../rlpx/peer'
7-
import { Protocol } from './protocol'
8-
9-
const DEBUG_BASE_NAME = 'eth'
10-
11-
type SendMethod = (code: ETH.MESSAGE_CODES, data: Buffer) => any
6+
import { EthProtocol, Protocol, SendMethod } from './protocol'
127

138
export class ETH extends Protocol {
14-
_version: number
15-
_status: ETH.StatusMsg | null
16-
_peerStatus: ETH.StatusMsg | null
17-
_send: SendMethod
9+
_status: ETH.StatusMsg | null = null
10+
_peerStatus: ETH.StatusMsg | null = null
1811

1912
// Eth64
20-
_hardfork: string = 'chainstart'
13+
_hardfork = 'chainstart'
2114
_latestBlock = new BN(0)
22-
_forkHash: string = ''
15+
_forkHash = ''
2316
_nextForkBlock = new BN(0)
2417

2518
constructor(version: number, peer: Peer, send: SendMethod) {
26-
super(peer, ETH.MESSAGE_CODES, DEBUG_BASE_NAME)
27-
28-
this._version = version
29-
this._peer = peer
30-
this._send = send
31-
this._debug = devp2pDebug.extend(DEBUG_BASE_NAME)
32-
this._status = null
33-
this._peerStatus = null
19+
super(peer, send, EthProtocol.ETH, version, ETH.MESSAGE_CODES)
3420

3521
// Set forkHash and nextForkBlock
3622
if (this._version >= 64) {

packages/devp2p/src/protocol/les.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
import ms from 'ms'
22
import { rlp } from 'ethereumjs-util'
33
import snappy from 'snappyjs'
4-
import { devp2pDebug } from '../util'
54
import { int2buffer, buffer2int, assertEq, formatLogData } from '../util'
65
import { Peer, DISCONNECT_REASONS } from '../rlpx/peer'
7-
import { Protocol } from './protocol'
8-
9-
const DEBUG_BASE_NAME = 'les'
6+
import { EthProtocol, Protocol, SendMethod } from './protocol'
107

118
export const DEFAULT_ANNOUNCE_TYPE = 1
129

13-
type SendMethod = (code: LES.MESSAGE_CODES, data: Buffer) => any
14-
1510
export class LES extends Protocol {
16-
_version: number
17-
_send: SendMethod
18-
_status: LES.Status | null
19-
_peerStatus: LES.Status | null
11+
_status: LES.Status | null = null
12+
_peerStatus: LES.Status | null = null
2013

2114
constructor(version: number, peer: Peer, send: SendMethod) {
22-
super(peer, LES.MESSAGE_CODES, DEBUG_BASE_NAME)
23-
24-
this._version = version
25-
this._peer = peer
26-
this._send = send
27-
this._debug = devp2pDebug
28-
this._status = null
29-
this._peerStatus = null
15+
super(peer, send, EthProtocol.LES, version, LES.MESSAGE_CODES)
16+
3017
this._statusTimeoutId = setTimeout(() => {
3118
this._peer.disconnect(DISCONNECT_REASONS.TIMEOUT)
3219
}, ms('5s'))

packages/devp2p/src/protocol/protocol.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import ms from 'ms'
22
import { debug as createDebugLogger, Debugger } from 'debug'
33
import { EventEmitter } from 'events'
4+
import { devp2pDebug } from '../util'
45
import { Peer, DISCONNECT_REASONS } from '../rlpx/peer'
56

7+
export enum EthProtocol {
8+
ETH = 'eth',
9+
LES = 'les',
10+
}
11+
612
type MessageCodes = { [key: number | string]: number | string }
713

14+
export type SendMethod = (code: number, data: Buffer) => any
15+
816
export class Protocol extends EventEmitter {
17+
_version: number
918
_peer: Peer
19+
_send: SendMethod
1020
_statusTimeoutId: NodeJS.Timeout
1121
_messageCodes: MessageCodes
1222
_debug: Debugger
@@ -16,38 +26,45 @@ export class Protocol extends EventEmitter {
1626
* Will be set to the first successfully connected peer to allow for
1727
* debugging with the `devp2p:FIRST_PEER` debugger
1828
*/
19-
_firstPeer: string
29+
_firstPeer = ''
2030

2131
// Message debuggers (e.g. { 'GET_BLOCK_HEADERS': [debug Object], ...})
2232
protected msgDebuggers: { [key: string]: (debug: string) => void } = {}
2333

24-
constructor(peer: Peer, _messageCodes: MessageCodes, debugBaseName: string) {
34+
constructor(
35+
peer: Peer,
36+
send: SendMethod,
37+
protocol: EthProtocol,
38+
version: number,
39+
messageCodes: MessageCodes
40+
) {
2541
super()
2642

27-
this._firstPeer = ''
2843
this._peer = peer
29-
this._messageCodes = _messageCodes
44+
this._send = send
45+
this._version = version
46+
this._messageCodes = messageCodes
3047
this._statusTimeoutId = setTimeout(() => {
3148
this._peer.disconnect(DISCONNECT_REASONS.TIMEOUT)
3249
}, ms('5s'))
3350

34-
this._debug = createDebugLogger(debugBaseName)
51+
this._debug = devp2pDebug.extend(protocol)
3552
this._verbose = createDebugLogger('verbose').enabled
36-
this.initMsgDebuggers(debugBaseName)
53+
this.initMsgDebuggers(protocol)
3754
}
3855

39-
private initMsgDebuggers(debugBaseName: string) {
56+
private initMsgDebuggers(protocol: EthProtocol) {
4057
const MESSAGE_NAMES = Object.values(this._messageCodes).filter(
4158
(value) => typeof value === 'string'
4259
) as string[]
4360
for (const name of MESSAGE_NAMES) {
44-
this.msgDebuggers[name] = createDebugLogger(`${debugBaseName}:${name}`)
61+
this.msgDebuggers[name] = devp2pDebug.extend(protocol).extend(name)
4562
}
4663

4764
// Remote Peer IP logger
4865
const ip = this._peer._socket.remoteAddress
4966
if (ip) {
50-
this.msgDebuggers[ip] = createDebugLogger(`devp2p:${ip}`)
67+
this.msgDebuggers[ip] = devp2pDebug.extend(ip)
5168
}
5269
}
5370

@@ -60,7 +77,7 @@ export class Protocol extends EventEmitter {
6077
_addFirstPeerDebugger() {
6178
const ip = this._peer._socket.remoteAddress
6279
if (ip) {
63-
this.msgDebuggers[ip] = createDebugLogger(`devp2p:FIRST_PEER`)
80+
this.msgDebuggers[ip] = devp2pDebug.extend('FIRST_PEER')
6481
this._peer._addFirstPeerDebugger()
6582
this._firstPeer = ip
6683
}

0 commit comments

Comments
 (0)