Handle idx panic (#1492)

* fix panic on empty first committee

* fix issue in validator server too
This commit is contained in:
Preston Van Loon
2019-02-05 11:15:54 -05:00
committed by GitHub
parent 4b27b8408b
commit 2a0eae163e
3 changed files with 21 additions and 7 deletions

View File

@@ -64,6 +64,10 @@ func BeaconProposerIdx(state *pb.BeaconState, slot uint64) (uint64, error) {
}
firstCommittee := committeeArray[0].Committee
if len(firstCommittee) == 0 {
return 0, fmt.Errorf("empty first committee at slot %d", slot)
}
return firstCommittee[slot%uint64(len(firstCommittee))], nil
}

View File

@@ -274,6 +274,14 @@ func TestBeaconProposerIdx(t *testing.T) {
}
}
func TestBeaconProposerIdx_returnsErrorWithEmptyCommittee(t *testing.T) {
_, err := BeaconProposerIdx(&pb.BeaconState{}, 0)
expected := "empty first committee at slot 0"
if err.Error() != expected {
t.Errorf("Unexpected error. got=%v want=%s", err, expected)
}
}
func TestAttestingValidatorIndices_Ok(t *testing.T) {
if params.BeaconConfig().EpochLength != 64 {
t.Errorf("EpochLength should be 64 for these tests to pass")

View File

@@ -58,20 +58,22 @@ func (vs *ValidatorServer) ValidatorEpochAssignments(
var attesterSlot uint64
var proposerSlot uint64
for i := req.EpochStart; i < req.EpochStart+params.BeaconConfig().EpochLength; i++ {
crossLinkCommittees, err := v.CrosslinkCommitteesAtSlot(beaconState, i)
for slot := req.EpochStart; slot < req.EpochStart+params.BeaconConfig().EpochLength; slot++ {
crossLinkCommittees, err := v.CrosslinkCommitteesAtSlot(beaconState, slot)
if err != nil {
return nil, fmt.Errorf("could not get crosslink committees at slot %d: %v", i, err)
return nil, err
}
proposerIndex, err := v.BeaconProposerIdx(beaconState, slot)
if err != nil {
return nil, err
}
firstCommittee := crossLinkCommittees[0].Committee
proposerIndex := firstCommittee[i%uint64(len(firstCommittee))]
if proposerIndex == validatorIndex {
proposerSlot = i
proposerSlot = slot
}
for _, committee := range crossLinkCommittees {
for _, idx := range committee.Committee {
if idx == validatorIndex {
attesterSlot = i
attesterSlot = slot
shard = committee.Shard
}
}