Skip to content

Commit

Permalink
cmd/geth, cmd/utils, ethdb/remotedb: refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Sep 26, 2022
1 parent c5080fc commit 71163ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 68 deletions.
32 changes: 1 addition & 31 deletions cmd/geth/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
package main

import (
"context"
"fmt"
"net/http"
"strings"

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

Expand Down Expand Up @@ -120,25 +116,13 @@ func remoteConsole(ctx *cli.Context) error {
if ctx.Args().Len() > 1 {
utils.Fatalf("invalid command-line: too many arguments")
}
var opts []rpc.ClientOption
if ctx.IsSet(utils.HttpHeaderFlag.Name) {
var customHeaders = make(http.Header)
for _, h := range ctx.StringSlice(utils.HttpHeaderFlag.Name) {
kv := strings.Split(h, ":")
if len(kv) != 2 {
utils.Fatalf("invalid http header directive: %q", h)
}
customHeaders.Add(kv[0], kv[1])
}
opts = append(opts, rpc.WithHeaders(customHeaders))
}
endpoint := ctx.Args().First()
if endpoint == "" {
cfg := defaultNodeConfig()
utils.SetDataDir(ctx, &cfg)
endpoint = cfg.IPCEndpoint()
}
client, err := dialRPC(endpoint, opts)
client, err := utils.DialRPCWithHeaders(endpoint, ctx.StringSlice(utils.HttpHeaderFlag.Name))
if err != nil {
utils.Fatalf("Unable to attach to remote geth: %v", err)
}
Expand Down Expand Up @@ -177,17 +161,3 @@ func ephemeralConsole(ctx *cli.Context) error {
geth --exec "%s" console`, b.String())
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, opts []rpc.ClientOption) (*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.DialOptions(context.Background(), endpoint, opts...)
}
33 changes: 32 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package utils

import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math"
"math/big"
"net/http"
"os"
"path/filepath"
godebug "runtime/debug"
Expand Down Expand Up @@ -2134,7 +2137,11 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node, readonly bool) ethdb.
switch {
case ctx.IsSet(RemoteDBFlag.Name):
log.Info("Using remote db", "url", ctx.String(RemoteDBFlag.Name), "headers", len(ctx.StringSlice(HttpHeaderFlag.Name)))
chainDb, err = remotedb.New(ctx.String(RemoteDBFlag.Name), ctx.StringSlice(HttpHeaderFlag.Name))
client, err := DialRPCWithHeaders(ctx.String(RemoteDBFlag.Name), ctx.StringSlice(HttpHeaderFlag.Name))
if err != nil {
break
}
chainDb = remotedb.New(client)
case ctx.String(SyncModeFlag.Name) == "light":
chainDb, err = stack.OpenDatabase("lightchaindata", cache, handles, "", readonly)
default:
Expand All @@ -2156,6 +2163,30 @@ func IsNetworkPreset(ctx *cli.Context) bool {
return false
}

func DialRPCWithHeaders(endpoint string, headers []string) (*rpc.Client, error) {
if endpoint == "" {
return nil, errors.New("endpoint must be specified")
}
if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
// Backwards compatibility with geth < 1.5 which required
// these prefixes.
endpoint = endpoint[4:]
}
var opts []rpc.ClientOption
if len(headers) > 0 {
var customHeaders = make(http.Header)
for _, h := range headers {
kv := strings.Split(h, ":")
if len(kv) != 2 {
return nil, fmt.Errorf("invalid http header directive: %q", h)
}
customHeaders.Add(kv[0], kv[1])
}
opts = append(opts, rpc.WithHeaders(customHeaders))
}
return rpc.DialOptions(context.Background(), endpoint, opts...)
}

func MakeGenesis(ctx *cli.Context) *core.Genesis {
var genesis *core.Genesis
switch {
Expand Down
38 changes: 2 additions & 36 deletions ethdb/remotedb/remotedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@
package remotedb

import (
"context"
"errors"
"fmt"
"net/http"
"strings"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -153,36 +147,8 @@ func (db *Database) Close() error {
return nil
}

func dialRPC(endpoint string, headers []string) (*rpc.Client, error) {
if endpoint == "" {
return nil, errors.New("endpoint must be specified")
}
if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
// Backwards compatibility with geth < 1.5 which required
// these prefixes.
endpoint = endpoint[4:]
}
var opts []rpc.ClientOption
if len(headers) > 0 {
var customHeaders = make(http.Header)
for _, h := range headers {
kv := strings.Split(h, ":")
if len(kv) != 2 {
return nil, fmt.Errorf("invalid http header directive: %q", h)
}
customHeaders.Add(kv[0], kv[1])
}
opts = append(opts, rpc.WithHeaders(customHeaders))
}
return rpc.DialOptions(context.Background(), endpoint, opts...)
}

func New(endpoint string, headers []string) (ethdb.Database, error) {
client, err := dialRPC(endpoint, headers)
if err != nil {
return nil, err
}
func New(client *rpc.Client) ethdb.Database {
return &Database{
remote: client,
}, nil
}
}

0 comments on commit 71163ce

Please sign in to comment.