From 28ccbeb7de18212954310588994058874fb180af Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Tue, 27 Oct 2020 14:04:01 -0400 Subject: Parse config to set enabled APIs --- plugin/evm/config.go | 19 ++++++++++--------- plugin/evm/service.go | 13 +++---------- plugin/evm/vm.go | 13 ++++++++++--- plugin/main.go | 5 ----- plugin/params.go | 16 +++++----------- scripts/build_coreth.sh | 22 ++++++++++++++++++++++ 6 files changed, 50 insertions(+), 38 deletions(-) create mode 100755 scripts/build_coreth.sh diff --git a/plugin/evm/config.go b/plugin/evm/config.go index 4669807..c38a798 100644 --- a/plugin/evm/config.go +++ b/plugin/evm/config.go @@ -3,19 +3,20 @@ package evm // CommandLineConfig ... type CommandLineConfig struct { // Coreth APIs - SnowmanAPIEnabled bool `json:"snowmanAPIEnabled"` - Web3APIEnabled bool `json:"web3APIEnabled"` - CorethAdminAPIEnabled bool `json:"corethAdminAPIEnabled"` + SnowmanAPIEnabled bool `json:"snowman-api-enabled"` + CorethAdminAPIEnabled bool `json:"coreth-admin-api-enabled"` // Coreth API Gas/Price Caps - RPCGasCap uint64 `json:"rpcGasCap"` - RPCTxFeeCap float64 `json:"rpcTxFeeCap"` + RPCGasCap uint64 `json:"rpc-gas-cap"` + RPCTxFeeCap float64 `json:"rpc-tx-fee-cap"` // Eth APIs - EthAPIEnabled bool `json:"ethAPIEnabled"` - PersonalAPIEnabled bool `json:"personalAPIEnabled"` - TxPoolAPIEnabled bool `json:"txPoolAPIEnabled"` - DebugAPIEnabled bool `json:"debugAPIEnabled"` + EthAPIEnabled bool `json:"eth-api-enabled"` + PersonalAPIEnabled bool `json:"personal-api-enabled"` + TxPoolAPIEnabled bool `json:"tx-pool-api-enabled"` + DebugAPIEnabled bool `json:"debug-api-enabled"` + + ParsingError error } // EthAPIs returns an array of strings representing the Eth APIs that should be enabled diff --git a/plugin/evm/service.go b/plugin/evm/service.go index 128b98e..148a3c3 100644 --- a/plugin/evm/service.go +++ b/plugin/evm/service.go @@ -19,7 +19,6 @@ import ( "github.com/ava-labs/avalanchego/utils/json" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - ethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" ) @@ -39,15 +38,6 @@ type SnowmanAPI struct{ vm *VM } // AvaxAPI offers Avalanche network related API methods type AvaxAPI struct{ vm *VM } -// Web3API offers helper API methods -type Web3API struct{} - -// ClientVersion returns the version of the vm running -func (s *Web3API) ClientVersion() string { return version } - -// Sha3 returns the bytes returned by hashing [input] with Keccak256 -func (s *Web3API) Sha3(input hexutil.Bytes) hexutil.Bytes { return ethcrypto.Keccak256(input) } - // GetAcceptedFrontReply defines the reply that will be sent from the // GetAcceptedFront API call type GetAcceptedFrontReply struct { @@ -71,6 +61,9 @@ func (api *SnowmanAPI) IssueBlock(ctx context.Context) error { return api.vm.tryBlockGen() } +// ClientVersion returns the version of the vm running +func (service *AvaxAPI) ClientVersion() string { return version } + // ExportKeyArgs are arguments for ExportKey type ExportKeyArgs struct { api.UserPass diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index c429bca..1f7e501 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "math/big" + "strings" "sync" "sync/atomic" "time" @@ -231,6 +232,10 @@ func (vm *VM) Initialize( toEngine chan<- commonEng.Message, fxs []*commonEng.Fx, ) error { + if vm.CLIConfig.ParsingError != nil { + return vm.CLIConfig.ParsingError + } + if len(fxs) > 0 { return errUnsupportedFXs } @@ -514,18 +519,20 @@ func newHandler(name string, service interface{}, lockOption ...commonEng.LockOp // CreateHandlers makes new http handlers that can handle API calls func (vm *VM) CreateHandlers() map[string]*commonEng.HTTPHandler { handler := vm.chain.NewRPCHandler() + enabledAPIs := vm.CLIConfig.EthAPIs() vm.chain.AttachEthService(handler, vm.CLIConfig.EthAPIs()) if vm.CLIConfig.SnowmanAPIEnabled { handler.RegisterName("snowman", &SnowmanAPI{vm}) - } - if vm.CLIConfig.Web3APIEnabled { - handler.RegisterName("web3", &Web3API{}) + enabledAPIs = append(enabledAPIs, "snowman") } if vm.CLIConfig.CorethAdminAPIEnabled { handler.RegisterName("admin", &admin.Performance{}) + enabledAPIs = append(enabledAPIs, "coreth-admin") } + log.Info(fmt.Sprintf("Enabled APIs: %s", strings.Join(enabledAPIs, ", "))) + return map[string]*commonEng.HTTPHandler{ "/rpc": {LockOptions: commonEng.NoLock, Handler: handler}, "/avax": newHandler("avax", &AvaxAPI{vm}), diff --git a/plugin/main.go b/plugin/main.go index b42ba14..9c0a28a 100644 --- a/plugin/main.go +++ b/plugin/main.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - "github.com/hashicorp/go-plugin" "github.com/ava-labs/avalanchego/vms/rpcchainvm" @@ -11,9 +9,6 @@ import ( ) func main() { - if errs.Errored() { - panic(fmt.Sprintf("Errored while parsing Coreth CLI Config: %w", errs.Err)) - } plugin.Serve(&plugin.ServeConfig{ HandshakeConfig: rpcchainvm.Handshake, Plugins: map[string]plugin.Plugin{ diff --git a/plugin/params.go b/plugin/params.go index 1810295..d780d00 100644 --- a/plugin/params.go +++ b/plugin/params.go @@ -5,37 +5,31 @@ import ( "flag" "os" - "github.com/ava-labs/avalanchego/utils/wrappers" "github.com/ava-labs/coreth/plugin/evm" ) -const ( - name = "coreth" -) - var ( cliConfig evm.CommandLineConfig - errs wrappers.Errs ) func init() { - errs := wrappers.Errs{} - fs := flag.NewFlagSet(name, flag.ContinueOnError) + fs := flag.NewFlagSet("coreth", flag.ContinueOnError) - config := fs.String("coreth-config", "default", "Pass in CLI Config to set runtime attributes for Coreth") + config := fs.String("config", "default", "Pass in CLI Config to set runtime attributes for Coreth") if err := fs.Parse(os.Args[1:]); err != nil { - errs.Add(err) + cliConfig.ParsingError = err return } if *config == "default" { cliConfig.EthAPIEnabled = true + cliConfig.PersonalAPIEnabled = true cliConfig.TxPoolAPIEnabled = true cliConfig.RPCGasCap = 2500000000 // 25000000 x 100 cliConfig.RPCTxFeeCap = 100 // 100 AVAX } else { // TODO only overwrite values that were explicitly set - errs.Add(json.Unmarshal([]byte(*config), &cliConfig)) + cliConfig.ParsingError = json.Unmarshal([]byte(*config), &cliConfig) } } diff --git a/scripts/build_coreth.sh b/scripts/build_coreth.sh new file mode 100755 index 0000000..41fab1b --- /dev/null +++ b/scripts/build_coreth.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +# Set GOPATH +GOPATH="$(go env GOPATH)" + +# Set default binary location +BINARY_PATH="$GOPATH/src/github.com/ava-labs/avalanchego/build/plugins/evm" + +if [[ $# -eq 1 ]]; then + BINARY_PATH=$1 +elif [[ $# -ne 0 ]]; then + echo "Invalid arguments to build coreth. Requires either no arguments (default) or one arguments to specify binary location." + exit 1 +fi + +# Build Coreth, which is run as a subprocess +echo "Building Coreth..." +go build -o "$BINARY_PATH" "plugin/"*.go -- cgit v1.2.3-70-g09d2