erigon-pulse/turbo/node/node.go
Mark Holt f110102023
Devnet scenarios (#7723)
This is an update to the devnet code which introduces the concept of
configurable scenarios. This replaces the previous hard coded execution
function.

The intention is that now both the network and the operations to run on
the network can be described in a data structure which is configurable
and composable.

The operating model is to create a network and then ask it to run
scenarios:

```go
network.Run(
		runCtx,
		scenarios.Scenario{
			Name: "all",
			Steps: []*scenarios.Step{
				&scenarios.Step{Text: "InitSubscriptions", Args: []any{[]requests.SubMethod{requests.Methods.ETHNewHeads}}},
				&scenarios.Step{Text: "PingErigonRpc"},
				&scenarios.Step{Text: "CheckTxPoolContent", Args: []any{0, 0, 0}},
				&scenarios.Step{Text: "SendTxWithDynamicFee", Args: []any{recipientAddress, services.DevAddress, sendValue}},
				&scenarios.Step{Text: "AwaitBlocks", Args: []any{2 * time.Second}},
			},
		})
```
The steps here refer to step handlers which can be defined as follows:

```go
func init() {
	scenarios.MustRegisterStepHandlers(
		scenarios.StepHandler(GetBalance),
	)
}

func GetBalance(ctx context.Context, addr string, blockNum requests.BlockNumber, checkBal uint64) {
...
```
This commit is an initial implementation of the scenario running - which
is working, but will need to be enhanced to make it more usable &
developable.

The current version of the code is working and has been tested with the
dev network, and bor withoutheimdall. There is a multi miner bor
heimdall configuration but this is yet to be tested.

Note that by default the scenario runner picks nodes at random on the
network to send transactions to. this causes the dev network to run very
slowly as it seems to take a long time to include transactions where the
nonce is incremented across nodes. It seems to take a long time for the
nonce to catch up in the transaction pool processing. This is yet to be
investigated.
2023-06-14 12:35:22 +01:00

132 lines
4.1 KiB
Go

// Package node contains classes for running a Erigon node.
package node
import (
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/log/v3"
"github.com/urfave/cli/v2"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/eth"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/node"
"github.com/ledgerwatch/erigon/node/nodecfg"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/params/networkname"
erigoncli "github.com/ledgerwatch/erigon/turbo/cli"
)
// ErigonNode represents a single node, that runs sync and p2p network.
// it also can export the private endpoint for RPC daemon, etc.
type ErigonNode struct {
stack *node.Node
backend *eth.Ethereum
}
// Serve runs the node and blocks the execution. It returns when the node is existed.
func (eri *ErigonNode) Serve() error {
defer eri.Close()
eri.run()
eri.stack.Wait()
return nil
}
func (eri *ErigonNode) Close() {
eri.stack.Close()
}
func (eri *ErigonNode) run() {
node.StartNode(eri.stack)
// we don't have accounts locally and we don't do mining
// so these parts are ignored
// see cmd/geth/daemon.go#startNode for full implementation
}
// Params contains optional parameters for creating a node.
// * GitCommit is a commit from which then node was built.
// * CustomBuckets is a `map[string]dbutils.TableCfgItem`, that contains bucket name and its properties.
//
// NB: You have to declare your custom buckets here to be able to use them in the app.
type Params struct {
CustomBuckets kv.TableCfg
}
func NewNodConfigUrfave(ctx *cli.Context, logger log.Logger) *nodecfg.Config {
// If we're running a known preset, log it for convenience.
chain := ctx.String(utils.ChainFlag.Name)
switch chain {
case networkname.SepoliaChainName:
logger.Info("Starting Erigon on Sepolia testnet...")
case networkname.GoerliChainName:
logger.Info("Starting Erigon on Görli testnet...")
case networkname.DevChainName:
logger.Info("Starting Erigon in ephemeral dev mode...")
case networkname.MumbaiChainName:
logger.Info("Starting Erigon on Mumbai testnet...")
case networkname.BorMainnetChainName:
logger.Info("Starting Erigon on Bor Mainnet...")
case networkname.BorDevnetChainName:
logger.Info("Starting Erigon on Bor Devnet...")
case "", networkname.MainnetChainName:
if !ctx.IsSet(utils.NetworkIdFlag.Name) {
logger.Info("Starting Erigon on Ethereum mainnet...")
}
default:
logger.Info("Starting Erigon on", "devnet", chain)
}
nodeConfig := NewNodeConfig()
utils.SetNodeConfig(ctx, nodeConfig, logger)
erigoncli.ApplyFlagsForNodeConfig(ctx, nodeConfig, logger)
return nodeConfig
}
func NewEthConfigUrfave(ctx *cli.Context, nodeConfig *nodecfg.Config, logger log.Logger) *ethconfig.Config {
ethConfig := &ethconfig.Defaults
utils.SetEthConfig(ctx, nodeConfig, ethConfig, logger)
erigoncli.ApplyFlagsForEthConfig(ctx, ethConfig, logger)
return ethConfig
}
// New creates a new `ErigonNode`.
// * ctx - `*cli.Context` from the main function. Necessary to be able to configure the node based on the command-line flags
// * sync - `stagedsync.StagedSync`, an instance of staged sync, setup just as needed.
// * optionalParams - additional parameters for running a node.
func New(
nodeConfig *nodecfg.Config,
ethConfig *ethconfig.Config,
logger log.Logger,
) (*ErigonNode, error) {
//prepareBuckets(optionalParams.CustomBuckets)
node, err := node.New(nodeConfig, logger)
if err != nil {
utils.Fatalf("Failed to create Erigon node: %v", err)
}
ethereum, err := eth.New(node, ethConfig, logger)
if err != nil {
return nil, err
}
err = ethereum.Init(node, ethConfig)
if err != nil {
return nil, err
}
return &ErigonNode{stack: node, backend: ethereum}, nil
}
func NewNodeConfig() *nodecfg.Config {
nodeConfig := nodecfg.DefaultConfig
// see simiar changes in `cmd/geth/config.go#defaultNodeConfig`
if commit := params.GitCommit; commit != "" {
nodeConfig.Version = params.VersionWithCommit(commit)
} else {
nodeConfig.Version = params.Version
}
nodeConfig.IPCPath = "" // force-disable IPC endpoint
nodeConfig.Name = "erigon"
return &nodeConfig
}