prysm-pulse/slasher/db/kv/block_header_test.go
Preston Van Loon 49a0d3caf0
Refactor dependencies, make Prysm "go gettable" (#6053)
* Fix a few deps to work with go.mod, check in generated files

* Update Gossipsub to 1.1 (#5998)

* update libs

* add new validators

* add new deps

* new set of deps

* tls

* further fix gossip update

* get everything to build

* clean up

* gaz

* fix build

* fix all tests

* add deps to images

* imports

Co-authored-by: rauljordan <raul@prysmaticlabs.com>

* Beacon chain builds with go build

* fix bazel

* fix dep

* lint

* Add github action for testing go

* on PR for any branch

* fix libp2p test failure

* Fix TestProcessBlock_PassesProcessingConditions by updating the proposer index in test

* Revert "Fix TestProcessBlock_PassesProcessingConditions by updating the proposer index in test"

This reverts commit 43676894ab01f03fe90a9b8ee3ecfbc2ec1ec4e4.

* Compute and set proposer index instead of hard code

* Add back go mod/sum, fix deps

* go build ./...

* Temporarily skip two tests

* Fix kafka confluent patch

* Fix kafka confluent patch

* fix kafka build

* fix kafka

* Add info in DEPENDENCIES. Added a stub link for Why Bazel? until https://github.com/prysmaticlabs/documentation/issues/138

* Update fuzz ssz files as well

* Update fuzz ssz files as well

* getting closer

* rollback rules_go and gazelle

* fix gogo protobuf

* install librdkafka-dev as part of github actions

* Update kafka to a recent version where librkafkfa is not required for go modules

* clarify comment

* fix kafka build

* disable go tests

* comment

* Fix geth dependencies for end to end

* rename word

* lint

* fix docker

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: rauljordan <raul@prysmaticlabs.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-05-31 14:44:34 +08:00

243 lines
7.0 KiB
Go

package kv
import (
"context"
"flag"
"reflect"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/urfave/cli/v2"
)
func TestNilDBHistoryBlkHdr(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(&app, set, nil))
ctx := context.Background()
slot := uint64(1)
validatorID := uint64(1)
hasBlockHeader := db.HasBlockHeader(ctx, slot, validatorID)
if hasBlockHeader {
t.Fatal("HasBlockHeader should return false")
}
bPrime, err := db.BlockHeaders(ctx, slot, validatorID)
if err != nil {
t.Fatalf("failed to get block: %v", err)
}
if bPrime != nil {
t.Fatalf("get should return nil for a non existent key")
}
}
func TestSaveHistoryBlkHdr(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(&app, set, nil))
ctx := context.Background()
tests := []struct {
bh *ethpb.SignedBeaconBlockHeader
}{
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in"), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 2nd"), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: &ethpb.BeaconBlockHeader{Slot: 1, ProposerIndex: 0}},
},
}
for _, tt := range tests {
err := db.SaveBlockHeader(ctx, tt.bh)
if err != nil {
t.Fatalf("save block failed: %v", err)
}
bha, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
if err != nil {
t.Fatalf("failed to get block: %v", err)
}
if bha == nil || !reflect.DeepEqual(bha[0], tt.bh) {
t.Fatalf("get should return bh: %v", bha)
}
}
}
func TestDeleteHistoryBlkHdr(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(&app, set, nil))
ctx := context.Background()
tests := []struct {
bh *ethpb.SignedBeaconBlockHeader
}{
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in"), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 2nd"), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}},
},
}
for _, tt := range tests {
err := db.SaveBlockHeader(ctx, tt.bh)
if err != nil {
t.Fatalf("save block failed: %v", err)
}
}
for _, tt := range tests {
bha, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
if err != nil {
t.Fatalf("failed to get block: %v", err)
}
if bha == nil || !reflect.DeepEqual(bha[0], tt.bh) {
t.Fatalf("get should return bh: %v", bha)
}
err = db.DeleteBlockHeader(ctx, tt.bh)
if err != nil {
t.Fatalf("save block failed: %v", err)
}
bh, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
if err != nil {
t.Fatal(err)
}
if bh != nil {
t.Errorf("Expected block to have been deleted, received: %v", bh)
}
}
}
func TestHasHistoryBlkHdr(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(&app, set, nil))
ctx := context.Background()
tests := []struct {
bh *ethpb.SignedBeaconBlockHeader
}{
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in"), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 2nd"), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 4th"), Header: &ethpb.BeaconBlockHeader{Slot: 1, ProposerIndex: 0}},
},
}
for _, tt := range tests {
found := db.HasBlockHeader(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
if found {
t.Fatal("has block header should return false for block headers that are not in db")
}
err := db.SaveBlockHeader(ctx, tt.bh)
if err != nil {
t.Fatalf("save block failed: %v", err)
}
}
for _, tt := range tests {
err := db.SaveBlockHeader(ctx, tt.bh)
if err != nil {
t.Fatalf("save block failed: %v", err)
}
found := db.HasBlockHeader(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
if !found {
t.Fatal("has block header should return true")
}
}
}
func TestPruneHistoryBlkHdr(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
db := setupDB(t, cli.NewContext(&app, set, nil))
ctx := context.Background()
tests := []struct {
bh *ethpb.SignedBeaconBlockHeader
}{
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in"), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 2nd"), Header: &ethpb.BeaconBlockHeader{Slot: 0, ProposerIndex: 1}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 3rd"), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch + 1, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 4th"), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch*2 + 1, ProposerIndex: 0}},
},
{
bh: &ethpb.SignedBeaconBlockHeader{Signature: []byte("let me in 5th"), Header: &ethpb.BeaconBlockHeader{Slot: params.BeaconConfig().SlotsPerEpoch*3 + 1, ProposerIndex: 0}},
},
}
for _, tt := range tests {
err := db.SaveBlockHeader(ctx, tt.bh)
if err != nil {
t.Fatalf("save block header failed: %v", err)
}
bha, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
if err != nil {
t.Fatalf("failed to get block header: %v", err)
}
if bha == nil || !reflect.DeepEqual(bha[0], tt.bh) {
t.Fatalf("get should return bh: %v", bha)
}
}
currentEpoch := uint64(3)
historyToKeep := uint64(2)
err := db.PruneBlockHistory(ctx, currentEpoch, historyToKeep)
if err != nil {
t.Fatalf("failed to prune: %v", err)
}
for _, tt := range tests {
bha, err := db.BlockHeaders(ctx, tt.bh.Header.Slot, tt.bh.Header.ProposerIndex)
if err != nil {
t.Fatalf("failed to get block header: %v", err)
}
if helpers.SlotToEpoch(tt.bh.Header.Slot) >= currentEpoch-historyToKeep {
if bha == nil || !reflect.DeepEqual(bha[0], tt.bh) {
t.Fatalf("get should return bh: %v", bha)
}
} else {
if bha != nil {
t.Fatalf("block header should have been pruned: %v", bha)
}
}
}
}