mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
178 lines
4.4 KiB
Go
178 lines
4.4 KiB
Go
// Package beacon-chain defines all the utilities needed for a beacon chain node.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
runtimeDebug "runtime/debug"
|
|
|
|
golog "github.com/ipfs/go-log"
|
|
joonix "github.com/joonix/log"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/flags"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/node"
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
|
"github.com/prysmaticlabs/prysm/shared/debug"
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
"github.com/prysmaticlabs/prysm/shared/logutil"
|
|
"github.com/prysmaticlabs/prysm/shared/version"
|
|
"github.com/sirupsen/logrus"
|
|
"gopkg.in/urfave/cli.v2"
|
|
gologging "github.com/whyrusleeping/go-logging"
|
|
prefixed "github.com/x-cray/logrus-prefixed-formatter"
|
|
_ "go.uber.org/automaxprocs"
|
|
"gopkg.in/urfave/cli.v2/altsrc"
|
|
)
|
|
|
|
var appFlags = []cli.Flag{
|
|
flags.DepositContractFlag,
|
|
flags.Web3ProviderFlag,
|
|
flags.HTTPWeb3ProviderFlag,
|
|
flags.RPCHost,
|
|
flags.RPCPort,
|
|
flags.CertFlag,
|
|
flags.KeyFlag,
|
|
flags.GRPCGatewayPort,
|
|
flags.MinSyncPeers,
|
|
flags.RPCMaxPageSize,
|
|
flags.ContractDeploymentBlock,
|
|
flags.SetGCPercent,
|
|
flags.UnsafeSync,
|
|
flags.InteropMockEth1DataVotesFlag,
|
|
flags.InteropGenesisStateFlag,
|
|
flags.InteropNumValidatorsFlag,
|
|
flags.InteropGenesisTimeFlag,
|
|
flags.ArchiveEnableFlag,
|
|
flags.ArchiveValidatorSetChangesFlag,
|
|
flags.ArchiveBlocksFlag,
|
|
flags.ArchiveAttestationsFlag,
|
|
flags.SlotsPerArchivedPoint,
|
|
cmd.BootstrapNode,
|
|
cmd.NoDiscovery,
|
|
cmd.StaticPeers,
|
|
cmd.RelayNode,
|
|
cmd.P2PUDPPort,
|
|
cmd.P2PTCPPort,
|
|
cmd.P2PIP,
|
|
cmd.P2PHost,
|
|
cmd.P2PHostDNS,
|
|
cmd.P2PMaxPeers,
|
|
cmd.P2PPrivKey,
|
|
cmd.P2PWhitelist,
|
|
cmd.P2PEncoding,
|
|
cmd.DataDirFlag,
|
|
cmd.VerbosityFlag,
|
|
cmd.EnableTracingFlag,
|
|
cmd.TracingProcessNameFlag,
|
|
cmd.TracingEndpointFlag,
|
|
cmd.TraceSampleFractionFlag,
|
|
cmd.MonitoringPortFlag,
|
|
cmd.DisableMonitoringFlag,
|
|
cmd.ClearDB,
|
|
cmd.ForceClearDB,
|
|
cmd.LogFormat,
|
|
cmd.MaxGoroutines,
|
|
debug.PProfFlag,
|
|
debug.PProfAddrFlag,
|
|
debug.PProfPortFlag,
|
|
debug.MemProfileRateFlag,
|
|
debug.CPUProfileFlag,
|
|
debug.TraceFlag,
|
|
cmd.LogFileName,
|
|
cmd.EnableUPnPFlag,
|
|
cmd.ConfigFileFlag,
|
|
}
|
|
|
|
func init() {
|
|
appFlags = cmd.WrapFlags(append(appFlags, featureconfig.BeaconChainFlags...))
|
|
}
|
|
|
|
func main() {
|
|
log := logrus.WithField("prefix", "main")
|
|
app := cli.App{}
|
|
app.Name = "beacon-chain"
|
|
app.Usage = "this is a beacon chain implementation for Ethereum 2.0"
|
|
app.Action = startNode
|
|
app.Version = version.GetVersion()
|
|
|
|
app.Flags = appFlags
|
|
|
|
app.Before = func(ctx *cli.Context) error {
|
|
// Load any flags from file, if specified.
|
|
if ctx.IsSet(cmd.ConfigFileFlag.Name) {
|
|
if err := altsrc.InitInputSourceWithContext(appFlags, altsrc.NewYamlSourceFromFlagFunc(cmd.ConfigFileFlag.Name))(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
format := ctx.String(cmd.LogFormat.Name)
|
|
switch format {
|
|
case "text":
|
|
formatter := new(prefixed.TextFormatter)
|
|
formatter.TimestampFormat = "2006-01-02 15:04:05"
|
|
formatter.FullTimestamp = true
|
|
// If persistent log files are written - we disable the log messages coloring because
|
|
// the colors are ANSI codes and seen as gibberish in the log files.
|
|
formatter.DisableColors = ctx.String(cmd.LogFileName.Name) != ""
|
|
logrus.SetFormatter(formatter)
|
|
break
|
|
case "fluentd":
|
|
f := joonix.NewFormatter()
|
|
if err := joonix.DisableTimestampFormat(f); err != nil {
|
|
panic(err)
|
|
}
|
|
logrus.SetFormatter(f)
|
|
break
|
|
case "json":
|
|
logrus.SetFormatter(&logrus.JSONFormatter{})
|
|
break
|
|
default:
|
|
return fmt.Errorf("unknown log format %s", format)
|
|
}
|
|
|
|
logFileName := ctx.String(cmd.LogFileName.Name)
|
|
if logFileName != "" {
|
|
if err := logutil.ConfigurePersistentLogging(logFileName); err != nil {
|
|
log.WithError(err).Error("Failed to configuring logging to disk.")
|
|
}
|
|
}
|
|
|
|
if ctx.IsSet(flags.SetGCPercent.Name) {
|
|
runtimeDebug.SetGCPercent(ctx.Int(flags.SetGCPercent.Name))
|
|
}
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
return debug.Setup(ctx)
|
|
}
|
|
|
|
defer func() {
|
|
if x := recover(); x != nil {
|
|
log.Errorf("Runtime panic: %v\n%v", x, string(runtimeDebug.Stack()))
|
|
panic(x)
|
|
}
|
|
}()
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
log.Error(err.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func startNode(ctx *cli.Context) error {
|
|
verbosity := ctx.String(cmd.VerbosityFlag.Name)
|
|
level, err := logrus.ParseLevel(verbosity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
logrus.SetLevel(level)
|
|
if level == logrus.TraceLevel {
|
|
golog.SetAllLoggers(gologging.DEBUG)
|
|
}
|
|
|
|
beacon, err := node.NewBeaconNode(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
beacon.Start()
|
|
return nil
|
|
}
|