diff --git a/beacon-chain/attestation/service_test.go b/beacon-chain/attestation/service_test.go index 3e875e31b..ce3cd0fca 100644 --- a/beacon-chain/attestation/service_test.go +++ b/beacon-chain/attestation/service_test.go @@ -33,7 +33,7 @@ func TestUpdateLatestAttestation_UpdatesLatest(t *testing.T) { service := NewAttestationService(context.Background(), &Config{BeaconDB: beaconDB}) attestation := &pb.Attestation{ - AggregationBitfield: []byte{0x01}, + AggregationBitfield: []byte{0x80}, Data: &pb.AttestationData{ Slot: 5, }, @@ -72,7 +72,7 @@ func TestAttestationPool_UpdatesAttestationPool(t *testing.T) { service := NewAttestationService(context.Background(), &Config{BeaconDB: beaconDB}) attestation := &pb.Attestation{ - AggregationBitfield: []byte{0x01}, + AggregationBitfield: []byte{0x80}, Data: &pb.AttestationData{}, } diff --git a/beacon-chain/core/epoch/epoch_operations_test.go b/beacon-chain/core/epoch/epoch_operations_test.go index 3712157ac..095b2f9d0 100644 --- a/beacon-chain/core/epoch/epoch_operations_test.go +++ b/beacon-chain/core/epoch/epoch_operations_test.go @@ -345,7 +345,7 @@ func TestHeadAttestations_InvalidRange(t *testing.T) { func TestWinningRoot_AccurateRoot(t *testing.T) { state := buildState(params.BeaconConfig().GenesisSlot, 100) var participationBitfield []byte - participationBitfield = append(participationBitfield, byte(0x01)) + participationBitfield = append(participationBitfield, byte(0x80)) // Generate 10 roots ([]byte{100}...[]byte{110}) var attestations []*pb.PendingAttestation @@ -404,7 +404,7 @@ func TestAttestingValidators_MatchActive(t *testing.T) { Slot: params.BeaconConfig().GenesisSlot, CrosslinkDataRootHash32: []byte{byte(i + 100)}, }, - AggregationBitfield: []byte{0x03}, + AggregationBitfield: []byte{0xC0}, } attestations = append(attestations, attestation) } @@ -455,7 +455,7 @@ func TestTotalAttestingBalance_CorrectBalance(t *testing.T) { CrosslinkDataRootHash32: []byte{byte(i + 100)}, }, // All validators attested to the above roots. - AggregationBitfield: []byte{0x03}, + AggregationBitfield: []byte{0xC0}, } attestations = append(attestations, attestation) } diff --git a/beacon-chain/core/helpers/committee_test.go b/beacon-chain/core/helpers/committee_test.go index 2f08893ca..9691e7a07 100644 --- a/beacon-chain/core/helpers/committee_test.go +++ b/beacon-chain/core/helpers/committee_test.go @@ -319,21 +319,21 @@ func TestAttestationParticipants_OK(t *testing.T) { attestationSlot: params.BeaconConfig().GenesisSlot + 2, stateSlot: params.BeaconConfig().GenesisSlot + 5, shard: 2, - bitfield: []byte{0x03}, + bitfield: []byte{0xC0}, wanted: []uint64{11, 121}, }, { attestationSlot: params.BeaconConfig().GenesisSlot + 1, stateSlot: params.BeaconConfig().GenesisSlot + 10, shard: 1, - bitfield: []byte{0x01}, + bitfield: []byte{0x80}, wanted: []uint64{4}, }, { attestationSlot: params.BeaconConfig().GenesisSlot + 10, stateSlot: params.BeaconConfig().GenesisSlot + 10, shard: 10, - bitfield: []byte{0x03}, + bitfield: []byte{0xC0}, wanted: []uint64{14, 30}, }, } @@ -381,7 +381,7 @@ func TestAttestationParticipants_IncorrectBitfield(t *testing.T) { } func TestVerifyBitfield_OK(t *testing.T) { - bitfield := []byte{0xff} + bitfield := []byte{0xFF} committeeSize := 8 isValidated, err := VerifyBitfield(bitfield, committeeSize) @@ -393,7 +393,7 @@ func TestVerifyBitfield_OK(t *testing.T) { t.Error("bitfield is not validated when it was supposed to be") } - bitfield = []byte{0xff, 0x80} + bitfield = []byte{0xff, 0x01} committeeSize = 9 isValidated, err = VerifyBitfield(bitfield, committeeSize) @@ -405,7 +405,7 @@ func TestVerifyBitfield_OK(t *testing.T) { t.Error("bitfield is validated when it was supposed to be") } - bitfield = []byte{0xff, 0x01} + bitfield = []byte{0xff, 0x80} committeeSize = 10 isValidated, err = VerifyBitfield(bitfield, committeeSize) if err != nil { diff --git a/beacon-chain/core/validators/validator_test.go b/beacon-chain/core/validators/validator_test.go index ccedb6db8..7339be593 100644 --- a/beacon-chain/core/validators/validator_test.go +++ b/beacon-chain/core/validators/validator_test.go @@ -67,9 +67,9 @@ func TestBoundaryAttesterIndices_OK(t *testing.T) { boundaryAttestations := []*pb.PendingAttestation{ {Data: &pb.AttestationData{Slot: params.BeaconConfig().GenesisSlot}, - AggregationBitfield: []byte{0x03}}, // returns indices 242 + AggregationBitfield: []byte{0xC0}}, // returns indices 242 {Data: &pb.AttestationData{Slot: params.BeaconConfig().GenesisSlot}, - AggregationBitfield: []byte{0x03}}, // returns indices 237,224,2 + AggregationBitfield: []byte{0xC0}}, // returns indices 237,224,2 } attesterIndices, err := ValidatorIndices(context.Background(), state, boundaryAttestations) @@ -119,9 +119,9 @@ func TestAttestingValidatorIndices_OK(t *testing.T) { t.Fatalf("Could not execute AttestingValidatorIndices: %v", err) } - if !reflect.DeepEqual(indices, []uint64{1117, 333}) { + if !reflect.DeepEqual(indices, []uint64{1141, 688}) { t.Errorf("Could not get incorrect validator indices. Wanted: %v, got: %v", - []uint64{1117, 333}, indices) + []uint64{1141, 688}, indices) } } diff --git a/shared/bitutil/BUILD.bazel b/shared/bitutil/BUILD.bazel index 6989adf06..f0f7b9a51 100644 --- a/shared/bitutil/BUILD.bazel +++ b/shared/bitutil/BUILD.bazel @@ -5,7 +5,10 @@ go_library( srcs = ["bit.go"], importpath = "github.com/prysmaticlabs/prysm/shared/bitutil", visibility = ["//visibility:public"], - deps = ["@com_github_steakknife_hamming//:go_default_library"], + deps = [ + "//shared/mathutil:go_default_library", + "@com_github_steakknife_hamming//:go_default_library", + ], ) go_test( diff --git a/shared/bitutil/bit.go b/shared/bitutil/bit.go index aa3200d71..94312366f 100644 --- a/shared/bitutil/bit.go +++ b/shared/bitutil/bit.go @@ -2,7 +2,8 @@ package bitutil import ( "fmt" - "math" + + "github.com/prysmaticlabs/prysm/shared/mathutil" "github.com/steakknife/hamming" ) @@ -22,7 +23,7 @@ func CheckBit(bitfield []byte, index int) (bool, error) { len(bitfield), chunkLocation-1) } - field := bitfield[chunkLocation-1] >> uint(indexLocation-1) + field := bitfield[chunkLocation-1] >> (7 - uint(indexLocation-1)) return field%2 != 0, nil } @@ -40,7 +41,7 @@ func BitLength(b int) int { // SetBitfield takes an index and returns bitfield with the index flipped. func SetBitfield(index int) []byte { chunkLocation := index / 8 - indexLocation := math.Pow(2, 7-float64(index%8)) + indexLocation := mathutil.PowerOf2(uint64(7 - (index % 8))) var bitfield []byte for i := 0; i < chunkLocation; i++ { diff --git a/shared/bitutil/bit_test.go b/shared/bitutil/bit_test.go index 34ef542f1..f7913fc1f 100644 --- a/shared/bitutil/bit_test.go +++ b/shared/bitutil/bit_test.go @@ -11,9 +11,9 @@ func TestCheckBit(t *testing.T) { b int c bool }{ - {a: []byte{200}, b: 3, c: true}, //11001000 - {a: []byte{148}, b: 2, c: true}, //10010100 - {a: []byte{146}, b: 3, c: false}, //10010010 + {a: []byte{200}, b: 4, c: true}, //11001000 + {a: []byte{148}, b: 3, c: true}, //10010100 + {a: []byte{146}, b: 7, c: false}, //10010010 {a: []byte{179}, b: 0, c: true}, //10110011 {a: []byte{49}, b: 1, c: false}, //00110001 {a: []byte{49}, b: 100, c: false}, //00110001 diff --git a/validator/client/validator_attest.go b/validator/client/validator_attest.go index 53ca426d2..b84967aab 100644 --- a/validator/client/validator_attest.go +++ b/validator/client/validator_attest.go @@ -97,7 +97,17 @@ func (v *validator) AttestToBlockHead(ctx context.Context, slot uint64) { // Note: calling get_attestation_participants(state, attestation.data, attestation.aggregation_bitfield) // should return a list of length equal to 1, containing validator_index. - aggregationBitfield := bitutil.SetBitfield(int(validatorIndexRes.Index)) + // Find the index in committee to be used for + // the aggregation bitfield + var indexInCommittee int + for i, vIndex := range resp.Committee { + if vIndex == validatorIndexRes.Index { + indexInCommittee = i + break + } + } + + aggregationBitfield := bitutil.SetBitfield(indexInCommittee) attestation.AggregationBitfield = aggregationBitfield // TODO(#1366): Use BLS to generate an aggregate signature. diff --git a/validator/client/validator_attest_test.go b/validator/client/validator_attest_test.go index f18c139aa..cfa86c418 100644 --- a/validator/client/validator_attest_test.go +++ b/validator/client/validator_attest_test.go @@ -115,7 +115,7 @@ func TestAttestToBlockHead_AttestsCorrectly(t *testing.T) { validator, m, finish := setup(t) defer finish() - validatorIndex := uint64(4) + validatorIndex := uint64(7) committee := []uint64{0, 3, 4, 2, validatorIndex, 6, 8, 9, 10} m.validatorClient.EXPECT().ValidatorIndex( gomock.Any(), // ctx @@ -166,7 +166,7 @@ func TestAttestToBlockHead_AttestsCorrectly(t *testing.T) { CustodyBitfield: make([]byte, (len(committee)+7)/8), AggregateSignature: []byte("signed"), } - aggregationBitfield := bitutil.SetBitfield(int(validatorIndex)) + aggregationBitfield := bitutil.SetBitfield(4) expectedAttestation.AggregationBitfield = aggregationBitfield if !proto.Equal(generatedAttestation, expectedAttestation) { t.Errorf("Incorrectly attested head, wanted %v, received %v", expectedAttestation, generatedAttestation)