Add P2P private key

This commit is contained in:
Ferran Borreguero 2021-10-05 09:59:10 +02:00
parent 3acd7a12de
commit 449e6a517a
3 changed files with 82 additions and 10 deletions

View file

@ -9,6 +9,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethstats" "github.com/ethereum/go-ethereum/ethstats"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -85,9 +86,7 @@ func (c *Command) Run(args []string) int {
// TODO: MakeChain? // TODO: MakeChain?
// TODO: Metrics // TODO: Metrics
// TODO: Node key, pre generate one
// TODO: apis // TODO: apis
// TODO: account management
// TODO: Graphql // TODO: Graphql
// TODO: embed // TODO: embed
@ -111,6 +110,24 @@ func (c *Command) Run(args []string) int {
} }
} }
// setup account manager (only keystore)
{
keydir := stack.KeyStoreDir()
scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP
stack.AccountManager().AddBackend(keystore.NewKeyStore(keydir, scryptN, scryptP))
}
// sealing (if enabled)
if *config.Sealer.Enabled {
if err := backend.StartMining(1); err != nil {
panic(err)
}
}
c.setupMetrics()
// start the node // start the node
if err := c.node.Start(); err != nil { if err := c.node.Start(); err != nil {
panic(err) panic(err)
@ -118,14 +135,15 @@ func (c *Command) Run(args []string) int {
return c.handleSignals() return c.handleSignals()
} }
func (c *Command) setupMetrics() { func (c *Command) setupMetrics() error {
if !metrics.Enabled { if !metrics.Enabled {
// metrics are globally disabled // metrics are globally disabled
return return nil
} }
// Start system runtime metrics collection // Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second) go metrics.CollectProcessMetrics(3 * time.Second)
return nil
} }
func (c *Command) handleSignals() int { func (c *Command) handleSignals() int {

View file

@ -1,17 +1,24 @@
package server package server
import ( import (
"crypto/ecdsa"
"fmt" "fmt"
"math/big"
"os"
"path/filepath"
"strconv" "strconv"
"time" "time"
"github.com/ethereum/go-ethereum/command/server/chains" "github.com/ethereum/go-ethereum/command/server/chains"
"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/crypto"
"github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
"github.com/imdario/mergo" "github.com/imdario/mergo"
) )
@ -78,9 +85,11 @@ type TxPoolConfig struct {
} }
type SealerConfig struct { type SealerConfig struct {
Seal *bool Enabled *bool
Etherbase *string Etherbase *string
ExtraData *string ExtraData *string
GasCeil *uint64
GasPrice *big.Int
} }
func DefaultConfig() *Config { func DefaultConfig() *Config {
@ -120,7 +129,9 @@ func DefaultConfig() *Config {
LifeTime: durPtr(3 * time.Hour), LifeTime: durPtr(3 * time.Hour),
}, },
Sealer: &SealerConfig{ Sealer: &SealerConfig{
Seal: boolPtr(false), Enabled: boolPtr(false),
GasCeil: uint64Ptr(8000000),
GasPrice: big.NewInt(params.GWei),
}, },
} }
} }
@ -173,7 +184,9 @@ func (c *Config) buildEth() (*ethconfig.Config, error) {
// miner options // miner options
{ {
cfg := n.Miner cfg := n.Miner
fmt.Println(cfg) cfg.Etherbase = common.HexToAddress(*c.Sealer.Etherbase)
cfg.GasPrice = c.Sealer.GasPrice
cfg.GasCeil = *c.Sealer.GasCeil
} }
// discovery (this params should be in node.Config) // discovery (this params should be in node.Config)
@ -195,10 +208,17 @@ func (c *Config) buildEth() (*ethconfig.Config, error) {
return &n, nil return &n, nil
} }
var (
clientIdentifier = "bor"
gitCommit = "" // Git SHA1 commit hash of the release (set via linker flags)
gitDate = "" // Git commit date YYYYMMDD of the release (set via linker flags)
)
func (c *Config) buildNode() (*node.Config, error) { func (c *Config) buildNode() (*node.Config, error) {
cfg := &node.Config{ cfg := &node.Config{
Name: "reader", Name: clientIdentifier,
DataDir: *c.DataDir, DataDir: *c.DataDir,
Version: params.VersionWithCommit(gitCommit, gitDate),
P2P: p2p.Config{ P2P: p2p.Config{
MaxPeers: int(*c.P2P.MaxPeers), MaxPeers: int(*c.P2P.MaxPeers),
ListenAddr: *c.P2P.Bind + ":" + strconv.Itoa(int(*c.P2P.Port)), ListenAddr: *c.P2P.Bind + ":" + strconv.Itoa(int(*c.P2P.Port)),
@ -212,8 +232,14 @@ func (c *Config) buildNode() (*node.Config, error) {
*/ */
} }
// setup private key for DevP2P if not found
devP2PPrivKey, err := readDevP2PKey(*c.DataDir)
if err != nil {
return nil, err
}
cfg.P2P.PrivateKey = devP2PPrivKey
// Discovery // Discovery
var err error
if cfg.P2P.BootstrapNodes, err = parseBootnodes(c.P2P.Discovery.Bootnodes); err != nil { if cfg.P2P.BootstrapNodes, err = parseBootnodes(c.P2P.Discovery.Bootnodes); err != nil {
return nil, err return nil, err
} }
@ -269,3 +295,31 @@ func parseBootnodes(urls []string) ([]*enode.Node, error) {
} }
return dst, nil return dst, nil
} }
const devP2PKeyPath = "devp2p.key"
func readDevP2PKey(dataDir string) (*ecdsa.PrivateKey, error) {
path := filepath.Join(dataDir, devP2PKeyPath)
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to stat (%s): %v", path, err)
}
if os.IsNotExist(err) {
priv, err := crypto.GenerateKey()
if err != nil {
return nil, err
}
if err := crypto.SaveECDSA(path, priv); err != nil {
return nil, err
}
return priv, nil
}
// exists
priv, err := crypto.LoadECDSA(path)
if err != nil {
return nil, err
}
return priv, nil
}

View file

@ -48,7 +48,7 @@ func (c *Command) Flags() *flagset.Flagset {
// sealer options // sealer options
f.BoolFlag(&flagset.BoolFlag{ f.BoolFlag(&flagset.BoolFlag{
Name: "mine", Name: "mine",
Value: c.cliConfig.Sealer.Seal, Value: c.cliConfig.Sealer.Enabled,
Usage: "", Usage: "",
}) })
f.StringFlag(&flagset.StringFlag{ f.StringFlag(&flagset.StringFlag{