rpc: return error instead of log.Crit

Resolves https://github.com/ethereum/go-ethereum/pull/20468/files#r366236052

Signed-off-by: meows <b5c6@protonmail.com>
This commit is contained in:
meows 2020-01-14 09:18:08 -06:00
parent 9b8f5cc143
commit 3ac3773dae
No known key found for this signature in database
GPG key ID: 5786AEA5C8D5A520

View file

@ -17,24 +17,28 @@
package rpc
import (
"fmt"
"net"
"github.com/ethereum/go-ethereum/log"
)
// mustAvailableModule enforces that requested api modules (eg. via --rpcapi) are actually
// isModuleAvailable enforces that requested api modules (eg. via --rpcapi) are actually
// available API services. If an invalid module is given (ie API "foo" wanted which does not exist),
// then log.Crit is used to cause program to exit, logging the invalid module and a list of available
// API service names.
func mustAvailableModule(module string, apis []API) {
func isModuleAvailable(module string, apis []API) (err error) {
for _, api := range apis {
if module == api.Namespace {
return
return nil
}
}
log.Crit("invalid api module", "module", module, "available", func() (available []string) {
// Module did not find a matching api namespace: this is an invalid module.
// Collect list of available modules for user debugging.
available := []string{}
outer:
for _, api := range apis {
// Only include unique api names
for _, av := range available {
if av == api.Namespace {
@ -43,8 +47,7 @@ func mustAvailableModule(module string, apis []API) {
}
available = append(available, api.Namespace)
}
return
}())
return fmt.Errorf("invalid api module: module=%s available=%v", module, available)
}
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
@ -52,7 +55,11 @@ func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []str
// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
for _, module := range modules {
mustAvailableModule(module, apis)
// Ensure the requested module is actually available.
if err := isModuleAvailable(module, apis); err != nil {
return nil, nil, err
}
whitelist[module] = true
}
// Register all the APIs exposed by the services
@ -83,7 +90,11 @@ func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []
// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
for _, module := range modules {
mustAvailableModule(module, apis)
// Ensure the requested module is actually available.
if err := isModuleAvailable(module, apis); err != nil {
return nil, nil, err
}
whitelist[module] = true
}
// Register all the APIs exposed by the services