prysm-pulse/beacon-chain/rpc/beacon/attestations_test.go
Raul Jordan 3332abbb5a
Restructure Beacon Chain RPC Servers Into Subpackages (#3975)
* Update seed domains (#3872)

* Remove Transfers (#3870)

* Remove active index roots and compact committee roots (#3869)

* Update inclusion reward (#3886)

* Alter proposer selection logic (#3884)

* Fix early committee bias (#3888)

* Remove shards and committees (#3896)

* Epoch spec tests v0.9 (#3907)

* Block spec test v0.9 (#3905)

* rm'ed in protobuf

* build proto

* build proto

* build proto

* fix core package

* Gazelle

* Fixed all the tests

* Fixed static test

* Comment out spec test for now

* One more skip

* fix-roundRobinSync (#3862)

* Starting but need new seed function

* Revert initial sync

* Updated Proposer Slashing

* Fixed all tests

* Lint

* Update inclusion reward

* Fill randao mixes with eth1 data hash

* Test

* Fixing test part1

* All tests passing

* One last test

* Updated config

* Build proto

* Proper skip message

* Conflict and fmt

* Removed crosslinks and shards. Built

* Format and gazelle

* Fixed all the block package tests

* Fixed all the helper tests

* All epoch package tests pass

* All core package tests pass

* Fixed operation tests

* Started fixing rpc test

* RPC tests passed!

* Fixed all init sync tests

* All tests pass

* Fixed blockchain tests

* Lint

* Lint

* Preston's feedback

* Starting

* Remove container

* Fixed block spec tests

* All passing except for block_processing test

* Failing block processing test

* Starting

* Add AggregateAndProof

* All mainnet test passes

* Update deposit contract (#3906)

* Proto spec tests v0.9 (#3908)

* Starting

* Add AggregateAndProof

* Unskip block util tests (#3910)

* rm'ed in protobuf

* build proto

* build proto

* build proto

* fix core package

* Gazelle

* Fixed all the tests

* Fixed static test

* Comment out spec test for now

* One more skip

* fix-roundRobinSync (#3862)

* Starting but need new seed function

* Revert initial sync

* Updated Proposer Slashing

* Fixed all tests

* Lint

* Update inclusion reward

* Fill randao mixes with eth1 data hash

* Test

* Fixing test part1

* All tests passing

* One last test

* Updated config

* Build proto

* Proper skip message

* Conflict and fmt

* Removed crosslinks and shards. Built

* Format and gazelle

* Fixed all the block package tests

* Fixed all the helper tests

* All epoch package tests pass

* All core package tests pass

* Fixed operation tests

* Started fixing rpc test

* RPC tests passed!

* Fixed all init sync tests

* All tests pass

* Fixed blockchain tests

* Lint

* Lint

* Preston's feedback

* Starting

* Remove container

* Fixed block spec tests

* All passing except for block_processing test

* Failing block processing test

* Starting

* Add AggregateAndProof

* All mainnet test passes

* Unskip block util tests

* Slot processing spec test V0.9 (#3912)

* Starting

* Add AggregateAndProof

* Unskip slot processing mainnet test

* Unskip minimal spec test for finalization (#3920)

* Remove outdated interop tests (#3922)

* Rm outdated interop tests

* Rm test runner

* Gazelle

* Update validator to use proposer slot (#3919)

* Fix committee assignment (#3931)

* Replace shard with committee index (#3930)

* Conflict

* Clean up (#3933)

* Remove shard filter in db (#3936)

* Remove lightouse compatibility test (#3939)

* Update Committee Cache for v0.9 (#3948)

* Updated committee cache

* Removed shuffled indices cache

* Started testing run time

* Lint

* Fixed test

* Safeguard against nil head state

* address edge case

* add test

* Fixed TestRoundRobinSync by doubling the epochs

* Unskip TestProtoCompatability (#3958)

* Unskip TestProtoCompatability

* Update WORKSPACE

* Fix minimal config (#3959)

* fix minimal configs

* fix hardcoded value in test

* Simplify verify att time (#3961)

* update readme for deposit contract, regen bindings for vyper 0.1.0b12 (#3963)

* update readme for deposit contract, regen bindings

* medium

* Check nil base state (#3964)

* Copy Block When Receiving it From Sync (#3966)

* copy block

* clone for other service methods too

* Change logging of Bitfield  (#3956)

* change logging of bits

* preston's review

* Unskip Beacon Server Test (#3962)

* run test till the end

* fix up proto message types

* fmt

* resolve broken tests

* better error handling

* fixing new logic to use archived proposer info

* fix up logic

* clip using the max effective balance

* broken build fix with num arg mismatch

* amend archive

* archival logic changed

* rename test

* archive both proposer and attester seeds

* page size 100

* further experiments

* further experimentation, archivedProposerIndex seems wrong

* test passes

* rem log

* fix broken test

* fix test

* gaz

* fix imports

* ethapis

* attester server split into subpackage

* attester impl split up successfully

* validator cleaned up

* all packages isolated

* include proposer

* proper naming

* test fix

* proper viz

* naming

* resolved timeout due to config values

* init use minimal

* added all subfiles

* subfile split and gazelle

* shards

* validator folder

* cleanup val

* shay feedback
2019-11-12 11:01:27 -06:00

461 lines
12 KiB
Go

package beacon
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"testing"
"github.com/gogo/protobuf/proto"
ptypes "github.com/gogo/protobuf/types"
"github.com/prysmaticlabs/go-bitfield"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
mockOps "github.com/prysmaticlabs/prysm/beacon-chain/operations/testing"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestServer_ListAttestations_NoPagination(t *testing.T) {
db := dbTest.SetupDB(t)
defer dbTest.TeardownDB(t, db)
ctx := context.Background()
count := uint64(10)
atts := make([]*ethpb.Attestation, 0, count)
for i := uint64(0); i < count; i++ {
attExample := &ethpb.Attestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: i,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1),
}
if err := db.SaveAttestation(ctx, attExample); err != nil {
t.Fatal(err)
}
atts = append(atts, attExample)
}
bs := &Server{
BeaconDB: db,
}
received, err := bs.ListAttestations(ctx, &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_HeadBlockRoot{
HeadBlockRoot: []byte("root"),
},
})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(atts, received.Attestations) {
t.Fatalf("incorrect attestations response: wanted %v, received %v", atts, received.Attestations)
}
}
func TestServer_ListAttestations_FiltersCorrectly(t *testing.T) {
db := dbTest.SetupDB(t)
defer dbTest.TeardownDB(t, db)
ctx := context.Background()
someRoot := []byte{1, 2, 3}
sourceRoot := []byte{4, 5, 6}
sourceEpoch := uint64(5)
targetRoot := []byte{7, 8, 9}
targetEpoch := uint64(7)
unknownRoot := []byte{1, 1, 1}
atts := []*ethpb.Attestation{
{
Data: &ethpb.AttestationData{
BeaconBlockRoot: someRoot,
Source: &ethpb.Checkpoint{
Root: sourceRoot,
Epoch: sourceEpoch,
},
Target: &ethpb.Checkpoint{
Root: targetRoot,
Epoch: targetEpoch,
},
Slot: 3,
},
AggregationBits: bitfield.Bitlist{0b11},
},
{
Data: &ethpb.AttestationData{
BeaconBlockRoot: unknownRoot,
Source: &ethpb.Checkpoint{
Root: sourceRoot,
Epoch: sourceEpoch,
},
Target: &ethpb.Checkpoint{
Root: targetRoot,
Epoch: targetEpoch,
},
Slot: 4,
},
AggregationBits: bitfield.Bitlist{0b11},
},
{
Data: &ethpb.AttestationData{
BeaconBlockRoot: someRoot,
Source: &ethpb.Checkpoint{
Root: unknownRoot,
Epoch: sourceEpoch,
},
Target: &ethpb.Checkpoint{
Root: unknownRoot,
Epoch: targetEpoch,
},
Slot: 5,
},
AggregationBits: bitfield.Bitlist{0b11},
},
}
if err := db.SaveAttestations(ctx, atts); err != nil {
t.Fatal(err)
}
bs := &Server{
BeaconDB: db,
}
received, err := bs.ListAttestations(ctx, &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_HeadBlockRoot{HeadBlockRoot: someRoot},
})
if err != nil {
t.Fatal(err)
}
if len(received.Attestations) != 2 {
t.Errorf("Wanted 2 matching attestations with root %#x, received %d", someRoot, len(received.Attestations))
}
received, err = bs.ListAttestations(ctx, &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_SourceEpoch{SourceEpoch: sourceEpoch},
})
if err != nil {
t.Fatal(err)
}
if len(received.Attestations) != 3 {
t.Errorf("Wanted 3 matching attestations with source epoch %d, received %d", sourceEpoch, len(received.Attestations))
}
received, err = bs.ListAttestations(ctx, &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_SourceRoot{SourceRoot: sourceRoot},
})
if err != nil {
t.Fatal(err)
}
if len(received.Attestations) != 2 {
t.Errorf("Wanted 2 matching attestations with source root %#x, received %d", sourceRoot, len(received.Attestations))
}
received, err = bs.ListAttestations(ctx, &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_TargetEpoch{TargetEpoch: targetEpoch},
})
if err != nil {
t.Fatal(err)
}
if len(received.Attestations) != 3 {
t.Errorf("Wanted 3 matching attestations with target epoch %d, received %d", targetEpoch, len(received.Attestations))
}
received, err = bs.ListAttestations(ctx, &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_TargetRoot{TargetRoot: targetRoot},
})
if err != nil {
t.Fatal(err)
}
if len(received.Attestations) != 2 {
t.Errorf("Wanted 2 matching attestations with target root %#x, received %d", targetRoot, len(received.Attestations))
}
}
func TestServer_ListAttestations_Pagination_CustomPageParameters(t *testing.T) {
db := dbTest.SetupDB(t)
defer dbTest.TeardownDB(t, db)
ctx := context.Background()
count := uint64(100)
atts := make([]*ethpb.Attestation, 0, count)
for i := uint64(0); i < count; i++ {
attExample := &ethpb.Attestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: i,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1),
}
if err := db.SaveAttestation(ctx, attExample); err != nil {
t.Fatal(err)
}
atts = append(atts, attExample)
}
bs := &Server{
BeaconDB: db,
}
tests := []struct {
req *ethpb.ListAttestationsRequest
res *ethpb.ListAttestationsResponse
}{
{
req: &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_HeadBlockRoot{
HeadBlockRoot: []byte("root"),
},
PageToken: strconv.Itoa(1),
PageSize: 3,
},
res: &ethpb.ListAttestationsResponse{
Attestations: []*ethpb.Attestation{
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 3,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 4,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 5,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
},
NextPageToken: strconv.Itoa(2),
TotalSize: int32(count)}},
{
req: &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_HeadBlockRoot{
HeadBlockRoot: []byte("root"),
},
PageToken: strconv.Itoa(10),
PageSize: 5,
},
res: &ethpb.ListAttestationsResponse{
Attestations: []*ethpb.Attestation{
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 50,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 51,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 52,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 53,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 54,
}, AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
},
NextPageToken: strconv.Itoa(11),
TotalSize: int32(count)}},
{
req: &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_HeadBlockRoot{
HeadBlockRoot: []byte("root"),
},
PageToken: strconv.Itoa(33),
PageSize: 3,
},
res: &ethpb.ListAttestationsResponse{
Attestations: []*ethpb.Attestation{
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 99,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
},
NextPageToken: strconv.Itoa(34),
TotalSize: int32(count)}},
{
req: &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_HeadBlockRoot{
HeadBlockRoot: []byte("root"),
},
PageSize: 2,
},
res: &ethpb.ListAttestationsResponse{
Attestations: []*ethpb.Attestation{
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
{Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: 1,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1),
},
},
NextPageToken: strconv.Itoa(1),
TotalSize: int32(count)}},
}
for _, test := range tests {
res, err := bs.ListAttestations(ctx, test.req)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(res, test.res) {
t.Errorf("Incorrect attestations response, wanted %v, received %v", test.res, res)
}
}
}
func TestServer_ListAttestations_Pagination_OutOfRange(t *testing.T) {
db := dbTest.SetupDB(t)
defer dbTest.TeardownDB(t, db)
ctx := context.Background()
count := uint64(1)
atts := make([]*ethpb.Attestation, 0, count)
for i := uint64(0); i < count; i++ {
attExample := &ethpb.Attestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: i,
},
AggregationBits: bitfield.Bitlist{0b11},
}
if err := db.SaveAttestation(ctx, attExample); err != nil {
t.Fatal(err)
}
atts = append(atts, attExample)
}
bs := &Server{
BeaconDB: db,
}
req := &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_HeadBlockRoot{
HeadBlockRoot: []byte("root"),
},
PageToken: strconv.Itoa(1),
PageSize: 100,
}
wanted := fmt.Sprintf("page start %d >= list %d", req.PageSize, len(atts))
if _, err := bs.ListAttestations(ctx, req); !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected error %v, received %v", wanted, err)
}
}
func TestServer_ListAttestations_Pagination_ExceedsMaxPageSize(t *testing.T) {
ctx := context.Background()
bs := &Server{}
exceedsMax := int32(params.BeaconConfig().MaxPageSize + 1)
wanted := fmt.Sprintf("requested page size %d can not be greater than max size %d", exceedsMax, params.BeaconConfig().MaxPageSize)
req := &ethpb.ListAttestationsRequest{PageToken: strconv.Itoa(0), PageSize: exceedsMax}
if _, err := bs.ListAttestations(ctx, req); !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected error %v, received %v", wanted, err)
}
}
func TestServer_ListAttestations_Pagination_DefaultPageSize(t *testing.T) {
db := dbTest.SetupDB(t)
defer dbTest.TeardownDB(t, db)
ctx := context.Background()
count := uint64(params.BeaconConfig().DefaultPageSize)
atts := make([]*ethpb.Attestation, 0, count)
for i := uint64(0); i < count; i++ {
attExample := &ethpb.Attestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("root"),
Slot: i,
},
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1),
}
if err := db.SaveAttestation(ctx, attExample); err != nil {
t.Fatal(err)
}
atts = append(atts, attExample)
}
bs := &Server{
BeaconDB: db,
}
req := &ethpb.ListAttestationsRequest{
QueryFilter: &ethpb.ListAttestationsRequest_HeadBlockRoot{
HeadBlockRoot: []byte("root"),
},
}
res, err := bs.ListAttestations(ctx, req)
if err != nil {
t.Fatal(err)
}
i := 0
j := params.BeaconConfig().DefaultPageSize
if !reflect.DeepEqual(res.Attestations, atts[i:j]) {
t.Log(res.Attestations, atts[i:j])
t.Error("Incorrect attestations response")
}
}
func TestServer_AttestationPool(t *testing.T) {
ctx := context.Background()
block := &ethpb.BeaconBlock{
Slot: 10,
}
bs := &Server{
Pool: &mockOps.Operations{
Attestations: []*ethpb.Attestation{
{
Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("1"),
},
},
{
Data: &ethpb.AttestationData{
BeaconBlockRoot: []byte("2"),
},
},
},
},
HeadFetcher: &mock.ChainService{
Block: block,
},
}
res, err := bs.AttestationPool(ctx, &ptypes.Empty{})
if err != nil {
t.Fatal(err)
}
want, _ := bs.Pool.AttestationPoolNoVerify(ctx)
if !reflect.DeepEqual(res.Attestations, want) {
t.Errorf("Wanted AttestationPool() = %v, received %v", want, res.Attestations)
}
}