mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
2349899658
* Wrap missing state for checkpoint error * terence's review
95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
package kv
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
bolt "go.etcd.io/bbolt"
|
|
"go.opencensus.io/trace"
|
|
)
|
|
|
|
var errMissingStateForCheckpoint = errors.New("missing state summary for checkpoint root")
|
|
|
|
// JustifiedCheckpoint returns the latest justified checkpoint in beacon chain.
|
|
func (s *Store) JustifiedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) {
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.JustifiedCheckpoint")
|
|
defer span.End()
|
|
var checkpoint *ethpb.Checkpoint
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket(checkpointBucket)
|
|
enc := bkt.Get(justifiedCheckpointKey)
|
|
if enc == nil {
|
|
checkpoint = ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
|
|
return nil
|
|
}
|
|
checkpoint = ðpb.Checkpoint{}
|
|
return decode(ctx, enc, checkpoint)
|
|
})
|
|
return checkpoint, err
|
|
}
|
|
|
|
// FinalizedCheckpoint returns the latest finalized checkpoint in beacon chain.
|
|
func (s *Store) FinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) {
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.FinalizedCheckpoint")
|
|
defer span.End()
|
|
var checkpoint *ethpb.Checkpoint
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket(checkpointBucket)
|
|
enc := bkt.Get(finalizedCheckpointKey)
|
|
if enc == nil {
|
|
checkpoint = ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
|
|
return nil
|
|
}
|
|
checkpoint = ðpb.Checkpoint{}
|
|
return decode(ctx, enc, checkpoint)
|
|
})
|
|
return checkpoint, err
|
|
}
|
|
|
|
// SaveJustifiedCheckpoint saves justified checkpoint in beacon chain.
|
|
func (s *Store) SaveJustifiedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error {
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveJustifiedCheckpoint")
|
|
defer span.End()
|
|
|
|
enc, err := encode(ctx, checkpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket(checkpointBucket)
|
|
hasStateSummary := s.hasStateSummaryBytes(tx, bytesutil.ToBytes32(checkpoint.Root))
|
|
hasStateInDB := tx.Bucket(stateBucket).Get(checkpoint.Root) != nil
|
|
if !(hasStateInDB || hasStateSummary) {
|
|
return errors.Wrapf(errMissingStateForCheckpoint, "could not save justified checkpoint, justified root: %#x", bytesutil.Trunc(checkpoint.Root))
|
|
}
|
|
return bucket.Put(justifiedCheckpointKey, enc)
|
|
})
|
|
}
|
|
|
|
// SaveFinalizedCheckpoint saves finalized checkpoint in beacon chain.
|
|
func (s *Store) SaveFinalizedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error {
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveFinalizedCheckpoint")
|
|
defer span.End()
|
|
|
|
enc, err := encode(ctx, checkpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket(checkpointBucket)
|
|
hasStateSummary := s.hasStateSummaryBytes(tx, bytesutil.ToBytes32(checkpoint.Root))
|
|
hasStateInDB := tx.Bucket(stateBucket).Get(checkpoint.Root) != nil
|
|
if !(hasStateInDB || hasStateSummary) {
|
|
return errors.Wrapf(errMissingStateForCheckpoint, "could not save Finalized checkpoint, finalized root: %#x", bytesutil.Trunc(checkpoint.Root))
|
|
}
|
|
if err := bucket.Put(finalizedCheckpointKey, enc); err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.updateFinalizedBlockRoots(ctx, tx, checkpoint)
|
|
})
|
|
}
|