Skip to content
This repository was archived by the owner on Dec 10, 2020. It is now read-only.

Commit 1c86422

Browse files
PhilippLghholgerd77
authored andcommitted
chore: fix test
1 parent bb3fb37 commit 1c86422

File tree

8 files changed

+50
-61
lines changed

8 files changed

+50
-61
lines changed

lib/net/server/rlpxserver.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class RlpxServer extends Server {
9797
id: undefined,
9898
ip: this.ip,
9999
listenAddr: `[${this.ip}]:${this.port}`,
100-
ports: { 'discovery': this.port, 'listener': this.port }
100+
ports: { discovery: this.port, listener: this.port },
101101
}
102102
}
103103
const id = this.rlpx._id.toString('hex')
@@ -106,7 +106,7 @@ export class RlpxServer extends Server {
106106
id: id,
107107
ip: this.ip,
108108
listenAddr: `[${this.ip}]:${this.port}`,
109-
ports: { 'discovery': this.port, 'listener': this.port }
109+
ports: { discovery: this.port, listener: this.port },
110110
}
111111
}
112112

@@ -147,8 +147,8 @@ export class RlpxServer extends Server {
147147
*/
148148
async stop(): Promise<boolean> {
149149
if (this.started) {
150-
; (this.rlpx as Devp2pRLPx).destroy() // eslint-disable-line no-extra-semi
151-
; (this.dpt as Devp2pDPT).destroy()
150+
;(this.rlpx as Devp2pRLPx).destroy() // eslint-disable-line no-extra-semi
151+
;(this.dpt as Devp2pDPT).destroy()
152152
await super.stop()
153153
this.started = false
154154
}
@@ -165,7 +165,7 @@ export class RlpxServer extends Server {
165165
if (!this.started) {
166166
return false
167167
}
168-
; (this.dpt as Devp2pDPT).banPeer(peerId, maxAge) // eslint-disable-line no-extra-semi
168+
;(this.dpt as Devp2pDPT).banPeer(peerId, maxAge) // eslint-disable-line no-extra-semi
169169
return true
170170
}
171171

lib/rpc/modules/admin.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { bufferToHex } from 'ethereumjs-util'
22
import { Chain } from '../../blockchain'
3-
import { EthProtocol } from '../../net/protocol'
3+
import { EthProtocol, Protocol } from '../../net/protocol'
44
import Node from '../../node'
5+
import { Service } from '../../service'
56
import { getClientVersion } from '../../util'
67
import { middleware } from '../validation'
78

@@ -19,10 +20,10 @@ export class Admin {
1920
* @param {Node} Node to which the module binds
2021
*/
2122
constructor(node: Node) {
22-
const service = node.services.find(s => s.name === 'eth')
23+
const service = node.services.find((s: Service) => s.name === 'eth')
2324
this._chain = service.chain
2425
this._node = node
25-
this._ethProtocol = service.protocols.find(p => p.name === 'eth')
26+
this._ethProtocol = service.protocols.find((p: Protocol) => p.name === 'eth')
2627

2728
this.nodeInfo = middleware(this.nodeInfo.bind(this), 0, [])
2829
}
@@ -31,21 +32,20 @@ export class Admin {
3132
* Returns information about the currently running node.
3233
* see for reference: https://geth.ethereum.org/docs/rpc/ns-admin#admin_nodeinfo
3334
* @param {*} [params] An empty array
34-
* @param {*} [cb] A function with an error object as the first argument and the
35+
* @param {*} [cb] A function with an error object as the first argument and the result as the second
3536
*/
36-
async nodeInfo(params, cb) {
37+
async nodeInfo(params: any, cb: Function) {
3738
const rlpxInfo = this._node.server('rlpx').getRlpxInfo()
3839
const { enode, id, ip, listenAddr, ports } = rlpxInfo
3940
const { discovery, listener } = ports
4041
const clientName = getClientVersion()
4142

4243
// TODO version not present in reference..
4344
// const ethVersion = Math.max.apply(Math, this._ethProtocol.versions)
44-
// TODO _chain._headers might be undefined
45-
const latestHeader = this._chain._headers.latest
46-
const difficulty = latestHeader.difficulty // should be number
45+
const latestHeader = (this._chain as any)._headers.latest
46+
const difficulty = latestHeader?.difficulty || 0 // should be number
4747
const genesis = bufferToHex(this._chain.genesis.hash)
48-
const head = bufferToHex(latestHeader.mixHash)
48+
const head = bufferToHex(latestHeader?.mixHash || null)
4949
const network = this._chain.networkId
5050

5151
const nodeInfo = {
@@ -56,16 +56,16 @@ export class Admin {
5656
listenAddr,
5757
ports: {
5858
discovery,
59-
listener
59+
listener,
6060
},
6161
protocols: {
6262
eth: {
6363
difficulty,
6464
genesis,
6565
head,
66-
network
67-
}
68-
}
66+
network,
67+
},
68+
},
6969
}
7070
return cb(null, nodeInfo)
7171
}

lib/rpc/modules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const list = ['Eth', 'Web3', 'Net', 'Admin']
2+
13
export * from './eth'
24
export * from './web3'
35
export * from './net'

lib/rpc/modules/web3.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { middleware, validators } from '../validation'
22
import { addHexPrefix, keccak, toBuffer } from 'ethereumjs-util'
3-
import { platform } from 'os'
43
import { getClientVersion } from '../../util'
54

65
/**

lib/util/index.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/net/server/rlpxserver.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const { defaultLogger } = require('../../../lib/logging')
66
defaultLogger.silent = true
77

88
tape('[RlpxServer]', (t) => {
9-
class RlpxPeer extends EventEmitter { }
9+
class RlpxPeer extends EventEmitter {}
1010
RlpxPeer.capabilities = td.func()
1111
RlpxPeer.prototype.accept = td.func()
1212
td.replace('../../../lib/net/peer/rlpxpeer', RlpxPeer)
13-
class RLPx extends EventEmitter { }
13+
class RLPx extends EventEmitter {}
1414
RLPx.prototype.listen = td.func()
15-
class DPT extends EventEmitter { }
15+
class DPT extends EventEmitter {}
1616
DPT.prototype.bind = td.func()
1717
td.replace('ethereumjs-devp2p', { DPT, RLPx })
1818
td.when(
@@ -75,32 +75,40 @@ tape('[RlpxServer]', (t) => {
7575
t.test('should return rlpx server info', async (t) => {
7676
const mockId = '123'
7777
const server = new RlpxServer({
78-
bootnodes: '10.0.0.1:1234,10.0.0.2:1234'
78+
bootnodes: '10.0.0.1:1234,10.0.0.2:1234',
7979
})
8080
server.initDpt = td.func()
8181
server.initRlpx = td.func()
8282
server.dpt = td.object()
8383
server.rlpx = td.object({
8484
_id: mockId,
85-
destroy: td.func()
85+
destroy: td.func(),
8686
})
87-
td.when(server.dpt.bootstrap({ address: '10.0.0.1', udpPort: '1234', tcpPort: '1234' })).thenResolve()
88-
td.when(server.dpt.bootstrap({ address: '10.0.0.2', udpPort: '1234', tcpPort: '1234' })).thenReject('err0')
89-
server.on('error', err => t.equals(err, 'err0', 'got error'))
87+
td.when(
88+
server.dpt?.bootstrap({ address: '10.0.0.1', udpPort: '1234', tcpPort: '1234' })
89+
).thenResolve()
90+
td.when(
91+
server.dpt?.bootstrap({ address: '10.0.0.2', udpPort: '1234', tcpPort: '1234' })
92+
).thenReject('err0')
93+
server.on('error', (err) => t.equals(err, 'err0', 'got error'))
9094
await server.start()
9195
const nodeInfo = server.getRlpxInfo()
92-
t.deepEqual(nodeInfo, {
93-
enode: `enode://${mockId}@[::]:30303`,
94-
id: mockId,
95-
ip: '::',
96-
listenAddr: '[::]:30303',
97-
ports: { discovery: 30303, listener: 30303 }
98-
}, 'get nodeInfo')
96+
t.deepEqual(
97+
nodeInfo,
98+
{
99+
enode: `enode://${mockId}@[::]:30303`,
100+
id: mockId,
101+
ip: '::',
102+
listenAddr: '[::]:30303',
103+
ports: { discovery: 30303, listener: 30303 },
104+
},
105+
'get nodeInfo'
106+
)
99107
await server.stop()
100108
t.end()
101109
})
102110

103-
t.test('should handle errors', t => {
111+
t.test('should handle errors', (t) => {
104112
t.plan(3)
105113
let count = 0
106114
const server = new RlpxServer()

test/rpc/admin/nodeInfo.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,18 @@ import { startRPC, createManager, createNode, params, baseRequest } from '../hel
33

44
const method = 'admin_nodeInfo'
55

6-
<<<<<<< HEAD:test/rpc/admin/nodeInfo.ts
76
tape(method, (t) => {
8-
=======
9-
test(method, (t) => {
10-
>>>>>>> chore: improve test:test/rpc/admin/nodeInfo.js
117
const manager = createManager(createNode({ opened: true }))
128
const server = startRPC(manager.getMethods())
139

1410
const req = params(method, [])
1511

1612
const expectRes = (res: any) => {
17-
const msg = 'should return the correct info'
1813
const { result } = res.body
1914
if (result) {
2015
t.pass('admin_nodeInfo returns a value')
2116
} else {
22-
throw new Error(msg)
17+
throw new Error('no return value')
2318
}
2419
}
2520
baseRequest(t, server, req, 200, expectRes)

test/rpc/helpers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { RPCManager as Manager } from '../../lib/rpc'
66
import * as Logger from '../../lib/logging'
77
import { blockChain } from './blockChainStub'
88
import { Chain } from '../../lib/blockchain/chain'
9-
import RlpxServer from '../../lib/net/server/rlpxserver'
9+
import { RlpxServer } from '../../lib/net/server/rlpxserver'
1010
import Blockchain from '@ethereumjs/blockchain'
1111

1212
const config: any = { loglevel: 'error' }
@@ -39,8 +39,8 @@ export function createNode(nodeConfig?: any) {
3939
const trueNodeConfig = { ...defaultNodeConfig, ...nodeConfig }
4040
const servers = [
4141
new RlpxServer({
42-
bootnodes: '10.0.0.1:1234,10.0.0.2:1234'
43-
})
42+
bootnodes: '10.0.0.1:1234,10.0.0.2:1234',
43+
}),
4444
]
4545
return {
4646
services: [
@@ -60,8 +60,8 @@ export function createNode(nodeConfig?: any) {
6060
common: trueNodeConfig.commonChain,
6161
opened: trueNodeConfig.opened,
6262
server: (name: string) => {
63-
return servers.find(s => s.name === name)
64-
}
63+
return servers.find((s) => s.name === name)
64+
},
6565
}
6666
}
6767

0 commit comments

Comments
 (0)