prysm-pulse/slasher/db/kv/block_header_test.go
Preston Van Loon 7cc32c4dda
Various code inspection resolutions (#7438)
* remove unused code

* remove defer use in loop

* Remove unused methods and constants

* gofmt and gaz

* nilness check

* remove unused args

* Add TODO for refactoring subscribeWithBase to remove unused arg. It seems too involved to include in this sweeping PR. https://github.com/prysmaticlabs/prysm/issues/7437

* replace empty slice declaration

* Remove unnecessary type conversions

* remove redundant type declaration

* rename receivers to be consistent

* Remove bootnode query tool. It is now obsolete by discv5

* Remove relay node. It is no longer used or supported

* Revert "Remove relay node. It is no longer used or supported"

This reverts commit 4bd7717334dad85ef4766ed9bc4da711fb5fa810.

* Delete unused test directory

* Delete unsupported gcp startup script

* Delete old k8s script

* build fixes

* fix build

* go mod tidy

* revert slasher/db/kv/block_header.go

* fix build

* remove redundant nil check

* combine func args

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
2020-10-12 08:11:05 +00:00

182 lines
6.8 KiB
Go

package kv
import (
"context"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestNilDBHistoryBlkHdr(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
slot := uint64(1)
validatorID := uint64(1)
require.Equal(t, false, db.HasBlockHeader(ctx, slot, validatorID))
bPrime, err := db.BlockHeaders(ctx, slot, validatorID)
require.NoError(t, err, "Failed to get block")
require.DeepEqual(t, ([]*ethpb.SignedBeaconBlockHeader)(nil), bPrime, "Should return nil for a non existent key")
}
func TestSaveHistoryBlkHdr(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
tests := []struct {
bh *ethpb.SignedBeaconBlockHeader
}{
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 2nd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 1, ProposerIndex: 0}},
},
}
for _, tt := range tests {
err := db.SaveBlockHeader(ctx, tt.bh)
require.NoError(t, err, "Save block failed")
bha, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
require.NoError(t, err, "Failed to get block")
require.NotNil(t, bha)
require.DeepEqual(t, tt.bh, bha[0], "Should return bh")
}
}
func TestDeleteHistoryBlkHdr(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
tests := []struct {
bh *ethpb.SignedBeaconBlockHeader
}{
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 2nd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}},
},
}
for _, tt := range tests {
err := db.SaveBlockHeader(ctx, tt.bh)
require.NoError(t, err, "Save block failed")
}
for _, tt := range tests {
bha, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
require.NoError(t, err, "Failed to get block")
require.NotNil(t, bha)
require.DeepEqual(t, tt.bh, bha[0], "Should return bh")
err = db.DeleteBlockHeader(ctx, tt.bh)
require.NoError(t, err, "Save block failed")
bh, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
require.NoError(t, err)
assert.DeepEqual(t, ([]*ethpb.SignedBeaconBlockHeader)(nil), bh, "Expected block to have been deleted")
}
}
func TestHasHistoryBlkHdr(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
tests := []struct {
bh *ethpb.SignedBeaconBlockHeader
}{
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 2nd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 4th"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 1, ProposerIndex: 0}},
},
}
for _, tt := range tests {
found := db.HasBlockHeader(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
require.Equal(t, false, found, "Has block header should return false for block headers that are not in db")
err := db.SaveBlockHeader(ctx, tt.bh)
require.NoError(t, err, "Save block failed")
}
for _, tt := range tests {
err := db.SaveBlockHeader(ctx, tt.bh)
require.NoError(t, err, "Save block failed")
found := db.HasBlockHeader(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
require.Equal(t, true, found, "Block header should exist")
}
}
func TestPruneHistoryBlkHdr(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
tests := []struct {
bh *ethpb.SignedBeaconBlockHeader
}{
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 2nd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 3rd"), 96), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 4th"), 96), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch*2 + 1, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: bytesutil.PadTo([]byte("let me in 5th"), 96), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch*3 + 1, ProposerIndex: 0}},
},
}
for _, tt := range tests {
err := db.SaveBlockHeader(ctx, tt.bh)
require.NoError(t, err, "Save block header failed")
bha, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
require.NoError(t, err, "Failed to get block header")
require.NotNil(t, bha)
require.DeepEqual(t, tt.bh, bha[0], "Should return bh")
}
currentEpoch := uint64(3)
historyToKeep := uint64(2)
err := db.PruneBlockHistory(ctx, currentEpoch, historyToKeep)
require.NoError(t, err, "Failed to prune")
for _, tt := range tests {
bha, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
require.NoError(t, err, "Failed to get block header")
if helpers.SlotToEpoch(tt.bh.Header.Slot) >= currentEpoch-historyToKeep {
require.NotNil(t, bha)
require.DeepEqual(t, tt.bh, bha[0], "Should return bh")
} else {
require.Equal(t, 0, len(bha), "Block header should have been pruned")
require.DeepEqual(t, []*ethpb.SignedBeaconBlockHeader(nil), bha, "Block header should have been pruned")
}
}
}