2022-11-02 12:12:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-04 02:02:24 +00:00
|
|
|
"errors"
|
2022-11-20 17:44:42 +00:00
|
|
|
"fmt"
|
2023-05-14 22:12:24 +00:00
|
|
|
"os"
|
|
|
|
|
2023-05-13 21:44:07 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core"
|
|
|
|
rawdb2 "github.com/ledgerwatch/erigon/cl/phase1/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/execution_client"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
|
|
|
|
network2 "github.com/ledgerwatch/erigon/cl/phase1/network"
|
|
|
|
stages2 "github.com/ledgerwatch/erigon/cl/phase1/stages"
|
2022-11-02 12:12:44 +00:00
|
|
|
|
2022-11-28 23:00:40 +00:00
|
|
|
sentinelrpc "github.com/ledgerwatch/erigon-lib/gointerfaces/sentinel"
|
2022-11-26 23:03:58 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2022-11-02 12:12:44 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
|
2023-01-23 23:03:48 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/clparams"
|
2023-03-17 12:37:51 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/clparams/initial_state"
|
2022-11-20 17:44:42 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
2022-12-05 00:25:12 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/fork"
|
2022-12-23 21:31:08 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/rpc"
|
2022-11-20 17:44:42 +00:00
|
|
|
lcCli "github.com/ledgerwatch/erigon/cmd/sentinel/cli"
|
2023-03-17 12:37:51 +00:00
|
|
|
|
2022-11-20 17:44:42 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/cli/flags"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel/service"
|
|
|
|
sentinelapp "github.com/ledgerwatch/erigon/turbo/app"
|
2023-05-07 06:28:15 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/debug"
|
2022-11-02 12:12:44 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-11-20 17:44:42 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-11-02 12:12:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-04-27 03:42:12 +00:00
|
|
|
app := sentinelapp.MakeApp("erigon-cl", runConsensusLayerNode, flags.CLDefaultFlags)
|
2022-11-20 17:44:42 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-11-02 12:12:44 +00:00
|
|
|
|
2022-11-20 17:44:42 +00:00
|
|
|
func runConsensusLayerNode(cliCtx *cli.Context) error {
|
2023-05-07 06:28:15 +00:00
|
|
|
var logger log.Logger
|
|
|
|
var err error
|
|
|
|
if logger, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-02 12:12:44 +00:00
|
|
|
ctx := context.Background()
|
2022-11-28 22:29:48 +00:00
|
|
|
cfg, _ := lcCli.SetupConsensusClientCfg(cliCtx)
|
2022-12-22 18:59:24 +00:00
|
|
|
var db kv.RwDB
|
|
|
|
if cfg.Chaindata == "" {
|
|
|
|
db, err = mdbx.NewTemporaryMdbx()
|
|
|
|
} else {
|
|
|
|
db, err = mdbx.Open(cfg.Chaindata, log.Root(), false)
|
|
|
|
}
|
2022-11-26 23:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error opening database", "err", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
2023-01-04 02:02:24 +00:00
|
|
|
if err := checkAndStoreBeaconDataConfigWithDB(ctx, db, cfg.BeaconDataCfg); err != nil {
|
|
|
|
log.Error("Could load beacon data configuration", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
2023-03-17 12:37:51 +00:00
|
|
|
|
|
|
|
tmpdir := "/tmp"
|
2023-03-27 10:12:31 +00:00
|
|
|
executionClient, err := execution_client.NewExecutionClient(ctx, "127.0.0.1:8989")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Could not connect to execution client", "err", err)
|
|
|
|
return err
|
2023-01-04 02:02:24 +00:00
|
|
|
}
|
2022-11-20 17:44:42 +00:00
|
|
|
|
2023-03-17 12:37:51 +00:00
|
|
|
if cfg.TransitionChain {
|
|
|
|
state, err := initial_state.GetGenesisState(cfg.NetworkType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Execute from genesis to whatever we have.
|
2023-05-13 21:44:07 +00:00
|
|
|
return stages2.SpawnStageBeaconState(stages2.StageBeaconState(db, cfg.BeaconCfg, state, executionClient), nil, ctx)
|
2023-03-17 12:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the checkpoint state.
|
|
|
|
cpState, err := getCheckpointState(ctx, db, cfg.BeaconCfg, cfg.GenesisCfg, cfg.CheckpointUri)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not get checkpoint", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:44:42 +00:00
|
|
|
log.Info("Starting sync from checkpoint.")
|
|
|
|
// Start the sentinel service
|
2022-12-17 15:05:56 +00:00
|
|
|
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(cfg.LogLvl), log.StderrHandler))
|
2022-11-28 22:29:48 +00:00
|
|
|
log.Info("[Sentinel] running sentinel with configuration", "cfg", cfg)
|
2023-05-19 17:41:53 +00:00
|
|
|
s, err := startSentinel(cliCtx, *cfg, cpState, log.Root())
|
2022-11-20 17:44:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not start sentinel service", "err", err)
|
|
|
|
}
|
|
|
|
|
2023-01-04 02:02:24 +00:00
|
|
|
genesisCfg := cfg.GenesisCfg
|
|
|
|
beaconConfig := cfg.BeaconCfg
|
2022-12-23 21:31:08 +00:00
|
|
|
beaconRpc := rpc.NewBeaconRpcP2P(ctx, s, beaconConfig, genesisCfg)
|
2023-05-13 21:44:07 +00:00
|
|
|
downloader := network2.NewForwardBeaconDownloader(ctx, beaconRpc)
|
|
|
|
bdownloader := network2.NewBackwardBeaconDownloader(ctx, beaconRpc)
|
2022-12-17 15:05:56 +00:00
|
|
|
|
2023-04-20 20:47:58 +00:00
|
|
|
forkChoice, err := forkchoice.NewForkChoiceStore(cpState, nil, true)
|
2023-04-17 18:06:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not start forkchoice service", "err", err)
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-13 21:44:07 +00:00
|
|
|
gossipManager := network2.NewGossipReceiver(ctx, s, forkChoice, beaconConfig, genesisCfg)
|
|
|
|
stageloop, err := stages2.NewConsensusStagedSync(ctx, db, downloader, bdownloader, genesisCfg, beaconConfig, cpState,
|
2023-05-07 06:28:15 +00:00
|
|
|
tmpdir, executionClient, cfg.BeaconDataCfg, gossipManager, forkChoice, logger)
|
2022-12-03 02:16:26 +00:00
|
|
|
if err != nil {
|
2022-11-26 23:03:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-12-03 02:16:26 +00:00
|
|
|
Loop:
|
|
|
|
for {
|
2023-05-08 16:52:31 +00:00
|
|
|
if err := stageloop.Run(db, nil, false); err != nil {
|
2022-12-03 02:16:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break Loop
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-11-20 17:44:42 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 17:41:53 +00:00
|
|
|
func startSentinel(cliCtx *cli.Context, cfg lcCli.ConsensusClientCliCfg, beaconState *state.BeaconState, logger log.Logger) (sentinelrpc.SentinelClient, error) {
|
2022-12-05 00:25:12 +00:00
|
|
|
forkDigest, err := fork.ComputeForkDigest(cfg.BeaconCfg, cfg.GenesisCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-20 17:44:42 +00:00
|
|
|
s, err := service.StartSentinelService(&sentinel.SentinelConfig{
|
2022-11-28 22:29:48 +00:00
|
|
|
IpAddr: cfg.Addr,
|
|
|
|
Port: int(cfg.Port),
|
|
|
|
TCPPort: cfg.ServerTcpPort,
|
|
|
|
GenesisConfig: cfg.GenesisCfg,
|
|
|
|
NetworkConfig: cfg.NetworkCfg,
|
|
|
|
BeaconConfig: cfg.BeaconCfg,
|
|
|
|
NoDiscovery: cfg.NoDiscovery,
|
2022-12-05 00:25:12 +00:00
|
|
|
}, nil, &service.ServerConfig{Network: cfg.ServerProtocol, Addr: cfg.ServerAddr}, nil, &cltypes.Status{
|
|
|
|
ForkDigest: forkDigest,
|
2023-05-14 22:12:24 +00:00
|
|
|
FinalizedRoot: beaconState.FinalizedCheckpoint().BlockRoot(),
|
|
|
|
FinalizedEpoch: beaconState.FinalizedCheckpoint().Epoch(),
|
|
|
|
HeadSlot: beaconState.FinalizedCheckpoint().Epoch() * cfg.BeaconCfg.SlotsPerEpoch,
|
|
|
|
HeadRoot: beaconState.FinalizedCheckpoint().BlockRoot(),
|
2023-05-19 17:41:53 +00:00
|
|
|
}, logger)
|
2022-11-20 17:44:42 +00:00
|
|
|
if err != nil {
|
2023-05-19 17:41:53 +00:00
|
|
|
logger.Error("Could not start sentinel", "err", err)
|
2022-11-20 17:44:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-19 17:41:53 +00:00
|
|
|
logger.Info("Sentinel started", "addr", cfg.ServerAddr)
|
2022-11-20 17:44:42 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2023-01-23 23:03:48 +00:00
|
|
|
func getCheckpointState(ctx context.Context, db kv.RwDB, beaconConfig *clparams.BeaconChainConfig, genesisConfig *clparams.GenesisConfig, uri string) (*state.BeaconState, error) {
|
|
|
|
state, err := core.RetrieveBeaconState(ctx, beaconConfig, genesisConfig, uri)
|
2022-11-02 12:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("[Checkpoint Sync] Failed", "reason", err)
|
2022-11-20 17:44:42 +00:00
|
|
|
return nil, err
|
2022-11-02 12:12:44 +00:00
|
|
|
}
|
|
|
|
tx, err := db.BeginRw(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[DB] Failed", "reason", err)
|
2022-11-20 17:44:42 +00:00
|
|
|
return nil, err
|
2022-11-02 12:12:44 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-05-13 21:44:07 +00:00
|
|
|
if err := rawdb2.WriteBeaconState(tx, state); err != nil {
|
2022-11-02 12:12:44 +00:00
|
|
|
log.Error("[DB] Failed", "reason", err)
|
2022-11-20 17:44:42 +00:00
|
|
|
return nil, err
|
2022-11-02 12:12:44 +00:00
|
|
|
}
|
2022-11-20 17:44:42 +00:00
|
|
|
log.Info("Checkpoint sync successful: hurray!")
|
2022-11-26 23:03:58 +00:00
|
|
|
return state, tx.Commit()
|
2022-11-02 12:12:44 +00:00
|
|
|
}
|
2023-01-04 02:02:24 +00:00
|
|
|
|
2023-05-13 21:44:07 +00:00
|
|
|
func checkAndStoreBeaconDataConfigWithDB(ctx context.Context, db kv.RwDB, provided *rawdb2.BeaconDataConfig) error {
|
2023-01-04 02:02:24 +00:00
|
|
|
tx, err := db.BeginRw(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[DB] Failed", "reason", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
if provided == nil {
|
|
|
|
return errors.New("no valid beacon data config found")
|
|
|
|
}
|
2023-05-13 21:44:07 +00:00
|
|
|
stored, err := rawdb2.ReadBeaconDataConfig(tx)
|
2023-01-04 02:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stored != nil {
|
|
|
|
if err := checkBeaconDataConfig(provided, stored); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-05-13 21:44:07 +00:00
|
|
|
return rawdb2.WriteBeaconDataConfig(tx, provided)
|
2023-01-04 02:02:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 21:44:07 +00:00
|
|
|
func checkBeaconDataConfig(provided *rawdb2.BeaconDataConfig, stored *rawdb2.BeaconDataConfig) error {
|
2023-01-04 02:02:24 +00:00
|
|
|
if provided.BackFillingAmount != stored.BackFillingAmount {
|
|
|
|
return fmt.Errorf("mismatching backfilling amount, provided %d, stored %d", provided.BackFillingAmount, stored.BackFillingAmount)
|
|
|
|
}
|
|
|
|
if provided.SlotPerRestorePoint != stored.SlotPerRestorePoint {
|
|
|
|
return fmt.Errorf("mismatching sprp, provided %d, stored %d", provided.SlotPerRestorePoint, stored.SlotPerRestorePoint)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|