2019-08-30 13:58:02 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
2019-10-24 05:50:33 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-08-08 18:39:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-08-30 13:58:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_JustifiedCheckpoint_CanSaveRetrieve(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
ctx := context.Background()
|
2020-01-06 14:41:51 +00:00
|
|
|
root := bytesutil.ToBytes32([]byte{'A'})
|
2019-08-30 13:58:02 +00:00
|
|
|
cp := ðpb.Checkpoint{
|
|
|
|
Epoch: 10,
|
2020-01-06 14:41:51 +00:00
|
|
|
Root: root[:],
|
|
|
|
}
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, st.SetSlot(1))
|
|
|
|
require.NoError(t, db.SaveState(ctx, st, root))
|
|
|
|
require.NoError(t, db.SaveJustifiedCheckpoint(ctx, cp))
|
2019-08-30 13:58:02 +00:00
|
|
|
|
2019-08-30 17:03:55 +00:00
|
|
|
retrieved, err := db.JustifiedCheckpoint(ctx)
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, proto.Equal(cp, retrieved), "Wanted %v, received %v", cp, retrieved)
|
2019-08-30 13:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_FinalizedCheckpoint_CanSaveRetrieve(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
ctx := context.Background()
|
2019-10-29 15:14:17 +00:00
|
|
|
|
|
|
|
genesis := bytesutil.ToBytes32([]byte{'G', 'E', 'N', 'E', 'S', 'I', 'S'})
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesis))
|
2019-10-29 15:14:17 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
blk := testutil.NewBeaconBlock()
|
|
|
|
blk.Block.ParentRoot = genesis[:]
|
|
|
|
blk.Block.Slot = 40
|
2019-10-29 15:14:17 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
root, err := blk.Block.HashTreeRoot()
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-29 15:14:17 +00:00
|
|
|
|
2019-08-30 13:58:02 +00:00
|
|
|
cp := ðpb.Checkpoint{
|
|
|
|
Epoch: 5,
|
2019-10-24 05:50:33 +00:00
|
|
|
Root: root[:],
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:14:17 +00:00
|
|
|
// a valid chain is required to save finalized checkpoint.
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, db.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(blk)))
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, st.SetSlot(1))
|
2019-10-24 05:50:33 +00:00
|
|
|
// a state is required to save checkpoint
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, db.SaveState(ctx, st, root))
|
2019-08-30 13:58:02 +00:00
|
|
|
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, db.SaveFinalizedCheckpoint(ctx, cp))
|
2019-08-30 17:03:55 +00:00
|
|
|
|
2019-08-30 13:58:02 +00:00
|
|
|
retrieved, err := db.FinalizedCheckpoint(ctx)
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, proto.Equal(cp, retrieved), "Wanted %v, received %v", cp, retrieved)
|
2019-08-30 17:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 03:34:17 +00:00
|
|
|
func TestStore_JustifiedCheckpoint_DefaultIsZeroHash(t *testing.T) {
|
2019-08-30 17:03:55 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-08-04 03:34:17 +00:00
|
|
|
cp := ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
|
2019-08-30 17:03:55 +00:00
|
|
|
retrieved, err := db.JustifiedCheckpoint(ctx)
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, proto.Equal(cp, retrieved), "Wanted %v, received %v", cp, retrieved)
|
2019-08-30 17:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 03:34:17 +00:00
|
|
|
func TestStore_FinalizedCheckpoint_DefaultIsZeroHash(t *testing.T) {
|
2019-08-30 17:03:55 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-08-04 03:34:17 +00:00
|
|
|
cp := ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
|
2019-08-30 17:03:55 +00:00
|
|
|
retrieved, err := db.FinalizedCheckpoint(ctx)
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, proto.Equal(cp, retrieved), "Wanted %v, received %v", cp, retrieved)
|
2019-08-30 13:58:02 +00:00
|
|
|
}
|
2019-10-24 05:50:33 +00:00
|
|
|
|
|
|
|
func TestStore_FinalizedCheckpoint_StateMustExist(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
cp := ðpb.Checkpoint{
|
|
|
|
Epoch: 5,
|
|
|
|
Root: []byte{'B'},
|
|
|
|
}
|
|
|
|
|
2020-08-08 18:39:01 +00:00
|
|
|
require.ErrorContains(t, errMissingStateForCheckpoint.Error(), db.SaveFinalizedCheckpoint(ctx, cp))
|
2019-10-24 05:50:33 +00:00
|
|
|
}
|