Fix Errors with Attestation to Current Chainhead (#1990)

* fix errors

* fix bit check to little endian

* fix all tests
This commit is contained in:
Nishant Das 2019-03-15 00:29:44 +08:00 committed by Raul Jordan
parent 2c1f29922e
commit 00ca1d7991
9 changed files with 39 additions and 25 deletions

View File

@ -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{},
}

View File

@ -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)
}

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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(

View File

@ -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++ {

View File

@ -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

View File

@ -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.

View File

@ -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)