forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.ts
69 lines (61 loc) · 1.83 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
import { bufferToHex } from '@ethereumjs/util'
import { getClientVersion } from '../../util'
import { middleware } from '../validation'
import type { Chain } from '../../blockchain'
import type { EthereumClient } from '../../client'
import type { RlpxServer } from '../../net/server'
import type { EthereumService } from '../../service'
/**
* admin_* RPC module
* @memberof module:rpc/modules
*/
export class Admin {
readonly _chain: Chain
readonly _client: EthereumClient
/**
* Create admin_* RPC module
* @param client Client 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.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
*/
async nodeInfo(_params: []) {
const rlpxInfo = (this._client.server('rlpx') as RlpxServer).getRlpxInfo()
const { enode, id, ip, listenAddr, ports } = rlpxInfo
const { discovery, listener } = ports
const clientName = getClientVersion()
const latestHeader = this._chain.headers.latest!
const difficulty = latestHeader.difficulty.toString()
const genesis = bufferToHex(this._chain.genesis.hash())
const head = bufferToHex(latestHeader.mixHash)
const network = this._chain.networkId.toString()
const nodeInfo = {
name: clientName,
enode,
id,
ip,
listenAddr,
ports: {
discovery,
listener,
},
protocols: {
eth: {
difficulty,
genesis,
head,
network,
},
},
}
return nodeInfo
}
}