-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathstartRPC.ts
More file actions
146 lines (128 loc) · 4.24 KB
/
startRPC.ts
File metadata and controls
146 lines (128 loc) · 4.24 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { RPCManager } from '../src/rpc/index.ts'
import * as modules from '../src/rpc/modules/index.ts'
import {
createRPCServer,
createRPCServerListener,
createWsRPCServerListener,
} from '../src/util/index.ts'
import type { Server } from 'jayson/promise/index.js'
import type { EthereumClient } from '../src/client.ts'
import type { RpcConfig } from '../src/rpc/config.ts'
export type RPCArgs = {
rpc: boolean
rpcAddr: string
rpcPort: number
ws: boolean
wsPort: number
wsAddr: string
rpcEngine: boolean
rpcEngineAddr: string
rpcEnginePort: number
wsEngineAddr: string
wsEnginePort: number
rpcDebug: string
rpcDebugVerbose: string
helpRPC: boolean
jwtSecret?: string
rpcEngineAuth: boolean
rpcCors: string
}
/**
* Starts and returns enabled RPCServers
*/
export function startRPCServers(client: EthereumClient, rpcConfigs: RpcConfig[]): Server[] {
const { config } = client
const servers: Server[] = []
const manager = new RPCManager(client, config)
const serverGroups: Map<string, { rpcConfig: RpcConfig; server: any }> = new Map()
for (const rpcConfig of rpcConfigs) {
// unique key for each server: eth-rpc (http & ws), engine-rpc (http & ws)
// used to create a single rpc server for each transport type
const key = `${rpcConfig.type}-${rpcConfig.methodConfig}`
let serverEntry = serverGroups.get(key)
if (!serverEntry) {
const { server, namespaces, methods } = createRPCServer(manager, {
methodConfig: rpcConfig.methodConfig,
rpcDebug: rpcConfig.debug,
rpcDebugVerbose: rpcConfig.debugVerbose,
logger: config.logger,
})
servers.push(server)
serverGroups.set(key, { rpcConfig, server })
serverEntry = { rpcConfig, server }
config.logger?.info(
`Created RPCServer for type=${rpcConfig.type} methodConfig=${rpcConfig.methodConfig} namespaces=${namespaces} methods=${Object.keys(
methods,
).join(',')}`,
)
}
const { server } = serverEntry
// middleware for engine auth
const middleware =
rpcConfig.engineAuth && rpcConfig.jwtSecret
? {
jwtSecret: rpcConfig.jwtSecret,
unlessFn: (req: any) =>
Array.isArray(req.body)
? req.body.some((r: any) => r.method.includes('engine_')) === false
: req.body.method.includes('engine_') === false,
}
: undefined
if (rpcConfig.transport === 'http') {
const httpServer = createRPCServerListener({
RPCCors: rpcConfig.cors,
server,
withEngineMiddleware: middleware,
})
httpServer.listen(rpcConfig.port, rpcConfig.address)
config.logger?.info(
`Started JSON RPC Server address=http://${rpcConfig.address}:${rpcConfig.port} type=${rpcConfig.type} ${
rpcConfig.engineAuth ? 'engineAuth=true' : ''
}`,
)
}
if (rpcConfig.transport === 'ws') {
const wsOpts: any = {
RPCCors: rpcConfig.cors,
server,
withEngineMiddleware: middleware,
}
// Attach to existing HTTP server for upgrades if same port/address
const httpKey = `${rpcConfig.type}-${rpcConfig.methodConfig}`
if (rpcConfig.address === rpcConfig.address && serverGroups.has(httpKey)) {
wsOpts.httpServer = serverGroups.get(httpKey)?.server
}
const wsServer = createWsRPCServerListener(wsOpts)
if (wsServer) {
wsServer.listen(rpcConfig.port)
config.logger?.info(
`Started JSON RPC WS Server address=ws://${rpcConfig.address}:${rpcConfig.port} namespaces=${rpcConfig.type} ${
rpcConfig.engineAuth ? 'engineAuth=true' : ''
}`,
)
}
}
}
return servers
}
/**
* Output RPC help and exit
*/
export function helpRPC() {
/* eslint-disable no-console */
console.log('-'.repeat(27))
console.log('JSON-RPC: Supported Methods')
console.log('-'.repeat(27))
console.log()
for (const modName of modules.list) {
console.log(`${modName}:`)
const methods = RPCManager.getMethodNames((modules as any)[modName])
for (const methodName of methods) {
console.log(`-> ${modName.toLowerCase()}_${methodName}`)
}
console.log()
}
console.log()
/* eslint-enable no-console */
process.exit()
}