forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.ts
68 lines (60 loc) · 1.8 KB
/
net.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
import { addHexPrefix } from '@ethereumjs/util'
import { callWithStackTrace } from '../helpers.js'
import { middleware } from '../validation.js'
import type { Chain } from '../../blockchain/index.js'
import type { EthereumClient } from '../../index.js'
import type { PeerPool } from '../../net/peerpool.js'
import type { FullEthereumService } from '../../service/fullethereumservice.js'
/**
* net_* RPC module
* @memberof module:rpc/modules
*/
export class Net {
private _chain: Chain
private _client: EthereumClient
private _peerPool: PeerPool
private _rpcDebug: boolean
/**
* Create net_* 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._peerPool = service.pool
this._rpcDebug = rpcDebug
this.version = middleware(callWithStackTrace(this.version.bind(this), this._rpcDebug), 0, [])
this.listening = middleware(
callWithStackTrace(this.listening.bind(this), this._rpcDebug),
0,
[],
)
this.peerCount = middleware(
callWithStackTrace(this.peerCount.bind(this), this._rpcDebug),
0,
[],
)
}
/**
* Returns the current network id
* @param params An empty array
*/
version(_params = []) {
return this._chain.config.chainCommon.chainId().toString()
}
/**
* Returns true if client is actively listening for network connections
* @param params An empty array
*/
listening(_params = []) {
return this._client.opened
}
/**
* Returns number of peers currently connected to the client
* @param params An empty array
*/
peerCount(_params = []) {
return addHexPrefix(this._peerPool.peers.length.toString(16))
}
}