Skip to content

Commit

Permalink
Merge branch 'master' into vm-remove-proof-statemanager-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 authored Jan 25, 2022
2 parents b15612f + 5de8729 commit b351717
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 164 deletions.
2 changes: 1 addition & 1 deletion packages/blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@ethereumjs/block": "^3.6.0",
"@ethereumjs/common": "^2.6.0",
"@ethereumjs/ethash": "^1.1.0",
"debug": "^2.2.0",
"debug": "^4.3.3",
"ethereumjs-util": "^7.1.3",
"level-mem": "^5.0.1",
"lru-cache": "^5.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@ethereumjs/tx": "^3.4.0",
"@ethereumjs/vm": "^5.6.0",
"chalk": "^4.1.2",
"debug": "^2.2.0",
"debug": "^4.3.3",
"ethereumjs-util": "^7.1.3",
"fs-extra": "^10.0.0",
"it-pipe": "^1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/devp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@types/lru-cache": "^5.1.0",
"base64url": "^3.0.1",
"bl": "^1.1.2",
"debug": "^2.2.0",
"debug": "^4.3.3",
"ethereumjs-util": "^7.1.3",
"hi-base32": "^0.5.0",
"inherits": "^2.0.1",
Expand Down
15 changes: 9 additions & 6 deletions packages/devp2p/src/dpt/dpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import ms from 'ms'
import { EventEmitter } from 'events'
import { publicKeyCreate } from 'secp256k1'
import { randomBytes } from 'crypto'
import { debug as createDebugLogger } from 'debug'
// import { debug as createDebugLogger } from 'debug'
import { devp2pDebug } from '../util'
import { buffer2int, pk2id } from '../util'
import { KBucket } from './kbucket'
import { BanList } from './ban-list'
import { Server as DPTServer } from './server'
import { DNS } from '../dns'
import { Debugger } from 'debug'

const debug = createDebugLogger('devp2p:dpt')
const DEBUG_BASE_NAME = 'dpt'

export interface PeerInfo {
id?: Uint8Array | Buffer
Expand Down Expand Up @@ -87,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 @@ -127,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 @@ -198,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 @@ -247,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 @@ -264,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
32 changes: 9 additions & 23 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 } from '../util'
import { keccak256, pk2id, createDeferred, formatLogId, devp2pDebug } from '../util'
import { DPT, PeerInfo } from './dpt'
import { Socket as DgramSocket, RemoteInfo } from 'dgram'

const DEBUG_BASE_NAME = 'devp2p:dpt:server'
const debug = createDebugLogger(DEBUG_BASE_NAME)
const DEBUG_BASE_NAME = 'dpt:server'
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,23 +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(msg)
if (this.msgDebuggers[messageName]) {
this.msgDebuggers[messageName](msg)
}
this._debug.extend(messageName)(msg)
}
}
30 changes: 11 additions & 19 deletions packages/devp2p/src/eth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +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 = 'devp2p:eth'
const debug = createDebugLogger(DEBUG_BASE_NAME)
const DEBUG_BASE_NAME = 'eth'
const verbose = createDebugLogger('verbose').enabled

/**
Expand All @@ -26,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 @@ -42,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 @@ -341,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 @@ -364,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 @@ -377,13 +371,11 @@ export class ETH extends EventEmitter {
* @param msg Message text to debug
*/
private debug(messageName: string, msg: string) {
debug(msg)
if (this.msgDebuggers[messageName]) {
this.msgDebuggers[messageName](msg)
}
const ip = this._peer._socket.remoteAddress
if (ip && this.msgDebuggers[ip]) {
this.msgDebuggers[ip](msg)
if (ip) {
this._debug.extend(ip).extend(messageName)(msg)
} else {
this._debug.extend(messageName)(msg)
}
}
}
Expand Down
30 changes: 11 additions & 19 deletions packages/devp2p/src/les/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +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 = 'devp2p:les'
const debug = createDebugLogger(DEBUG_BASE_NAME)
const DEBUG_BASE_NAME = 'les'
const verbose = createDebugLogger('verbose').enabled

export const DEFAULT_ANNOUNCE_TYPE = 1
Expand All @@ -27,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 @@ -37,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 @@ -272,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 @@ -290,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 @@ -303,9 +293,11 @@ export class LES extends EventEmitter {
* @param msg Message text to debug
*/
private debug(messageName: string, msg: string) {
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
Loading

0 comments on commit b351717

Please sign in to comment.