This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
admin.ts
73 lines (67 loc) · 2.25 KB
/
admin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { bufferToHex } from 'ethereumjs-util'
import { Chain } from '../../blockchain'
import { EthProtocol } from '../../net/protocol'
import { RlpxServer } from '../../net/server'
import EthereumClient from '../../client'
import { EthereumService } from '../../service'
import { getClientVersion } from '../../util'
import { middleware } from '../validation'
/**
* admin_* RPC module
* @memberof module:rpc/modules
*/
export class Admin {
readonly _chain: Chain
readonly _client: EthereumClient
readonly _ethProtocol: EthProtocol
/**
* Create admin_* RPC module
* @param {client} EthereumClient to which the module binds
*/
constructor(client: EthereumClient) {
const service = client.services.find((s) => s.name === 'eth') as EthereumService
this._chain = service.chain
this._client = client
this._ethProtocol = service.protocols.find((p) => p.name === 'eth') as EthProtocol
this.nodeInfo = middleware(this.nodeInfo.bind(this), 0, [])
}
/**
* Returns information about the currently running node.
* see for reference: https://geth.ethereum.org/docs/rpc/ns-admin#admin_nodeinfo
* @param {*} [params] An empty array
* @param {*} [cb] A function with an error object as the first argument and the result as the second
*/
async nodeInfo(params: any, cb: Function) {
const rlpxInfo = (this._client.server('rlpx') as RlpxServer).getRlpxInfo()
const { enode, id, ip, listenAddr, ports } = rlpxInfo
const { discovery, listener } = ports
const clientName = getClientVersion()
// TODO version not present in reference..
// const ethVersion = Math.max.apply(Math, this._ethProtocol.versions)
const latestHeader = (this._chain as any)._headers.latest
const difficulty = latestHeader?.difficulty ?? 0 // should be number
const genesis = bufferToHex(this._chain.genesis.hash)
const head = bufferToHex(latestHeader?.mixHash ?? null)
const network = this._chain.networkId
const nodeInfo = {
name: clientName,
enode,
id,
ip,
listenAddr,
ports: {
discovery,
listener,
},
protocols: {
eth: {
difficulty,
genesis,
head,
network,
},
},
}
return cb(null, nodeInfo)
}
}