diff options
author | aaronbuchwald <[email protected]> | 2020-10-26 22:03:05 -0400 |
---|---|---|
committer | GitHub <[email protected]> | 2020-10-26 22:03:05 -0400 |
commit | 806d04e9aa4b6e22fc2a484ada6fecb0c9a348e3 (patch) | |
tree | 36822cea4b16638e6a246af209ccdf77b3eb2088 /plugin | |
parent | 8a8ef56dd1a0f2cd28c6d4b3a579b16cdda6e2cf (diff) | |
parent | cccb47666b2bccce378a9a56824061ea3a9a95fa (diff) |
Merge pull request #43 from ava-labs/handle-block-number-requests
Handle block number requests
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/evm/config.go | 39 | ||||
-rw-r--r-- | plugin/evm/service.go | 67 | ||||
-rw-r--r-- | plugin/evm/vm.go | 23 | ||||
-rw-r--r-- | plugin/main.go | 7 | ||||
-rw-r--r-- | plugin/params.go | 41 |
5 files changed, 103 insertions, 74 deletions
diff --git a/plugin/evm/config.go b/plugin/evm/config.go new file mode 100644 index 0000000..4669807 --- /dev/null +++ b/plugin/evm/config.go @@ -0,0 +1,39 @@ +package evm + +// CommandLineConfig ... +type CommandLineConfig struct { + // Coreth APIs + SnowmanAPIEnabled bool `json:"snowmanAPIEnabled"` + Web3APIEnabled bool `json:"web3APIEnabled"` + CorethAdminAPIEnabled bool `json:"corethAdminAPIEnabled"` + + // Coreth API Gas/Price Caps + RPCGasCap uint64 `json:"rpcGasCap"` + RPCTxFeeCap float64 `json:"rpcTxFeeCap"` + + // Eth APIs + EthAPIEnabled bool `json:"ethAPIEnabled"` + PersonalAPIEnabled bool `json:"personalAPIEnabled"` + TxPoolAPIEnabled bool `json:"txPoolAPIEnabled"` + DebugAPIEnabled bool `json:"debugAPIEnabled"` +} + +// EthAPIs returns an array of strings representing the Eth APIs that should be enabled +func (c CommandLineConfig) EthAPIs() []string { + ethAPIs := make([]string, 0) + + if c.EthAPIEnabled { + ethAPIs = append(ethAPIs, "eth") + } + if c.PersonalAPIEnabled { + ethAPIs = append(ethAPIs, "personal") + } + if c.TxPoolAPIEnabled { + ethAPIs = append(ethAPIs, "txpool") + } + if c.DebugAPIEnabled { + ethAPIs = append(ethAPIs, "debug") + } + + return ethAPIs +} diff --git a/plugin/evm/service.go b/plugin/evm/service.go index a844f10..128b98e 100644 --- a/plugin/evm/service.go +++ b/plugin/evm/service.go @@ -5,22 +5,18 @@ package evm import ( "context" - "crypto/rand" "errors" "fmt" "math/big" "net/http" "strings" - "github.com/ava-labs/coreth" - "github.com/ava-labs/avalanchego/api" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/crypto" "github.com/ava-labs/avalanchego/utils/formatting" "github.com/ava-labs/avalanchego/utils/json" - "github.com/ava-labs/coreth/core/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ethcrypto "github.com/ethereum/go-ethereum/crypto" @@ -28,7 +24,7 @@ import ( ) const ( - version = "Athereum 1.0" + version = "coreth-v0.3.7" ) // test constants @@ -37,30 +33,12 @@ const ( GenesisTestKey = "0xabd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1" ) -// DebugAPI introduces helper functions for debuging -type DebugAPI struct{ vm *VM } - // SnowmanAPI introduces snowman specific functionality to the evm type SnowmanAPI struct{ vm *VM } -// NetAPI offers network related API methods -type NetAPI struct{ vm *VM } - // AvaxAPI offers Avalanche network related API methods type AvaxAPI struct{ vm *VM } -// NewNetAPI creates a new net API instance. -func NewNetAPI(vm *VM) *NetAPI { return &NetAPI{vm} } - -// 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 - -// Version returns the current ethereum protocol version. -func (s *NetAPI) Version() string { return fmt.Sprintf("%d", s.vm.networkID) } - // Web3API offers helper API methods type Web3API struct{} @@ -86,49 +64,8 @@ func (api *SnowmanAPI) GetAcceptedFront(ctx context.Context) (*GetAcceptedFrontR }, nil } -// GetGenesisBalance returns the current funds in the genesis -func (api *DebugAPI) GetGenesisBalance(ctx context.Context) (*hexutil.Big, error) { - lastAccepted := api.vm.getLastAccepted() - log.Trace(fmt.Sprintf("Currently accepted block front: %s", lastAccepted.ethBlock.Hash().Hex())) - state, err := api.vm.chain.BlockState(lastAccepted.ethBlock) - if err != nil { - return nil, err - } - return (*hexutil.Big)(state.GetBalance(common.HexToAddress(GenesisTestAddr))), nil -} - -// SpendGenesis funds -func (api *DebugAPI) SpendGenesis(ctx context.Context, nonce uint64) error { - log.Info("Spending the genesis") - - value := big.NewInt(1000000000000) - gasLimit := 21000 - gasPrice := big.NewInt(1000000000) - - genPrivateKey, err := ethcrypto.HexToECDSA(GenesisTestKey[2:]) - if err != nil { - return err - } - bob, err := coreth.NewKey(rand.Reader) - if err != nil { - return err - } - - tx := types.NewTransaction(nonce, bob.Address, value, uint64(gasLimit), gasPrice, nil) - signedTx, err := types.SignTx(tx, types.NewEIP155Signer(api.vm.chainID), genPrivateKey) - if err != nil { - return err - } - - if err := api.vm.issueRemoteTxs([]*types.Transaction{signedTx}); err != nil { - return err - } - - return nil -} - // IssueBlock to the chain -func (api *DebugAPI) IssueBlock(ctx context.Context) error { +func (api *SnowmanAPI) IssueBlock(ctx context.Context) error { log.Info("Issuing a new block") return api.vm.tryBlockGen() diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index f19c105..c429bca 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -154,6 +154,8 @@ func init() { type VM struct { ctx *snow.Context + CLIConfig CommandLineConfig + chainID *big.Int networkID uint64 genesisHash common.Hash @@ -259,8 +261,8 @@ func (vm *VM) Initialize( // Set minimum price for mining and default gas price oracle value to the min // gas price to prevent so transactions and blocks all use the correct fees config.Miner.GasPrice = params.MinGasPrice - config.RPCGasCap = 2500000000 // 25000000 x 100 - config.RPCTxFeeCap = 100 // 100 AVAX + config.RPCGasCap = vm.CLIConfig.RPCGasCap + config.RPCTxFeeCap = vm.CLIConfig.RPCTxFeeCap config.GPO.Default = params.MinGasPrice config.TxPool.PriceLimit = params.MinGasPrice.Uint64() config.TxPool.NoLocals = true @@ -512,12 +514,17 @@ 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() - vm.chain.AttachEthService(handler, []string{"eth", "personal", "txpool"}) - handler.RegisterName("net", &NetAPI{vm}) - handler.RegisterName("snowman", &SnowmanAPI{vm}) - handler.RegisterName("web3", &Web3API{}) - handler.RegisterName("debug", &DebugAPI{vm}) - handler.RegisterName("admin", &admin.Performance{}) + vm.chain.AttachEthService(handler, vm.CLIConfig.EthAPIs()) + + if vm.CLIConfig.SnowmanAPIEnabled { + handler.RegisterName("snowman", &SnowmanAPI{vm}) + } + if vm.CLIConfig.Web3APIEnabled { + handler.RegisterName("web3", &Web3API{}) + } + if vm.CLIConfig.CorethAdminAPIEnabled { + handler.RegisterName("admin", &admin.Performance{}) + } return map[string]*commonEng.HTTPHandler{ "/rpc": {LockOptions: commonEng.NoLock, Handler: handler}, diff --git a/plugin/main.go b/plugin/main.go index c79a305..b42ba14 100644 --- a/plugin/main.go +++ b/plugin/main.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "github.com/hashicorp/go-plugin" "github.com/ava-labs/avalanchego/vms/rpcchainvm" @@ -9,10 +11,13 @@ 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{ - "vm": rpcchainvm.New(&evm.VM{}), + "vm": rpcchainvm.New(&evm.VM{CLIConfig: cliConfig}), }, // A non-nil value here enables gRPC serving for this plugin... diff --git a/plugin/params.go b/plugin/params.go new file mode 100644 index 0000000..1810295 --- /dev/null +++ b/plugin/params.go @@ -0,0 +1,41 @@ +package main + +import ( + "encoding/json" + "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) + + config := fs.String("coreth-config", "default", "Pass in CLI Config to set runtime attributes for Coreth") + + if err := fs.Parse(os.Args[1:]); err != nil { + errs.Add(err) + return + } + + if *config == "default" { + cliConfig.EthAPIEnabled = 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)) + } +} |