Compare commits

...

1 Commits

Author SHA1 Message Date
Zahoor Mohamed
59e97d268e add db functions for last checkpoint 2022-03-11 16:35:21 +05:30
5 changed files with 90 additions and 2 deletions

View File

@@ -25,6 +25,7 @@ go_library(
"state_summary.go",
"state_summary_cache.go",
"utils.go",
"validated_checkpoint.go",
"wss.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db/kv",
@@ -91,6 +92,7 @@ go_test(
"state_summary_test.go",
"state_test.go",
"utils_test.go",
"validated_checkpoint_test.go",
],
data = glob(["testdata/**"]),
embed = [":go_default_library"],

View File

@@ -175,7 +175,7 @@ func NewKVStore(ctx context.Context, dirPath string, config *Config) (*Store, er
powchainBucket,
stateSummaryBucket,
stateValidatorsBucket,
validatedTips,
lastValidatedCheckpoint,
// Indices buckets.
attestationHeadBlockRootBucket,
attestationSourceRootIndicesBucket,

View File

@@ -19,7 +19,7 @@ var (
powchainBucket = []byte("powchain")
stateValidatorsBucket = []byte("state-validators")
feeRecipientBucket = []byte("fee-recipient")
validatedTips = []byte("validated-synced-tips")
lastValidatedCheckpoint = []byte("last-validated-checkpoint")
// Deprecated: This bucket was migrated in PR 6461. Do not use, except for migrations.
slotsHasObjectBucket = []byte("slots-has-objects")

View File

@@ -0,0 +1,47 @@
package kv
import (
"context"
"encoding/binary"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
bolt "go.etcd.io/bbolt"
"go.opencensus.io/trace"
)
func (s *Store) LastValidatedCheckpoint(ctx context.Context) ([32]byte, types.Slot, error) {
ctx, span := trace.StartSpan(ctx, "BeaconDB.LastValidatedCheckpoint")
defer span.End()
var lastChkPoint [32]byte
var slot types.Slot
err := s.db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(lastValidatedCheckpoint)
val := bkt.Get([]byte("lastChkPoint"))
if len(val) != 40 {
return errors.New("invalid checkpoint point")
}
lastChkPoint = bytesutil.ToBytes32(val[:32])
slot = types.Slot(binary.LittleEndian.Uint64(val[32:]))
return nil
})
return lastChkPoint, slot, err
}
func (s *Store) saveLastValidatedCheckpoint(ctx context.Context, checkPoint [32]byte, slot types.Slot) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.saveLastValidatedCheckpoint")
defer span.End()
updateErr := s.db.Update(func(tx *bolt.Tx) error {
value := make([]byte, 40)
copy(value[:32], checkPoint[:])
binary.LittleEndian.PutUint64(value[32:], uint64(slot))
bkt := tx.Bucket(lastValidatedCheckpoint)
err := bkt.Put([]byte("lastChkPoint"), value)
return err
})
return updateErr
}

View File

@@ -0,0 +1,39 @@
package kv
import (
"bytes"
"context"
"testing"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/testing/require"
)
func TestValidateCheckpoint(t *testing.T) {
ctx := context.Background()
db := setupDB(t)
checkpointA := [32]byte{'A'}
slotA := types.Slot(1)
checkpointB := [32]byte{'B'}
slotB := types.Slot(2)
// add first checkpoint
require.NoError(t, db.saveLastValidatedCheckpoint(ctx, checkpointA, slotA))
rcvdRoot, rcvdSlot, err := db.LastValidatedCheckpoint(ctx)
require.NoError(t, err)
require.Equal(t, 0, bytes.Compare(checkpointA[:], rcvdRoot[:]))
require.Equal(t, true, uint64(slotA) == uint64(rcvdSlot))
// update the checkpoint and slot
require.NoError(t, db.saveLastValidatedCheckpoint(ctx, checkpointB, slotB))
rcvdRoot, rcvdSlot, err = db.LastValidatedCheckpoint(ctx)
require.NoError(t, err)
require.Equal(t, 0, bytes.Compare(checkpointB[:], rcvdRoot[:]))
require.Equal(t, true, uint64(slotB) == uint64(rcvdSlot))
}