2019-08-30 13:58:02 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-24 05:50:33 +00:00
|
|
|
"errors"
|
2019-08-30 13:58:02 +00:00
|
|
|
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-03-30 22:10:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-03-16 19:07:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2019-10-24 05:50:33 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/traceutil"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
var errMissingStateForCheckpoint = errors.New("missing state summary for finalized 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.
|
|
|
|
func (k *Store) JustifiedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.JustifiedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
var checkpoint *ethpb.Checkpoint
|
|
|
|
err := k.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(checkpointBucket)
|
|
|
|
enc := bkt.Get(justifiedCheckpointKey)
|
|
|
|
if enc == nil {
|
2019-08-30 17:03:55 +00:00
|
|
|
blockBucket := tx.Bucket(blocksBucket)
|
|
|
|
genesisRoot := blockBucket.Get(genesisBlockRootKey)
|
|
|
|
checkpoint = ðpb.Checkpoint{Root: genesisRoot}
|
2019-08-30 13:58:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
checkpoint = ðpb.Checkpoint{}
|
2019-11-27 06:32:56 +00:00
|
|
|
return decode(enc, checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
})
|
|
|
|
return checkpoint, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FinalizedCheckpoint returns the latest finalized checkpoint in beacon chain.
|
|
|
|
func (k *Store) FinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.FinalizedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
var checkpoint *ethpb.Checkpoint
|
|
|
|
err := k.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(checkpointBucket)
|
|
|
|
enc := bkt.Get(finalizedCheckpointKey)
|
|
|
|
if enc == nil {
|
2019-08-30 17:03:55 +00:00
|
|
|
blockBucket := tx.Bucket(blocksBucket)
|
|
|
|
genesisRoot := blockBucket.Get(genesisBlockRootKey)
|
|
|
|
checkpoint = ðpb.Checkpoint{Root: genesisRoot}
|
2019-08-30 13:58:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
checkpoint = ðpb.Checkpoint{}
|
2019-11-27 06:32:56 +00:00
|
|
|
return decode(enc, checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
})
|
|
|
|
return checkpoint, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveJustifiedCheckpoint saves justified checkpoint in beacon chain.
|
|
|
|
func (k *Store) SaveJustifiedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveJustifiedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-11-27 06:32:56 +00:00
|
|
|
enc, err := encode(checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(checkpointBucket)
|
2020-04-21 22:30:22 +00:00
|
|
|
if featureconfig.Get().NewStateMgmt {
|
2020-04-14 20:27:03 +00:00
|
|
|
hasStateSummaryInDB := tx.Bucket(stateSummaryBucket).Get(checkpoint.Root) != nil
|
|
|
|
hasStateSummaryInCache := k.stateSummaryCache.Has(bytesutil.ToBytes32(checkpoint.Root))
|
|
|
|
hasStateInDB := tx.Bucket(stateBucket).Get(checkpoint.Root) != nil
|
|
|
|
if !(hasStateInDB || hasStateSummaryInDB || hasStateSummaryInCache) {
|
2020-03-16 19:07:07 +00:00
|
|
|
return errors.New("missing state summary for finalized root")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The corresponding state must exist or there is a risk that the beacondb enters a state
|
|
|
|
// where the justified beaconState is missing. This may be a fatal condition requiring
|
|
|
|
// a new sync from genesis.
|
|
|
|
if tx.Bucket(stateBucket).Get(checkpoint.Root) == nil {
|
|
|
|
traceutil.AnnotateError(span, errMissingStateForCheckpoint)
|
|
|
|
return errMissingStateForCheckpoint
|
|
|
|
}
|
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.
|
|
|
|
func (k *Store) SaveFinalizedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveFinalizedCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-11-27 06:32:56 +00:00
|
|
|
enc, err := encode(checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return k.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(checkpointBucket)
|
2020-04-21 22:30:22 +00:00
|
|
|
if featureconfig.Get().NewStateMgmt {
|
2020-04-14 20:27:03 +00:00
|
|
|
hasStateSummaryInDB := tx.Bucket(stateSummaryBucket).Get(checkpoint.Root) != nil
|
|
|
|
hasStateSummaryInCache := k.stateSummaryCache.Has(bytesutil.ToBytes32(checkpoint.Root))
|
|
|
|
hasStateInDB := tx.Bucket(stateBucket).Get(checkpoint.Root) != nil
|
|
|
|
if !(hasStateInDB || hasStateSummaryInDB || hasStateSummaryInCache) {
|
2020-03-16 19:07:07 +00:00
|
|
|
return errors.New("missing state summary for finalized root")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The corresponding state must exist or there is a risk that the beacondb enters a state
|
|
|
|
// where the finalized beaconState is missing. This would be a fatal condition requiring
|
|
|
|
// a new sync from genesis.
|
|
|
|
if tx.Bucket(stateBucket).Get(checkpoint.Root) == nil {
|
|
|
|
traceutil.AnnotateError(span, errMissingStateForCheckpoint)
|
|
|
|
return errMissingStateForCheckpoint
|
|
|
|
}
|
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
|
|
|
|
2019-11-08 03:00:47 +00:00
|
|
|
return k.updateFinalizedBlockRoots(ctx, tx, checkpoint)
|
2019-08-30 13:58:02 +00:00
|
|
|
})
|
|
|
|
}
|