2022-03-28 21:01:55 +00:00
|
|
|
package checkpoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/api/client/beacon"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/db"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
2022-06-14 23:23:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-03-28 21:01:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// APIInitializer manages initializing the beacon node using checkpoint sync, retrieving the checkpoint state and root
|
|
|
|
// from the remote beacon node api.
|
|
|
|
type APIInitializer struct {
|
|
|
|
c *beacon.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPIInitializer creates an APIInitializer, handling the set up of a beacon node api client
|
|
|
|
// using the provided host string.
|
|
|
|
func NewAPIInitializer(beaconNodeHost string) (*APIInitializer, error) {
|
|
|
|
c, err := beacon.NewClient(beaconNodeHost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "unable to parse beacon node url or hostname - %s", beaconNodeHost)
|
|
|
|
}
|
|
|
|
return &APIInitializer{c: c}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize downloads origin state and block for checkpoint sync and initializes database records to
|
|
|
|
// prepare the node to begin syncing from that point.
|
|
|
|
func (dl *APIInitializer) Initialize(ctx context.Context, d db.Database) error {
|
2022-06-14 23:23:25 +00:00
|
|
|
origin, err := d.OriginCheckpointBlockRoot(ctx)
|
|
|
|
if err == nil && origin != params.BeaconConfig().ZeroHash {
|
|
|
|
log.Warnf("origin checkpoint root %#x found in db, ignoring checkpoint sync flags", origin)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
if !errors.Is(err, db.ErrNotFound) {
|
|
|
|
return errors.Wrap(err, "error while checking database for origin root")
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 22:52:43 +00:00
|
|
|
od, err := beacon.DownloadFinalizedData(ctx, dl.c)
|
2022-03-28 21:01:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error retrieving checkpoint origin state and block")
|
|
|
|
}
|
|
|
|
return d.SaveOrigin(ctx, od.StateBytes(), od.BlockBytes())
|
|
|
|
}
|