mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-16 09:50:45 +00:00
rpc: check module availability at startup (#20597)
This commit is contained in:
parent
d3c023ed37
commit
3f1b7d3a7b
2 changed files with 33 additions and 4 deletions
|
|
@ -120,8 +120,8 @@ func defaultNodeConfig() node.Config {
|
||||||
cfg := node.DefaultConfig
|
cfg := node.DefaultConfig
|
||||||
cfg.Name = clientIdentifier
|
cfg.Name = clientIdentifier
|
||||||
cfg.Version = params.VersionWithCommit(gitCommit)
|
cfg.Version = params.VersionWithCommit(gitCommit)
|
||||||
cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
|
cfg.HTTPModules = append(cfg.HTTPModules, "eth")
|
||||||
cfg.WSModules = append(cfg.WSModules, "eth", "shh")
|
cfg.WSModules = append(cfg.WSModules, "eth")
|
||||||
cfg.IPCPath = "XDC.ipc"
|
cfg.IPCPath = "XDC.ipc"
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,40 @@
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/log"
|
"github.com/XinFinOrg/XDPoSChain/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
|
// checkModuleAvailability check that all names given in modules are actually
|
||||||
|
// available API services.
|
||||||
|
func checkModuleAvailability(modules []string, apis []API) error {
|
||||||
|
available := make(map[string]struct{})
|
||||||
|
var availableNames string
|
||||||
|
for i, api := range apis {
|
||||||
|
if _, ok := available[api.Namespace]; !ok {
|
||||||
|
available[api.Namespace] = struct{}{}
|
||||||
|
if i > 0 {
|
||||||
|
availableNames += ", "
|
||||||
|
}
|
||||||
|
availableNames += api.Namespace
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, name := range modules {
|
||||||
|
if _, ok := available[name]; !ok {
|
||||||
|
return fmt.Errorf("invalid API %q in whitelist (available: %s)", name, availableNames)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules.
|
||||||
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
|
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
|
||||||
|
if err := checkModuleAvailability(modules, apis); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
// Generate the whitelist based on the allowed modules
|
// Generate the whitelist based on the allowed modules
|
||||||
whitelist := make(map[string]bool)
|
whitelist := make(map[string]bool)
|
||||||
for _, module := range modules {
|
for _, module := range modules {
|
||||||
|
|
@ -52,8 +78,11 @@ func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []str
|
||||||
return listener, handler, err
|
return listener, handler, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartWSEndpoint starts a websocket endpoint
|
// StartWSEndpoint starts a websocket endpoint.
|
||||||
func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
|
func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
|
||||||
|
if err := checkModuleAvailability(modules, apis); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
// Generate the whitelist based on the allowed modules
|
// Generate the whitelist based on the allowed modules
|
||||||
whitelist := make(map[string]bool)
|
whitelist := make(map[string]bool)
|
||||||
for _, module := range modules {
|
for _, module := range modules {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue