2019-08-30 13:58:02 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-04 13:01:07 +00:00
|
|
|
"fmt"
|
2019-08-30 13:58:02 +00:00
|
|
|
|
2022-08-03 16:49:11 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2019-08-30 13:58:02 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2022-08-03 16:49:11 +00:00
|
|
|
var errMissingStateForCheckpoint = errors.New("missing state summary for checkpoint root")
|
2019-10-24 05:50:33 +00:00
|
|
|
|
2019-08-30 13:58:02 +00:00
|
|
|
// JustifiedCheckpoint returns the latest justified checkpoint in beacon chain.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) JustifiedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) {
|
2019-08-30 13:58:02 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.JustifiedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
var checkpoint *ethpb.Checkpoint
|
2020-10-12 08:11:05 +00:00
|
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
2019-08-30 13:58:02 +00:00
|
|
|
bkt := tx.Bucket(checkpointBucket)
|
|
|
|
enc := bkt.Get(justifiedCheckpointKey)
|
|
|
|
if enc == nil {
|
2020-08-04 03:34:17 +00:00
|
|
|
checkpoint = ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
|
2019-08-30 13:58:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
checkpoint = ðpb.Checkpoint{}
|
2020-07-01 04:00:49 +00:00
|
|
|
return decode(ctx, enc, checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
})
|
|
|
|
return checkpoint, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FinalizedCheckpoint returns the latest finalized checkpoint in beacon chain.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) FinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) {
|
2019-08-30 13:58:02 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.FinalizedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
var checkpoint *ethpb.Checkpoint
|
2020-10-12 08:11:05 +00:00
|
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
2019-08-30 13:58:02 +00:00
|
|
|
bkt := tx.Bucket(checkpointBucket)
|
|
|
|
enc := bkt.Get(finalizedCheckpointKey)
|
|
|
|
if enc == nil {
|
2020-08-04 03:34:17 +00:00
|
|
|
checkpoint = ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
|
2019-08-30 13:58:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
checkpoint = ðpb.Checkpoint{}
|
2020-07-01 04:00:49 +00:00
|
|
|
return decode(ctx, enc, checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
})
|
|
|
|
return checkpoint, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveJustifiedCheckpoint saves justified checkpoint in beacon chain.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) SaveJustifiedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error {
|
2019-08-30 13:58:02 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveJustifiedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-07-01 04:00:49 +00:00
|
|
|
enc, err := encode(ctx, checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-08 19:32:04 +00:00
|
|
|
hasStateSummary := s.HasStateSummary(ctx, bytesutil.ToBytes32(checkpoint.Root))
|
2020-10-12 08:11:05 +00:00
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
2019-08-30 13:58:02 +00:00
|
|
|
bucket := tx.Bucket(checkpointBucket)
|
2020-06-23 21:36:21 +00:00
|
|
|
hasStateInDB := tx.Bucket(stateBucket).Get(checkpoint.Root) != nil
|
2021-08-19 21:42:30 +00:00
|
|
|
if !(hasStateInDB || hasStateSummary) {
|
2022-08-04 13:01:07 +00:00
|
|
|
log.Warnf("Recovering state summary for justified root: %#x", bytesutil.Trunc(checkpoint.Root))
|
|
|
|
if err := recoverStateSummary(ctx, tx, checkpoint.Root); err != nil {
|
|
|
|
return errors.Wrapf(errMissingStateForCheckpoint, "could not save justified checkpoint, finalized root: %#x", bytesutil.Trunc(checkpoint.Root))
|
|
|
|
}
|
2020-01-06 14:41:51 +00:00
|
|
|
}
|
2019-08-30 13:58:02 +00:00
|
|
|
return bucket.Put(justifiedCheckpointKey, enc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveFinalizedCheckpoint saves finalized checkpoint in beacon chain.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Store) SaveFinalizedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error {
|
2019-08-30 13:58:02 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveFinalizedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-07-01 04:00:49 +00:00
|
|
|
enc, err := encode(ctx, checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-08 19:32:04 +00:00
|
|
|
hasStateSummary := s.HasStateSummary(ctx, bytesutil.ToBytes32(checkpoint.Root))
|
2020-10-12 08:11:05 +00:00
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
2019-08-30 13:58:02 +00:00
|
|
|
bucket := tx.Bucket(checkpointBucket)
|
2020-06-23 21:36:21 +00:00
|
|
|
hasStateInDB := tx.Bucket(stateBucket).Get(checkpoint.Root) != nil
|
2021-08-19 21:42:30 +00:00
|
|
|
if !(hasStateInDB || hasStateSummary) {
|
2022-08-04 13:01:07 +00:00
|
|
|
log.Warnf("Recovering state summary for finalized root: %#x", bytesutil.Trunc(checkpoint.Root))
|
|
|
|
if err := recoverStateSummary(ctx, tx, checkpoint.Root); err != nil {
|
|
|
|
return errors.Wrapf(errMissingStateForCheckpoint, "could not save finalized checkpoint, finalized root: %#x", bytesutil.Trunc(checkpoint.Root))
|
|
|
|
}
|
2019-10-24 05:50:33 +00:00
|
|
|
}
|
2019-10-29 15:14:17 +00:00
|
|
|
if err := bucket.Put(finalizedCheckpointKey, enc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-30 18:04:10 +00:00
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
return s.updateFinalizedBlockRoots(ctx, tx, checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
})
|
|
|
|
}
|
2022-08-04 13:01:07 +00:00
|
|
|
|
|
|
|
// Recovers and saves state summary for a given root if the root has a block in the DB.
|
|
|
|
func recoverStateSummary(ctx context.Context, tx *bolt.Tx, root []byte) error {
|
|
|
|
blkBucket := tx.Bucket(blocksBucket)
|
|
|
|
blkEnc := blkBucket.Get(root)
|
|
|
|
if blkEnc == nil {
|
|
|
|
return fmt.Errorf("nil block, root: %#x", bytesutil.Trunc(root))
|
|
|
|
}
|
|
|
|
blk, err := unmarshalBlock(ctx, blkEnc)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Could not unmarshal block: %#x", bytesutil.Trunc(root))
|
|
|
|
}
|
|
|
|
summaryEnc, err := encode(ctx, ðpb.StateSummary{
|
|
|
|
Slot: blk.Block().Slot(),
|
|
|
|
Root: root,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
summaryBucket := tx.Bucket(stateBucket)
|
|
|
|
return summaryBucket.Put(root, summaryEnc)
|
|
|
|
}
|