Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error on AttestingIndices bitfield length check #8285

Merged
merged 15 commits into from Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion beacon-chain/blockchain/process_attestation_helpers.go
Expand Up @@ -129,7 +129,10 @@ func (s *Service) verifyAttestation(ctx context.Context, baseState *stateTrie.Be
if err != nil {
return nil, err
}
indexedAtt := attestationutil.ConvertToIndexed(ctx, a, committee)
indexedAtt, err := attestationutil.ConvertToIndexed(ctx, a, committee)
if err != nil {
return nil, err
}
if err := blocks.VerifyIndexedAttestation(ctx, baseState, indexedAtt); err != nil {
return nil, errors.Wrap(err, "could not verify indexed attestation")
}
Expand Down
5 changes: 4 additions & 1 deletion beacon-chain/blockchain/process_block.go
Expand Up @@ -382,7 +382,10 @@ func (s *Service) insertBlockAndAttestationsToForkChoiceStore(ctx context.Contex
if err != nil {
return err
}
indices := attestationutil.AttestingIndices(a.AggregationBits, committee)
indices, err := attestationutil.AttestingIndices(a.AggregationBits, committee)
if err != nil {
return err
}
s.forkChoiceStore.ProcessAttestation(ctx, indices, bytesutil.ToBytes32(a.Data.BeaconBlockRoot), a.Data.Target.Epoch)
}
return nil
Expand Down
10 changes: 8 additions & 2 deletions beacon-chain/core/blocks/attestation.go
Expand Up @@ -208,7 +208,10 @@ func ProcessAttestationNoVerifySignature(
if err != nil {
return nil, err
}
indexedAtt := attestationutil.ConvertToIndexed(ctx, att, committee)
indexedAtt, err := attestationutil.ConvertToIndexed(ctx, att, committee)
if err != nil {
return nil, err
}
if err := attestationutil.IsValidAttestationIndices(ctx, indexedAtt); err != nil {
return nil, err
}
Expand Down Expand Up @@ -280,7 +283,10 @@ func VerifyAttestationSignature(ctx context.Context, beaconState *stateTrie.Beac
if err != nil {
return err
}
indexedAtt := attestationutil.ConvertToIndexed(ctx, att, committee)
indexedAtt, err := attestationutil.ConvertToIndexed(ctx, att, committee)
if err != nil {
return err
}
return VerifyIndexedAttestation(ctx, beaconState, indexedAtt)
}

Expand Down
19 changes: 9 additions & 10 deletions beacon-chain/core/blocks/attestation_test.go
Expand Up @@ -226,7 +226,7 @@ func TestProcessAttestations_OK(t *testing.T) {

committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
require.NoError(t, err)
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(att.AggregationBits, committee)
require.NoError(t, err)
sigs := make([]bls.Signature, len(attestingIndices))
for i, indice := range attestingIndices {
Expand All @@ -253,10 +253,9 @@ func TestProcessAggregatedAttestation_OverlappingBits(t *testing.T) {
Source: &ethpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
Target: &ethpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
})
aggBits1 := bitfield.NewBitlist(4)
aggBits1 := bitfield.NewBitlist(3)
aggBits1.SetBitAt(0, true)
aggBits1.SetBitAt(1, true)
aggBits1.SetBitAt(2, true)
att1 := &ethpb.Attestation{
Data: data,
AggregationBits: aggBits1,
Expand All @@ -269,7 +268,7 @@ func TestProcessAggregatedAttestation_OverlappingBits(t *testing.T) {

committee, err := helpers.BeaconCommitteeFromState(beaconState, att1.Data.Slot, att1.Data.CommitteeIndex)
require.NoError(t, err)
attestingIndices1 := attestationutil.AttestingIndices(att1.AggregationBits, committee)
attestingIndices1, err := attestationutil.AttestingIndices(att1.AggregationBits, committee)
require.NoError(t, err)
sigs := make([]bls.Signature, len(attestingIndices1))
for i, indice := range attestingIndices1 {
Expand All @@ -281,18 +280,17 @@ func TestProcessAggregatedAttestation_OverlappingBits(t *testing.T) {
}
att1.Signature = bls.AggregateSignatures(sigs).Marshal()

aggBits2 := bitfield.NewBitlist(4)
aggBits2 := bitfield.NewBitlist(3)
aggBits2.SetBitAt(1, true)
aggBits2.SetBitAt(2, true)
aggBits2.SetBitAt(3, true)
att2 := &ethpb.Attestation{
Data: data,
AggregationBits: aggBits2,
}

committee, err = helpers.BeaconCommitteeFromState(beaconState, att2.Data.Slot, att2.Data.CommitteeIndex)
require.NoError(t, err)
attestingIndices2 := attestationutil.AttestingIndices(att2.AggregationBits, committee)
attestingIndices2, err := attestationutil.AttestingIndices(att2.AggregationBits, committee)
require.NoError(t, err)
sigs = make([]bls.Signature, len(attestingIndices2))
for i, indice := range attestingIndices2 {
Expand Down Expand Up @@ -333,7 +331,7 @@ func TestProcessAggregatedAttestation_NoOverlappingBits(t *testing.T) {

committee, err := helpers.BeaconCommitteeFromState(beaconState, att1.Data.Slot, att1.Data.CommitteeIndex)
require.NoError(t, err)
attestingIndices1 := attestationutil.AttestingIndices(att1.AggregationBits, committee)
attestingIndices1, err := attestationutil.AttestingIndices(att1.AggregationBits, committee)
require.NoError(t, err)
sigs := make([]bls.Signature, len(attestingIndices1))
for i, indice := range attestingIndices1 {
Expand All @@ -356,7 +354,7 @@ func TestProcessAggregatedAttestation_NoOverlappingBits(t *testing.T) {

committee, err = helpers.BeaconCommitteeFromState(beaconState, att2.Data.Slot, att2.Data.CommitteeIndex)
require.NoError(t, err)
attestingIndices2 := attestationutil.AttestingIndices(att2.AggregationBits, committee)
attestingIndices2, err := attestationutil.AttestingIndices(att2.AggregationBits, committee)
require.NoError(t, err)
sigs = make([]bls.Signature, len(attestingIndices2))
for i, indice := range attestingIndices2 {
Expand Down Expand Up @@ -498,7 +496,8 @@ func TestConvertToIndexed_OK(t *testing.T) {

committee, err := helpers.BeaconCommitteeFromState(state, attestation.Data.Slot, attestation.Data.CommitteeIndex)
require.NoError(t, err)
ia := attestationutil.ConvertToIndexed(context.Background(), attestation, committee)
ia, err := attestationutil.ConvertToIndexed(context.Background(), attestation, committee)
require.NoError(t, err)
assert.DeepEqual(t, wanted, ia, "Convert attestation to indexed attestation didn't result as wanted")
}
}
Expand Down
5 changes: 4 additions & 1 deletion beacon-chain/core/blocks/signature.go
Expand Up @@ -138,7 +138,10 @@ func createAttestationSignatureSet(ctx context.Context, beaconState *stateTrie.B
if err != nil {
return nil, err
}
ia := attestationutil.ConvertToIndexed(ctx, a, c)
ia, err := attestationutil.ConvertToIndexed(ctx, a, c)
if err != nil {
return nil, err
}
if err := attestationutil.IsValidAttestationIndices(ctx, ia); err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion beacon-chain/core/epoch/epoch_processing.go
Expand Up @@ -349,7 +349,10 @@ func UnslashedAttestingIndices(state *stateTrie.BeaconState, atts []*pb.PendingA
if err != nil {
return nil, err
}
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(att.AggregationBits, committee)
if err != nil {
return nil, err
}
// Create a set for attesting indices
for _, index := range attestingIndices {
if !seen[index] {
Expand Down
5 changes: 4 additions & 1 deletion beacon-chain/core/epoch/precompute/attestation.go
Expand Up @@ -47,7 +47,10 @@ func ProcessAttestations(
if err != nil {
return nil, nil, err
}
indices := attestationutil.AttestingIndices(a.AggregationBits, committee)
indices, err := attestationutil.AttestingIndices(a.AggregationBits, committee)
if err != nil {
return nil, nil, err
}
vp = UpdateValidator(vp, v, indices, a, a.Data.Slot)
}

Expand Down
11 changes: 7 additions & 4 deletions beacon-chain/core/epoch/precompute/attestation_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
Expand Down Expand Up @@ -152,8 +153,8 @@ func TestProcessAttestations(t *testing.T) {
validators := uint64(64)
beaconState, _ := testutil.DeterministicGenesisState(t, validators)
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))

bf := []byte{0xff}
c := helpers.SlotCommitteeCount(64)
bf := bitfield.NewBitlist(c)
att1 := &ethpb.Attestation{Data: &ethpb.AttestationData{
Target: &ethpb.Checkpoint{Epoch: 0}},
AggregationBits: bf}
Expand Down Expand Up @@ -183,15 +184,17 @@ func TestProcessAttestations(t *testing.T) {

committee, err := helpers.BeaconCommitteeFromState(beaconState, att1.Data.Slot, att1.Data.CommitteeIndex)
require.NoError(t, err)
indices := attestationutil.AttestingIndices(att1.AggregationBits, committee)
indices, err := attestationutil.AttestingIndices(att1.AggregationBits, committee)
require.NoError(t, err)
for _, i := range indices {
if !pVals[i].IsPrevEpochAttester {
t.Error("Not a prev epoch attester")
}
}
committee, err = helpers.BeaconCommitteeFromState(beaconState, att2.Data.Slot, att2.Data.CommitteeIndex)
require.NoError(t, err)
indices = attestationutil.AttestingIndices(att2.AggregationBits, committee)
indices, err = attestationutil.AttestingIndices(att2.AggregationBits, committee)
require.NoError(t, err)
for _, i := range indices {
assert.Equal(t, true, pVals[i].IsPrevEpochAttester, "Not a prev epoch attester")
assert.Equal(t, true, pVals[i].IsPrevEpochTargetAttester, "Not a prev epoch target attester")
Expand Down
6 changes: 4 additions & 2 deletions beacon-chain/core/state/transition_test.go
Expand Up @@ -415,7 +415,8 @@ func createFullBlockWithOperations(t *testing.T) (*beaconstate.BeaconState,

committee, err := helpers.BeaconCommitteeFromState(beaconState, blockAtt.Data.Slot, blockAtt.Data.CommitteeIndex)
assert.NoError(t, err)
attestingIndices := attestationutil.AttestingIndices(blockAtt.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(blockAtt.AggregationBits, committee)
require.NoError(t, err)
assert.NoError(t, err)
hashTreeRoot, err = helpers.ComputeSigningRoot(blockAtt.Data, domain)
assert.NoError(t, err)
Expand Down Expand Up @@ -712,7 +713,8 @@ func TestProcessBlk_AttsBasedOnValidatorCount(t *testing.T) {

committee, err := helpers.BeaconCommitteeFromState(s, att.Data.Slot, att.Data.CommitteeIndex)
assert.NoError(t, err)
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(att.AggregationBits, committee)
require.NoError(t, err)
domain, err := helpers.Domain(s.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, s.GenesisValidatorRoot())
require.NoError(t, err)
sigs := make([]bls.Signature, len(attestingIndices))
Expand Down
10 changes: 8 additions & 2 deletions beacon-chain/rpc/beacon/attestations.go
Expand Up @@ -173,7 +173,10 @@ func (bs *Server) ListIndexedAttestations(
err,
)
}
idxAtt := attestationutil.ConvertToIndexed(ctx, att, committee)
idxAtt, err := attestationutil.ConvertToIndexed(ctx, att, committee)
if err != nil {
return nil, err
}
indexedAtts = append(indexedAtts, idxAtt)
}
}
Expand Down Expand Up @@ -311,7 +314,10 @@ func (bs *Server) StreamIndexedAttestations(
continue
}
committee := committeesForSlot.Committees[att.Data.CommitteeIndex]
idxAtt := attestationutil.ConvertToIndexed(stream.Context(), att, committee.ValidatorIndices)
idxAtt, err := attestationutil.ConvertToIndexed(stream.Context(), att, committee.ValidatorIndices)
if err != nil {
continue
}
if err := stream.Send(idxAtt); err != nil {
return status.Errorf(codes.Unavailable, "Could not send over stream: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions beacon-chain/rpc/beacon/attestations_test.go
Expand Up @@ -539,15 +539,15 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) {
att := atts[i]
committee, err := helpers.BeaconCommitteeFromState(state, att.Data.Slot, att.Data.CommitteeIndex)
require.NoError(t, err)
idxAtt := attestationutil.ConvertToIndexed(ctx, atts[i], committee)
idxAtt, err := attestationutil.ConvertToIndexed(ctx, atts[i], committee)
require.NoError(t, err, "Could not convert attestation to indexed")
indexedAtts[i] = idxAtt
}
for i := 0; i < len(atts2); i++ {
att := atts2[i]
committee, err := helpers.BeaconCommitteeFromState(state, att.Data.Slot, att.Data.CommitteeIndex)
require.NoError(t, err)
idxAtt := attestationutil.ConvertToIndexed(ctx, atts2[i], committee)
idxAtt, err := attestationutil.ConvertToIndexed(ctx, atts2[i], committee)
require.NoError(t, err, "Could not convert attestation to indexed")
indexedAtts[i+len(atts)] = idxAtt
}
Expand Down Expand Up @@ -646,7 +646,7 @@ func TestServer_ListIndexedAttestations_OldEpoch(t *testing.T) {
att := atts[i]
committee, err := helpers.BeaconCommitteeFromState(state, att.Data.Slot, att.Data.CommitteeIndex)
require.NoError(t, err)
idxAtt := attestationutil.ConvertToIndexed(ctx, atts[i], committee)
idxAtt, err := attestationutil.ConvertToIndexed(ctx, atts[i], committee)
require.NoError(t, err, "Could not convert attestation to indexed")
indexedAtts[i] = idxAtt
}
Expand Down Expand Up @@ -949,7 +949,8 @@ func TestServer_StreamIndexedAttestations_OK(t *testing.T) {
allAtts = append(allAtts, aggAtts...)
for _, att := range aggAtts {
committee := committees[att.Data.Slot].Committees[att.Data.CommitteeIndex]
idxAtt := attestationutil.ConvertToIndexed(ctx, att, committee.ValidatorIndices)
idxAtt, err := attestationutil.ConvertToIndexed(ctx, att, committee.ValidatorIndices)
require.NoError(t, err)
indexedAtts[dataRoot] = append(indexedAtts[dataRoot], idxAtt)
}
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/rpc/beacon/validators_test.go
Expand Up @@ -1633,7 +1633,7 @@ func TestGetValidatorPerformance_OK(t *testing.T) {
Target: &ethpb.Checkpoint{Root: make([]byte, 32)},
Source: &ethpb.Checkpoint{Root: make([]byte, 32)},
},
AggregationBits: bitfield.Bitlist{0xC0, 0xC0, 0xC0, 0xC0, 0x01},
AggregationBits: bitfield.Bitlist{},
InclusionDelay: 1,
}
}
Expand Down Expand Up @@ -1971,7 +1971,7 @@ func TestServer_GetIndividualVotes_Working(t *testing.T) {
require.NoError(t, beaconState.SetValidators(stateWithValidators.Validators()))
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))

bf := []byte{0xff}
bf := bitfield.NewBitlist(helpers.SlotCommitteeCount(validators))
att1 := testutil.NewAttestation()
att1.AggregationBits = bf
att2 := testutil.NewAttestation()
Expand Down
5 changes: 4 additions & 1 deletion beacon-chain/rpc/debug/block.go
Expand Up @@ -77,7 +77,10 @@ func (ds *Server) GetInclusionSlot(ctx context.Context, req *pbrpc.InclusionSlot
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get committee: %v", err)
}
indices := attestationutil.AttestingIndices(a.AggregationBits, c)
indices, err := attestationutil.AttestingIndices(a.AggregationBits, c)
if err != nil {
return nil, err
}
for _, i := range indices {
if req.Id == i && req.Slot == a.Data.Slot {
inclusionSlot = blk.Block.Slot
Expand Down
10 changes: 8 additions & 2 deletions beacon-chain/rpc/validator/aggregator_test.go
Expand Up @@ -230,7 +230,10 @@ func generateAtt(state *beaconstate.BeaconState, index uint64, privKeys []bls.Se
if err != nil {
return nil, err
}
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(att.AggregationBits, committee)
if err != nil {
return nil, err
}

sigs := make([]bls.Signature, len(attestingIndices))
zeroSig := [96]byte{}
Expand Down Expand Up @@ -266,7 +269,10 @@ func generateUnaggregatedAtt(state *beaconstate.BeaconState, index uint64, privK
if err != nil {
return nil, err
}
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(att.AggregationBits, committee)
if err != nil {
return nil, err
}
domain, err := helpers.Domain(state.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, params.BeaconConfig().ZeroHash[:])
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/rpc/validator/proposer_test.go
Expand Up @@ -1860,7 +1860,8 @@ func TestProposer_FilterAttestation(t *testing.T) {
})
committee, err := helpers.BeaconCommitteeFromState(state, atts[i].Data.Slot, atts[i].Data.CommitteeIndex)
assert.NoError(t, err)
attestingIndices := attestationutil.AttestingIndices(atts[i].AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(atts[i].AggregationBits, committee)
require.NoError(t, err)
assert.NoError(t, err)
domain, err := helpers.Domain(state.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, params.BeaconConfig().ZeroHash[:])
require.NoError(t, err)
Expand Down
9 changes: 6 additions & 3 deletions beacon-chain/sync/pending_attestations_queue_test.go
Expand Up @@ -80,7 +80,8 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) {

committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
assert.NoError(t, err)
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(att.AggregationBits, committee)
require.NoError(t, err)
assert.NoError(t, err)
attesterDomain, err := helpers.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot())
require.NoError(t, err)
Expand Down Expand Up @@ -191,7 +192,8 @@ func TestProcessPendingAtts_NoBroadcastWithBadSignature(t *testing.T) {
}
committee, err := helpers.BeaconCommitteeFromState(s, att.Data.Slot, att.Data.CommitteeIndex)
assert.NoError(t, err)
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(att.AggregationBits, committee)
require.NoError(t, err)
assert.NoError(t, err)
attesterDomain, err := helpers.Domain(s.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, s.GenesisValidatorRoot())
require.NoError(t, err)
Expand Down Expand Up @@ -263,7 +265,8 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) {

committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
assert.NoError(t, err)
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
attestingIndices, err := attestationutil.AttestingIndices(att.AggregationBits, committee)
require.NoError(t, err)
assert.NoError(t, err)
attesterDomain, err := helpers.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot())
require.NoError(t, err)
Expand Down