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

Commit 5806f10

Browse files
PhilippLghholgerd77
authored andcommitted
feat: add rpc admin namespace draft
1 parent ddb106e commit 5806f10

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

lib/rpc/modules/admin.ts

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

lib/rpc/modules/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const list = ['Eth', 'Web3', 'Net']
21
export * from './eth'
32
export * from './web3'
43
export * from './net'
4+
export * from './admin'

test/rpc/admin/nodeInfo.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import tape from 'tape'
2+
import { startRPC, createManager, createNode, params, baseRequest } from '../helpers'
3+
4+
const method = 'admin_nodeInfo'
5+
6+
tape(method, (t) => {
7+
const manager = createManager(createNode({ opened: true }))
8+
const server = startRPC(manager.getMethods())
9+
10+
const req = params(method, [])
11+
12+
const expectRes = (res: any) => {
13+
const msg = 'should return the correct info'
14+
const { result } = res.body
15+
if (result) {
16+
t.pass(msg)
17+
} else {
18+
throw new Error(msg)
19+
}
20+
}
21+
baseRequest(t, server, req, 200, expectRes)
22+
})
23+

test/rpc/helpers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +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'
910
import Blockchain from '@ethereumjs/blockchain'
1011

1112
const config: any = { loglevel: 'error' }
@@ -36,6 +37,11 @@ export function createNode(nodeConfig?: any) {
3637
ethProtocolVersions: [63],
3738
}
3839
const trueNodeConfig = { ...defaultNodeConfig, ...nodeConfig }
40+
const servers = [
41+
new RlpxServer({
42+
bootnodes: '10.0.0.1:1234,10.0.0.2:1234'
43+
})
44+
]
3945
return {
4046
services: [
4147
{
@@ -50,8 +56,12 @@ export function createNode(nodeConfig?: any) {
5056
],
5157
},
5258
],
59+
servers,
5360
common: trueNodeConfig.commonChain,
5461
opened: trueNodeConfig.opened,
62+
server: (name: string) => {
63+
return servers.find(s => s.name === name)
64+
}
5565
}
5666
}
5767

0 commit comments

Comments
 (0)