Skip to content

Commit

Permalink
fix tests and remove multiple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shayzluf committed Apr 30, 2019
1 parent 36fc716 commit 243e6aa
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 183 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/block_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func verifySlashableAttestation(att *pb.SlashableAttestation, verifySignatures b
}
}

if isValidated, err := helpers.VerifyBitfieldNew(att.CustodyBitfield, len(att.ValidatorIndices)); !isValidated || err != nil {
if isValidated, err := helpers.VerifyBitfield(att.CustodyBitfield, len(att.ValidatorIndices)); !isValidated || err != nil {
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/epoch/epoch_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func TestAttestingValidators_MatchActive(t *testing.T) {
}

// Verify the winner root is attested by validators based on shuffling.
if !reflect.DeepEqual(attestedValidators, []uint64{123, 65}) {
t.Errorf("Active validators don't match. Wanted:[123,65], Got: %v", attestedValidators)
if !reflect.DeepEqual(attestedValidators, []uint64{65, 123}) {
t.Errorf("Active validators don't match. Wanted:[65,123], Got: %v", attestedValidators)
}
}

Expand Down
92 changes: 6 additions & 86 deletions beacon-chain/core/helpers/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,16 @@ func Shuffling(
// AttestationParticipants returns the attesting participants indices.
//
// Spec pseudocode definition:
// def get_attestation_participants(state: BeaconState,
// def get_attesting_indices(state: BeaconState,
// attestation_data: AttestationData,
// bitfield: bytes) -> List[ValidatorIndex]:
// bitfield: bytes) -> List[ValidatorIndex]:
// """
// Returns the participant indices at for the ``attestation_data`` and ``bitfield``.
// Return the sorted attesting indices corresponding to ``attestation_data`` and ``bitfield``.
// """
// # Find the committee in the list with the desired shard
// crosslink_committees = get_crosslink_committees_at_slot(state, attestation_data.slot)
//
// assert attestation_data.shard in [shard for _, shard in crosslink_committees]
// crosslink_committee = [committee for committee,
// shard in crosslink_committees if shard == attestation_data.shard][0]
//
// assert verify_bitfield(bitfield, len(crosslink_committee))
//
// # Find the participating attesters in the committee
// participants = []
// for i, validator_index in enumerate(crosslink_committee):
// aggregation_bit = get_bitfield_bit(bitfield, i)
// if aggregation_bit == 0b1:
// participants.append(validator_index)
// return participants
// crosslink_committee = [committee for committee, shard in crosslink_committees if shard == attestation_data.shard][0]
// assert verify_bitfield(bitfield, len(crosslink_committee))
// return sorted([index for i, index in enumerate(crosslink_committee) if get_bitfield_bit(bitfield, i) == 0b1])
func AttestationParticipants(
state *pb.BeaconState,
attestationData *pb.AttestationData,
Expand Down Expand Up @@ -305,74 +293,6 @@ func AttestationParticipants(
participants = append(participants, validatorIndex)
}
}
return participants, nil
}

// AttestationParticipantsNew returns the attesting participants indices.
//
// Spec pseudocode definition:
// def get_attesting_indices(state: BeaconState,
// attestation_data: AttestationData,
// bitfield: bytes) -> List[ValidatorIndex]:
// """
// Return the sorted attesting indices corresponding to ``attestation_data`` and ``bitfield``.
// """
// crosslink_committees = get_crosslink_committees_at_slot(state, attestation_data.slot)
// crosslink_committee = [committee for committee, shard in crosslink_committees if shard == attestation_data.shard][0]
// assert verify_bitfield(bitfield, len(crosslink_committee))
// return sorted([index for i, index in enumerate(crosslink_committee) if get_bitfield_bit(bitfield, i) == 0b1])
func AttestationParticipantsNew(
state *pb.BeaconState,
attestationData *pb.AttestationData,
bitfield []byte) ([]uint64, error) {

var cachedCommittees *cache.CommitteesInSlot
var err error
slot := attestationData.Slot

// When enabling committee cache, we fetch the committees using slot.
// If it's not prev cached, we compute for the committees of slot and
// add it to the cache.
cachedCommittees, err = committeeCache.CommitteesInfoBySlot(slot)
if err != nil {
return nil, err
}

if cachedCommittees == nil {
crosslinkCommittees, err := CrosslinkCommitteesAtSlot(state, slot, false /* registryChange */)
if err != nil {
return nil, err
}
cachedCommittees = ToCommitteeCache(slot, crosslinkCommittees)

if err := committeeCache.AddCommittees(cachedCommittees); err != nil {
return nil, err
}
}

var selectedCommittee []uint64
for _, committee := range cachedCommittees.Committees {
if committee.Shard == attestationData.Shard {
selectedCommittee = committee.Committee
break
}
}

if isValidated, err := VerifyBitfieldNew(bitfield, len(selectedCommittee)); !isValidated || err != nil {
if err != nil {
return nil, err
}
return nil, errors.New("bitfield is unable to be verified")
}

// Find the participating validators in the committee.
var participants []uint64
for i, validatorIndex := range selectedCommittee {
bit := bitutil.BitfieldBit(bitfield, i)
if bit == 0x01 {
participants = append(participants, validatorIndex)
}
}
sort.Slice(participants, func(i, j int) bool { return participants[i] < participants[j] })
return participants, nil
}
Expand Down
90 changes: 0 additions & 90 deletions beacon-chain/core/helpers/committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,96 +293,6 @@ func TestCrosslinkCommitteesAtSlot_ShuffleFailed(t *testing.T) {
}
}

func TestAttestationParticipantsNew_IncorrectBitfield(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("SlotsPerEpoch should be 64 for these tests to pass")
}

validators := make([]*pb.Validator, params.BeaconConfig().DepositsForChainStart)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}

