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

Add Configuration parameter 'SkipGethAdmin' to support skipping 'admin_peers' checks #38

Merged
merged 10 commits into from
Jun 18, 2021
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func runRunCmd(cmd *cobra.Command, args []string) error {
}

var err error
client, err = ethereum.NewClient(cfg.GethURL, cfg.Params)
client, err = ethereum.NewClient(cfg.GethURL, cfg.Params, cfg.SkipGethAdmin)
if err != nil {
return fmt.Errorf("%w: cannot initialize ethereum client", err)
}
Expand Down
16 changes: 16 additions & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ const (
// when GethEnv is not populated.
DefaultGethURL = "http://localhost:8545"

// SkipGethAdmin is an optional environment variable
// to skip geth `admin` calls which are typically not supported
// by hosted node services.
SkipGethAdmin = "SKIP_GETH_ADMIN"
akramhussein marked this conversation as resolved.
Show resolved Hide resolved

// MiddlewareVersion is the version of rosetta-ethereum.
MiddlewareVersion = "0.0.4"
)
Expand All @@ -85,6 +90,7 @@ type Configuration struct {
RemoteGeth bool
Port int
GethArguments string
SkipGethAdmin bool

// Block Reward Data
Params *params.ChainConfig
Expand Down Expand Up @@ -138,6 +144,16 @@ func LoadConfiguration() (*Configuration, error) {
config.GethURL = envGethURL
}

config.SkipGethAdmin = false
envSkipGethAdmin := os.Getenv(SkipGethAdmin)
if len(envSkipGethAdmin) > 0 {
val, err := strconv.ParseBool(envSkipGethAdmin)
if err != nil {
return nil, fmt.Errorf("%w: unable to parse SKIP_GETH_ADMIN %s", err, envSkipGethAdmin)
}
config.SkipGethAdmin = val
}

portValue := os.Getenv(PortEnv)
if len(portValue) == 0 {
return nil, errors.New("PORT must be populated")
Expand Down
31 changes: 20 additions & 11 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package configuration
import (
"errors"
"os"
"strconv"
"testing"

"github.com/coinbase/rosetta-ethereum/ethereum"
Expand All @@ -28,10 +29,11 @@ import (

func TestLoadConfiguration(t *testing.T) {
tests := map[string]struct {
Mode string
Network string
Port string
Geth string
Mode string
Network string
Port string
Geth string
SkipGethAdmin bool

cfg *Configuration
err error
Expand Down Expand Up @@ -63,13 +65,15 @@ func TestLoadConfiguration(t *testing.T) {
Port: 1000,
GethURL: DefaultGethURL,
GethArguments: ethereum.MainnetGethArguments,
SkipGethAdmin: false,
},
},
"all set (mainnet) + geth": {
Mode: string(Online),
Network: Mainnet,
Port: "1000",
Geth: "http://blah",
Mode: string(Online),
Network: Mainnet,
Port: "1000",
Geth: "http://blah",
SkipGethAdmin: true,
cfg: &Configuration{
Mode: Online,
Network: &types.NetworkIdentifier{
Expand All @@ -82,12 +86,14 @@ func TestLoadConfiguration(t *testing.T) {
GethURL: "http://blah",
RemoteGeth: true,
GethArguments: ethereum.MainnetGethArguments,
SkipGethAdmin: true,
},
},
"all set (testnet)": {
Mode: string(Online),
Network: Testnet,
Port: "1000",
Mode: string(Online),
Network: Testnet,
Port: "1000",
SkipGethAdmin: true,
cfg: &Configuration{
Mode: Online,
Network: &types.NetworkIdentifier{
Expand All @@ -99,6 +105,7 @@ func TestLoadConfiguration(t *testing.T) {
Port: 1000,
GethURL: DefaultGethURL,
GethArguments: ethereum.TestnetGethArguments,
SkipGethAdmin: true,
},
},
"invalid mode": {
Expand Down Expand Up @@ -127,6 +134,8 @@ func TestLoadConfiguration(t *testing.T) {
os.Setenv(NetworkEnv, test.Network)
os.Setenv(PortEnv, test.Port)
os.Setenv(GethEnv, test.Geth)
os.Setenv(GethEnv, test.Geth)
os.Setenv(SkipGethAdmin, strconv.FormatBool(test.SkipGethAdmin))
akramhussein marked this conversation as resolved.
Show resolved Hide resolved

cfg, err := LoadConfiguration()
if test.err != nil {
Expand Down
11 changes: 9 additions & 2 deletions ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ type Client struct {
g GraphQL

traceSemaphore *semaphore.Weighted

skipAdminCalls bool
}

// NewClient creates a Client that from the provided url and params.
func NewClient(url string, params *params.ChainConfig) (*Client, error) {
func NewClient(url string, params *params.ChainConfig, skipAdminCalls bool) (*Client, error) {
c, err := rpc.DialHTTPWithClient(url, &http.Client{
Timeout: gethHTTPTimeout,
})
Expand All @@ -81,7 +83,7 @@ func NewClient(url string, params *params.ChainConfig) (*Client, error) {
return nil, fmt.Errorf("%w: unable to create GraphQL client", err)
}

return &Client{params, tc, c, g, semaphore.NewWeighted(maxTraceConcurrency)}, nil
return &Client{params, tc, c, g, semaphore.NewWeighted(maxTraceConcurrency), skipAdminCalls}, nil
}

// Close shuts down the RPC client connection.
Expand Down Expand Up @@ -155,6 +157,11 @@ func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
// Peers retrieves all peers of the node.
func (ec *Client) peers(ctx context.Context) ([]*RosettaTypes.Peer, error) {
var info []*p2p.PeerInfo

if ec.skipAdminCalls {
return []*RosettaTypes.Peer{}, nil
}

if err := ec.c.CallContext(ctx, &info, "admin_peers"); err != nil {
return nil, err
}
Expand Down