2022-08-31 18:18:35 +00:00
|
|
|
package checkpointsync
|
2022-03-25 17:18:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/api/client"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/api/client/beacon"
|
2022-03-25 17:18:03 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2022-08-31 18:18:35 +00:00
|
|
|
var downloadFlags = struct {
|
2022-03-25 17:18:03 +00:00
|
|
|
BeaconNodeHost string
|
|
|
|
Timeout time.Duration
|
|
|
|
}{}
|
|
|
|
|
2022-08-31 18:18:35 +00:00
|
|
|
var downloadCmd = &cli.Command{
|
|
|
|
Name: "download",
|
|
|
|
Aliases: []string{"dl"},
|
|
|
|
Usage: "Download the latest finalized state and the most recent block it integrates. To be used for checkpoint sync.",
|
2022-12-13 12:13:33 +00:00
|
|
|
Action: func(cliCtx *cli.Context) error {
|
|
|
|
if err := cliActionDownload(cliCtx); err != nil {
|
|
|
|
log.WithError(err).Fatal("Could not download checkpoint-sync data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2022-03-25 17:18:03 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "beacon-node-host",
|
|
|
|
Usage: "host:port for beacon node connection",
|
2022-08-31 18:18:35 +00:00
|
|
|
Destination: &downloadFlags.BeaconNodeHost,
|
2022-03-25 17:18:03 +00:00
|
|
|
Value: "localhost:3500",
|
|
|
|
},
|
|
|
|
&cli.DurationFlag{
|
|
|
|
Name: "http-timeout",
|
|
|
|
Usage: "timeout for http requests made to beacon-node-url (uses duration format, ex: 2m31s). default: 4m",
|
2022-08-31 18:18:35 +00:00
|
|
|
Destination: &downloadFlags.Timeout,
|
2022-03-25 17:18:03 +00:00
|
|
|
Value: time.Minute * 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-31 18:18:35 +00:00
|
|
|
func cliActionDownload(_ *cli.Context) error {
|
2022-03-25 17:18:03 +00:00
|
|
|
ctx := context.Background()
|
2022-08-31 18:18:35 +00:00
|
|
|
f := downloadFlags
|
2022-03-25 17:18:03 +00:00
|
|
|
|
2023-06-06 17:03:30 +00:00
|
|
|
opts := []client.ClientOpt{client.WithTimeout(f.Timeout)}
|
2022-08-31 18:18:35 +00:00
|
|
|
client, err := beacon.NewClient(downloadFlags.BeaconNodeHost, opts...)
|
2022-03-25 17:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-25 22:52:43 +00:00
|
|
|
od, err := beacon.DownloadFinalizedData(ctx, client)
|
2022-03-25 17:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blockPath, err := od.SaveBlock(cwd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-18 22:01:27 +00:00
|
|
|
log.Printf("saved ssz-encoded block to %s", blockPath)
|
2022-03-25 17:18:03 +00:00
|
|
|
|
|
|
|
statePath, err := od.SaveState(cwd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-18 22:01:27 +00:00
|
|
|
log.Printf("saved ssz-encoded state to %s", statePath)
|
2022-03-25 17:18:03 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|