mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-03 09:37:38 +00:00
8ea0096d56
This is a non functional change which consolidates the various packages under metrics into the top level package now that the dead code is removed. It is a precursor to the removal of Victoria metrics after which all erigon metrics code will be contained in this single package.
134 lines
3.4 KiB
Go
134 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/common/dbg"
|
|
"github.com/ledgerwatch/erigon/metrics"
|
|
"github.com/ledgerwatch/log/v3"
|
|
"github.com/pelletier/go-toml"
|
|
"github.com/urfave/cli/v2"
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
"github.com/ledgerwatch/erigon/params"
|
|
erigonapp "github.com/ledgerwatch/erigon/turbo/app"
|
|
erigoncli "github.com/ledgerwatch/erigon/turbo/cli"
|
|
"github.com/ledgerwatch/erigon/turbo/debug"
|
|
"github.com/ledgerwatch/erigon/turbo/node"
|
|
)
|
|
|
|
func main() {
|
|
defer func() {
|
|
panicResult := recover()
|
|
if panicResult == nil {
|
|
return
|
|
}
|
|
|
|
log.Error("catch panic", "err", panicResult, "stack", dbg.Stack())
|
|
os.Exit(1)
|
|
}()
|
|
|
|
app := erigonapp.MakeApp("erigon", runErigon, erigoncli.DefaultFlags)
|
|
if err := app.Run(os.Args); err != nil {
|
|
_, printErr := fmt.Fprintln(os.Stderr, err)
|
|
if printErr != nil {
|
|
log.Warn("Fprintln error", "err", printErr)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func runErigon(cliCtx *cli.Context) error {
|
|
configFilePath := cliCtx.String(utils.ConfigFlag.Name)
|
|
if configFilePath != "" {
|
|
if err := setFlagsFromConfigFile(cliCtx, configFilePath); err != nil {
|
|
log.Error("failed setting config flags from yaml/toml file", "err", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
var logger log.Logger
|
|
var err error
|
|
if logger, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
|
|
return err
|
|
}
|
|
|
|
// initializing the node and providing the current git commit there
|
|
|
|
logger.Info("Build info", "git_branch", params.GitBranch, "git_tag", params.GitTag, "git_commit", params.GitCommit)
|
|
erigonInfoGauge := metrics.GetOrCreateCounter(fmt.Sprintf(`erigon_info{version="%s",commit="%s"}`, params.Version, params.GitCommit))
|
|
erigonInfoGauge.Set(1)
|
|
|
|
nodeCfg := node.NewNodConfigUrfave(cliCtx, logger)
|
|
ethCfg := node.NewEthConfigUrfave(cliCtx, nodeCfg, logger)
|
|
|
|
ethNode, err := node.New(nodeCfg, ethCfg, logger)
|
|
if err != nil {
|
|
log.Error("Erigon startup", "err", err)
|
|
return err
|
|
}
|
|
err = ethNode.Serve()
|
|
if err != nil {
|
|
log.Error("error while serving an Erigon node", "err", err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func setFlagsFromConfigFile(ctx *cli.Context, filePath string) error {
|
|
fileExtension := filepath.Ext(filePath)
|
|
|
|
fileConfig := make(map[string]interface{})
|
|
|
|
if fileExtension == ".yaml" {
|
|
yamlFile, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = yaml.Unmarshal(yamlFile, fileConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if fileExtension == ".toml" {
|
|
tomlFile, err := os.ReadFile(filePath)
|
|
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 {
|
|
if !ctx.IsSet(key) {
|
|
if reflect.ValueOf(value).Kind() == reflect.Slice {
|
|
sliceInterface := value.([]interface{})
|
|
s := make([]string, len(sliceInterface))
|
|
for i, v := range sliceInterface {
|
|
s[i] = fmt.Sprintf("%v", v)
|
|
}
|
|
err := ctx.Set(key, strings.Join(s, ","))
|
|
if err != nil {
|
|
return fmt.Errorf("failed setting %s flag with values=%s error=%s", key, s, err)
|
|
}
|
|
} else {
|
|
err := ctx.Set(key, fmt.Sprintf("%v", value))
|
|
if err != nil {
|
|
return fmt.Errorf("failed setting %s flag with value=%v error=%s", key, value, err)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|