2022-03-15 20:52:59 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-08-03 16:49:11 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
2022-03-15 20:52:59 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LastValidatedCheckpoint returns the latest fully validated checkpoint in beacon chain.
|
|
|
|
func (s *Store) LastValidatedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.LastValidatedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
var checkpoint *ethpb.Checkpoint
|
|
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(checkpointBucket)
|
|
|
|
enc := bkt.Get(lastValidatedCheckpointKey)
|
|
|
|
if enc == nil {
|
|
|
|
var finErr error
|
|
|
|
checkpoint, finErr = s.FinalizedCheckpoint(ctx)
|
|
|
|
return finErr
|
|
|
|
}
|
|
|
|
checkpoint = ðpb.Checkpoint{}
|
|
|
|
return decode(ctx, enc, checkpoint)
|
|
|
|
})
|
|
|
|
return checkpoint, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveLastValidatedCheckpoint saves the last validated checkpoint in beacon chain.
|
|
|
|
func (s *Store) SaveLastValidatedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveLastValidatedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
enc, err := encode(ctx, checkpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-08 19:32:04 +00:00
|
|
|
hasStateSummary := s.HasStateSummary(ctx, bytesutil.ToBytes32(checkpoint.Root))
|
2022-03-15 20:52:59 +00:00
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(checkpointBucket)
|
|
|
|
hasStateInDB := tx.Bucket(stateBucket).Get(checkpoint.Root) != nil
|
|
|
|
if !(hasStateInDB || hasStateSummary) {
|
2022-08-04 13:01:07 +00:00
|
|
|
log.Warnf("Recovering state summary for last validated root: %#x", bytesutil.Trunc(checkpoint.Root))
|
|
|
|
if err := recoverStateSummary(ctx, tx, checkpoint.Root); err != nil {
|
|
|
|
return errors.Wrapf(errMissingStateForCheckpoint, "could not save finalized checkpoint, last validated root: %#x", bytesutil.Trunc(checkpoint.Root))
|
|
|
|
}
|
2022-03-15 20:52:59 +00:00
|
|
|
}
|
|
|
|
return bucket.Put(lastValidatedCheckpointKey, enc)
|
|
|
|
})
|
|
|
|
}
|