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 (
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2022-01-14 19:06:35 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
|
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"
|
|
|
|
"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
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// see cmd/geth/main.go#startNode for full implementation
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-04 07:50:32 +00:00
|
|
|
func NewNodConfigUrfave(ctx *cli.Context) *node.Config {
|
|
|
|
// If we're running a known preset, log it for convenience.
|
|
|
|
chain := ctx.GlobalString(utils.ChainFlag.Name)
|
|
|
|
switch chain {
|
2021-12-14 10:13:17 +00:00
|
|
|
case networkname.RopstenChainName:
|
2021-07-04 07:50:32 +00:00
|
|
|
log.Info("Starting Erigon on Ropsten 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:
|
|
|
|
log.Info("Starting Erigon in ephemerasl dev mode...")
|
2021-12-14 10:13:17 +00:00
|
|
|
case "", networkname.MainnetChainName:
|
2021-07-04 07:50:32 +00:00
|
|
|
if !ctx.GlobalIsSet(utils.NetworkIdFlag.Name) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
func NewEthConfigUrfave(ctx *cli.Context, nodeConfig *node.Config) *ethconfig.Config {
|
|
|
|
ethConfig := ðconfig.Defaults
|
|
|
|
utils.SetEthConfig(ctx, nodeConfig, ethConfig)
|
|
|
|
erigoncli.ApplyFlagsForEthConfig(ctx, ethConfig)
|
|
|
|
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(
|
2021-07-04 07:50:32 +00:00
|
|
|
nodeConfig *node.Config,
|
|
|
|
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)
|
2020-09-05 16:07:27 +00:00
|
|
|
node := makeConfigNode(nodeConfig)
|
2021-07-31 05:24:20 +00:00
|
|
|
ethereum, err := RegisterEthService(node, ethConfig, logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ErigonNode{stack: node, backend: ethereum}, nil
|
2020-09-05 16:07:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 04:20:50 +00:00
|
|
|
// RegisterEthService adds an Ethereum client to the stack.
|
2021-07-31 05:24:20 +00:00
|
|
|
func RegisterEthService(stack *node.Node, cfg *ethconfig.Config, logger log.Logger) (*eth.Ethereum, error) {
|
|
|
|
return eth.New(stack, cfg, logger)
|
2021-04-25 04:20:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 08:34:37 +00:00
|
|
|
func NewNodeConfig() *node.Config {
|
2020-09-05 16:07:27 +00:00
|
|
|
nodeConfig := node.DefaultConfig
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-09-07 06:44:40 +00:00
|
|
|
func MakeConfigNodeDefault() *node.Node {
|
|
|
|
return makeConfigNode(NewNodeConfig())
|
|
|
|
}
|
|
|
|
|
2020-09-05 16:07:27 +00:00
|
|
|
func makeConfigNode(config *node.Config) *node.Node {
|
|
|
|
stack, err := node.New(config)
|
|
|
|
if err != nil {
|
2021-05-26 10:35:39 +00:00
|
|
|
utils.Fatalf("Failed to create Erigon node: %v", err)
|
2020-09-05 16:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stack
|
|
|
|
}
|