prysm-pulse/beacon-chain/sync/validate_committee_index_beacon_attestation_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

196 lines
5.7 KiB
Go

package sync
import (
"bytes"
"context"
"fmt"
"testing"
"time"
lru "github.com/hashicorp/golang-lru"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-bitfield"
mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) {
t.Skip("Temporarily disabled, fixed in v0.12 branch.")
ctx := context.Background()
p := p2ptest.NewTestP2P(t)
db := dbtest.SetupDB(t)
chain := &mockChain.ChainService{
Genesis: time.Now().Add(time.Duration(-64*int64(params.BeaconConfig().SecondsPerSlot)) * time.Second), // 64 slots ago
ValidatorsRoot: [32]byte{'A'},
ValidAttestation: true,
}
c, err := lru.New(10)
if err != nil {
t.Fatal(err)
}
s := &Service{
initialSync: &mockSync.Sync{IsSyncing: false},
p2p: p,
db: db,
chain: chain,
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
seenAttestationCache: c,
stateSummaryCache: cache.NewStateSummaryCache(),
}
digest, err := s.forkDigest()
if err != nil {
t.Fatal(err)
}
blk := &ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{
Slot: 55,
},
}
if err := db.SaveBlock(ctx, blk); err != nil {
t.Fatal(err)
}
validBlockRoot, err := stateutil.BlockRoot(blk.Block)
if err != nil {
t.Fatal(err)
}
savedState := testutil.NewBeaconState()
if err := db.SaveState(context.Background(), savedState, validBlockRoot); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
msg *ethpb.Attestation
topic string
validAttestationSignature bool
want bool
}{
{
name: "validAttestationSignature",
msg: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b1010},
Data: &ethpb.AttestationData{
BeaconBlockRoot: validBlockRoot[:],
CommitteeIndex: 1,
Slot: 63,
},
},
topic: fmt.Sprintf("/eth2/%x/committee_index1_beacon_attestation", digest),
validAttestationSignature: true,
want: true,
},
{
name: "alreadySeen",
msg: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b1010},
Data: &ethpb.AttestationData{
BeaconBlockRoot: validBlockRoot[:],
CommitteeIndex: 1,
Slot: 63,
Target: &ethpb.Checkpoint{},
},
},
topic: fmt.Sprintf("/eth2/%x/committee_index1_beacon_attestation", digest),
validAttestationSignature: true,
want: false,
},
{
name: "wrong committee index",
msg: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b1010},
Data: &ethpb.AttestationData{
BeaconBlockRoot: validBlockRoot[:],
CommitteeIndex: 2,
Slot: 63,
Target: &ethpb.Checkpoint{},
},
},
topic: fmt.Sprintf("/eth2/%x/committee_index3_beacon_attestation", digest),
validAttestationSignature: true,
want: false,
},
{
name: "already aggregated",
msg: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b1011},
Data: &ethpb.AttestationData{
BeaconBlockRoot: validBlockRoot[:],
CommitteeIndex: 1,
Slot: 63,
Target: &ethpb.Checkpoint{},
},
},
topic: fmt.Sprintf("/eth2/%x/committee_index1_beacon_attestation", digest),
validAttestationSignature: true,
want: false,
},
{
name: "missing block",
msg: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b1010},
Data: &ethpb.AttestationData{
BeaconBlockRoot: bytesutil.PadTo([]byte("missing"), 32),
CommitteeIndex: 1,
Slot: 63,
Target: &ethpb.Checkpoint{},
},
},
topic: fmt.Sprintf("/eth2/%x/committee_index1_beacon_attestation", digest),
validAttestationSignature: true,
want: false,
},
{
name: "invalid attestation",
msg: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b1010},
Data: &ethpb.AttestationData{
BeaconBlockRoot: validBlockRoot[:],
CommitteeIndex: 1,
Slot: 63,
Target: &ethpb.Checkpoint{},
},
},
topic: fmt.Sprintf("/eth2/%x/committee_index1_beacon_attestation", digest),
validAttestationSignature: false,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := new(bytes.Buffer)
_, err := p.Encoding().Encode(buf, tt.msg)
if err != nil {
t.Error(err)
}
m := &pubsub.Message{
Message: &pubsubpb.Message{
Data: buf.Bytes(),
TopicIDs: []string{tt.topic},
},
}
received := s.validateCommitteeIndexBeaconAttestation(ctx, "" /*peerID*/, m) == pubsub.ValidationAccept
if received != tt.want {
t.Fatalf("Did not received wanted validation. Got %v, wanted %v", !tt.want, tt.want)
}
if tt.want && m.ValidatorData == nil {
t.Error("Expected validator data to be set")
}
})
}
}