mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 15:16:43 +00:00
Merge pull request #309 from maticnetwork/manav/dev-mode
Enable developer mode for new cli
This commit is contained in:
commit
fe21391e89
6 changed files with 224 additions and 35 deletions
47
internal/cli/server/chains/developer.go
Normal file
47
internal/cli/server/chains/developer.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package chains
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetDeveloperChain returns the developer mode configs.
|
||||||
|
func GetDeveloperChain(period uint64, faucet common.Address) *Chain {
|
||||||
|
// Override the default period to the user requested one
|
||||||
|
config := *params.AllCliqueProtocolChanges
|
||||||
|
config.Clique = ¶ms.CliqueConfig{
|
||||||
|
Period: period,
|
||||||
|
Epoch: config.Clique.Epoch,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assemble and return the chain having genesis with the
|
||||||
|
// precompiles and faucet pre-funded
|
||||||
|
return &Chain{
|
||||||
|
Hash: common.Hash{},
|
||||||
|
NetworkId: 1337,
|
||||||
|
Genesis: &core.Genesis{
|
||||||
|
Config: &config,
|
||||||
|
ExtraData: append(append(make([]byte, 32), faucet[:]...), make([]byte, crypto.SignatureLength)...),
|
||||||
|
GasLimit: 11500000,
|
||||||
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
||||||
|
Difficulty: big.NewInt(1),
|
||||||
|
Alloc: map[common.Address]core.GenesisAccount{
|
||||||
|
common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover
|
||||||
|
common.BytesToAddress([]byte{2}): {Balance: big.NewInt(1)}, // SHA256
|
||||||
|
common.BytesToAddress([]byte{3}): {Balance: big.NewInt(1)}, // RIPEMD
|
||||||
|
common.BytesToAddress([]byte{4}): {Balance: big.NewInt(1)}, // Identity
|
||||||
|
common.BytesToAddress([]byte{5}): {Balance: big.NewInt(1)}, // ModExp
|
||||||
|
common.BytesToAddress([]byte{6}): {Balance: big.NewInt(1)}, // ECAdd
|
||||||
|
common.BytesToAddress([]byte{7}): {Balance: big.NewInt(1)}, // ECScalarMul
|
||||||
|
common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing
|
||||||
|
common.BytesToAddress([]byte{9}): {Balance: big.NewInt(1)}, // BLAKE2b
|
||||||
|
faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Bootnodes: []string{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,6 +14,8 @@ import (
|
||||||
|
|
||||||
godebug "runtime/debug"
|
godebug "runtime/debug"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/accounts"
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/fdlimit"
|
"github.com/ethereum/go-ethereum/common/fdlimit"
|
||||||
"github.com/ethereum/go-ethereum/eth/downloader"
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
||||||
|
|
@ -91,6 +93,9 @@ type Config struct {
|
||||||
|
|
||||||
// GRPC has the grpc server related settings
|
// GRPC has the grpc server related settings
|
||||||
GRPC *GRPCConfig
|
GRPC *GRPCConfig
|
||||||
|
|
||||||
|
// Developer has the developer mode related settings
|
||||||
|
Developer *DeveloperConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type P2PConfig struct {
|
type P2PConfig struct {
|
||||||
|
|
@ -365,6 +370,14 @@ type AccountsConfig struct {
|
||||||
UseLightweightKDF bool `hcl:"use-lightweight-kdf,optional"`
|
UseLightweightKDF bool `hcl:"use-lightweight-kdf,optional"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DeveloperConfig struct {
|
||||||
|
// Enabled enables the developer mode
|
||||||
|
Enabled bool `hcl:"dev,optional"`
|
||||||
|
|
||||||
|
// Period is the block period to use in developer mode
|
||||||
|
Period uint64 `hcl:"period,optional"`
|
||||||
|
}
|
||||||
|
|
||||||
func DefaultConfig() *Config {
|
func DefaultConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Chain: "mainnet",
|
Chain: "mainnet",
|
||||||
|
|
@ -486,6 +499,10 @@ func DefaultConfig() *Config {
|
||||||
GRPC: &GRPCConfig{
|
GRPC: &GRPCConfig{
|
||||||
Addr: ":3131",
|
Addr: ":3131",
|
||||||
},
|
},
|
||||||
|
Developer: &DeveloperConfig{
|
||||||
|
Enabled: false,
|
||||||
|
Period: 0,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -573,6 +590,9 @@ func readConfigFile(path string) (*Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) loadChain() error {
|
func (c *Config) loadChain() error {
|
||||||
|
if c.Developer.Enabled {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
chain, ok := chains.GetChain(c.Chain)
|
chain, ok := chains.GetChain(c.Chain)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("chain '%s' not found", c.Chain)
|
return fmt.Errorf("chain '%s' not found", c.Chain)
|
||||||
|
|
@ -585,7 +605,7 @@ func (c *Config) loadChain() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// depending on the chain we have different cache values
|
// depending on the chain we have different cache values
|
||||||
if c.Chain != "mainnet" {
|
if c.Chain == "mainnet" {
|
||||||
c.Cache.Cache = 4096
|
c.Cache.Cache = 4096
|
||||||
} else {
|
} else {
|
||||||
c.Cache.Cache = 1024
|
c.Cache.Cache = 1024
|
||||||
|
|
@ -593,14 +613,19 @@ func (c *Config) loadChain() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) buildEth() (*ethconfig.Config, error) {
|
func (c *Config) buildEth(stack *node.Node) (*ethconfig.Config, error) {
|
||||||
dbHandles, err := makeDatabaseHandles()
|
dbHandles, err := makeDatabaseHandles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
n := ethconfig.Defaults
|
n := ethconfig.Defaults
|
||||||
|
|
||||||
|
// only update for non-developer mode as we don't yet
|
||||||
|
// have the chain object for it.
|
||||||
|
if !c.Developer.Enabled {
|
||||||
n.NetworkId = c.chain.NetworkId
|
n.NetworkId = c.chain.NetworkId
|
||||||
n.Genesis = c.chain.Genesis
|
n.Genesis = c.chain.Genesis
|
||||||
|
}
|
||||||
n.HeimdallURL = c.Heimdall.URL
|
n.HeimdallURL = c.Heimdall.URL
|
||||||
n.WithoutHeimdall = c.Heimdall.Without
|
n.WithoutHeimdall = c.Heimdall.Without
|
||||||
|
|
||||||
|
|
@ -640,6 +665,55 @@ func (c *Config) buildEth() (*ethconfig.Config, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update for developer mode
|
||||||
|
if c.Developer.Enabled {
|
||||||
|
// Get a keystore
|
||||||
|
var ks *keystore.KeyStore
|
||||||
|
if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
|
||||||
|
ks = keystores[0].(*keystore.KeyStore)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new developer account or reuse existing one
|
||||||
|
var (
|
||||||
|
developer accounts.Account
|
||||||
|
passphrase string
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
// etherbase has been set above, configuring the miner address from command line flags.
|
||||||
|
if n.Miner.Etherbase != (common.Address{}) {
|
||||||
|
developer = accounts.Account{Address: n.Miner.Etherbase}
|
||||||
|
} else if accs := ks.Accounts(); len(accs) > 0 {
|
||||||
|
developer = ks.Accounts()[0]
|
||||||
|
} else {
|
||||||
|
developer, err = ks.NewAccount(passphrase)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create developer account: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := ks.Unlock(developer, passphrase); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unlock developer account: %v", err)
|
||||||
|
}
|
||||||
|
log.Info("Using developer account", "address", developer.Address)
|
||||||
|
|
||||||
|
// get developer mode chain config
|
||||||
|
c.chain = chains.GetDeveloperChain(c.Developer.Period, developer.Address)
|
||||||
|
|
||||||
|
// update the parameters
|
||||||
|
n.NetworkId = c.chain.NetworkId
|
||||||
|
n.Genesis = c.chain.Genesis
|
||||||
|
|
||||||
|
// Update cache
|
||||||
|
c.Cache.Cache = 1024
|
||||||
|
|
||||||
|
// Update sync mode
|
||||||
|
c.SyncMode = "full"
|
||||||
|
|
||||||
|
// update miner gas price
|
||||||
|
if n.Miner.GasPrice == nil {
|
||||||
|
n.Miner.GasPrice = big.NewInt(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// discovery (this params should be in node.Config)
|
// discovery (this params should be in node.Config)
|
||||||
{
|
{
|
||||||
n.EthDiscoveryURLs = c.P2P.Discovery.DNS
|
n.EthDiscoveryURLs = c.P2P.Discovery.DNS
|
||||||
|
|
@ -788,6 +862,17 @@ func (c *Config) buildNode() (*node.Config, error) {
|
||||||
GraphQLVirtualHosts: c.JsonRPC.VHost,
|
GraphQLVirtualHosts: c.JsonRPC.VHost,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dev mode
|
||||||
|
if c.Developer.Enabled {
|
||||||
|
cfg.UseLightweightKDF = true
|
||||||
|
|
||||||
|
// disable p2p networking
|
||||||
|
c.P2P.NoDiscover = true
|
||||||
|
cfg.P2P.ListenAddr = ""
|
||||||
|
cfg.P2P.NoDial = true
|
||||||
|
cfg.P2P.DiscoveryV5 = false
|
||||||
|
}
|
||||||
|
|
||||||
// enable jsonrpc endpoints
|
// enable jsonrpc endpoints
|
||||||
{
|
{
|
||||||
if c.JsonRPC.Http.Enabled {
|
if c.JsonRPC.Http.Enabled {
|
||||||
|
|
@ -806,6 +891,8 @@ func (c *Config) buildNode() (*node.Config, error) {
|
||||||
}
|
}
|
||||||
cfg.P2P.NAT = natif
|
cfg.P2P.NAT = natif
|
||||||
|
|
||||||
|
// only check for non-developer modes
|
||||||
|
if !c.Developer.Enabled {
|
||||||
// Discovery
|
// Discovery
|
||||||
// if no bootnodes are defined, use the ones from the chain file.
|
// if no bootnodes are defined, use the ones from the chain file.
|
||||||
bootnodes := c.P2P.Discovery.Bootnodes
|
bootnodes := c.P2P.Discovery.Bootnodes
|
||||||
|
|
@ -824,6 +911,7 @@ func (c *Config) buildNode() (*node.Config, error) {
|
||||||
if cfg.P2P.TrustedNodes, err = parseBootnodes(c.P2P.Discovery.TrustedNodes); err != nil {
|
if cfg.P2P.TrustedNodes, err = parseBootnodes(c.P2P.Discovery.TrustedNodes); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if c.P2P.NoDiscover {
|
if c.P2P.NoDiscover {
|
||||||
// Disable networking, for now, we will not even allow incomming connections
|
// Disable networking, for now, we will not even allow incomming connections
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ func TestConfigDefault(t *testing.T) {
|
||||||
_, err := config.buildNode()
|
_, err := config.buildNode()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
_, err = config.buildEth()
|
_, err = config.buildEth(nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -461,5 +461,17 @@ func (c *Command) Flags() *flagset.Flagset {
|
||||||
Usage: "Address and port to bind the GRPC server",
|
Usage: "Address and port to bind the GRPC server",
|
||||||
Value: &c.cliConfig.GRPC.Addr,
|
Value: &c.cliConfig.GRPC.Addr,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// developer
|
||||||
|
f.BoolFlag(&flagset.BoolFlag{
|
||||||
|
Name: "dev",
|
||||||
|
Usage: "Enable developer mode with ephemeral proof-of-authority network and a pre-funded developer account, mining enabled",
|
||||||
|
Value: &c.cliConfig.Developer.Enabled,
|
||||||
|
})
|
||||||
|
f.Uint64Flag(&flagset.Uint64Flag{
|
||||||
|
Name: "dev.period",
|
||||||
|
Usage: "Block period to use in developer mode (0 = mine only if transaction pending)",
|
||||||
|
Value: &c.cliConfig.Developer.Period,
|
||||||
|
})
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,11 +69,22 @@ func NewServer(config *Config) (*Server, error) {
|
||||||
}
|
}
|
||||||
srv.node = stack
|
srv.node = stack
|
||||||
|
|
||||||
|
// setup account manager (only keystore)
|
||||||
|
{
|
||||||
|
keydir := stack.KeyStoreDir()
|
||||||
|
n, p := keystore.StandardScryptN, keystore.StandardScryptP
|
||||||
|
if config.Accounts.UseLightweightKDF {
|
||||||
|
n, p = keystore.LightScryptN, keystore.LightScryptP
|
||||||
|
}
|
||||||
|
stack.AccountManager().AddBackend(keystore.NewKeyStore(keydir, n, p))
|
||||||
|
}
|
||||||
|
|
||||||
// register the ethereum backend
|
// register the ethereum backend
|
||||||
ethCfg, err := config.buildEth()
|
ethCfg, err := config.buildEth(stack)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
backend, err := eth.New(stack, ethCfg)
|
backend, err := eth.New(stack, ethCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -97,18 +108,8 @@ func NewServer(config *Config) (*Server, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup account manager (only keystore)
|
// sealing (if enabled) or in dev mode
|
||||||
{
|
if config.Sealer.Enabled || config.Developer.Enabled {
|
||||||
keydir := stack.KeyStoreDir()
|
|
||||||
n, p := keystore.StandardScryptN, keystore.StandardScryptP
|
|
||||||
if config.Accounts.UseLightweightKDF {
|
|
||||||
n, p = keystore.LightScryptN, keystore.LightScryptP
|
|
||||||
}
|
|
||||||
stack.AccountManager().AddBackend(keystore.NewKeyStore(keydir, n, p))
|
|
||||||
}
|
|
||||||
|
|
||||||
// sealing (if enabled)
|
|
||||||
if config.Sealer.Enabled {
|
|
||||||
if err := backend.StartMining(1); err != nil {
|
if err := backend.StartMining(1); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
41
internal/cli/server/server_test.go
Normal file
41
internal/cli/server/server_test.go
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServer_DeveloperMode(t *testing.T) {
|
||||||
|
|
||||||
|
// get the default config
|
||||||
|
config := DefaultConfig()
|
||||||
|
|
||||||
|
// enable developer mode
|
||||||
|
config.Developer.Enabled = true
|
||||||
|
config.Developer.Period = 2 // block time
|
||||||
|
|
||||||
|
// start the server
|
||||||
|
server, err1 := NewServer(config)
|
||||||
|
if err1 != nil {
|
||||||
|
t.Fatalf("failed to start server: %v", err1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// record the initial block number
|
||||||
|
blockNumber := server.backend.BlockChain().CurrentBlock().Header().Number.Int64()
|
||||||
|
|
||||||
|
var i int64 = 0
|
||||||
|
for i = 0; i < 10; i++ {
|
||||||
|
// We expect the node to mine blocks every `config.Developer.Period` time period
|
||||||
|
time.Sleep(time.Duration(config.Developer.Period) * time.Second)
|
||||||
|
currBlock := server.backend.BlockChain().CurrentBlock().Header().Number.Int64()
|
||||||
|
expected := blockNumber + i + 1
|
||||||
|
if res := assert.Equal(t, currBlock, expected); res == false {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// stop the server
|
||||||
|
server.Stop()
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue