Skip to content

Commit

Permalink
Add WebSocket support to Fluffy. (#2636)
Browse files Browse the repository at this point in the history
* Add WebSocket support to Fluffy.

* Support websocket compression.

* Create setupRpcServer closure to remove code duplication.
  • Loading branch information
bhartnett authored Sep 18, 2024
1 parent 3fb2e08 commit ea74e03
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
23 changes: 20 additions & 3 deletions fluffy/conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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* {.
Expand All @@ -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:
Expand Down
43 changes: 28 additions & 15 deletions fluffy/fluffy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit ea74e03

Please sign in to comment.