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 enable-ws flag in server command #152

Merged
merged 3 commits into from
Sep 1, 2022
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
2 changes: 2 additions & 0 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Config struct {
GraphQLAddr string `json:"graphql_addr"`
JSONRPCBatchRequestLimit uint64 `json:"json_rpc_batch_request_limit" yaml:"json_rpc_batch_request_limit"`
JSONRPCBlockRangeLimit uint64 `json:"json_rpc_block_range_limit" yaml:"json_rpc_block_range_limit"`
EnableWS bool `json:"enable_ws"`
}

// Telemetry holds the config details for metric services.
Expand Down Expand Up @@ -101,6 +102,7 @@ func DefaultConfig() *Config {
EnableGraphQL: false,
JSONRPCBatchRequestLimit: jsonrpc.DefaultJSONRPCBatchRequestLimit,
JSONRPCBlockRangeLimit: jsonrpc.DefaultJSONRPCBlockRangeLimit,
EnableWS: false,
abrahamcruise321 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
2 changes: 2 additions & 0 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
enableGraphQLFlag = "enable-graphql"
jsonRPCBatchRequestLimitFlag = "json-rpc-batch-request-limit"
jsonRPCBlockRangeLimitFlag = "json-rpc-block-range-limit"
enableWSFlag = "enable-ws"
)

const (
Expand Down Expand Up @@ -179,6 +180,7 @@ func (p *serverParams) generateConfig() *server.Config {
AccessControlAllowOrigin: p.corsAllowedOrigins,
BatchLengthLimit: p.rawConfig.JSONRPCBatchRequestLimit,
BlockRangeLimit: p.rawConfig.JSONRPCBlockRangeLimit,
EnableWS: p.rawConfig.EnableWS,
},
EnableGraphQL: p.rawConfig.EnableGraphQL,
GraphQL: &server.GraphQL{
Expand Down
7 changes: 7 additions & 0 deletions command/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ func setFlags(cmd *cobra.Command) {
"that consider fromBlock/toBlock values (e.g. eth_getLogs)",
)

cmd.Flags().BoolVar(
&params.rawConfig.EnableWS,
enableWSFlag,
false,
"the flag indicating that node enable websocket service",
)

setDevFlags(cmd)
}

Expand Down
5 changes: 5 additions & 0 deletions e2e/framework/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type TestServerConfig struct {
Signer *crypto.EIP155Signer // Signer used for transactions
BridgeOwner types.Address // bridge contract owner
BridgeSigners []types.Address // bridge contract signers
IsWSEnable bool // enable websocket or not
}

// DataDir returns path of data directory server uses
Expand Down Expand Up @@ -165,3 +166,7 @@ func (t *TestServerConfig) SetBridgeOwner(owner types.Address) {
func (t *TestServerConfig) SetBridgeSigners(signers []types.Address) {
t.BridgeSigners = signers
}

func (t *TestServerConfig) EnableWebSocket() {
t.IsWSEnable = true
}
4 changes: 4 additions & 0 deletions e2e/framework/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ func (t *TestServer) Start(ctx context.Context) error {
"--jsonrpc", t.JSONRPCAddr(),
}

if t.Config.IsWSEnable {
args = append(args, "--enable-ws")
}

switch t.Config.Consensus {
case ConsensusIBFT:
args = append(args, "--data-dir", filepath.Join(t.Config.RootDir, t.Config.IBFTDir))
Expand Down
1 change: 1 addition & 0 deletions e2e/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestWS_Response(t *testing.T) {
srvs := framework.NewTestServers(t, 1, func(config *framework.TestServerConfig) {
config.SetConsensus(framework.ConsensusDev)
config.SetSeal(true)
config.EnableWebSocket()

for _, account := range preminedAccounts {
config.Premine(account.address, account.balance)
Expand Down
6 changes: 5 additions & 1 deletion jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Config struct {
AccessControlAllowOrigin []string
BatchLengthLimit uint64
BlockRangeLimit uint64
EnableWS bool
}

// NewJSONRPC returns the JSONRPC http server
Expand Down Expand Up @@ -95,7 +96,10 @@ func (j *JSONRPC) setupHTTP() error {
jsonRPCHandler := http.HandlerFunc(j.handle)
mux.Handle("/", middlewareFactory(j.config)(jsonRPCHandler))

mux.HandleFunc("/ws", j.handleWs)
// would only enable websocket when set
if j.config.EnableWS {
mux.HandleFunc("/ws", j.handleWs)
}

srv := http.Server{
Handler: mux,
Expand Down
1 change: 1 addition & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type JSONRPC struct {
AccessControlAllowOrigin []string
BatchLengthLimit uint64
BlockRangeLimit uint64
EnableWS bool
}

type GraphQL struct {
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ func (s *Server) setupJSONRPC() error {
AccessControlAllowOrigin: s.config.JSONRPC.AccessControlAllowOrigin,
BatchLengthLimit: s.config.JSONRPC.BatchLengthLimit,
BlockRangeLimit: s.config.JSONRPC.BlockRangeLimit,
EnableWS: s.config.JSONRPC.EnableWS,
}

srv, err := jsonrpc.NewJSONRPC(s.logger, conf)
Expand Down