Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebSocket support to Fluffy. #2636

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading