prysm-pulse/beacon-chain/core/helpers/committee_test.go
terence tsao 2441266898
Aligning ETH2.0 spec - Crosslink Records (#1507)
* updated proto state fields to unlock alignment

* moved AttestationParticipants to rightful location

* crosslink committees to use epoch
2019-02-06 15:00:50 +01:00

317 lines
9.6 KiB
Go

package helpers
import (
"fmt"
"reflect"
"strings"
"testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)
var size = 1<<(params.BeaconConfig().RandBytes*8) - 1
var validatorsUpperBound = make([]*pb.ValidatorRecord, size)
var validator = &pb.ValidatorRecord{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
func populateValidatorsMax() {
for i := 0; i < len(validatorsUpperBound); i++ {
validatorsUpperBound[i] = validator
}
}
func TestEpochCommitteeCount_Ok(t *testing.T) {
// this defines the # of validators required to have 1 committee
// per slot for epoch length.
validatorsPerEpoch := params.BeaconConfig().EpochLength * params.BeaconConfig().TargetCommitteeSize
tests := []struct {
validatorCount uint64
committeeCount uint64
}{
{0, params.BeaconConfig().EpochLength},
{1000, params.BeaconConfig().EpochLength},
{2 * validatorsPerEpoch, 2 * params.BeaconConfig().EpochLength},
{5 * validatorsPerEpoch, 5 * params.BeaconConfig().EpochLength},
{16 * validatorsPerEpoch, 16 * params.BeaconConfig().EpochLength},
{32 * validatorsPerEpoch, 16 * params.BeaconConfig().EpochLength},
}
for _, test := range tests {
if test.committeeCount != EpochCommitteeCount(test.validatorCount) {
t.Errorf("wanted: %d, got: %d",
test.committeeCount, EpochCommitteeCount(test.validatorCount))
}
}
}
func TestCurrentEpochCommitteeCount_Ok(t *testing.T) {
validatorsPerEpoch := params.BeaconConfig().EpochLength * params.BeaconConfig().TargetCommitteeSize
committeesPerEpoch := uint64(8)
// set curr epoch total validators count to 8 committees per slot.
validators := make([]*pb.ValidatorRecord, committeesPerEpoch*validatorsPerEpoch)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.ValidatorRecord{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
state := &pb.BeaconState{
ValidatorRegistry: validators,
}
if CurrentEpochCommitteeCount(state) != committeesPerEpoch*params.BeaconConfig().EpochLength {
t.Errorf("Incorrect current epoch committee count per slot. Wanted: %d, got: %d",
committeesPerEpoch, CurrentEpochCommitteeCount(state))
}
}
func TestPrevEpochCommitteeCount_Ok(t *testing.T) {
validatorsPerEpoch := params.BeaconConfig().EpochLength * params.BeaconConfig().TargetCommitteeSize
committeesPerEpoch := uint64(3)
// set prev epoch total validators count to 3 committees per slot.
validators := make([]*pb.ValidatorRecord, committeesPerEpoch*validatorsPerEpoch)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.ValidatorRecord{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
state := &pb.BeaconState{
ValidatorRegistry: validators,
}
if PrevEpochCommitteeCount(state) != committeesPerEpoch*params.BeaconConfig().EpochLength {
t.Errorf("Incorrect prev epoch committee count per slot. Wanted: %d, got: %d",
committeesPerEpoch, PrevEpochCommitteeCount(state))
}
}
func TestNextEpochCommitteeCount_Ok(t *testing.T) {
validatorsPerEpoch := params.BeaconConfig().EpochLength * params.BeaconConfig().TargetCommitteeSize
committeesPerEpoch := uint64(6)
// set prev epoch total validators count to 3 committees per slot.
validators := make([]*pb.ValidatorRecord, committeesPerEpoch*validatorsPerEpoch)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.ValidatorRecord{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
state := &pb.BeaconState{
ValidatorRegistry: validators,
}
if NextEpochCommitteeCount(state) != committeesPerEpoch*params.BeaconConfig().EpochLength {
t.Errorf("Incorrect next epoch committee count per slot. Wanted: %d, got: %d",
committeesPerEpoch, NextEpochCommitteeCount(state))
}
}
func TestShuffling_Ok(t *testing.T) {
validatorsPerEpoch := params.BeaconConfig().EpochLength * params.BeaconConfig().TargetCommitteeSize
committeesPerEpoch := uint64(6)
// Set epoch total validators count to 6 committees per slot.
validators := make([]*pb.ValidatorRecord, committeesPerEpoch*validatorsPerEpoch)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.ValidatorRecord{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
randaoSeed := [32]byte{'A'}
slot := uint64(10)
committees, err := Shuffling(randaoSeed, validators, slot)
if err != nil {
t.Fatalf("Could not shuffle validators: %v", err)
}
// Verify shuffled list is correctly split into committees_per_slot pieces.
committeesPerEpoch = EpochCommitteeCount(uint64(len(validators)))
committeesPerSlot := committeesPerEpoch / params.BeaconConfig().EpochLength
if committeesPerSlot != committeesPerSlot {
t.Errorf("Incorrect committee count after splitting. Wanted: %d, got: %d",
committeesPerSlot, len(committees))
}
// Verify each shuffled committee is TARGET_COMMITTEE_SIZE.
for i := 0; i < len(committees); i++ {
committeeCount := uint64(len(committees[i]))
if committeeCount != params.BeaconConfig().TargetCommitteeSize {
t.Errorf("Incorrect validator count per committee. Wanted: %d, got: %d",
params.BeaconConfig().TargetCommitteeSize, committeeCount)
}
}
}
func TestShuffling_OutOfBound(t *testing.T) {
populateValidatorsMax()
if _, err := Shuffling([32]byte{}, validatorsUpperBound, 0); err == nil {
t.Fatalf("Shuffling should have failed with exceeded upper bound")
}
}
func TestCrosslinkCommitteesAtSlot_Ok(t *testing.T) {
validatorsPerEpoch := params.BeaconConfig().EpochLength * params.BeaconConfig().TargetCommitteeSize
committeesPerEpoch := uint64(6)
// Set epoch total validators count to 6 committees per slot.
validators := make([]*pb.ValidatorRecord, committeesPerEpoch*validatorsPerEpoch)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.ValidatorRecord{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
state := &pb.BeaconState{
ValidatorRegistry: validators,
Slot: 200,
}
committees, err := CrosslinkCommitteesAtSlot(state, 132, false)
if err != nil {
t.Fatalf("Could not get crosslink committee: %v", err)
}
if len(committees) != int(committeesPerEpoch) {
t.Errorf("Incorrect committee count per slot. Wanted: %d, got: %d",
committeesPerEpoch, len(committees))
}
newCommittees, err := CrosslinkCommitteesAtSlot(state, 180, false)
if err != nil {
t.Fatalf("Could not get crosslink committee: %v", err)
}
if reflect.DeepEqual(committees, newCommittees) {
t.Error("Committees from different slot shall not be equal")
}
}
func TestCrosslinkCommitteesAtSlot_OutOfBound(t *testing.T) {
want := fmt.Sprintf(
"input committee epoch %d out of bounds: %d <= epoch <= %d",
2, 0, 0,
)
if _, err := CrosslinkCommitteesAtSlot(&pb.BeaconState{}, params.BeaconConfig().EpochLength*2, false); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestCrosslinkCommitteesAtSlot_ShuffleFailed(t *testing.T) {
state := &pb.BeaconState{
ValidatorRegistry: validatorsUpperBound,
Slot: 100,
}
want := fmt.Sprint(
"could not shuffle epoch validators: " +
"input list exceeded upper bound and reached modulo bias",
)
if _, err := CrosslinkCommitteesAtSlot(state, 1, false); !strings.Contains(err.Error(), want) {
t.Errorf("Expected: %s, received: %v", want, err)
}
}
func TestAttestationParticipants_ok(t *testing.T) {
if params.BeaconConfig().EpochLength != 64 {
t.Errorf("EpochLength should be 64 for these tests to pass")
}
validators := make([]*pb.ValidatorRecord, 2*params.BeaconConfig().EpochLength)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.ValidatorRecord{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
state := &pb.BeaconState{
ValidatorRegistry: validators,
}
attestationData := &pb.AttestationData{}
tests := []struct {
attestationSlot uint64
stateSlot uint64
shard uint64
bitfield []byte
wanted []uint64
}{
{
attestationSlot: 2,
stateSlot: 5,
shard: 2,
bitfield: []byte{0xFF},
wanted: []uint64{11, 121},
},
{
attestationSlot: 1,
stateSlot: 10,
shard: 1,
bitfield: []byte{77},
wanted: []uint64{117},
},
{
attestationSlot: 10,
stateSlot: 20,
shard: 10,
bitfield: []byte{0xFF},
wanted: []uint64{14, 30},
},
{
attestationSlot: 64,
stateSlot: 100,
shard: 0,
bitfield: []byte{0xFF},
wanted: []uint64{109, 97},
},
{
attestationSlot: 999,
stateSlot: 1000,
shard: 39,
bitfield: []byte{99},
wanted: []uint64{89},
},
}
for _, tt := range tests {
state.Slot = tt.stateSlot
attestationData.Slot = tt.attestationSlot
attestationData.Shard = tt.shard
result, err := AttestationParticipants(state, attestationData, tt.bitfield)
if err != nil {
t.Errorf("Failed to get attestation participants: %v", err)
}
if !reflect.DeepEqual(tt.wanted, result) {
t.Errorf(
"Result indices was an unexpected value. Wanted %d, got %d",
tt.wanted,
result,
)
}
}
}
func TestAttestationParticipants_IncorrectBitfield(t *testing.T) {
if params.BeaconConfig().EpochLength != 64 {
t.Errorf("EpochLength should be 64 for these tests to pass")
}
validators := make([]*pb.ValidatorRecord, params.BeaconConfig().DepositsForChainStart)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.ValidatorRecord{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
state := &pb.BeaconState{
ValidatorRegistry: validators,
}
attestationData := &pb.AttestationData{}
if _, err := AttestationParticipants(state, attestationData, []byte{}); err == nil {
t.Error("attestation participants should have failed with incorrect bitfield")
}
}