Skip to content

Commit

Permalink
remove duplicated methods in child classes, lint:fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Mar 9, 2022
1 parent 086d057 commit 5b685d4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 87 deletions.
47 changes: 0 additions & 47 deletions packages/devp2p/src/protocol/eth.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import assert from 'assert'
import snappy from 'snappyjs'
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 } from '../rlpx/peer'
import { Protocol } from './protocol'

const DEBUG_BASE_NAME = 'eth'
const verbose = createDebugLogger('verbose').enabled

/**
* Will be set to the first successfully connected peer to allow for
* debugging with the `devp2p:FIRST_PEER` debugger
*/
let _firstPeer = ''

type SendMethod = (code: ETH.MESSAGE_CODES, data: Buffer) => any

Expand All @@ -23,7 +15,6 @@ export class ETH extends Protocol {
_status: ETH.StatusMsg | null
_peerStatus: ETH.StatusMsg | null
_send: SendMethod
_debug: Debugger

// Eth64
_hardfork: string = 'chainstart'
Expand Down Expand Up @@ -330,44 +321,6 @@ export class ETH extends Protocol {
getMsgPrefix(msgCode: ETH.MESSAGE_CODES): string {
return ETH.MESSAGE_CODES[msgCode]
}

private initMsgDebuggers() {
// Remote Peer IP logger
const ip = this._peer._socket.remoteAddress
if (ip) {
this._debug = devp2pDebug.extend(ip)
}
}

/**
* Called once on the peer where a first successful `STATUS`
* msg exchange could be achieved.
*
* Can be used together with the `devp2p:FIRST_PEER` debugger.
*/
_addFirstPeerDebugger() {
const ip = this._peer._socket.remoteAddress
if (ip) {
this._debug = this._debug.extend(`FIRST_PEER`)
this._peer._addFirstPeerDebugger()
_firstPeer = ip
}
}

/**
* Debug message both on the generic as well as the
* per-message debug logger
* @param messageName Capitalized message name (e.g. `GET_BLOCK_HEADERS`)
* @param msg Message text to debug
*/
private debug(messageName: string, msg: string) {
const ip = this._peer._socket.remoteAddress
if (ip) {
this._debug.extend(ip).extend(messageName)(msg)
} else {
this._debug.extend(messageName)(msg)
}
}
}

export namespace ETH {
Expand Down
37 changes: 2 additions & 35 deletions packages/devp2p/src/protocol/les.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import ms from 'ms'
import { rlp } from 'ethereumjs-util'
import snappy from 'snappyjs'
import { debug as createDebugLogger, Debugger } from 'debug'
import { devp2pDebug } from '../util'
import { int2buffer, buffer2int, assertEq, formatLogData } from '../util'
import { Peer } from '../rlpx/peer'
import { Peer, DISCONNECT_REASONS } from '../rlpx/peer'
import { Protocol } from './protocol'

const DEBUG_BASE_NAME = 'les'
const verbose = createDebugLogger('verbose').enabled

export const DEFAULT_ANNOUNCE_TYPE = 1

Expand All @@ -18,8 +17,6 @@ export class LES extends Protocol {
_send: SendMethod
_status: LES.Status | null
_peerStatus: LES.Status | null
_statusTimeoutId: NodeJS.Timeout
_debug: Debugger

constructor(version: number, peer: Peer, send: SendMethod) {
super(peer, LES.MESSAGE_CODES, DEBUG_BASE_NAME)
Expand Down Expand Up @@ -259,36 +256,6 @@ export class LES extends Protocol {
getMsgPrefix(msgCode: LES.MESSAGE_CODES) {
return LES.MESSAGE_CODES[msgCode]
}

/**
* Called once on the peer where a first successful `STATUS`
* msg exchange could be achieved.
*
* Can be used together with the `devp2p:FIRST_PEER` debugger.
*/
_addFirstPeerDebugger() {
const ip = this._peer._socket.remoteAddress
if (ip) {
this._debug = this._debug.extend('FIRST_PEER')
this._peer._addFirstPeerDebugger()
_firstPeer = ip
}
}

/**
* Debug message both on the generic as well as the
* per-message debug logger
* @param messageName Capitalized message name (e.g. `GET_BLOCK_HEADERS`)
* @param msg Message text to debug
*/
private debug(messageName: string, msg: string) {
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)
}
}
}

export namespace LES {
Expand Down
10 changes: 6 additions & 4 deletions packages/devp2p/src/protocol/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import ms from 'ms'
import { debug as createDebugLogger } from 'debug'
import { debug as createDebugLogger, Debugger } from 'debug'
import { EventEmitter } from 'events'
import { Peer, DISCONNECT_REASONS } from '../rlpx/peer'

type MessageCodes = { [key: number | string]: number | string }

export class Protocol extends EventEmitter {
_peer: Peer
_statusTimeoutId: NodeJS.Timeout
_messageCodes: { [key: string : number }
_debug: createDebugLogger
_messageCodes: MessageCodes
_debug: Debugger
_verbose: boolean

/**
Expand All @@ -19,7 +21,7 @@ export class Protocol extends EventEmitter {
// Message debuggers (e.g. { 'GET_BLOCK_HEADERS': [debug Object], ...})
protected msgDebuggers: { [key: string]: (debug: string) => void } = {}

constructor(peer: Peer, _messageCodes: any, debugBaseName: string) {
constructor(peer: Peer, _messageCodes: MessageCodes, debugBaseName: string) {
super()

this._firstPeer = ''
Expand Down
3 changes: 2 additions & 1 deletion packages/devp2p/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { randomBytes } from 'crypto'
import { privateKeyVerify, publicKeyConvert } from 'secp256k1'
import createKeccakHash from 'keccak'
import { rlp } from 'ethereumjs-util'
import { ETH } from './protocol/eth'
import { LES } from './protocol/les'

import { debug as createDebugLogger } from 'debug'

export const devp2pDebug = createDebugLogger('devp2p')


export function keccak256(...buffers: Buffer[]) {
const buffer = Buffer.concat(buffers)
return createKeccakHash('keccak256').update(buffer).digest()
Expand Down

0 comments on commit 5b685d4

Please sign in to comment.