Fix active validator indices for shuffling (#2101)

This commit is contained in:
terence tsao 2019-03-28 15:31:46 -07:00 committed by GitHub
parent 64508dc3c3
commit c777ce6546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 44 deletions

View File

@ -153,7 +153,7 @@ func TestInclusionDistRewards_AccurateRewards(t *testing.T) {
voted []uint64
}{
{[]uint64{}},
{[]uint64{237, 224}},
{[]uint64{251, 192}},
}
for _, tt := range tests {
validatorBalances := make([]uint64, len(validators))
@ -392,7 +392,7 @@ func TestInactivityInclusionPenalty_AccuratePenalties(t *testing.T) {
voted []uint64
}{
{[]uint64{}},
{[]uint64{237, 224}},
{[]uint64{251, 192}},
}
for _, tt := range tests {
validatorBalances := make([]uint64, params.BeaconConfig().SlotsPerEpoch*4)
@ -478,7 +478,7 @@ func TestAttestationInclusionRewards_AccurateRewards(t *testing.T) {
voted []uint64
}{
{[]uint64{}},
{[]uint64{237}},
{[]uint64{251}},
}
for _, tt := range tests {
validatorBalances := make([]uint64, params.BeaconConfig().SlotsPerEpoch*4)

View File

@ -609,10 +609,11 @@ func TestProcessAttesterSlashings_EmptyVoteIndexIntersection(t *testing.T) {
func TestProcessAttesterSlashings_AppliesCorrectStatus(t *testing.T) {
// We test the case when data is correct and verify the validator
// registry has been updated.
validators := make([]*pb.Validator, 100)
validators := make([]*pb.Validator, params.BeaconConfig().DepositsForChainStart)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.Validator{
ExitEpoch: params.BeaconConfig().GenesisEpoch + 1,
ActivationEpoch: params.BeaconConfig().GenesisEpoch,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
SlashedEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawalEpoch: params.BeaconConfig().GenesisEpoch + 1*params.BeaconConfig().SlotsPerEpoch,
}

View File

@ -420,8 +420,8 @@ func TestAttestingValidators_MatchActive(t *testing.T) {
}
// Verify the winner root is attested by validators based on shuffling.
if !reflect.DeepEqual(attestedValidators, []uint64{109, 97}) {
t.Errorf("Active validators don't match. Wanted:[109,97], Got: %v", attestedValidators)
if !reflect.DeepEqual(attestedValidators, []uint64{123, 65}) {
t.Errorf("Active validators don't match. Wanted:[123,65], Got: %v", attestedValidators)
}
}
@ -525,7 +525,7 @@ func TestInclusionSlot_GetsCorrectSlot(t *testing.T) {
AggregationBitfield: participationBitfield,
InclusionSlot: 102},
}
slot, err := InclusionSlot(state, 237)
slot, err := InclusionSlot(state, 251)
if err != nil {
t.Fatalf("Could not execute InclusionSlot: %v", err)
}
@ -572,7 +572,7 @@ func TestInclusionDistance_CorrectDistance(t *testing.T) {
AggregationBitfield: participationBitfield,
InclusionSlot: params.BeaconConfig().GenesisSlot + 100},
}
distance, err := InclusionDistance(state, 237)
distance, err := InclusionDistance(state, 251)
if err != nil {
t.Fatalf("Could not execute InclusionDistance: %v", err)
}

View File

@ -203,20 +203,17 @@ func CrosslinkCommitteesAtSlot(
func Shuffling(
seed [32]byte,
validators []*pb.Validator,
slot uint64) ([][]uint64, error) {
// Normalize slot to start of epoch boundary.
slot -= slot % params.BeaconConfig().SlotsPerEpoch
epoch uint64) ([][]uint64, error) {
// Figure out how many committees can be in a single slot.
activeIndices := ActiveValidatorIndices(validators, slot)
activeIndices := ActiveValidatorIndices(validators, epoch)
activeCount := uint64(len(activeIndices))
committeesPerEpoch := EpochCommitteeCount(activeCount)
// Convert slot to bytes and xor it with seed.
slotInBytes := make([]byte, 32)
binary.LittleEndian.PutUint64(slotInBytes, slot)
seed = bytesutil.ToBytes32(bytesutil.Xor(seed[:], slotInBytes))
epochInBytes := make([]byte, 32)
binary.LittleEndian.PutUint64(epochInBytes, epoch)
seed = bytesutil.ToBytes32(bytesutil.Xor(seed[:], epochInBytes))
shuffledIndices, err := utils.ShuffleIndices(seed, activeIndices)
if err != nil {
@ -594,7 +591,7 @@ func crosslinkCommittees(state *pb.BeaconState, input *shufflingInput) ([]*Cross
shuffledIndices, err := Shuffling(
bytesutil.ToBytes32(input.seed),
state.ValidatorRegistry,
input.shufflingEpoch)
CurrentEpoch(state))
if err != nil {
return nil, err
}

View File

@ -322,21 +322,21 @@ func TestAttestationParticipants_OK(t *testing.T) {
stateSlot: params.BeaconConfig().GenesisSlot + 5,
shard: 2,
bitfield: []byte{0xC0},
wanted: []uint64{11, 121},
wanted: []uint64{11, 14},
},
{
attestationSlot: params.BeaconConfig().GenesisSlot + 1,
stateSlot: params.BeaconConfig().GenesisSlot + 10,
shard: 1,
bitfield: []byte{0x80},
wanted: []uint64{4},
wanted: []uint64{5},
},
{
attestationSlot: params.BeaconConfig().GenesisSlot + 10,
stateSlot: params.BeaconConfig().GenesisSlot + 10,
shard: 10,
bitfield: []byte{0xC0},
wanted: []uint64{14, 30},
wanted: []uint64{55, 105},
},
}
@ -440,31 +440,31 @@ func TestCommitteeAssignment_CanRetrieve(t *testing.T) {
}{
{
index: 0,
slot: params.BeaconConfig().GenesisSlot + 146,
committee: []uint64{105, 0},
shard: 18,
slot: params.BeaconConfig().GenesisSlot + 159,
committee: []uint64{0, 123},
shard: 31,
isProposer: false,
},
{
index: 105,
slot: params.BeaconConfig().GenesisSlot + 146,
committee: []uint64{105, 0},
shard: 18,
slot: params.BeaconConfig().GenesisSlot + 185,
committee: []uint64{1, 105},
shard: 57,
isProposer: true,
},
{
index: 64,
slot: params.BeaconConfig().GenesisSlot + 139,
committee: []uint64{64, 52},
shard: 11,
isProposer: false,
slot: params.BeaconConfig().GenesisSlot + 176,
committee: []uint64{64, 12},
shard: 48,
isProposer: true,
},
{
index: 11,
slot: params.BeaconConfig().GenesisSlot + 130,
committee: []uint64{11, 121},
shard: 2,
isProposer: true,
slot: params.BeaconConfig().GenesisSlot + 188,
committee: []uint64{112, 11},
shard: 60,
isProposer: false,
},
}

View File

@ -51,23 +51,23 @@ func TestBeaconProposerIndex_OK(t *testing.T) {
}{
{
slot: params.BeaconConfig().GenesisSlot + 1,
index: 511,
index: 504,
},
{
slot: params.BeaconConfig().GenesisSlot + 10,
index: 2807,
index: 2821,
},
{
slot: params.BeaconConfig().GenesisSlot + 19,
index: 5122,
index: 5132,
},
{
slot: params.BeaconConfig().GenesisSlot + 30,
index: 7947,
index: 7961,
},
{
slot: params.BeaconConfig().GenesisSlot + 39,
index: 10262,
index: 10272,
},
}

View File

@ -77,9 +77,9 @@ func TestBoundaryAttesterIndices_OK(t *testing.T) {
t.Fatalf("Failed to run BoundaryAttesterIndices: %v", err)
}
if !reflect.DeepEqual(attesterIndices, []uint64{109, 97}) {
if !reflect.DeepEqual(attesterIndices, []uint64{123, 65}) {
t.Errorf("Incorrect boundary attester indices. Wanted: %v, got: %v",
[]uint64{109, 97}, attesterIndices)
[]uint64{123, 65}, attesterIndices)
}
}
@ -119,9 +119,9 @@ func TestAttestingValidatorIndices_OK(t *testing.T) {
t.Fatalf("Could not execute AttestingValidatorIndices: %v", err)
}
if !reflect.DeepEqual(indices, []uint64{1141, 688}) {
if !reflect.DeepEqual(indices, []uint64{1134, 1150}) {
t.Errorf("Could not get incorrect validator indices. Wanted: %v, got: %v",
[]uint64{1141, 688}, indices)
[]uint64{1134, 1150}, indices)
}
}