2022-10-16 17:54:12 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-07-08 14:42:30 +00:00
|
|
|
"errors"
|
2022-10-16 17:54:12 +00:00
|
|
|
"fmt"
|
2023-09-12 05:18:47 +00:00
|
|
|
common2 "github.com/ledgerwatch/erigon-lib/common"
|
2023-07-08 14:42:30 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-06-11 17:34:33 +00:00
|
|
|
"time"
|
2023-05-15 18:07:27 +00:00
|
|
|
|
2023-08-20 13:26:34 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/datadir"
|
2023-05-13 21:44:07 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/rawdb"
|
2023-05-15 18:07:27 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
|
2023-07-08 14:42:30 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2022-10-16 17:54:12 +00:00
|
|
|
|
2023-04-23 03:54:55 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
2022-11-14 16:33:57 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2022-10-29 19:51:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/clparams"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/cli/flags"
|
2023-05-05 11:16:50 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/logging"
|
|
|
|
|
2023-04-17 18:06:50 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-10-16 17:54:12 +00:00
|
|
|
)
|
|
|
|
|
2022-11-28 22:29:48 +00:00
|
|
|
type ConsensusClientCliCfg struct {
|
2023-07-08 14:42:30 +00:00
|
|
|
GenesisCfg *clparams.GenesisConfig
|
|
|
|
BeaconCfg *clparams.BeaconChainConfig
|
|
|
|
NetworkCfg *clparams.NetworkConfig
|
|
|
|
BeaconDataCfg *rawdb.BeaconDataConfig
|
|
|
|
Port uint `json:"port"`
|
|
|
|
Addr string `json:"address"`
|
2023-08-08 23:21:19 +00:00
|
|
|
ServerAddr string `json:"server_addr"`
|
|
|
|
ServerProtocol string `json:"server_protocol"`
|
|
|
|
ServerTcpPort uint `json:"server_tcp_port"`
|
|
|
|
LogLvl uint `json:"log_level"`
|
|
|
|
NoDiscovery bool `json:"no_discovery"`
|
|
|
|
LocalDiscovery bool `json:"local_discovery"`
|
|
|
|
CheckpointUri string `json:"checkpoint_uri"`
|
2023-07-08 14:42:30 +00:00
|
|
|
Chaindata string `json:"chaindata"`
|
2023-08-08 23:21:19 +00:00
|
|
|
ErigonPrivateApi string `json:"erigon_private_api"`
|
|
|
|
TransitionChain bool `json:"transition_chain"`
|
2023-07-08 14:42:30 +00:00
|
|
|
NetworkType clparams.NetworkType
|
2023-08-08 23:21:19 +00:00
|
|
|
InitialSync bool `json:"initial_sync"`
|
|
|
|
NoBeaconApi bool `json:"no_beacon_api"`
|
|
|
|
BeaconApiReadTimeout time.Duration `json:"beacon_api_read_timeout"`
|
|
|
|
BeaconApiWriteTimeout time.Duration `json:"beacon_api_write_timeout"`
|
|
|
|
BeaconAddr string `json:"beacon_addr"`
|
|
|
|
BeaconProtocol string `json:"beacon_protocol"`
|
|
|
|
RecordMode bool `json:"record_mode"`
|
|
|
|
RecordDir string `json:"record_dir"`
|
|
|
|
DataDir string `json:"data_dir"`
|
2023-07-08 14:42:30 +00:00
|
|
|
RunEngineAPI bool `json:"run_engine_api"`
|
|
|
|
EngineAPIAddr string `json:"engine_api_addr"`
|
|
|
|
EngineAPIPort int `json:"engine_api_port"`
|
|
|
|
JwtSecret []byte
|
2023-05-15 18:07:27 +00:00
|
|
|
|
2023-07-19 22:20:33 +00:00
|
|
|
InitalState *state.CachingBeaconState
|
2023-08-20 13:26:34 +00:00
|
|
|
Dirs datadir.Dirs
|
2022-10-16 17:54:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 22:29:48 +00:00
|
|
|
func SetupConsensusClientCfg(ctx *cli.Context) (*ConsensusClientCliCfg, error) {
|
|
|
|
cfg := &ConsensusClientCliCfg{}
|
|
|
|
chainName := ctx.String(flags.Chain.Name)
|
2022-10-17 17:13:23 +00:00
|
|
|
var err error
|
2023-03-17 12:37:51 +00:00
|
|
|
cfg.GenesisCfg, cfg.NetworkCfg, cfg.BeaconCfg, cfg.NetworkType, err = clparams.GetConfigsByNetworkName(chainName)
|
2022-10-17 17:13:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-07 23:06:07 +00:00
|
|
|
cfg.ErigonPrivateApi = ctx.String(flags.ErigonPrivateApiFlag.Name)
|
2023-01-23 23:03:48 +00:00
|
|
|
if ctx.String(flags.BeaconConfigFlag.Name) != "" {
|
|
|
|
cfg.BeaconCfg = new(clparams.BeaconChainConfig)
|
|
|
|
if *cfg.BeaconCfg, err = clparams.CustomConfig(ctx.String(flags.BeaconConfigFlag.Name)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ctx.String(flags.GenesisSSZFlag.Name) == "" {
|
|
|
|
return nil, fmt.Errorf("no genesis file provided")
|
|
|
|
}
|
|
|
|
cfg.GenesisCfg = new(clparams.GenesisConfig)
|
2023-05-15 18:07:27 +00:00
|
|
|
var stateByte []byte
|
2023-01-23 23:03:48 +00:00
|
|
|
// Now parse genesis time and genesis fork
|
2023-05-15 18:07:27 +00:00
|
|
|
if *cfg.GenesisCfg, stateByte, err = clparams.ParseGenesisSSZToGenesisConfig(
|
|
|
|
ctx.String(flags.GenesisSSZFlag.Name),
|
|
|
|
cfg.BeaconCfg.GetCurrentStateVersion(0)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfg.InitalState = state.New(cfg.BeaconCfg)
|
|
|
|
if cfg.InitalState.DecodeSSZ(stateByte, int(cfg.BeaconCfg.GetCurrentStateVersion(0))); err != nil {
|
2023-01-23 23:03:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-11-28 22:29:48 +00:00
|
|
|
cfg.ServerAddr = fmt.Sprintf("%s:%d", ctx.String(flags.SentinelServerAddr.Name), ctx.Int(flags.SentinelServerPort.Name))
|
|
|
|
cfg.ServerProtocol = "tcp"
|
2022-10-16 17:54:12 +00:00
|
|
|
|
2023-06-11 17:34:33 +00:00
|
|
|
cfg.NoBeaconApi = ctx.Bool(flags.NoBeaconApi.Name)
|
|
|
|
cfg.BeaconApiReadTimeout = time.Duration(ctx.Uint64(flags.BeaconApiReadTimeout.Name)) * time.Second
|
|
|
|
cfg.BeaconApiWriteTimeout = time.Duration(ctx.Uint(flags.BeaconApiWriteTimeout.Name)) * time.Second
|
2023-06-08 07:43:27 +00:00
|
|
|
cfg.BeaconAddr = fmt.Sprintf("%s:%d", ctx.String(flags.BeaconApiAddr.Name), ctx.Int(flags.BeaconApiPort.Name))
|
|
|
|
cfg.BeaconProtocol = "tcp"
|
2023-06-04 23:52:55 +00:00
|
|
|
cfg.RecordMode = ctx.Bool(flags.RecordModeFlag.Name)
|
|
|
|
cfg.RecordDir = ctx.String(flags.RecordModeDir.Name)
|
2023-08-08 23:21:19 +00:00
|
|
|
cfg.DataDir = ctx.String(utils.DataDirFlag.Name)
|
2023-08-20 13:26:34 +00:00
|
|
|
cfg.Dirs = datadir.New(cfg.DataDir)
|
2023-06-04 23:52:55 +00:00
|
|
|
|
2023-07-08 14:42:30 +00:00
|
|
|
cfg.RunEngineAPI = ctx.Bool(flags.RunEngineAPI.Name)
|
|
|
|
cfg.EngineAPIAddr = ctx.String(flags.EngineApiHostFlag.Name)
|
|
|
|
cfg.EngineAPIPort = ctx.Int(flags.EngineApiPortFlag.Name)
|
|
|
|
if cfg.RunEngineAPI {
|
|
|
|
secret, err := ObtainJwtSecret(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to obtain jwt secret", "err", err)
|
|
|
|
cfg.RunEngineAPI = false
|
|
|
|
} else {
|
|
|
|
cfg.JwtSecret = secret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 22:29:48 +00:00
|
|
|
cfg.Port = uint(ctx.Int(flags.SentinelDiscoveryPort.Name))
|
|
|
|
cfg.Addr = ctx.String(flags.SentinelDiscoveryAddr.Name)
|
2022-10-16 17:54:12 +00:00
|
|
|
|
2023-05-05 11:16:50 +00:00
|
|
|
cfg.LogLvl = ctx.Uint(logging.LogVerbosityFlag.Name)
|
|
|
|
if cfg.LogLvl == uint(log.LvlInfo) || cfg.LogLvl == 0 {
|
2023-04-17 18:06:50 +00:00
|
|
|
cfg.LogLvl = uint(log.LvlDebug)
|
|
|
|
}
|
2022-11-28 22:29:48 +00:00
|
|
|
cfg.NoDiscovery = ctx.Bool(flags.NoDiscovery.Name)
|
2023-07-16 06:31:06 +00:00
|
|
|
cfg.LocalDiscovery = ctx.Bool(flags.LocalDiscovery.Name)
|
2023-01-23 23:03:48 +00:00
|
|
|
if ctx.String(flags.CheckpointSyncUrlFlag.Name) != "" {
|
|
|
|
cfg.CheckpointUri = ctx.String(flags.CheckpointSyncUrlFlag.Name)
|
|
|
|
} else {
|
2023-03-17 12:37:51 +00:00
|
|
|
cfg.CheckpointUri = clparams.GetCheckpointSyncEndpoint(cfg.NetworkType)
|
2023-01-23 23:03:48 +00:00
|
|
|
}
|
2022-11-16 15:19:40 +00:00
|
|
|
cfg.Chaindata = ctx.String(flags.ChaindataFlag.Name)
|
2023-01-04 02:02:24 +00:00
|
|
|
cfg.BeaconDataCfg = rawdb.BeaconDataConfigurations[ctx.String(flags.BeaconDBModeFlag.Name)]
|
2023-01-23 23:03:48 +00:00
|
|
|
// Process bootnodes
|
|
|
|
if ctx.String(flags.BootnodesFlag.Name) != "" {
|
2023-09-12 05:18:47 +00:00
|
|
|
cfg.NetworkCfg.BootNodes = common2.CliString2Array(ctx.String(flags.BootnodesFlag.Name))
|
2023-01-23 23:03:48 +00:00
|
|
|
}
|
2023-02-15 17:56:10 +00:00
|
|
|
if ctx.String(flags.SentinelStaticPeersFlag.Name) != "" {
|
2023-09-12 05:18:47 +00:00
|
|
|
cfg.NetworkCfg.StaticPeers = common2.CliString2Array(ctx.String(flags.SentinelStaticPeersFlag.Name))
|
2023-05-15 18:07:27 +00:00
|
|
|
fmt.Println(cfg.NetworkCfg.StaticPeers)
|
2023-02-15 17:56:10 +00:00
|
|
|
}
|
2023-03-17 12:37:51 +00:00
|
|
|
cfg.TransitionChain = ctx.Bool(flags.TransitionChainFlag.Name)
|
2023-05-15 18:07:27 +00:00
|
|
|
cfg.InitialSync = ctx.Bool(flags.InitSyncFlag.Name)
|
2022-10-17 17:13:23 +00:00
|
|
|
return cfg, nil
|
2022-10-16 17:54:12 +00:00
|
|
|
}
|
2023-07-08 14:42:30 +00:00
|
|
|
|
|
|
|
func ObtainJwtSecret(ctx *cli.Context) ([]byte, error) {
|
|
|
|
path := ctx.String(flags.JwtSecret.Name)
|
|
|
|
if len(strings.TrimSpace(path)) == 0 {
|
|
|
|
return nil, errors.New("Missing jwt secret path")
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwtSecret := common.FromHex(strings.TrimSpace(string(data)))
|
|
|
|
if len(jwtSecret) == 32 {
|
|
|
|
return jwtSecret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Invalid JWT secret at %s, invalid size", path)
|
|
|
|
}
|