Skip to content

Commit

Permalink
devnet: generate node keys for static peers config
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Oct 25, 2023
1 parent 45d2573 commit 4bd59f0
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 75 deletions.
53 changes: 36 additions & 17 deletions cmd/devnet/args/node.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package args

import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/p2p/enode"
"math/big"
"net"
"path/filepath"
Expand Down Expand Up @@ -46,6 +50,9 @@ type Node struct {
HeimdallGRpc string `arg:"--bor.heimdallgRPC" json:"bor.heimdallgRPC,omitempty"`
WithHeimdallMilestones bool `arg:"--bor.milestone" json:"bor.milestone"`
VMDebug bool `arg:"--vmdebug" flag:"" default:"false" json:"dmdebug"`

NodeKey *ecdsa.PrivateKey `arg:"-"`
NodeKeyHex string `arg:"--nodekeyhex" json:"nodekeyhex,omitempty"`
}

func (node *Node) configure(base Node, nodeNumber int) error {
Expand All @@ -63,14 +70,19 @@ func (node *Node) configure(base Node, nodeNumber int) error {

node.StaticPeers = base.StaticPeers

var err error
node.NodeKey, err = crypto.GenerateKey()
if err != nil {
return err
}
node.NodeKeyHex = hex.EncodeToString(crypto.FromECDSA(node.NodeKey))

node.Metrics = base.Metrics
node.MetricsPort = base.MetricsPort
node.MetricsAddr = base.MetricsAddr

node.Snapshots = base.Snapshots

var err error

node.PrivateApiAddr, _, err = portFromBase(base.PrivateApiAddr, nodeNumber, 1)

if err != nil {
Expand All @@ -92,10 +104,19 @@ func (node *Node) configure(base Node, nodeNumber int) error {
return nil
}

func (node Node) ChainID() *big.Int {
func (node *Node) ChainID() *big.Int {
return &big.Int{}
}

func (node *Node) GetHttpPort() int {
return node.HttpPort
}

func (node *Node) GetEnodeURL() string {
port := node.Port
return enode.NewV4(&node.NodeKey.PublicKey, net.ParseIP("127.0.0.1"), port, port).URLv4()
}

type BlockProducer struct {
Node
Mine bool `arg:"--mine" flag:"true"`
Expand All @@ -108,11 +129,10 @@ type BlockProducer struct {
account *accounts.Account
}

func (m BlockProducer) Configure(baseNode Node, nodeNumber int) (int, interface{}, error) {
func (m *BlockProducer) Configure(baseNode Node, nodeNumber int) (interface{}, error) {
err := m.configure(baseNode, nodeNumber)

if err != nil {
return -1, nil, err
return nil, err
}

switch m.Chain {
Expand All @@ -134,18 +154,18 @@ func (m BlockProducer) Configure(baseNode Node, nodeNumber int) (int, interface{
m.Etherbase = m.account.Address.Hex()
}

return m.HttpPort, m, nil
return m, nil
}

func (n BlockProducer) Name() string {
func (n *BlockProducer) Name() string {
return n.Node.Name
}

func (n BlockProducer) Account() *accounts.Account {
func (n *BlockProducer) Account() *accounts.Account {
return n.account
}

func (n BlockProducer) IsBlockProducer() bool {
func (n *BlockProducer) IsBlockProducer() bool {
return true
}

Expand All @@ -156,25 +176,24 @@ type NonBlockProducer struct {
NoDiscover string `arg:"--nodiscover" flag:"" default:"true" json:"nodiscover"`
}

func (n NonBlockProducer) Configure(baseNode Node, nodeNumber int) (int, interface{}, error) {
func (n *NonBlockProducer) Configure(baseNode Node, nodeNumber int) (interface{}, error) {
err := n.configure(baseNode, nodeNumber)

if err != nil {
return -1, nil, err
return nil, err
}

return n.HttpPort, n, nil
return n, nil
}

func (n NonBlockProducer) Name() string {
func (n *NonBlockProducer) Name() string {
return n.Node.Name
}

func (n NonBlockProducer) IsBlockProducer() bool {
func (n *NonBlockProducer) IsBlockProducer() bool {
return false
}

func (n NonBlockProducer) Account() *accounts.Account {
func (n *NonBlockProducer) Account() *accounts.Account {
return nil
}

Expand Down
60 changes: 12 additions & 48 deletions cmd/devnet/devnet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ func (nw *Network) ChainID() *big.Int {

// Start starts the process for multiple erigon nodes running on the dev chain
func (nw *Network) Start(ctx context.Context) error {

type configurable interface {
Configure(baseNode args.Node, nodeNumber int) (int, interface{}, error)
}

for _, service := range nw.Services {
if err := service.Start(ctx); err != nil {
nw.Stop()
Expand Down Expand Up @@ -89,29 +84,33 @@ func (nw *Network) Start(ctx context.Context) error {
metricsNode := cliCtx.Int("metrics.node")
nw.namedNodes = map[string]Node{}

for i, node := range nw.Nodes {
if configurable, ok := node.(configurable); ok {

for i, nodeConfig := range nw.Nodes {
{
base := baseNode

if metricsEnabled && metricsNode == i {
base.Metrics = true
base.MetricsPort = cliCtx.Int("metrics.port")
}
base.StaticPeers = strings.Join(nw.peers, ",")

nodePort, args, err := configurable.Configure(base, i)

if err == nil {
node, err = nw.createNode(fmt.Sprintf("%s:%d", nw.BaseRPCHost, nodePort), args)
argsObj, err := nodeConfig.Configure(base, i)
if err != nil {
nw.Stop()
return err
}

nodePort := nodeConfig.GetHttpPort()
nodeAddr := fmt.Sprintf("%s:%d", nw.BaseRPCHost, nodePort)

node, err := nw.createNode(nodeAddr, argsObj)
if err != nil {
nw.Stop()
return err
}

nw.Nodes[i] = node
nw.namedNodes[node.Name()] = node
nw.peers = append(nw.peers, nodeConfig.GetEnodeURL())

for _, service := range nw.Services {
service.NodeCreated(ctx, node)
Expand All @@ -126,23 +125,6 @@ func (nw *Network) Start(ctx context.Context) error {
return err
}

// get the enode of the node
// - note this has the side effect of waiting for the node to start
enode, err := getEnode(node)
if err != nil {
if errors.Is(err, devnetutils.ErrInvalidEnodeString) {
continue
}

nw.Stop()
return err
}
nw.peers = append(nw.peers, enode)

// TODO do we need to call AddPeer to the nodes to make them aware of this one
// the current model only works for an appending node network where the peers gossip
// connections - not sure if this is the case ?

for _, service := range nw.Services {
service.NodeStarted(ctx, node)
}
Expand Down Expand Up @@ -205,28 +187,10 @@ func (nw *Network) startNode(n Node) error {
node := n.(*node)

args, err := args.AsArgs(node.args)

if err != nil {
return err
}

if len(nw.peers) > 0 {
peersIndex := -1

for i, arg := range args {
if strings.HasPrefix(arg, "--staticpeers") {
peersIndex = i
break
}
}

if peersIndex >= 0 {
args[peersIndex] = args[peersIndex] + "," + strings.Join(nw.peers, ",")
} else {
args = append(args, "--staticpeers="+strings.Join(nw.peers, ","))
}
}

go func() {
nw.Logger.Info("Running node", "name", node.Name(), "args", args)

Expand Down
19 changes: 19 additions & 0 deletions cmd/devnet/devnet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package devnet

import (
"context"
"errors"
"fmt"
"math/big"
"net"
"net/http"
"sync"

Expand All @@ -14,6 +16,7 @@ import (
"github.com/ledgerwatch/erigon/diagnostics"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/node/nodecfg"
p2p_enode "github.com/ledgerwatch/erigon/p2p/enode"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/debug"
enode "github.com/ledgerwatch/erigon/turbo/node"
Expand All @@ -25,8 +28,11 @@ type Node interface {
requests.RequestGenerator
Name() string
ChainID() *big.Int
GetHttpPort() int
GetEnodeURL() string
Account() *accounts.Account
IsBlockProducer() bool
Configure(baseNode args.Node, nodeNumber int) (interface{}, error)
}

type NodeSelector interface {
Expand Down Expand Up @@ -98,6 +104,10 @@ func (n *node) done() {
}
}

func (n *node) Configure(args.Node, int) (interface{}, error) {
return nil, errors.New("N/A")
}

func (n *node) IsBlockProducer() bool {
_, isBlockProducer := n.args.(args.BlockProducer)
return isBlockProducer
Expand Down Expand Up @@ -127,6 +137,15 @@ func (n *node) ChainID() *big.Int {
return nil
}

func (n *node) GetHttpPort() int {
return n.nodeCfg.HTTPPort
}

func (n *node) GetEnodeURL() string {
port := n.nodeCfg.P2P.ListenPort()
return p2p_enode.NewV4(&n.nodeCfg.P2P.PrivateKey.PublicKey, net.ParseIP("127.0.0.1"), port, port).URLv4()
}

// run configures, creates and serves an erigon node
func (n *node) run(ctx *cli.Context) error {
var logger log.Logger
Expand Down
20 changes: 10 additions & 10 deletions cmd/devnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,15 @@ func initDevnet(ctx *cli.Context, logger log.Logger) (devnet.Devnet, error) {
account_services.NewFaucet(networkname.BorDevnetChainName, faucetSource),
},
Nodes: []devnet.Node{
args.BlockProducer{
&args.BlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
WithoutHeimdall: true,
},
AccountSlots: 200,
},
args.NonBlockProducer{
&args.NonBlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
Expand Down Expand Up @@ -404,31 +404,31 @@ func initDevnet(ctx *cli.Context, logger log.Logger) (devnet.Devnet, error) {
faucetSource.Address: {Balance: accounts.EtherAmount(200_000)},
},
Nodes: []devnet.Node{
args.BlockProducer{
&args.BlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
HeimdallGRpc: heimdallGrpc,
},
AccountSlots: 200,
},
args.BlockProducer{
&args.BlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
HeimdallGRpc: heimdallGrpc,
},
AccountSlots: 200,
},
/*args.BlockProducer{
/*&args.BlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
HeimdallGRpc: heimdallGrpc,
},
AccountSlots: 200,
},*/
args.NonBlockProducer{
&args.NonBlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
Expand All @@ -451,7 +451,7 @@ func initDevnet(ctx *cli.Context, logger log.Logger) (devnet.Devnet, error) {
checkpointOwner.Address: {Balance: accounts.EtherAmount(10_000)},
},
Nodes: []devnet.Node{
args.BlockProducer{
&args.BlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
Expand All @@ -461,7 +461,7 @@ func initDevnet(ctx *cli.Context, logger log.Logger) (devnet.Devnet, error) {
DevPeriod: 5,
AccountSlots: 200,
},
args.NonBlockProducer{
&args.NonBlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "3",
Expand All @@ -487,14 +487,14 @@ func initDevnet(ctx *cli.Context, logger log.Logger) (devnet.Devnet, error) {
account_services.NewFaucet(networkname.DevChainName, faucetSource),
},
Nodes: []devnet.Node{
args.BlockProducer{
&args.BlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
},
AccountSlots: 200,
},
args.NonBlockProducer{
&args.NonBlockProducer{
Node: args.Node{
ConsoleVerbosity: "0",
DirVerbosity: "5",
Expand Down
Loading

0 comments on commit 4bd59f0

Please sign in to comment.