2022-12-02 04:39:12 +00:00
|
|
|
# Nimbus
|
2024-01-24 17:18:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-12-02 04:39:12 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import
|
2024-01-29 13:20:04 +00:00
|
|
|
chronicles,
|
|
|
|
websock/websock,
|
|
|
|
json_rpc/rpcserver,
|
|
|
|
graphql/httpserver,
|
2022-12-02 04:39:12 +00:00
|
|
|
./rpc/common,
|
2024-06-18 11:14:02 +00:00
|
|
|
#./rpc/debug,
|
2022-12-02 04:39:12 +00:00
|
|
|
./rpc/engine_api,
|
|
|
|
./rpc/jwt_auth,
|
|
|
|
./rpc/cors,
|
2024-01-22 09:11:37 +00:00
|
|
|
./rpc/rpc_server,
|
2024-09-04 09:54:54 +00:00
|
|
|
./rpc/server_api,
|
2024-01-29 13:20:04 +00:00
|
|
|
./nimbus_desc,
|
|
|
|
./graphql/ethapi
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
common,
|
|
|
|
debug,
|
|
|
|
engine_api,
|
|
|
|
jwt_auth,
|
|
|
|
cors,
|
2024-01-22 09:11:37 +00:00
|
|
|
rpc_server,
|
2024-09-04 09:54:54 +00:00
|
|
|
server_api
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
{.push gcsafe, raises: [].}
|
|
|
|
|
|
|
|
const DefaultChunkSize = 8192
|
|
|
|
|
|
|
|
func serverEnabled(conf: NimbusConf): bool =
|
|
|
|
conf.httpServerEnabled or
|
|
|
|
conf.engineApiServerEnabled
|
|
|
|
|
|
|
|
func combinedServer(conf: NimbusConf): bool =
|
|
|
|
conf.httpServerEnabled and
|
|
|
|
conf.shareServerWithEngineApi
|
|
|
|
|
2024-09-18 08:53:26 +00:00
|
|
|
func installRPC(server: RpcServer,
|
2024-01-29 13:20:04 +00:00
|
|
|
nimbus: NimbusNode,
|
|
|
|
conf: NimbusConf,
|
|
|
|
com: CommonRef,
|
2024-09-27 00:53:27 +00:00
|
|
|
serverApi: ServerAPIRef,
|
2024-01-29 13:20:04 +00:00
|
|
|
flags: set[RpcFlag]) =
|
|
|
|
|
|
|
|
setupCommonRpc(nimbus.ethNode, conf, server)
|
|
|
|
|
|
|
|
if RpcFlag.Eth in flags:
|
2024-11-02 09:30:45 +00:00
|
|
|
setupServerAPI(serverApi, server, nimbus.ctx)
|
2024-01-29 13:20:04 +00:00
|
|
|
|
2024-06-18 11:14:02 +00:00
|
|
|
# # Tracer is currently disabled
|
|
|
|
# if RpcFlag.Debug in flags:
|
|
|
|
# setupDebugRpc(com, nimbus.txPool, server)
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
server.rpc("admin_quit") do() -> string:
|
|
|
|
{.gcsafe.}:
|
|
|
|
nimbus.state = NimbusState.Stopping
|
|
|
|
result = "EXITING"
|
|
|
|
|
|
|
|
proc newRpcWebsocketHandler(): RpcWebSocketHandler =
|
|
|
|
let rng = HmacDrbgContext.new()
|
|
|
|
RpcWebSocketHandler(
|
|
|
|
wsserver: WSServer.new(rng = rng),
|
|
|
|
)
|
|
|
|
|
2024-09-18 08:53:26 +00:00
|
|
|
func newRpcHttpHandler(): RpcHttpHandler =
|
2024-01-29 13:20:04 +00:00
|
|
|
RpcHttpHandler(
|
|
|
|
maxChunkSize: DefaultChunkSize,
|
|
|
|
)
|
|
|
|
|
2024-09-18 08:53:26 +00:00
|
|
|
func addHandler(handlers: var seq[RpcHandlerProc],
|
2024-01-29 13:20:04 +00:00
|
|
|
server: RpcHttpHandler) =
|
|
|
|
|
|
|
|
proc handlerProc(request: HttpRequestRef):
|
|
|
|
Future[RpcHandlerResult] {.async: (raises: []).} =
|
|
|
|
try:
|
|
|
|
let res = await server.serveHTTP(request)
|
|
|
|
if res.isNil:
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.Skip)
|
|
|
|
else:
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.Response, response: res)
|
|
|
|
except CancelledError:
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.Error)
|
|
|
|
|
|
|
|
handlers.add handlerProc
|
|
|
|
|
2024-09-18 08:53:26 +00:00
|
|
|
func addHandler(handlers: var seq[RpcHandlerProc],
|
2024-01-29 13:20:04 +00:00
|
|
|
server: RpcWebSocketHandler) =
|
|
|
|
|
|
|
|
proc handlerProc(request: HttpRequestRef):
|
|
|
|
Future[RpcHandlerResult] {.async: (raises: []).} =
|
|
|
|
|
|
|
|
if not request.headers.contains("Sec-WebSocket-Version"):
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.Skip)
|
|
|
|
|
|
|
|
let stream = websock.AsyncStream(
|
|
|
|
reader: request.connection.mainReader,
|
|
|
|
writer: request.connection.mainWriter,
|
|
|
|
)
|
|
|
|
|
|
|
|
let req = websock.HttpRequest(
|
|
|
|
meth: request.meth,
|
|
|
|
uri: request.uri,
|
|
|
|
version: request.version,
|
|
|
|
headers: request.headers,
|
|
|
|
stream: stream,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await server.serveHTTP(req)
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.KeepConnection)
|
|
|
|
except CancelledError:
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.Error)
|
|
|
|
|
|
|
|
handlers.add handlerProc
|
|
|
|
|
2024-09-18 08:53:26 +00:00
|
|
|
func addHandler(handlers: var seq[RpcHandlerProc],
|
2024-01-29 13:20:04 +00:00
|
|
|
server: GraphqlHttpHandlerRef) =
|
|
|
|
|
|
|
|
proc handlerProc(request: HttpRequestRef):
|
|
|
|
Future[RpcHandlerResult] {.async: (raises: []).} =
|
|
|
|
try:
|
|
|
|
let res = await server.serveHTTP(request)
|
|
|
|
if res.isNil:
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.Skip)
|
|
|
|
else:
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.Response, response: res)
|
|
|
|
except CatchableError:
|
|
|
|
return RpcHandlerResult(status: RpcHandlerStatus.Error)
|
|
|
|
|
|
|
|
handlers.add handlerProc
|
|
|
|
|
|
|
|
proc addHttpServices(handlers: var seq[RpcHandlerProc],
|
|
|
|
nimbus: NimbusNode, conf: NimbusConf,
|
2024-09-27 00:53:27 +00:00
|
|
|
com: CommonRef, serverApi: ServerAPIRef,
|
2024-08-08 10:03:30 +00:00
|
|
|
address: TransportAddress) =
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
# The order is important: graphql, ws, rpc
|
|
|
|
# graphql depends on /graphl path
|
|
|
|
# ws depends on Sec-WebSocket-Version header
|
|
|
|
# json-rpc have no reliable identification
|
|
|
|
|
|
|
|
if conf.graphqlEnabled:
|
2024-12-13 07:34:32 +00:00
|
|
|
let ctx = setupGraphqlContext(nimbus.chainRef, nimbus.ethNode, nimbus.txPool)
|
2024-01-29 13:20:04 +00:00
|
|
|
let server = GraphqlHttpHandlerRef.new(ctx)
|
|
|
|
handlers.addHandler(server)
|
2024-08-08 10:03:30 +00:00
|
|
|
info "GraphQL API enabled", url = "http://" & $address
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
if conf.wsEnabled:
|
|
|
|
let server = newRpcWebsocketHandler()
|
2024-10-28 22:17:07 +00:00
|
|
|
let rpcFlags = conf.getWsFlags() + {RpcFlag.Eth}
|
2024-09-27 00:53:27 +00:00
|
|
|
installRPC(server, nimbus, conf, com, serverApi, rpcFlags)
|
2024-01-29 13:20:04 +00:00
|
|
|
handlers.addHandler(server)
|
2024-08-08 10:03:30 +00:00
|
|
|
info "JSON-RPC WebSocket API enabled", url = "ws://" & $address
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
if conf.rpcEnabled:
|
|
|
|
let server = newRpcHttpHandler()
|
2024-10-28 22:17:07 +00:00
|
|
|
let rpcFlags = conf.getRpcFlags() + {RpcFlag.Eth}
|
2024-09-27 00:53:27 +00:00
|
|
|
installRPC(server, nimbus, conf, com, serverApi, rpcFlags)
|
2024-01-29 13:20:04 +00:00
|
|
|
handlers.addHandler(server)
|
2024-08-08 10:03:30 +00:00
|
|
|
info "JSON-RPC API enabled", url = "http://" & $address
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
proc addEngineApiServices(handlers: var seq[RpcHandlerProc],
|
|
|
|
nimbus: NimbusNode, conf: NimbusConf,
|
2024-09-27 00:53:27 +00:00
|
|
|
com: CommonRef, serverApi: ServerAPIRef,
|
2024-08-08 10:03:30 +00:00
|
|
|
address: TransportAddress) =
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
# The order is important: ws, rpc
|
|
|
|
|
|
|
|
if conf.engineApiWsEnabled:
|
|
|
|
let server = newRpcWebsocketHandler()
|
|
|
|
setupEngineAPI(nimbus.beaconEngine, server)
|
2024-09-27 00:53:27 +00:00
|
|
|
installRPC(server, nimbus, conf, com, serverApi, {RpcFlag.Eth})
|
2024-01-29 13:20:04 +00:00
|
|
|
handlers.addHandler(server)
|
2024-08-08 10:03:30 +00:00
|
|
|
info "Engine WebSocket API enabled", url = "ws://" & $address
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
if conf.engineApiEnabled:
|
|
|
|
let server = newRpcHttpHandler()
|
|
|
|
setupEngineAPI(nimbus.beaconEngine, server)
|
2024-09-27 00:53:27 +00:00
|
|
|
installRPC(server, nimbus, conf, com, serverApi, {RpcFlag.Eth})
|
2024-01-29 13:20:04 +00:00
|
|
|
handlers.addHandler(server)
|
2024-08-08 10:03:30 +00:00
|
|
|
info "Engine API enabled", url = "http://" & $address
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
proc addServices(handlers: var seq[RpcHandlerProc],
|
|
|
|
nimbus: NimbusNode, conf: NimbusConf,
|
2024-10-28 22:17:07 +00:00
|
|
|
com: CommonRef, serverApi: ServerAPIRef,
|
2024-08-08 10:03:30 +00:00
|
|
|
address: TransportAddress) =
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
# The order is important: graphql, ws, rpc
|
|
|
|
|
|
|
|
if conf.graphqlEnabled:
|
2024-12-13 07:34:32 +00:00
|
|
|
let ctx = setupGraphqlContext(nimbus.chainRef, nimbus.ethNode, nimbus.txPool)
|
2024-01-29 13:20:04 +00:00
|
|
|
let server = GraphqlHttpHandlerRef.new(ctx)
|
|
|
|
handlers.addHandler(server)
|
2024-08-08 10:03:30 +00:00
|
|
|
info "GraphQL API enabled", url = "http://" & $address
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
if conf.wsEnabled or conf.engineApiWsEnabled:
|
|
|
|
let server = newRpcWebsocketHandler()
|
|
|
|
if conf.engineApiWsEnabled:
|
|
|
|
setupEngineAPI(nimbus.beaconEngine, server)
|
2024-08-08 10:03:30 +00:00
|
|
|
|
2024-01-29 13:20:04 +00:00
|
|
|
if not conf.wsEnabled:
|
2024-09-27 00:53:27 +00:00
|
|
|
installRPC(server, nimbus, conf, com, serverApi, {RpcFlag.Eth})
|
2024-01-29 13:20:04 +00:00
|
|
|
|
2024-08-08 10:03:30 +00:00
|
|
|
info "Engine WebSocket API enabled", url = "ws://" & $address
|
|
|
|
|
2024-01-29 13:20:04 +00:00
|
|
|
if conf.wsEnabled:
|
2024-10-28 22:17:07 +00:00
|
|
|
let rpcFlags = conf.getWsFlags() + {RpcFlag.Eth}
|
2024-09-27 00:53:27 +00:00
|
|
|
installRPC(server, nimbus, conf, com, serverApi, rpcFlags)
|
2024-08-08 10:03:30 +00:00
|
|
|
info "JSON-RPC WebSocket API enabled", url = "ws://" & $address
|
|
|
|
|
2024-01-29 13:20:04 +00:00
|
|
|
handlers.addHandler(server)
|
|
|
|
|
|
|
|
if conf.rpcEnabled or conf.engineApiEnabled:
|
|
|
|
let server = newRpcHttpHandler()
|
|
|
|
if conf.engineApiEnabled:
|
|
|
|
setupEngineAPI(nimbus.beaconEngine, server)
|
|
|
|
if not conf.rpcEnabled:
|
2024-09-27 00:53:27 +00:00
|
|
|
installRPC(server, nimbus, conf, com, serverApi, {RpcFlag.Eth})
|
2024-01-29 13:20:04 +00:00
|
|
|
|
2024-08-08 10:03:30 +00:00
|
|
|
info "Engine API enabled", url = "http://" & $address
|
|
|
|
|
2024-01-29 13:20:04 +00:00
|
|
|
if conf.rpcEnabled:
|
2024-10-28 22:17:07 +00:00
|
|
|
let rpcFlags = conf.getRpcFlags() + {RpcFlag.Eth}
|
2024-09-27 00:53:27 +00:00
|
|
|
installRPC(server, nimbus, conf, com, serverApi, rpcFlags)
|
2024-08-08 10:03:30 +00:00
|
|
|
|
|
|
|
info "JSON-RPC API enabled", url = "http://" & $address
|
|
|
|
|
2024-01-29 13:20:04 +00:00
|
|
|
handlers.addHandler(server)
|
|
|
|
|
|
|
|
proc setupRpc*(nimbus: NimbusNode, conf: NimbusConf,
|
2024-10-28 22:17:07 +00:00
|
|
|
com: CommonRef) =
|
2024-08-08 10:03:30 +00:00
|
|
|
if not conf.engineApiEnabled:
|
|
|
|
warn "Engine API disabled, the node will not respond to consensus client updates (enable with `--engine-api`)"
|
|
|
|
|
2024-01-29 13:20:04 +00:00
|
|
|
if not conf.serverEnabled:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Provide JWT authentication handler for rpcHttpServer
|
|
|
|
let jwtKey = block:
|
|
|
|
# Create or load shared secret
|
|
|
|
let rc = nimbus.ctx.rng.jwtSharedSecret(conf)
|
|
|
|
if rc.isErr:
|
|
|
|
fatal "Failed create or load shared secret",
|
|
|
|
msg = $(rc.unsafeError) # avoid side effects
|
|
|
|
quit(QuitFailure)
|
|
|
|
rc.value
|
|
|
|
|
|
|
|
let
|
|
|
|
allowedOrigins = conf.getAllowedOrigins()
|
|
|
|
jwtAuthHook = httpJwtAuth(jwtKey)
|
|
|
|
corsHook = httpCors(allowedOrigins)
|
2024-10-02 03:56:39 +00:00
|
|
|
serverApi = newServerAPI(nimbus.chainRef, nimbus.txPool)
|
2024-01-29 13:20:04 +00:00
|
|
|
|
|
|
|
if conf.combinedServer:
|
2024-08-08 10:03:30 +00:00
|
|
|
let hooks: seq[RpcAuthHook] = @[jwtAuthHook, corsHook]
|
2024-01-29 13:20:04 +00:00
|
|
|
var handlers: seq[RpcHandlerProc]
|
|
|
|
let address = initTAddress(conf.httpAddress, conf.httpPort)
|
2024-10-28 22:17:07 +00:00
|
|
|
handlers.addServices(nimbus, conf, com, serverApi, address)
|
2024-01-29 13:20:04 +00:00
|
|
|
let res = newHttpServerWithParams(address, hooks, handlers)
|
|
|
|
if res.isErr:
|
|
|
|
fatal "Cannot create RPC server", msg=res.error
|
|
|
|
quit(QuitFailure)
|
|
|
|
nimbus.httpServer = res.get
|
|
|
|
nimbus.httpServer.start()
|
|
|
|
return
|
|
|
|
|
|
|
|
if conf.httpServerEnabled:
|
|
|
|
let hooks = @[corsHook]
|
|
|
|
var handlers: seq[RpcHandlerProc]
|
|
|
|
let address = initTAddress(conf.httpAddress, conf.httpPort)
|
2024-10-28 22:17:07 +00:00
|
|
|
handlers.addHttpServices(nimbus, conf, com, serverApi, address)
|
2024-01-29 13:20:04 +00:00
|
|
|
let res = newHttpServerWithParams(address, hooks, handlers)
|
|
|
|
if res.isErr:
|
|
|
|
fatal "Cannot create RPC server", msg=res.error
|
|
|
|
quit(QuitFailure)
|
|
|
|
nimbus.httpServer = res.get
|
|
|
|
nimbus.httpServer.start()
|
|
|
|
|
|
|
|
if conf.engineApiServerEnabled:
|
|
|
|
let hooks = @[jwtAuthHook, corsHook]
|
|
|
|
var handlers: seq[RpcHandlerProc]
|
|
|
|
let address = initTAddress(conf.engineApiAddress, conf.engineApiPort)
|
2024-09-27 00:53:27 +00:00
|
|
|
handlers.addEngineApiServices(nimbus, conf, com, serverApi, address)
|
2024-01-29 13:20:04 +00:00
|
|
|
let res = newHttpServerWithParams(address, hooks, handlers)
|
|
|
|
if res.isErr:
|
|
|
|
fatal "Cannot create RPC server", msg=res.error
|
|
|
|
quit(QuitFailure)
|
|
|
|
nimbus.engineApiServer = res.get
|
|
|
|
nimbus.engineApiServer.start()
|