Skip to content

Commit 2b23746

Browse files
committed
cmd/console: use all apis if rpc_module not found
- use all api modules if rpc_module not found - adds header flags in the console
1 parent d839515 commit 2b23746

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

cmd/geth/consolecmd.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
var (
31-
consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
31+
consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag, utils.HeaderFlag}
3232

3333
consoleCommand = &cli.Command{
3434
Action: localConsole,
@@ -114,17 +114,13 @@ func localConsole(ctx *cli.Context) error {
114114
// remoteConsole will connect to a remote geth instance, attaching a JavaScript
115115
// console to it.
116116
func remoteConsole(ctx *cli.Context) error {
117-
if ctx.Args().Len() > 1 {
118-
utils.Fatalf("invalid command-line: too many arguments")
119-
}
120-
121117
endpoint := ctx.Args().First()
122118
if endpoint == "" {
123119
cfg := defaultNodeConfig()
124120
utils.SetDataDir(ctx, &cfg)
125121
endpoint = cfg.IPCEndpoint()
126122
}
127-
client, err := dialRPC(endpoint)
123+
client, err := dialRPC(ctx, endpoint)
128124
if err != nil {
129125
utils.Fatalf("Unable to attach to remote geth: %v", err)
130126
}
@@ -167,13 +163,27 @@ geth --exec "%s" console`, b.String())
167163
// dialRPC returns a RPC client which connects to the given endpoint.
168164
// The check for empty endpoint implements the defaulting logic
169165
// for "geth attach" with no argument.
170-
func dialRPC(endpoint string) (*rpc.Client, error) {
166+
func dialRPC(ctx *cli.Context, endpoint string) (*rpc.Client, error) {
171167
if endpoint == "" {
172168
endpoint = node.DefaultIPCEndpoint(clientIdentifier)
173169
} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
174170
// Backwards compatibility with geth < 1.5 which required
175171
// these prefixes.
176172
endpoint = endpoint[4:]
177173
}
178-
return rpc.Dial(endpoint)
174+
c, err := rpc.Dial(endpoint)
175+
if err != nil {
176+
return nil, err
177+
}
178+
if ctx.IsSet(utils.HeaderFlag.Name) {
179+
for _, keyValues := range ctx.StringSlice(utils.HeaderFlag.Name) {
180+
keyValue := strings.Split(keyValues, ":")
181+
if len(keyValue) != 2 {
182+
return nil, fmt.Errorf("invalid header value: %s", keyValues)
183+
}
184+
k, v := keyValue[0], keyValue[1]
185+
c.SetHeader(k, v)
186+
}
187+
}
188+
return c, nil
179189
}

cmd/utils/flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,11 @@ var (
773773
Usage: "Allow for unprotected (non EIP155 signed) transactions to be submitted via RPC",
774774
Category: flags.APICategory,
775775
}
776+
HeaderFlag = &cli.StringSliceFlag{
777+
Name: "header",
778+
Usage: "Set an HTTP header. <header:value>",
779+
Category: flags.APICategory,
780+
}
776781

777782
// Network Settings
778783
MaxPeersFlag = &cli.IntFlag{

console/console.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ var (
4646
exit = regexp.MustCompile(`^\s*exit\s*;*\s*$`)
4747
)
4848

49+
var apisAll = map[string]string{
50+
"admin": "1.0",
51+
"clique": "1.0",
52+
"debug": "1.0",
53+
"eth": "1.0",
54+
"les": "1.0",
55+
"miner": "1.0",
56+
"personal": "1.0",
57+
"txpool": "1.0",
58+
"web3": "1.0",
59+
}
60+
4961
// HistoryFile is the file within the data directory to store input scrollback.
5062
const HistoryFile = "history"
5163

@@ -203,7 +215,10 @@ func (c *Console) initExtensions() error {
203215
// Compute aliases from server-provided modules.
204216
apis, err := c.client.SupportedModules()
205217
if err != nil {
206-
return fmt.Errorf("api modules: %v", err)
218+
if !isMethodNotFoundErr(err) {
219+
return fmt.Errorf("api modules: %v", err)
220+
}
221+
apis = apisAll
207222
}
208223
aliases := map[string]struct{}{"eth": {}, "personal": {}}
209224
for api := range apis {
@@ -337,6 +352,8 @@ func (c *Console) Welcome() {
337352
}
338353
sort.Strings(modules)
339354
message += " modules: " + strings.Join(modules, " ") + "\n"
355+
} else if isMethodNotFoundErr(err) {
356+
message += ` use all modules because does not supported "rpc_module" method`
340357
}
341358
message += "\nTo exit, press ctrl-d or type exit"
342359
fmt.Fprintln(c.printer, message)
@@ -558,3 +575,11 @@ func (c *Console) writeHistory() error {
558575
}
559576
return os.Chmod(c.histPath, 0600) // Force 0600, even if it was different previously
560577
}
578+
579+
func isMethodNotFoundErr(err error) bool {
580+
rpcErr, ok := err.(rpc.Error)
581+
if !ok {
582+
return false
583+
}
584+
return rpcErr.ErrorCode() == -32601
585+
}

0 commit comments

Comments
 (0)