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