Skip to content

Commit

Permalink
Add robust db check for checkpoint sync #3
Browse files Browse the repository at this point in the history
  • Loading branch information
syjn99 committed Sep 24, 2024
1 parent f4a305c commit 8b8e69b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/sync/checkpoint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = [
"api.go",
"common.go",
"file.go",
"log.go",
],
Expand All @@ -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",
Expand Down
13 changes: 5 additions & 8 deletions beacon-chain/sync/checkpoint/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
31 changes: 31 additions & 0 deletions beacon-chain/sync/checkpoint/common.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 5 additions & 15 deletions beacon-chain/sync/checkpoint/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 8b8e69b

Please sign in to comment.