2020-09-06 12:35:32 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-31 06:46:00 +03:00
|
|
|
"errors"
|
2020-09-06 12:35:32 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-07-31 06:46:00 +03:00
|
|
|
"path/filepath"
|
2022-08-19 12:20:51 +03:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2020-09-06 12:35:32 +01:00
|
|
|
|
2022-11-14 17:33:57 +01:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/dbg"
|
2023-09-03 02:09:27 +01:00
|
|
|
"github.com/ledgerwatch/erigon/metrics"
|
2022-11-14 17:33:57 +01:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-09-20 16:51:09 +01:00
|
|
|
"github.com/pelletier/go-toml"
|
2022-11-14 17:33:57 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-09-20 16:51:09 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2022-07-31 06:46:00 +03:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
2021-06-11 15:34:37 +07:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2022-01-20 13:04:50 +05:30
|
|
|
erigonapp "github.com/ledgerwatch/erigon/turbo/app"
|
2021-05-26 13:35:39 +03:00
|
|
|
erigoncli "github.com/ledgerwatch/erigon/turbo/cli"
|
2023-05-07 07:28:15 +01:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/debug"
|
2021-05-21 01:25:53 +07:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/node"
|
2020-09-06 12:35:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-04-17 15:22:00 +07:00
|
|
|
defer func() {
|
|
|
|
panicResult := recover()
|
|
|
|
if panicResult == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error("catch panic", "err", panicResult, "stack", dbg.Stack())
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
|
2023-04-27 10:42:12 +07:00
|
|
|
app := erigonapp.MakeApp("erigon", runErigon, erigoncli.DefaultFlags)
|
2020-09-06 12:35:32 +01:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
2022-08-04 14:23:00 +01:00
|
|
|
_, printErr := fmt.Fprintln(os.Stderr, err)
|
|
|
|
if printErr != nil {
|
|
|
|
log.Warn("Fprintln error", "err", printErr)
|
|
|
|
}
|
2020-09-06 12:35:32 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 20:25:01 +01:00
|
|
|
|
2022-11-14 17:33:57 +01:00
|
|
|
func runErigon(cliCtx *cli.Context) error {
|
|
|
|
configFilePath := cliCtx.String(utils.ConfigFlag.Name)
|
2022-07-31 06:46:00 +03:00
|
|
|
if configFilePath != "" {
|
|
|
|
if err := setFlagsFromConfigFile(cliCtx, configFilePath); err != nil {
|
2023-08-22 10:50:31 +02:00
|
|
|
log.Error("failed setting config flags from yaml/toml file", "err", err)
|
|
|
|
return err
|
2022-07-31 06:46:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-07 07:28:15 +01:00
|
|
|
var logger log.Logger
|
|
|
|
var err error
|
|
|
|
if logger, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:04:47 +00:00
|
|
|
// initializing the node and providing the current git commit there
|
2023-06-21 11:13:50 +07:00
|
|
|
|
2023-05-16 10:53:50 +01:00
|
|
|
logger.Info("Build info", "git_branch", params.GitBranch, "git_tag", params.GitTag, "git_commit", params.GitCommit)
|
2023-06-21 11:13:50 +07:00
|
|
|
erigonInfoGauge := metrics.GetOrCreateCounter(fmt.Sprintf(`erigon_info{version="%s",commit="%s"}`, params.Version, params.GitCommit))
|
|
|
|
erigonInfoGauge.Set(1)
|
2022-12-09 11:04:47 +00:00
|
|
|
|
2023-05-16 10:53:50 +01:00
|
|
|
nodeCfg := node.NewNodConfigUrfave(cliCtx, logger)
|
2023-05-17 07:36:06 +01:00
|
|
|
ethCfg := node.NewEthConfigUrfave(cliCtx, nodeCfg, logger)
|
2020-09-06 12:35:32 +01:00
|
|
|
|
2023-05-07 07:28:15 +01:00
|
|
|
ethNode, err := node.New(nodeCfg, ethCfg, logger)
|
2020-09-06 12:35:32 +01:00
|
|
|
if err != nil {
|
2021-07-31 12:24:20 +07:00
|
|
|
log.Error("Erigon startup", "err", err)
|
2022-11-14 17:33:57 +01:00
|
|
|
return err
|
2021-07-31 12:24:20 +07:00
|
|
|
}
|
|
|
|
err = ethNode.Serve()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error while serving an Erigon node", "err", err)
|
2020-09-06 12:35:32 +01:00
|
|
|
}
|
2022-11-14 17:33:57 +01:00
|
|
|
return err
|
2020-09-06 12:35:32 +01:00
|
|
|
}
|
2022-07-31 06:46:00 +03:00
|
|
|
|
|
|
|
func setFlagsFromConfigFile(ctx *cli.Context, filePath string) error {
|
|
|
|
fileExtension := filepath.Ext(filePath)
|
|
|
|
|
2022-08-19 12:20:51 +03:00
|
|
|
fileConfig := make(map[string]interface{})
|
2022-07-31 06:46:00 +03:00
|
|
|
|
|
|
|
if fileExtension == ".yaml" {
|
2022-08-17 07:17:32 +02:00
|
|
|
yamlFile, err := os.ReadFile(filePath)
|
2022-07-31 06:46:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(yamlFile, fileConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if fileExtension == ".toml" {
|
2022-08-17 07:17:32 +02:00
|
|
|
tomlFile, err := os.ReadFile(filePath)
|
2022-07-31 06:46:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = toml.Unmarshal(tomlFile, &fileConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("config files only accepted are .yaml and .toml")
|
|
|
|
}
|
|
|
|
// sets global flags to value in yaml/toml file
|
|
|
|
for key, value := range fileConfig {
|
2022-11-14 17:33:57 +01:00
|
|
|
if !ctx.IsSet(key) {
|
2022-08-19 12:20:51 +03:00
|
|
|
if reflect.ValueOf(value).Kind() == reflect.Slice {
|
|
|
|
sliceInterface := value.([]interface{})
|
|
|
|
s := make([]string, len(sliceInterface))
|
|
|
|
for i, v := range sliceInterface {
|
2022-09-26 05:10:14 +03:00
|
|
|
s[i] = fmt.Sprintf("%v", v)
|
2022-08-19 12:20:51 +03:00
|
|
|
}
|
2022-11-14 17:33:57 +01:00
|
|
|
err := ctx.Set(key, strings.Join(s, ","))
|
2022-08-19 12:20:51 +03:00
|
|
|
if err != nil {
|
2022-09-26 05:10:14 +03:00
|
|
|
return fmt.Errorf("failed setting %s flag with values=%s error=%s", key, s, err)
|
2022-08-19 12:20:51 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-11-14 17:33:57 +01:00
|
|
|
err := ctx.Set(key, fmt.Sprintf("%v", value))
|
2022-08-19 12:20:51 +03:00
|
|
|
if err != nil {
|
2022-09-26 05:10:14 +03:00
|
|
|
return fmt.Errorf("failed setting %s flag with value=%v error=%s", key, value, err)
|
|
|
|
|
2022-08-19 12:20:51 +03:00
|
|
|
}
|
2022-07-31 06:46:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|