diff --git a/fluffy/conf.nim b/fluffy/conf.nim index 4521b7662..9e3bf5294 100644 --- a/fluffy/conf.nim +++ b/fluffy/conf.nim @@ -174,11 +174,12 @@ type name: "metrics-port" .}: Port - rpcEnabled* {.desc: "Enable the JSON-RPC server", defaultValue: false, name: "rpc".}: - bool + rpcEnabled* {. + desc: "Enable the HTTP JSON-RPC server", defaultValue: false, name: "rpc" + .}: bool rpcPort* {. - desc: "HTTP port for the JSON-RPC server", defaultValue: 8545, name: "rpc-port" + desc: "Port for the HTTP JSON-RPC server", defaultValue: 8545, name: "rpc-port" .}: Port rpcAddress* {. @@ -188,6 +189,22 @@ type name: "rpc-address" .}: IpAddress + wsEnabled* {. + desc: "Enable the WebSocket JSON-RPC server", defaultValue: false, name: "ws" + .}: bool + + wsPort* {. + desc: "Port for the WebSocket JSON-RPC server", + defaultValue: 8546, + name: "ws-port" + .}: Port + + wsCompression* {. + desc: "Enable compression for the WebSocket JSON-RPC server", + defaultValue: false, + name: "ws-compression" + .}: bool + tableIpLimit* {. hidden, desc: diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index bb8aa208a..cdb4933d4 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -208,36 +208,49 @@ proc run(config: PortalConf) {.raises: [CatchableError].} = node.start() ## Start the JSON-RPC APIs - if config.rpcEnabled: - let ta = initTAddress(config.rpcAddress, config.rpcPort) - - let rpcHttpServer = RpcHttpServer.new() - # Note: Set maxRequestBodySize to 4MB instead of 1MB as there are blocks - # that reach that limit (in hex, for gossip method). - rpcHttpServer.addHttpServer(ta, maxRequestBodySize = 4 * 1_048_576) - rpcHttpServer.installDiscoveryApiHandlers(d) - rpcHttpServer.installWeb3ApiHandlers() + proc setupRpcServer( + rpcServer: RpcHttpServer | RpcWebSocketServer + ) {.raises: [CatchableError].} = + rpcServer.installDiscoveryApiHandlers(d) + rpcServer.installWeb3ApiHandlers() if node.stateNetwork.isSome(): - rpcHttpServer.installPortalApiHandlers( + rpcServer.installPortalApiHandlers( node.stateNetwork.value.portalProtocol, "state" ) if node.historyNetwork.isSome(): - rpcHttpServer.installEthApiHandlers( + rpcServer.installEthApiHandlers( node.historyNetwork.value, node.beaconLightClient, node.stateNetwork ) - rpcHttpServer.installPortalApiHandlers( + rpcServer.installPortalApiHandlers( node.historyNetwork.value.portalProtocol, "history" ) - rpcHttpServer.installPortalDebugApiHandlers( + rpcServer.installPortalDebugApiHandlers( node.historyNetwork.value.portalProtocol, "history" ) if node.beaconNetwork.isSome(): - rpcHttpServer.installPortalApiHandlers( + rpcServer.installPortalApiHandlers( node.beaconNetwork.value.portalProtocol, "beacon" ) - rpcHttpServer.start() + rpcServer.start() + + if config.rpcEnabled: + let + ta = initTAddress(config.rpcAddress, config.rpcPort) + rpcHttpServer = RpcHttpServer.new() + # Note: Set maxRequestBodySize to 4MB instead of 1MB as there are blocks + # that reach that limit (in hex, for gossip method). + rpcHttpServer.addHttpServer(ta, maxRequestBodySize = 4 * 1_048_576) + + setupRpcServer(rpcHttpServer) + + if config.wsEnabled: + let + ta = initTAddress(config.rpcAddress, config.wsPort) + rpcWsServer = newRpcWebSocketServer(ta, compression = config.wsCompression) + + setupRpcServer(rpcWsServer) runForever()