Skip to content

Commit

Permalink
Update debug to use IP as first tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Jan 24, 2022
1 parent a570226 commit 18aa6a8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 129 deletions.
13 changes: 7 additions & 6 deletions packages/devp2p/src/dpt/dpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { KBucket } from './kbucket'
import { BanList } from './ban-list'
import { Server as DPTServer } from './server'
import { DNS } from '../dns'
import { Debugger } from 'debug'

const DEFAULT_BASE_NAME = 'dpt'
const debug = devp2pDebug.extend(DEFAULT_BASE_NAME)
const DEBUG_BASE_NAME = 'dpt'

export interface PeerInfo {
id?: Uint8Array | Buffer
Expand Down Expand Up @@ -89,6 +89,7 @@ export class DPT extends EventEmitter {
privateKey: Buffer
banlist: BanList
dns: DNS
_debug: Debugger

private _id: Buffer | undefined
private _kbucket: KBucket
Expand Down Expand Up @@ -129,7 +130,7 @@ export class DPT extends EventEmitter {
this._server.once('listening', () => this.emit('listening'))
this._server.once('close', () => this.emit('close'))
this._server.on('error', (err) => this.emit('error', err))

this._debug = devp2pDebug.extend(DEBUG_BASE_NAME)
// When not using peer neighbour discovery we don't add peers here
// because it results in duplicate calls for the same targets
this._server.on('peers', (peers) => {
Expand Down Expand Up @@ -200,7 +201,7 @@ export class DPT extends EventEmitter {

async addPeer(obj: PeerInfo): Promise<any> {
if (this.banlist.has(obj)) throw new Error('Peer is banned')
debug(`attempt adding peer ${obj.address}:${obj.udpPort}`)
this._debug(`attempt adding peer ${obj.address}:${obj.udpPort}`)

// check k-bucket first
const peer = this._kbucket.get(obj)
Expand Down Expand Up @@ -249,7 +250,7 @@ export class DPT extends EventEmitter {
this._refreshIntervalSelectionCounter = (this._refreshIntervalSelectionCounter + 1) % 10

const peers = this.getPeers()
debug(
this._debug(
`call .refresh() (selector ${this._refreshIntervalSelectionCounter}) (${peers.length} peers in table)`
)

Expand All @@ -266,7 +267,7 @@ export class DPT extends EventEmitter {
if (this._shouldGetDnsPeers) {
const dnsPeers = await this.getDnsPeers()

debug(
this._debug(
`.refresh() Adding ${dnsPeers.length} from DNS tree, (${
this.getPeers().length
} current peers in table)`
Expand Down
29 changes: 7 additions & 22 deletions packages/devp2p/src/dpt/server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { EventEmitter } from 'events'
import * as dgram from 'dgram'
import ms from 'ms'
import { debug as createDebugLogger } from 'debug'
import { debug as createDebugLogger, Debugger } from 'debug'
import LRUCache = require('lru-cache')
import { encode, decode } from './message'
import { keccak256, pk2id, createDeferred, formatLogId, devp2pDebug } from '../util'
import { DPT, PeerInfo } from './dpt'
import { Socket as DgramSocket, RemoteInfo } from 'dgram'

const DEBUG_BASE_NAME = 'dpt:server'
const debug = devp2pDebug.extend(DEBUG_BASE_NAME)
const verbose = createDebugLogger('verbose').enabled

const VERSION = 0x04
Expand Down Expand Up @@ -46,9 +45,7 @@ export class Server extends EventEmitter {
_parityRequestMap: Map<string, string>
_requestsCache: LRUCache<string, Promise<any>>
_socket: DgramSocket | null

// Message debuggers (e.g. { 'findneighbours': [debug Object], ...})
// private msgDebuggers: { [key: string]: (debug: string) => void } = {}
_debug: Debugger

constructor(dpt: DPT, privateKey: Buffer, options: DPTServerOptions) {
super()
Expand All @@ -62,10 +59,9 @@ export class Server extends EventEmitter {
this._parityRequestMap = new Map()
this._requestsCache = new LRUCache({ max: 1000, maxAge: ms('1s'), stale: false })

// this.initMsgDebuggers()

const createSocket = options.createSocket ?? dgram.createSocket.bind(null, { type: 'udp4' })
this._socket = createSocket()
this._debug = devp2pDebug.extend(DEBUG_BASE_NAME)
if (this._socket) {
this._socket.once('listening', () => this.emit('listening'))
this._socket.once('close', () => this.emit('close'))
Expand All @@ -82,14 +78,14 @@ export class Server extends EventEmitter {

bind(...args: any[]) {
this._isAliveCheck()
debug('call .bind')
this._debug('call .bind')

if (this._socket) this._socket.bind(...args)
}

destroy(...args: any[]) {
this._isAliveCheck()
debug('call .destroy')
this._debug('call .destroy')

if (this._socket) {
this._socket.close(...args)
Expand Down Expand Up @@ -117,7 +113,7 @@ export class Server extends EventEmitter {
deferred,
timeoutId: setTimeout(() => {
if (this._requests.get(rkey) !== undefined) {
debug(
this._debug(
`ping timeout: ${peer.address}:${peer.udpPort} ${
peer.id ? formatLogId(peer.id.toString('hex'), verbose) : '-'
}`
Expand Down Expand Up @@ -241,24 +237,13 @@ export class Server extends EventEmitter {
}
}

// private initMsgDebuggers() {
// const MESSAGE_NAMES = ['ping', 'pong', 'findneighbours', 'neighbours']
// for (const name of MESSAGE_NAMES) {
// this.msgDebuggers[name] = createDebugLogger(`${DEBUG_BASE_NAME}:${name}`)
// }
// }

/**
* Debug message both on the generic as well as the
* per-message debug logger
* @param messageName Lower capital message name (e.g. `findneighbours`)
* @param msg Message text to debug
*/
private debug(messageName: string, msg: string) {
debug.extend(messageName)(msg)

// if (this.msgDebuggers[messageName]) {
// this.msgDebuggers[messageName](msg)
// } else debug(msg)
this._debug.extend(messageName)(msg)
}
}
26 changes: 7 additions & 19 deletions packages/devp2p/src/eth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import assert from 'assert'
import { EventEmitter } from 'events'
import ms from 'ms'
import snappy from 'snappyjs'
import { debug as createDebugLogger } from 'debug'
import { debug as createDebugLogger, Debugger } from 'debug'
import { devp2pDebug } from '../util'
import { BN, rlp } from 'ethereumjs-util'
import { int2buffer, buffer2int, assertEq, formatLogId, formatLogData } from '../util'
import { Peer, DISCONNECT_REASONS } from '../rlpx/peer'

const DEBUG_BASE_NAME = 'eth'
const debug = devp2pDebug.extend(DEBUG_BASE_NAME)
const verbose = createDebugLogger('verbose').enabled

/**
Expand All @@ -27,6 +26,7 @@ export class ETH extends EventEmitter {
_peerStatus: ETH.StatusMsg | null
_statusTimeoutId: NodeJS.Timeout
_send: SendMethod
_debug: Debugger

// Eth64
_hardfork: string = 'chainstart'
Expand All @@ -43,7 +43,7 @@ export class ETH extends EventEmitter {
this._version = version
this._peer = peer
this._send = send

this._debug = devp2pDebug.extend(DEBUG_BASE_NAME)
this._status = null
this._peerStatus = null
this._statusTimeoutId = setTimeout(() => {
Expand Down Expand Up @@ -342,17 +342,10 @@ export class ETH extends EventEmitter {
}

private initMsgDebuggers() {
const MESSAGE_NAMES = Object.values(ETH.MESSAGE_CODES).filter(
(value) => typeof value === 'string'
) as string[]
for (const name of MESSAGE_NAMES) {
this.msgDebuggers[name] = createDebugLogger(`${DEBUG_BASE_NAME}:${name}`)
}

// Remote Peer IP logger
const ip = this._peer._socket.remoteAddress
if (ip) {
this.msgDebuggers[ip] = createDebugLogger(`devp2p:${ip}`)
this._debug = devp2pDebug.extend(ip)
}
}

Expand All @@ -365,7 +358,7 @@ export class ETH extends EventEmitter {
_addFirstPeerDebugger() {
const ip = this._peer._socket.remoteAddress
if (ip) {
this.msgDebuggers[ip] = createDebugLogger(`devp2p:FIRST_PEER`)
this._debug = this._debug.extend(`FIRST_PEER`)
this._peer._addFirstPeerDebugger()
_firstPeer = ip
}
Expand All @@ -380,15 +373,10 @@ export class ETH extends EventEmitter {
private debug(messageName: string, msg: string) {
const ip = this._peer._socket.remoteAddress
if (ip) {
debug.extend(ip).extend(messageName)(msg)
this._debug.extend(ip).extend(messageName)(msg)
} else {
debug.extend(messageName)(msg)
this._debug.extend(messageName)(msg)
}
// if (this.msgDebuggers[messageName]) {
// this.msgDebuggers[messageName](msg)
// } else if (ip && this.msgDebuggers[ip]) {
// this.msgDebuggers[ip](msg)
// } else debug(msg)
}
}

Expand Down
30 changes: 10 additions & 20 deletions packages/devp2p/src/les/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { EventEmitter } from 'events'
import { rlp } from 'ethereumjs-util'
import ms from 'ms'
import snappy from 'snappyjs'
import { debug as createDebugLogger } from 'debug'
import { debug as createDebugLogger, Debugger } from 'debug'
import { devp2pDebug } from '../util'
import { int2buffer, buffer2int, assertEq, formatLogData } from '../util'
import { Peer, DISCONNECT_REASONS } from '../rlpx/peer'

const DEBUG_BASE_NAME = 'les'
const debug = devp2pDebug.extend(DEBUG_BASE_NAME)
const verbose = createDebugLogger('verbose').enabled

export const DEFAULT_ANNOUNCE_TYPE = 1
Expand All @@ -28,6 +27,7 @@ export class LES extends EventEmitter {
_status: LES.Status | null
_peerStatus: LES.Status | null
_statusTimeoutId: NodeJS.Timeout
_debug: Debugger

// Message debuggers (e.g. { 'GET_BLOCK_HEADERS': [debug Object], ...})
private msgDebuggers: { [key: string]: (debug: string) => void } = {}
Expand All @@ -38,14 +38,12 @@ export class LES extends EventEmitter {
this._version = version
this._peer = peer
this._send = send

this._debug = devp2pDebug
this._status = null
this._peerStatus = null
this._statusTimeoutId = setTimeout(() => {
this._peer.disconnect(DISCONNECT_REASONS.TIMEOUT)
}, ms('5s'))

this.initMsgDebuggers()
}

static les2 = { name: 'les', version: 2, length: 21, constructor: LES }
Expand Down Expand Up @@ -273,15 +271,6 @@ export class LES extends EventEmitter {
return LES.MESSAGE_CODES[msgCode]
}

private initMsgDebuggers() {
const MESSAGE_NAMES = Object.values(LES.MESSAGE_CODES).filter(
(value) => typeof value === 'string'
) as string[]
for (const name of MESSAGE_NAMES) {
this.msgDebuggers[name] = createDebugLogger(`${DEBUG_BASE_NAME}:${name}`)
}
}

/**
* Called once on the peer where a first successful `STATUS`
* msg exchange could be achieved.
Expand All @@ -291,7 +280,7 @@ export class LES extends EventEmitter {
_addFirstPeerDebugger() {
const ip = this._peer._socket.remoteAddress
if (ip) {
this.msgDebuggers[ip] = createDebugLogger(`devp2p:FIRST_PEER`)
this._debug = this._debug.extend('FIRST_PEER')
this._peer._addFirstPeerDebugger()
_firstPeer = ip
}
Expand All @@ -304,11 +293,12 @@ export class LES extends EventEmitter {
* @param msg Message text to debug
*/
private debug(messageName: string, msg: string) {
debug.extend(messageName)(msg)
// debug(msg)
// if (this.msgDebuggers[messageName]) {
// this.msgDebuggers[messageName](msg)
// }
const ip = this._peer._socket.remoteAddress
if (ip) {
this._debug.extend(ip).extend(DEBUG_BASE_NAME).extend(messageName)(msg)
} else {
this._debug.extend(DEBUG_BASE_NAME).extend(messageName)(msg)
}
}
}

Expand Down
42 changes: 2 additions & 40 deletions packages/devp2p/src/rlpx/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { int2buffer, buffer2int, formatLogData } from '../util'
import { ECIES } from './ecies'

const DEBUG_BASE_NAME = 'rlpx:peer'
// const debug = devp2pDebug.extend(DEBUG_BASE_NAME)
const verbose = createDebugLogger('verbose').enabled

export const BASE_PROTOCOL_VERSION = 5
Expand Down Expand Up @@ -102,9 +101,6 @@ export class Peer extends EventEmitter {
_pingTimeout: number
_logger: Debugger

// Message debuggers (e.g. { 'HELLO': [debug Object], ...})
private msgDebuggers: { [key: string]: (debug: string) => void } = {}

/**
* Subprotocols (e.g. `ETH`) derived from the exchange on
* capabilities
Expand Down Expand Up @@ -152,8 +148,6 @@ export class Peer extends EventEmitter {
// sub-protocols
this._protocols = []

this.initMsgDebuggers()

// send AUTH if outgoing connection
if (this._remoteId !== null) {
this._sendAuth()
Expand Down Expand Up @@ -662,29 +656,6 @@ export class Peer extends EventEmitter {
this._sendDisconnect(reason)
}

private initMsgDebuggers() {
const MESSAGE_NAMES = Object.values(PREFIXES).filter(
(value) => typeof value === 'string'
) as string[]
for (const name of MESSAGE_NAMES) {
this.msgDebuggers[name] = createDebugLogger(`${DEBUG_BASE_NAME}:${name}`)
}

// Remote Peer IP logger
const ip = this._socket.remoteAddress
if (ip) {
this.msgDebuggers[ip] = createDebugLogger(`devp2p:${ip}`)
}

// Special DISCONNECT per-reason logger
const DISCONNECT_NAMES = Object.values(DISCONNECT_REASONS).filter(
(value) => typeof value === 'string'
) as string[]
for (const name of DISCONNECT_NAMES) {
this.msgDebuggers[name] = createDebugLogger(`${DEBUG_BASE_NAME}:DISCONNECT:${name}`)
}
}

/**
* Called once from the subprotocol (e.g. `ETH`) on the peer
* where a first successful `STATUS` msg exchange could be achieved.
Expand All @@ -694,7 +665,7 @@ export class Peer extends EventEmitter {
_addFirstPeerDebugger() {
const ip = this._socket.remoteAddress
if (ip) {
this.msgDebuggers[ip] = createDebugLogger(`devp2p:FIRST_PEER`)
this._logger = devp2pDebug.extend(ip).extend(`FIRST_PEER`).extend(DEBUG_BASE_NAME)
}
}

Expand All @@ -703,22 +674,13 @@ export class Peer extends EventEmitter {
* per-message debug logger
* @param messageName Capitalized message name (e.g. `HELLO`)
* @param msg Message text to debug
* @param disconnectReason Capitalized disconnect reason (e.g. 'TIMEOUT')
*/
private debug(messageName: string, msg: string, disconnectReason?: string) {
if (disconnectReason) {
this._logger.extend(messageName).extend(disconnectReason)(msg)
} else {
this._logger.extend(messageName)(msg)
}

// if (disconnectReason && messageName === 'DISCONNECT') {
// if (this.msgDebuggers[disconnectReason]) {
// this.msgDebuggers[disconnectReason](msg)
// }
// } else if (ip && this.msgDebuggers[ip]) {
// this.msgDebuggers[ip](msg)
// } else if (this.msgDebuggers[messageName]) {
// this.msgDebuggers[messageName](msg)
// } else debug(msg)
}
}
Loading

0 comments on commit 18aa6a8

Please sign in to comment.