state := &pb.BeaconState{
ValidatorRegistry: validators,
}
attestationData := &pb.AttestationData{}

if _, err := AttestationParticipantsNew(state, attestationData, []byte{}); err == nil {
t.Error("attestation participants should have failed with incorrect bitfield")
}
}

func TestAttestationParticipantsNew_NoCommitteeCache(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("SlotsPerEpoch should be 64 for these tests to pass")
}

validators := make([]*pb.Validator, 2*params.BeaconConfig().SlotsPerEpoch)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.Validator{
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: params.BeaconConfig().GenesisSlot + 2,
stateSlot: params.BeaconConfig().GenesisSlot + 5,
shard: 2,
bitfield: []byte{0x03},
wanted: []uint64{11, 14},
},
{
attestationSlot: params.BeaconConfig().GenesisSlot + 1,
stateSlot: params.BeaconConfig().GenesisSlot + 10,
shard: 1,
bitfield: []byte{0x01},
wanted: []uint64{5},
},
{
attestationSlot: params.BeaconConfig().GenesisSlot + 10,
stateSlot: params.BeaconConfig().GenesisSlot + 10,
shard: 10,
bitfield: []byte{0x03},
wanted: []uint64{55, 105},
},
}

for _, tt := range tests {
state.Slot = tt.stateSlot
attestationData.Slot = tt.attestationSlot
attestationData.Shard = tt.shard

result, err := AttestationParticipantsNew(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_NoCommitteeCache(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("SlotsPerEpoch should be 64 for these tests to pass")
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/validators/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func TestBoundaryAttesterIndices_OK(t *testing.T) {
t.Fatalf("Failed to run BoundaryAttesterIndices: %v", err)
}

if !reflect.DeepEqual(attesterIndices, []uint64{123, 65}) {
if !reflect.DeepEqual(attesterIndices, []uint64{65, 123}) {
t.Errorf("Incorrect boundary attester indices. Wanted: %v, got: %v",
[]uint64{123, 65}, attesterIndices)
[]uint64{65, 123}, attesterIndices)
}
}

Expand Down Expand Up @@ -119,9 +119,9 @@ func TestAttestingValidatorIndices_OK(t *testing.T) {
t.Fatalf("Could not execute AttestingValidatorIndices: %v", err)
}

if !reflect.DeepEqual(indices, []uint64{1131, 1015}) {
if !reflect.DeepEqual(indices, []uint64{1015, 1131}) {
t.Errorf("Could not get incorrect validator indices. Wanted: %v, got: %v",
[]uint64{1131, 1015}, indices)
[]uint64{1015, 1131}, indices)
}
}

Expand Down

0 comments on commit 243e6aa

Please sign in to comment.