Skip to content

Commit

Permalink
Applies assertion funcs to slasher/* tests (prysmaticlabs#6998)
Browse files Browse the repository at this point in the history
* slasher/beaconclient tests
* slasher/db/kv tests
* Merge branch 'master' into apply-testutils-assertions-to-slasher
* fix build
* slasher/detection tests
* rest of the tests
* misc tests
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Update slasher/db/kv/spanner_test.go

Co-authored-by: Shay Zluf <[email protected]>
* Update slasher/node/node_test.go

Co-authored-by: Shay Zluf <[email protected]>
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
* Update slasher/db/kv/spanner_test.go
* Merge refs/heads/master into apply-testutils-assertions-to-slasher
  • Loading branch information
farazdagi authored Aug 18, 2020
1 parent 14cd25f commit 9bf8021
Show file tree
Hide file tree
Showing 39 changed files with 491 additions and 1,038 deletions.
1 change: 1 addition & 0 deletions fuzz/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ go_test(
name = "go_default_test",
srcs = ["beacon_fuzz_states_test.go"],
embed = [":go_default_library"],
deps = ["//shared/testutil/require:go_default_library"],
)
7 changes: 4 additions & 3 deletions fuzz/testing/beacon_fuzz_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package testing

import (
"testing"

"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

func TestGetBeaconFuzzState(t *testing.T) {
if _, err := GetBeaconFuzzState(1); err != nil {
t.Fatal(err)
}
_, err := GetBeaconFuzzState(1)
require.NoError(t, err)
}
1 change: 1 addition & 0 deletions slasher/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ go_test(
embed = [":go_default_library"],
deps = [
"//shared/featureconfig:go_default_library",
"//shared/testutil/assert:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
)
Expand Down
2 changes: 1 addition & 1 deletion slasher/beaconclient/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ go_test(
"//shared/mock:go_default_library",
"//shared/params:go_default_library",
"//shared/slotutil:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"//slasher/cache:go_default_library",
"//slasher/db/testing:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
Expand Down
27 changes: 7 additions & 20 deletions slasher/beaconclient/chain_data_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package beaconclient

import (
"bytes"
"context"
"testing"
"time"

"github.com/gogo/protobuf/proto"
"github.com/golang/mock/gomock"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/mock"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
logTest "github.com/sirupsen/logrus/hooks/test"
)
Expand All @@ -29,12 +28,8 @@ func TestService_ChainHead(t *testing.T) {
}
client.EXPECT().GetChainHead(gomock.Any(), gomock.Any()).Return(wanted, nil)
res, err := bs.ChainHead(context.Background())
if err != nil {
t.Fatal(err)
}
if !proto.Equal(res, wanted) {
t.Errorf("Wanted %v, received %v", wanted, res)
}
require.NoError(t, err)
require.DeepEqual(t, wanted, res)
}

func TestService_GenesisValidatorsRoot(t *testing.T) {
Expand All @@ -50,20 +45,12 @@ func TestService_GenesisValidatorsRoot(t *testing.T) {
}
client.EXPECT().GetGenesis(gomock.Any(), gomock.Any()).Return(wanted, nil)
res, err := bs.GenesisValidatorsRoot(context.Background())
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(res, wanted.GenesisValidatorsRoot) {
t.Errorf("Wanted %#x, received %#x", wanted.GenesisValidatorsRoot, res)
}
require.NoError(t, err)
assert.DeepEqual(t, wanted.GenesisValidatorsRoot, res, "Wanted %#x, received %#x", wanted.GenesisValidatorsRoot, res)
// test next fetch uses memory and not the rpc call.
res, err = bs.GenesisValidatorsRoot(context.Background())
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(res, wanted.GenesisValidatorsRoot) {
t.Errorf("Wanted %#x, received %#x", wanted.GenesisValidatorsRoot, res)
}
require.NoError(t, err)
assert.DeepEqual(t, wanted.GenesisValidatorsRoot, res, "Wanted %#x, received %#x", wanted.GenesisValidatorsRoot, res)
}

func TestService_QuerySyncStatus(t *testing.T) {
Expand Down
10 changes: 3 additions & 7 deletions slasher/beaconclient/historical_data_retrieval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package beaconclient

import (
"context"
"reflect"
"strconv"
"testing"

"github.com/golang/mock/gomock"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/mock"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
testDB "github.com/prysmaticlabs/prysm/slasher/db/testing"
logTest "github.com/sirupsen/logrus/hooks/test"
Expand Down Expand Up @@ -77,12 +77,8 @@ func TestService_RequestHistoricalAttestations(t *testing.T) {

// We request attestations for epoch 0.
res, err := bs.RequestHistoricalAttestations(context.Background(), 0)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(res, wanted) {
t.Errorf("Wanted %v, received %v", wanted, res)
}
require.NoError(t, err)
assert.DeepEqual(t, wanted, res)
require.LogsContain(t, hook, "Retrieved 100/1000 indexed attestations for epoch 0")
require.LogsContain(t, hook, "Retrieved 500/1000 indexed attestations for epoch 0")
require.LogsContain(t, hook, "Retrieved 1000/1000 indexed attestations for epoch 0")
Expand Down
5 changes: 2 additions & 3 deletions slasher/beaconclient/receivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/mock"
"github.com/prysmaticlabs/prysm/shared/slotutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
testDB "github.com/prysmaticlabs/prysm/slasher/db/testing"
)

Expand Down Expand Up @@ -117,7 +118,5 @@ func TestService_ReceiveAttestations_Batched(t *testing.T) {
att.Data.Target.Root = []byte("test root 3")
bs.receivedAttestationsBuffer <- att
atts := <-bs.collectedAttestationsBuffer
if len(atts) != 3 {
t.Fatalf("Expected %d received attestations to be batched", len(atts))
}
require.Equal(t, 3, len(atts), "Unexpected number of attestations batched")
}
15 changes: 15 additions & 0 deletions slasher/beaconclient/service_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package beaconclient

import (
"io/ioutil"
"os"
"testing"

"github.com/sirupsen/logrus"
)

var (
_ = Notifier(&Service{})
_ = ChainFetcher(&Service{})
)

func TestMain(m *testing.M) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(ioutil.Discard)

os.Exit(m.Run())
}
22 changes: 6 additions & 16 deletions slasher/beaconclient/validator_retrieval_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package beaconclient

import (
"bytes"
"context"
"testing"

"github.com/golang/mock/gomock"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/mock"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/slasher/cache"
"github.com/sirupsen/logrus"
Expand All @@ -21,9 +21,7 @@ func TestService_RequestValidator(t *testing.T) {
defer ctrl.Finish()
client := mock.NewMockBeaconChainClient(ctrl)
validatorCache, err := cache.NewPublicKeyCache(0, nil)
if err != nil {
t.Fatalf("could not create new cache: %v", err)
}
require.NoError(t, err, "Could not create new cache")
bs := Service{
beaconClient: client,
publicKeyCache: validatorCache,
Expand Down Expand Up @@ -57,27 +55,19 @@ func TestService_RequestValidator(t *testing.T) {

// We request public key of validator id 0,1.
res, err := bs.FindOrGetPublicKeys(context.Background(), []uint64{0, 1})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
for i, v := range wanted.ValidatorList {
if !bytes.Equal(res[v.Index], wanted.ValidatorList[i].Validator.PublicKey) {
t.Errorf("Wanted %v, received %v", wanted, res)
}
assert.DeepEqual(t, wanted.ValidatorList[i].Validator.PublicKey, res[v.Index])
}

require.LogsContain(t, hook, "Retrieved validators id public key map:")
require.LogsDoNotContain(t, hook, "Retrieved validators public keys from cache:")
// We expect public key of validator id 0 to be in cache.
res, err = bs.FindOrGetPublicKeys(context.Background(), []uint64{0, 3})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

for i, v := range wanted2.ValidatorList {
if !bytes.Equal(res[v.Index], wanted2.ValidatorList[i].Validator.PublicKey) {
t.Errorf("Wanted %v, received %v", wanted2, res)
}
assert.DeepEqual(t, wanted2.ValidatorList[i].Validator.PublicKey, res[v.Index])
}
require.LogsContain(t, hook, "Retrieved validators public keys from cache: map[0:[1 2 3]]")
}
4 changes: 3 additions & 1 deletion slasher/db/kv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ go_test(
"//shared/bytesutil:go_default_library",
"//shared/params:go_default_library",
"//shared/testutil:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"//slasher/db/types:go_default_library",
"//slasher/detection/attestations/types:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@in_gopkg_d4l3k_messagediff_v1//:go_default_library",
],
Expand Down
95 changes: 26 additions & 69 deletions slasher/db/kv/attester_slashings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package kv
import (
"context"
"flag"
"reflect"
"sort"
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/slasher/db/types"
"github.com/urfave/cli/v2"
)
Expand All @@ -21,20 +21,13 @@ func TestStore_AttesterSlashingNilBucket(t *testing.T) {

as := &ethpb.AttesterSlashing{Attestation_1: &ethpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("hello"), 96)}}
has, _, err := db.HasAttesterSlashing(ctx, as)
if err != nil {
t.Fatalf("HasAttesterSlashing should not return error: %v", err)
}
if has {
t.Fatal("HasAttesterSlashing should return false")
}
require.NoError(t, err, "HasAttesterSlashing should not return error")
require.Equal(t, false, has)

p, err := db.AttesterSlashings(ctx, types.SlashingStatus(types.Active))
if err != nil {
t.Fatalf("Failed to get attester slashing: %v", err)
}
if p == nil || len(p) != 0 {
t.Fatalf("Get should return empty attester slashing array for a non existent key")
}
require.NoError(t, err, "Failed to get attester slashing")
require.NotNil(t, p, "Get should return empty attester slashing array for a non existent key")
require.Equal(t, 0, len(p), "Get should return empty attester slashing array for a non existent key")
}

func TestStore_SaveAttesterSlashing(t *testing.T) {
Expand Down Expand Up @@ -68,20 +61,13 @@ func TestStore_SaveAttesterSlashing(t *testing.T) {

for _, tt := range tests {
err := db.SaveAttesterSlashing(ctx, tt.ss, tt.as)
if err != nil {
t.Fatalf("save attester slashing failed: %v", err)
}
require.NoError(t, err, "Save attester slashing failed")

attesterSlashings, err := db.AttesterSlashings(ctx, tt.ss)
if err != nil {
t.Fatalf("failed to get attester slashings: %v", err)
}

if attesterSlashings == nil || !reflect.DeepEqual(attesterSlashings[0], tt.as) {
t.Fatalf("attester slashing: %v should be part of attester slashings response: %v", tt.as, attesterSlashings)
}
require.NoError(t, err, "Failed to get attester slashings")
require.NotNil(t, attesterSlashings)
require.DeepEqual(t, tt.as, attesterSlashings[0], "Slashing: %v should be part of slashings response: %v", tt.as, attesterSlashings)
}

}

func TestStore_SaveAttesterSlashings(t *testing.T) {
Expand All @@ -99,19 +85,14 @@ func TestStore_SaveAttesterSlashings(t *testing.T) {
{Attestation_1: &ethpb.IndexedAttestation{Signature: bytesutil.PadTo([]byte("3"), 96), Data: data}, Attestation_2: att},
}
err := db.SaveAttesterSlashings(ctx, types.Active, as)
if err != nil {
t.Fatalf("save attester slashing failed: %v", err)
}
require.NoError(t, err, "Save attester slashing failed")
attesterSlashings, err := db.AttesterSlashings(ctx, types.Active)
if err != nil {
t.Fatalf("failed to get attester slashings: %v", err)
}
require.NoError(t, err, "Failed to get attester slashings")
sort.SliceStable(attesterSlashings, func(i, j int) bool {
return attesterSlashings[i].Attestation_1.Signature[0] < attesterSlashings[j].Attestation_1.Signature[0]
})
if attesterSlashings == nil || !reflect.DeepEqual(attesterSlashings, as) {
t.Fatalf("Attester slashing: %v should be part of attester slashings response: %v", as, attesterSlashings)
}
require.NotNil(t, attesterSlashings)
require.DeepEqual(t, as, attesterSlashings, "Slashing: %v should be part of slashings response: %v", as, attesterSlashings)
}

func TestStore_UpdateAttesterSlashingStatus(t *testing.T) {
Expand Down Expand Up @@ -140,34 +121,20 @@ func TestStore_UpdateAttesterSlashingStatus(t *testing.T) {

for _, tt := range tests {
err := db.SaveAttesterSlashing(ctx, tt.ss, tt.as)
if err != nil {
t.Fatalf("save attester slashing failed: %v", err)
}
require.NoError(t, err, "Save attester slashing failed")
}

for _, tt := range tests {
has, st, err := db.HasAttesterSlashing(ctx, tt.as)
if err != nil {
t.Fatalf("Failed to get attester slashing: %v", err)
}
if !has {
t.Fatalf("Failed to find attester slashing: %v", tt.as)
}
if st != tt.ss {
t.Fatalf("Failed to find attester slashing with the correct status: %v", tt.as)
}
require.NoError(t, err, "Failed to get attester slashing")
require.Equal(t, true, has, "Failed to find attester slashing: %v", tt.as)
require.Equal(t, tt.ss, st, "Failed to find attester slashing with the correct status: %v", tt.as)

err = db.SaveAttesterSlashing(ctx, types.SlashingStatus(types.Included), tt.as)
has, st, err = db.HasAttesterSlashing(ctx, tt.as)
if err != nil {
t.Fatalf("Failed to get attester slashing: %v", err)
}
if !has {
t.Fatalf("Failed to find attester slashing: %v", tt.as)
}
if st != types.Included {
t.Fatalf("Failed to find attester slashing with the correct status: %v", tt.as)
}
require.NoError(t, err, "Failed to get attester slashing")
require.Equal(t, true, has, "Failed to find attester slashing: %v", tt.as)
require.Equal(t, (types.SlashingStatus)(types.Included), st, "Failed to find attester slashing with the correct status: %v", tt.as)
}
}

Expand All @@ -178,22 +145,12 @@ func TestStore_LatestEpochDetected(t *testing.T) {
ctx := context.Background()

e, err := db.GetLatestEpochDetected(ctx)
if err != nil {
t.Fatalf("Get latest epoch detected failed: %v", err)
}
if e != 0 {
t.Fatalf("Latest epoch detected should have been 0 before setting got: %d", e)
}
require.NoError(t, err, "Get latest epoch detected failed")
require.Equal(t, uint64(0), e, "Latest epoch detected should have been 0 before setting got: %d", e)
epoch := uint64(1)
err = db.SetLatestEpochDetected(ctx, epoch)
if err != nil {
t.Fatalf("Set latest epoch detected failed: %v", err)
}
require.NoError(t, err, "Set latest epoch detected failed")
e, err = db.GetLatestEpochDetected(ctx)
if err != nil {
t.Fatalf("Get latest epoch detected failed: %v", err)
}
if e != epoch {
t.Fatalf("Latest epoch detected should have been: %d got: %d", epoch, e)
}
require.NoError(t, err, "Get latest epoch detected failed")
require.Equal(t, epoch, e, "Latest epoch detected should have been: %d got: %d", epoch, e)
}
Loading

0 comments on commit 9bf8021

Please sign in to comment.