aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorStephen Buttolph <[email protected]>2020-10-30 17:29:21 -0400
committerGitHub <[email protected]>2020-10-30 17:29:21 -0400
commitc613e21ea107e8369926f4b53b7830a9d5dabf18 (patch)
tree7fbd50b5628c6f687d7c5e68886b8f39fa135481 /plugin
parent806d04e9aa4b6e22fc2a484ada6fecb0c9a348e3 (diff)
parenta6ffdbbdd269fae35c5600e954bd373b619fe733 (diff)
Merge pull request #45 from ava-labs/handle-config
Parse config to set enabled APIs
Diffstat (limited to 'plugin')
-rw-r--r--plugin/evm/config.go20
-rw-r--r--plugin/evm/service.go29
-rw-r--r--plugin/evm/vm.go17
-rw-r--r--plugin/main.go5
-rw-r--r--plugin/params.go17
5 files changed, 48 insertions, 40 deletions
diff --git a/plugin/evm/config.go b/plugin/evm/config.go
index 4669807..4d6650a 100644
--- a/plugin/evm/config.go
+++ b/plugin/evm/config.go
@@ -3,19 +3,21 @@ 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"`
+ NetAPIEnabled bool `json:"net-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..a934941 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,14 +38,17 @@ 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{}
+// NetAPI offers network related API methods
+type NetAPI struct{ vm *VM }
-// ClientVersion returns the version of the vm running
-func (s *Web3API) ClientVersion() string { return version }
+// Listening returns an indication if the node is listening for network connections.
+func (s *NetAPI) Listening() bool { return true } // always listening
+
+// PeerCount returns the number of connected peers
+func (s *NetAPI) PeerCount() hexutil.Uint { return hexutil.Uint(0) } // TODO: report number of connected peers
-// Sha3 returns the bytes returned by hashing [input] with Keccak256
-func (s *Web3API) Sha3(input hexutil.Bytes) hexutil.Bytes { return ethcrypto.Keccak256(input) }
+// Version returns the current ethereum protocol version.
+func (s *NetAPI) Version() string { return fmt.Sprintf("%d", s.vm.networkID) }
// GetAcceptedFrontReply defines the reply that will be sent from the
// GetAcceptedFront API call
@@ -71,6 +73,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
@@ -116,7 +121,7 @@ type ImportKeyArgs struct {
}
// ImportKey adds a private key to the provided user
-func (service *AvaxAPI) ImportKey(r *http.Request, args *ImportKeyArgs, reply *api.JsonAddress) error {
+func (service *AvaxAPI) ImportKey(r *http.Request, args *ImportKeyArgs, reply *api.JSONAddress) error {
log.Info(fmt.Sprintf("EVM: ImportKey called for user '%s'", args.Username))
if !strings.HasPrefix(args.PrivateKey, constants.SecretKeyPrefix) {
@@ -164,13 +169,13 @@ type ImportArgs struct {
}
// ImportAVAX is a deprecated name for Import.
-func (service *AvaxAPI) ImportAVAX(_ *http.Request, args *ImportArgs, response *api.JsonTxID) error {
+func (service *AvaxAPI) ImportAVAX(_ *http.Request, args *ImportArgs, response *api.JSONTxID) error {
return service.Import(nil, args, response)
}
// Import issues a transaction to import AVAX from the X-chain. The AVAX
// must have already been exported from the X-Chain.
-func (service *AvaxAPI) Import(_ *http.Request, args *ImportArgs, response *api.JsonTxID) error {
+func (service *AvaxAPI) Import(_ *http.Request, args *ImportArgs, response *api.JSONTxID) error {
log.Info("EVM: ImportAVAX called")
chainID, err := service.vm.ctx.BCLookup.Lookup(args.SourceChain)
@@ -221,7 +226,7 @@ type ExportAVAXArgs struct {
// ExportAVAX exports AVAX from the C-Chain to the X-Chain
// It must be imported on the X-Chain to complete the transfer
-func (service *AvaxAPI) ExportAVAX(_ *http.Request, args *ExportAVAXArgs, response *api.JsonTxID) error {
+func (service *AvaxAPI) ExportAVAX(_ *http.Request, args *ExportAVAXArgs, response *api.JSONTxID) error {
return service.Export(nil, &ExportArgs{
ExportAVAXArgs: *args,
AssetID: service.vm.ctx.AVAXAssetID,
@@ -237,7 +242,7 @@ type ExportArgs struct {
// Export exports an asset from the C-Chain to the X-Chain
// It must be imported on the X-Chain to complete the transfer
-func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api.JsonTxID) error {
+func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api.JSONTxID) error {
log.Info("EVM: Export called")
if args.AssetID.IsZero() {
return fmt.Errorf("assetID is required")
diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go
index c429bca..c902f85 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,17 +519,23 @@ 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")
}
+ if vm.CLIConfig.NetAPIEnabled {
+ handler.RegisterName("net", &NetAPI{vm})
+ enabledAPIs = append(enabledAPIs, "net")
+ }
+
+ log.Info(fmt.Sprintf("Enabled APIs: %s", strings.Join(enabledAPIs, ", ")))
return map[string]*commonEng.HTTPHandler{
"/rpc": {LockOptions: commonEng.NoLock, Handler: handler},
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..6615798 100644
--- a/plugin/params.go
+++ b/plugin/params.go
@@ -5,37 +5,32 @@ 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.NetAPIEnabled = 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)
}
}