2022-09-25 19:01:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 Erigon-Lightclient contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-23 23:53:27 +00:00
|
|
|
package main
|
|
|
|
|
2022-10-12 23:05:01 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2022-10-16 17:54:12 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-10-12 23:05:01 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/lightclient/lightclient"
|
2022-10-13 16:26:29 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/service"
|
2022-10-16 17:54:12 +00:00
|
|
|
lcCli "github.com/ledgerwatch/erigon/cmd/sentinel_node/cli"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel_node/cli/flags"
|
|
|
|
lightclientapp "github.com/ledgerwatch/erigon/turbo/app"
|
2022-10-12 23:05:01 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-10-16 17:54:12 +00:00
|
|
|
"github.com/urfave/cli"
|
2022-10-13 16:26:29 +00:00
|
|
|
)
|
|
|
|
|
2022-10-12 23:05:01 +00:00
|
|
|
const DefaultUri = "https://beaconstate.ethstaker.cc/eth/v2/debug/beacon/states/finalized"
|
|
|
|
|
2022-09-23 23:53:27 +00:00
|
|
|
func main() {
|
2022-10-16 17:54:12 +00:00
|
|
|
app := lightclientapp.MakeApp(runLightClientNode, flags.LightClientDefaultFlags)
|
|
|
|
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-10-14 18:32:33 +00:00
|
|
|
|
2022-10-16 17:54:12 +00:00
|
|
|
func runLightClientNode(cliCtx *cli.Context) {
|
2022-10-13 16:26:29 +00:00
|
|
|
ctx := context.Background()
|
2022-10-16 17:54:12 +00:00
|
|
|
lcCfg, chain := lcCli.SetUpLightClientCfg(cliCtx)
|
|
|
|
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(lcCfg.LogLvl), log.StderrHandler))
|
|
|
|
log.Info("[LC]", "chain", chain)
|
|
|
|
log.Info("[LC] Running lightclient", "cfg", lcCfg)
|
2022-10-13 16:26:29 +00:00
|
|
|
sentinel, err := service.StartSentinelService(&sentinel.SentinelConfig{
|
2022-10-16 17:54:12 +00:00
|
|
|
IpAddr: lcCfg.Addr,
|
|
|
|
Port: int(lcCfg.Port),
|
|
|
|
TCPPort: lcCfg.ServerTcpPort,
|
|
|
|
GenesisConfig: lcCfg.GenesisCfg,
|
|
|
|
NetworkConfig: lcCfg.NetworkCfg,
|
|
|
|
BeaconConfig: lcCfg.BeaconCfg,
|
|
|
|
IsDiscoverable: lcCfg.IsDiscoverable,
|
|
|
|
}, &service.ServerConfig{Network: lcCfg.ServerProtocol, Addr: lcCfg.ServerAddr})
|
2022-10-13 16:26:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not start sentinel", "err", err)
|
|
|
|
}
|
2022-10-16 17:54:12 +00:00
|
|
|
log.Info("Sentinel started", "addr", lcCfg.ServerAddr)
|
2022-10-13 16:26:29 +00:00
|
|
|
|
|
|
|
bs, err := lightclient.RetrieveBeaconState(ctx, DefaultUri)
|
2022-10-04 20:03:30 +00:00
|
|
|
|
2022-10-12 23:05:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("[Checkpoint Sync] Failed", "reason", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-13 16:26:29 +00:00
|
|
|
log.Info("Finalized Checkpoint", "Epoch", bs.FinalizedCheckpoint.Epoch)
|
2022-10-16 17:54:12 +00:00
|
|
|
lc, err := lightclient.NewLightClient(ctx, lcCfg.GenesisCfg, lcCfg.BeaconCfg, nil, sentinel)
|
2022-10-15 21:01:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not make Lightclient", "err", err)
|
|
|
|
}
|
2022-10-13 16:26:29 +00:00
|
|
|
if err := lc.BootstrapCheckpoint(ctx, bs.FinalizedCheckpoint.Root); err != nil {
|
|
|
|
log.Error("[Bootstrap] failed to bootstrap", "err", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-15 21:01:52 +00:00
|
|
|
lc.Start()
|
2022-10-04 20:03:30 +00:00
|
|
|
}
|