forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.ts
100 lines (90 loc) · 2.95 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { bytesToHex } from '@ethereumjs/util'
import { getClientVersion } from '../../util/index.js'
import { callWithStackTrace } from '../helpers.js'
import { middleware } from '../validation.js'
import type { Chain } from '../../blockchain/index.js'
import type { EthereumClient } from '../../client.js'
import type { RlpxPeer } from '../../net/peer/rlpxpeer.js'
import type { FullEthereumService } from '../../service/index.js'
/**
* admin_* RPC module
* @memberof module:rpc/modules
*/
export class Admin {
readonly _chain: Chain
readonly _client: EthereumClient
private _rpcDebug: boolean
/**
* Create admin_* RPC module
* @param client Client to which the module binds
*/
constructor(client: EthereumClient, rpcDebug: boolean) {
const service = client.service as FullEthereumService
this._chain = service.chain
this._client = client
this._rpcDebug = rpcDebug
this.nodeInfo = middleware(callWithStackTrace(this.nodeInfo.bind(this), this._rpcDebug), 0, [])
this.peers = middleware(callWithStackTrace(this.peers.bind(this), this._rpcDebug), 0, [])
}
/**
* Returns information about the currently running node.
* see for reference: https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-admin#admin_peers
*/
async nodeInfo() {
const rlpxInfo = this._client.config.server!.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 = bytesToHex(this._chain.genesis.hash())
const head = bytesToHex(latestHeader.mixHash)
const network = this._chain.chainId.toString()
const nodeInfo = {
name: clientName,
enode,
id,
ip,
listenAddr,
ports: {
discovery,
listener,
},
protocols: {
eth: {
difficulty,
genesis,
head,
network,
},
},
}
return nodeInfo
}
/**
* Returns information about currently connected peers
* @returns an array of objects containing information about peers (including id, eth protocol versions supported, client name, etc.)
*/
async peers() {
const peers = this._client.service!.pool.peers as RlpxPeer[]
return peers?.map((peer) => {
return {
id: peer.id,
name: peer.rlpxPeer?.['_hello']?.clientId ?? null,
protocols: {
eth: {
head: peer.eth?.updatedBestHeader
? bytesToHex(peer.eth.updatedBestHeader?.hash())
: bytesToHex(peer.eth?.status.bestHash),
difficulty: peer.eth?.status.td.toString(10),
version: peer.eth?.['versions'].slice(-1)[0] ?? null,
},
},
caps: peer.eth?.['versions'].map((ver) => 'eth/' + ver),
network: {
remoteAddress: peer.address,
},
}
})
}
}