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

console: use all APIs if rpc_module not found #25148

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 18 additions & 8 deletions cmd/geth/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var (
consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag, utils.HeaderFlag}

consoleCommand = &cli.Command{
Action: localConsole,
Expand Down Expand Up @@ -114,17 +114,13 @@ func localConsole(ctx *cli.Context) error {
// remoteConsole will connect to a remote geth instance, attaching a JavaScript
// console to it.
func remoteConsole(ctx *cli.Context) error {
if ctx.Args().Len() > 1 {
utils.Fatalf("invalid command-line: too many arguments")
}

endpoint := ctx.Args().First()
if endpoint == "" {
cfg := defaultNodeConfig()
utils.SetDataDir(ctx, &cfg)
endpoint = cfg.IPCEndpoint()
}
client, err := dialRPC(endpoint)
client, err := dialRPC(ctx, endpoint)
if err != nil {
utils.Fatalf("Unable to attach to remote geth: %v", err)
}
Expand Down Expand Up @@ -167,13 +163,27 @@ geth --exec "%s" console`, b.String())
// dialRPC returns a RPC client which connects to the given endpoint.
// The check for empty endpoint implements the defaulting logic
// for "geth attach" with no argument.
func dialRPC(endpoint string) (*rpc.Client, error) {
func dialRPC(ctx *cli.Context, endpoint string) (*rpc.Client, error) {
if endpoint == "" {
endpoint = node.DefaultIPCEndpoint(clientIdentifier)
} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
// Backwards compatibility with geth < 1.5 which required
// these prefixes.
endpoint = endpoint[4:]
}
return rpc.Dial(endpoint)
c, err := rpc.Dial(endpoint)
if err != nil {
return nil, err
}
if ctx.IsSet(utils.HeaderFlag.Name) {
for _, keyValues := range ctx.StringSlice(utils.HeaderFlag.Name) {
keyValue := strings.Split(keyValues, ":")
if len(keyValue) != 2 {
return nil, fmt.Errorf("invalid header value: %s", keyValues)
}
k, v := keyValue[0], keyValue[1]
c.SetHeader(k, v)
}
}
return c, nil
}
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,11 @@ var (
Usage: "Allow for unprotected (non EIP155 signed) transactions to be submitted via RPC",
Category: flags.APICategory,
}
HeaderFlag = &cli.StringSliceFlag{
Name: "header",
Usage: "Set an HTTP header. <header:value>",
Category: flags.APICategory,
}

// Network Settings
MaxPeersFlag = &cli.IntFlag{
Expand Down
27 changes: 26 additions & 1 deletion console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ var (
exit = regexp.MustCompile(`^\s*exit\s*;*\s*$`)
)

var apisAll = map[string]string{
"admin": "1.0",
"clique": "1.0",
"debug": "1.0",
"eth": "1.0",
"les": "1.0",
"miner": "1.0",
"personal": "1.0",
"txpool": "1.0",
"web3": "1.0",
}

// HistoryFile is the file within the data directory to store input scrollback.
const HistoryFile = "history"

Expand Down Expand Up @@ -203,7 +215,10 @@ func (c *Console) initExtensions() error {
// Compute aliases from server-provided modules.
apis, err := c.client.SupportedModules()
if err != nil {
return fmt.Errorf("api modules: %v", err)
if !isMethodNotFoundErr(err) {
return fmt.Errorf("api modules: %v", err)
}
apis = apisAll
}
aliases := map[string]struct{}{"eth": {}, "personal": {}}
for api := range apis {
Expand Down Expand Up @@ -337,6 +352,8 @@ func (c *Console) Welcome() {
}
sort.Strings(modules)
message += " modules: " + strings.Join(modules, " ") + "\n"
} else if isMethodNotFoundErr(err) {
message += ` use all modules because does not supported "rpc_module" method`
}
message += "\nTo exit, press ctrl-d or type exit"
fmt.Fprintln(c.printer, message)
Expand Down Expand Up @@ -558,3 +575,11 @@ func (c *Console) writeHistory() error {
}
return os.Chmod(c.histPath, 0600) // Force 0600, even if it was different previously
}

func isMethodNotFoundErr(err error) bool {
rpcErr, ok := err.(rpc.Error)
if !ok {
return false
}
return rpcErr.ErrorCode() == -32601
}