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 all 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
29 changes: 14 additions & 15 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 @@ -474,11 +472,11 @@ func TestConvertToIndexed_OK(t *testing.T) {
wantedAttestingIndices: []uint64{43, 47},
},
{
aggregationBitfield: bitfield.Bitlist{0x03},
aggregationBitfield: bitfield.Bitlist{0x05},
wantedAttestingIndices: []uint64{47},
},
{
aggregationBitfield: bitfield.Bitlist{0x01},
aggregationBitfield: bitfield.Bitlist{0x04},
wantedAttestingIndices: []uint64{},
},
}
Expand All @@ -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 Expand Up @@ -608,10 +607,10 @@ func TestValidateIndexedAttestation_AboveMaxLength(t *testing.T) {
}

func TestValidateIndexedAttestation_BadAttestationsSignatureSet(t *testing.T) {
beaconState, keys := testutil.DeterministicGenesisState(t, 1000)
beaconState, keys := testutil.DeterministicGenesisState(t, 128)

sig := keys[0].Sign([]byte{'t', 'e', 's', 't'})
list := bitfield.Bitlist{0b11111111}
list := bitfield.Bitlist{0b11111}
var atts []*ethpb.Attestation
for i := uint64(0); i < 1000; i++ {
atts = append(atts, &ethpb.Attestation{
Expand All @@ -629,7 +628,7 @@ func TestValidateIndexedAttestation_BadAttestationsSignatureSet(t *testing.T) {
assert.ErrorContains(t, want, err)

atts = []*ethpb.Attestation{}
list = bitfield.Bitlist{0b00000000}
list = bitfield.Bitlist{0b10000}
for i := uint64(0); i < 1000; i++ {
atts = append(atts, &ethpb.Attestation{
Data: &ethpb.AttestationData{
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
4 changes: 2 additions & 2 deletions beacon-chain/core/epoch/epoch_processing_test.go
Expand Up @@ -25,7 +25,7 @@ func TestUnslashedAttestingIndices_CanSortAndFilter(t *testing.T) {
Data: &ethpb.AttestationData{Source: &ethpb.Checkpoint{Root: make([]byte, 32)},
Target: &ethpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)},
},
AggregationBits: bitfield.Bitlist{0xFF, 0xFF, 0xFF},
AggregationBits: bitfield.Bitlist{0x00, 0xFF, 0xFF, 0xFF},
}
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func TestUnslashedAttestingIndices_DuplicatedAttestations(t *testing.T) {
atts[i] = &pb.PendingAttestation{
Data: &ethpb.AttestationData{Source: &ethpb.Checkpoint{Root: make([]byte, 32)},
Target: &ethpb.Checkpoint{Epoch: 0}},
AggregationBits: bitfield.Bitlist{0xFF, 0xFF, 0xFF},
AggregationBits: bitfield.Bitlist{0x00, 0xFF, 0xFF, 0xFF},
}
}

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
13 changes: 8 additions & 5 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 @@ -149,11 +150,11 @@ func TestProcessAttestations(t *testing.T) {
params.UseMinimalConfig()
defer params.UseMainnetConfig()

validators := uint64(64)
validators := uint64(128)
beaconState, _ := testutil.DeterministicGenesisState(t, validators)
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))

bf := []byte{0xff}
c := helpers.SlotCommitteeCount(validators)
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
8 changes: 4 additions & 4 deletions beacon-chain/core/epoch/precompute/reward_penalty_test.go
Expand Up @@ -27,7 +27,7 @@ func TestProcessRewardsAndPenaltiesPrecompute(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{0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0xC0, 0xC0, 0x01},
InclusionDelay: 1,
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestAttestationDeltaPrecompute(t *testing.T) {
},
BeaconBlockRoot: emptyRoot[:],
},
AggregationBits: bitfield.Bitlist{0xC0, 0xC0, 0xC0, 0xC0, 0x01},
AggregationBits: bitfield.Bitlist{0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x01},
InclusionDelay: 1,
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestAttestationDeltas_ZeroEpoch(t *testing.T) {
},
BeaconBlockRoot: emptyRoot[:],
},
AggregationBits: bitfield.Bitlist{0xC0, 0xC0, 0xC0, 0xC0, 0x01},
AggregationBits: bitfield.Bitlist{0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0xC0, 0xC0, 0x01},
InclusionDelay: 1,
}
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestProcessRewardsAndPenaltiesPrecompute_SlashedInactivePenalty(t *testing.
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{0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0xC0, 0xC0, 0x01},
InclusionDelay: 1,
}
}
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
11 changes: 6 additions & 5 deletions beacon-chain/rpc/beacon/attestations_test.go
Expand Up @@ -517,7 +517,7 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) {
Slot: i,
CommitteeIndex: 0,
},
AggregationBits: bitfield.Bitlist{0b11},
AggregationBits: bitfield.NewBitlist(128 / params.BeaconConfig().SlotsPerEpoch),
},
}
require.NoError(t, db.SaveBlock(ctx, blockExample))
Expand All @@ -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