Skip to content

Commit

Permalink
Merge pull request #821 from lochjin/dev2.0
Browse files Browse the repository at this point in the history
feat:optimize consolecmd
  • Loading branch information
dindinw authored Oct 30, 2024
2 parents 59adbad + 5e8c457 commit b783e84
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions meerevm/cmd/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
package cmd

import (
"fmt"
"github.com/Qitmeer/qng/config"
"github.com/Qitmeer/qng/meerevm/amana"
"github.com/Qitmeer/qng/meerevm/meer"
"github.com/ethereum/go-ethereum/node"
"strings"

"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/node"
"github.com/urfave/cli/v2"
)

Expand All @@ -24,7 +20,7 @@ var (
Name: "attach",
Usage: "Start an interactive JavaScript environment (connect to node)",
ArgsUsage: "[endpoint]",
Flags: append(consoleFlags, utils.DataDirFlag),
Flags: append(consoleFlags, utils.DataDirFlag, utils.HttpHeaderFlag),
Category: "CONSOLE COMMANDS",
Description: `
The QNG console is an interactive shell for the JavaScript runtime environment
Expand All @@ -39,19 +35,25 @@ This command allows to open a console on a running geth node.`,
func remoteConsole(ctx *cli.Context) error {
// Attach to a remotely running geth instance and start the JavaScript console
endpoint := ctx.Args().First()
nconfig := &node.Config{DataDir: config.Cfg.DataDir}
if ctx.IsSet(utils.DataDirFlag.Name) {
nconfig.DataDir = ctx.String(utils.DataDirFlag.Name)
}
if endpoint == "" {
path := config.Cfg.DataDir
if ctx.IsSet(utils.DataDirFlag.Name) {
path = ctx.String(utils.DataDirFlag.Name)
if config.Cfg.Amana {
nconfig.IPCPath = amana.ClientIdentifier + ".ipc"
} else {
nconfig.IPCPath = meer.ClientIdentifier + ".ipc"
}
endpoint = fmt.Sprintf("%s/qng.ipc", path)
endpoint = nconfig.IPCEndpoint()

}
client, err := dialRPC(endpoint)
client, err := utils.DialRPCWithHeaders(endpoint, ctx.StringSlice(utils.HttpHeaderFlag.Name))
if err != nil {
utils.Fatalf("Unable to attach to remote qng: %v", err)
}
config := console.Config{
DataDir: utils.MakeDataDir(ctx),
DataDir: nconfig.DataDir,
DocRoot: ctx.String(utils.JSpathFlag.Name),
Client: client,
Preload: utils.MakeConsolePreloads(ctx),
Expand All @@ -74,22 +76,3 @@ func remoteConsole(ctx *cli.Context) error {

return nil
}

// 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) {
if endpoint == "" {
if config.Cfg.Amana {
endpoint = node.DefaultIPCEndpoint(amana.ClientIdentifier)
} else {
endpoint = node.DefaultIPCEndpoint(meer.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)
}

0 comments on commit b783e84

Please sign in to comment.