diff --git a/beacon-chain/sync/checkpoint/BUILD.bazel b/beacon-chain/sync/checkpoint/BUILD.bazel index 1e5527253..4c9f551bf 100644 --- a/beacon-chain/sync/checkpoint/BUILD.bazel +++ b/beacon-chain/sync/checkpoint/BUILD.bazel @@ -4,6 +4,7 @@ go_library( name = "go_default_library", srcs = [ "api.go", + "common.go", "file.go", "log.go", ], @@ -12,7 +13,6 @@ go_library( deps = [ "//api/client/beacon:go_default_library", "//beacon-chain/db:go_default_library", - "//config/params:go_default_library", "//io/file:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", diff --git a/beacon-chain/sync/checkpoint/api.go b/beacon-chain/sync/checkpoint/api.go index 4ed9dc658..d008b0ab9 100644 --- a/beacon-chain/sync/checkpoint/api.go +++ b/beacon-chain/sync/checkpoint/api.go @@ -6,7 +6,6 @@ import ( "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v5/api/client/beacon" "github.com/prysmaticlabs/prysm/v5/beacon-chain/db" - "github.com/prysmaticlabs/prysm/v5/config/params" ) // APIInitializer manages initializing the beacon node using checkpoint sync, retrieving the checkpoint state and root @@ -28,14 +27,12 @@ func NewAPIInitializer(beaconNodeHost string) (*APIInitializer, error) { // 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 { - 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) + exists, err := isCheckpointStatePresent(ctx, d) + if err != nil { + return err + } + if exists { return nil - } else { - if !errors.Is(err, db.ErrNotFound) { - return errors.Wrap(err, "error while checking database for origin root") - } } od, err := beacon.DownloadFinalizedData(ctx, dl.c) if err != nil { diff --git a/beacon-chain/sync/checkpoint/common.go b/beacon-chain/sync/checkpoint/common.go new file mode 100644 index 000000000..d7f22cf1f --- /dev/null +++ b/beacon-chain/sync/checkpoint/common.go @@ -0,0 +1,31 @@ +package checkpoint + +import ( + "context" + + "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/v5/beacon-chain/db" +) + +// Initializer describes a type that is able to obtain the checkpoint sync data (BeaconState and SignedBeaconBlock) +// in some way and perform database setup to prepare the beacon node for syncing from the given checkpoint. +// See FileInitializer and APIInitializer. +type Initializer interface { + Initialize(ctx context.Context, d db.Database) error +} + +// isCheckpointStatePresent checks if the checkpoint and corresponding state exist in the database. +func isCheckpointStatePresent(ctx context.Context, d db.Database) (bool, error) { + origin, err := d.OriginCheckpointBlockRoot(ctx) + if err != nil { + if errors.Is(err, db.ErrNotFound) { + return false, nil + } + return false, errors.Wrap(err, "error while checking database for origin root") + } + // Check corresponding state + if d.HasState(ctx, origin) { + return true, nil + } + return false, nil +} \ No newline at end of file diff --git a/beacon-chain/sync/checkpoint/file.go b/beacon-chain/sync/checkpoint/file.go index ba2ca1dc8..0e9fbeede 100644 --- a/beacon-chain/sync/checkpoint/file.go +++ b/beacon-chain/sync/checkpoint/file.go @@ -7,17 +7,9 @@ import ( "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v5/beacon-chain/db" - "github.com/prysmaticlabs/prysm/v5/config/params" "github.com/prysmaticlabs/prysm/v5/io/file" ) -// Initializer describes a type that is able to obtain the checkpoint sync data (BeaconState and SignedBeaconBlock) -// in some way and perform database setup to prepare the beacon node for syncing from the given checkpoint. -// See FileInitializer and APIInitializer. -type Initializer interface { - Initialize(ctx context.Context, d db.Database) error -} - // NewFileInitializer validates the given path information and creates an Initializer which will // use the provided state and block files to prepare the node for checkpoint sync. func NewFileInitializer(blockPath string, statePath string) (*FileInitializer, error) { @@ -42,14 +34,12 @@ type FileInitializer struct { // Initialize is called in the BeaconNode db startup code if an Initializer is present. // Initialize does what is needed to prepare the beacon node database for syncing from the weak subjectivity checkpoint. func (fi *FileInitializer) Initialize(ctx context.Context, d db.Database) error { - 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) + exists, err := isCheckpointStatePresent(ctx, d) + if err != nil { + return err + } + if exists { return nil - } else { - if !errors.Is(err, db.ErrNotFound) { - return errors.Wrap(err, "error while checking database for origin root") - } } serBlock, err := file.ReadFileAsBytes(fi.blockPath) if err != nil {