2021-05-26 10:35:39 +00:00
|
|
|
// Package node contains classes for running a Erigon node.
|
2020-09-05 16:07:27 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2022-07-28 09:57:38 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2022-11-14 16:33:57 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/eth"
|
|
|
|
"github.com/ledgerwatch/erigon/eth/ethconfig"
|
|
|
|
"github.com/ledgerwatch/erigon/node"
|
2022-07-28 09:57:38 +00:00
|
|
|
"github.com/ledgerwatch/erigon/node/nodecfg"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2021-12-14 10:13:17 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params/networkname"
|
2021-05-26 10:35:39 +00:00
|
|
|
erigoncli "github.com/ledgerwatch/erigon/turbo/cli"
|
2020-09-05 16:07:27 +00:00
|
|
|
)
|
|
|
|
|
2021-05-26 10:35:39 +00:00
|
|
|
// ErigonNode represents a single node, that runs sync and p2p network.
|
2020-09-21 14:10:25 +00:00
|
|
|
// it also can export the private endpoint for RPC daemon, etc.
|
2021-05-26 10:35:39 +00:00
|
|
|
type ErigonNode struct {
|
2020-09-05 16:07:27 +00:00
|
|
|
stack *node.Node
|
|
|
|
backend *eth.Ethereum
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:10:25 +00:00
|
|
|
// Serve runs the node and blocks the execution. It returns when the node is existed.
|
2021-05-26 10:35:39 +00:00
|
|
|
func (eri *ErigonNode) Serve() error {
|
|
|
|
defer eri.stack.Close()
|
2020-09-05 16:07:27 +00:00
|
|
|
|
2021-05-26 10:35:39 +00:00
|
|
|
eri.run()
|
2020-09-05 16:07:27 +00:00
|
|
|
|
2021-05-26 10:35:39 +00:00
|
|
|
eri.stack.Wait()
|
2020-09-05 16:07:27 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 10:35:39 +00:00
|
|
|
func (eri *ErigonNode) run() {
|
|
|
|
utils.StartNode(eri.stack)
|
2020-09-05 16:07:27 +00:00
|
|
|
// we don't have accounts locally and we don't do mining
|
|
|
|
// so these parts are ignored
|
2022-05-26 12:08:25 +00:00
|
|
|
// see cmd/geth/daemon.go#startNode for full implementation
|
2020-09-05 16:07:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 14:10:25 +00:00
|
|
|
// Params contains optional parameters for creating a node.
|
|
|
|
// * GitCommit is a commit from which then node was built.
|
2021-07-28 03:43:51 +00:00
|
|
|
// * CustomBuckets is a `map[string]dbutils.TableCfgItem`, that contains bucket name and its properties.
|
2020-09-21 14:10:25 +00:00
|
|
|
//
|
|
|
|
// NB: You have to declare your custom buckets here to be able to use them in the app.
|
2020-09-06 11:35:32 +00:00
|
|
|
type Params struct {
|
2021-07-28 02:47:38 +00:00
|
|
|
CustomBuckets kv.TableCfg
|
2020-09-06 11:35:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 05:27:44 +00:00
|
|
|
func NewNodConfigUrfave(ctx *cli.Context) *nodecfg.Config {
|
2021-07-04 07:50:32 +00:00
|
|
|
// If we're running a known preset, log it for convenience.
|
2022-11-14 16:33:57 +00:00
|
|
|
chain := ctx.String(utils.ChainFlag.Name)
|
2021-07-04 07:50:32 +00:00
|
|
|
switch chain {
|
2022-02-10 07:27:36 +00:00
|
|
|
case networkname.SepoliaChainName:
|
|
|
|
log.Info("Starting Erigon on Sepolia testnet...")
|
2021-12-14 10:13:17 +00:00
|
|
|
case networkname.RinkebyChainName:
|
2021-07-04 07:50:32 +00:00
|
|
|
log.Info("Starting Erigon on Rinkeby testnet...")
|
2021-12-14 10:13:17 +00:00
|
|
|
case networkname.GoerliChainName:
|
2021-07-04 07:50:32 +00:00
|
|
|
log.Info("Starting Erigon on Görli testnet...")
|
2022-01-14 19:06:35 +00:00
|
|
|
case networkname.BSCChainName:
|
2021-12-16 07:26:44 +00:00
|
|
|
log.Info("Starting Erigon on BSC mainnet...")
|
2022-01-14 19:06:35 +00:00
|
|
|
case networkname.ChapelChainName:
|
|
|
|
log.Info("Starting Erigon on Chapel testnet...")
|
|
|
|
case networkname.RialtoChainName:
|
|
|
|
log.Info("Starting Erigon on Chapel testnet...")
|
|
|
|
case networkname.DevChainName:
|
2022-03-10 05:13:55 +00:00
|
|
|
log.Info("Starting Erigon in ephemeral dev mode...")
|
2022-06-10 08:32:04 +00:00
|
|
|
case networkname.MumbaiChainName:
|
|
|
|
log.Info("Starting Erigon on Mumbai testnet...")
|
2022-02-07 21:30:46 +00:00
|
|
|
case networkname.BorMainnetChainName:
|
2022-06-10 08:32:04 +00:00
|
|
|
log.Info("Starting Erigon on Bor Mainnet...")
|
|
|
|
case networkname.BorDevnetChainName:
|
|
|
|
log.Info("Starting Erigon on Bor Devnet...")
|
2021-12-14 10:13:17 +00:00
|
|
|
case "", networkname.MainnetChainName:
|
2022-11-14 16:33:57 +00:00
|
|
|
if !ctx.IsSet(utils.NetworkIdFlag.Name) {
|
2021-07-04 07:50:32 +00:00
|
|
|
log.Info("Starting Erigon on Ethereum mainnet...")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Info("Starting Erigon on", "devnet", chain)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeConfig := NewNodeConfig()
|
|
|
|
utils.SetNodeConfig(ctx, nodeConfig)
|
|
|
|
erigoncli.ApplyFlagsForNodeConfig(ctx, nodeConfig)
|
|
|
|
return nodeConfig
|
|
|
|
}
|
2022-05-26 05:27:44 +00:00
|
|
|
func NewEthConfigUrfave(ctx *cli.Context, nodeConfig *nodecfg.Config) *ethconfig.Config {
|
2021-07-04 07:50:32 +00:00
|
|
|
ethConfig := ðconfig.Defaults
|
|
|
|
utils.SetEthConfig(ctx, nodeConfig, ethConfig)
|
|
|
|
erigoncli.ApplyFlagsForEthConfig(ctx, ethConfig)
|
2022-05-26 05:27:44 +00:00
|
|
|
|
2021-07-04 07:50:32 +00:00
|
|
|
return ethConfig
|
|
|
|
}
|
|
|
|
|
2021-05-26 10:35:39 +00:00
|
|
|
// New creates a new `ErigonNode`.
|
2020-09-21 14:10:25 +00:00
|
|
|
// * 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.
|
2020-09-06 15:33:05 +00:00
|
|
|
func New(
|
2022-05-26 05:27:44 +00:00
|
|
|
nodeConfig *nodecfg.Config,
|
2021-07-04 07:50:32 +00:00
|
|
|
ethConfig *ethconfig.Config,
|
2021-07-28 02:47:38 +00:00
|
|
|
logger log.Logger,
|
2021-07-31 05:24:20 +00:00
|
|
|
) (*ErigonNode, error) {
|
2021-07-04 07:50:32 +00:00
|
|
|
//prepareBuckets(optionalParams.CustomBuckets)
|
2022-08-12 09:13:14 +00:00
|
|
|
node, err := node.New(nodeConfig)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to create Erigon node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ethereum, err := eth.New(node, ethConfig, logger)
|
2021-07-31 05:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-17 06:20:31 +00:00
|
|
|
err = ethereum.Init(node, ethConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-31 05:24:20 +00:00
|
|
|
return &ErigonNode{stack: node, backend: ethereum}, nil
|
2020-09-05 16:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 05:27:44 +00:00
|
|
|
func NewNodeConfig() *nodecfg.Config {
|
|
|
|
nodeConfig := nodecfg.DefaultConfig
|
2020-09-05 16:07:27 +00:00
|
|
|
// see simiar changes in `cmd/geth/config.go#defaultNodeConfig`
|
2021-06-11 08:34:37 +00:00
|
|
|
if commit := params.GitCommit; commit != "" {
|
2020-09-11 16:42:33 +00:00
|
|
|
nodeConfig.Version = params.VersionWithCommit(commit, "")
|
2020-09-06 11:35:32 +00:00
|
|
|
} else {
|
|
|
|
nodeConfig.Version = params.Version
|
|
|
|
}
|
2021-09-08 08:25:10 +00:00
|
|
|
nodeConfig.IPCPath = "" // force-disable IPC endpoint
|
2021-05-26 10:35:39 +00:00
|
|
|
nodeConfig.Name = "erigon"
|
2020-09-05 16:07:27 +00:00
|
|
|
return &nodeConfig
|
|
|
|
}
|