From b1c6f91199d1cb11d9a702a1a8495336c5e2e487 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 20 Apr 2020 22:14:09 -0400 Subject: [PATCH] Change ListIndexedAttestations to convert attestations from target root --- beacon-chain/rpc/beacon/attestations.go | 14 +++--- beacon-chain/rpc/beacon/attestations_test.go | 52 +++++++++++--------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/beacon-chain/rpc/beacon/attestations.go b/beacon-chain/rpc/beacon/attestations.go index 8480993f0d53..d325ab6b56c2 100644 --- a/beacon-chain/rpc/beacon/attestations.go +++ b/beacon-chain/rpc/beacon/attestations.go @@ -34,13 +34,13 @@ func (s sortableAttestations) Less(i, j int) bool { return s[i].Data.Slot < s[j].Data.Slot } -func mapAttestationsByBlockRoot(ctx context.Context, atts []*ethpb.Attestation) map[[32]byte][]*ethpb.Attestation { +func mapAttestationsByTargetRoot(ctx context.Context, atts []*ethpb.Attestation) map[[32]byte][]*ethpb.Attestation { attsMap := make(map[[32]byte][]*ethpb.Attestation) if len(atts) == 0 { return attsMap } for _, att := range atts { - attsMap[bytesutil.ToBytes32(att.Data.BeaconBlockRoot)] = append(attsMap[bytesutil.ToBytes32(att.Data.BeaconBlockRoot)], att) + attsMap[bytesutil.ToBytes32(att.Data.Target.Root)] = append(attsMap[bytesutil.ToBytes32(att.Data.Target.Root)], att) } return attsMap } @@ -151,16 +151,16 @@ func (bs *Server) ListIndexedAttestations( } // We use the retrieved committees for the block root to convert all attestations // into indexed form effectively. - mappedAttestations := mapAttestationsByBlockRoot(ctx, attsArray) - indexedAtts := make([]*ethpb.IndexedAttestation, numAttestations, numAttestations) + mappedAttestations := mapAttestationsByTargetRoot(ctx, attsArray) + indexedAtts := make([]*ethpb.IndexedAttestation, numAttestations) attIndex := 0 - for blockRoot, atts := range mappedAttestations { - attState, err := bs.StateGen.StateByRoot(ctx, blockRoot) + for targetRoot, atts := range mappedAttestations { + attState, err := bs.StateGen.StateByRoot(ctx, targetRoot) if err != nil { return nil, status.Errorf( codes.Internal, "Could not retrieve state for attestation data block root %v: %v", - blockRoot, + targetRoot, err, ) } diff --git a/beacon-chain/rpc/beacon/attestations_test.go b/beacon-chain/rpc/beacon/attestations_test.go index 69b3081d1eda..c8b822919f43 100644 --- a/beacon-chain/rpc/beacon/attestations_test.go +++ b/beacon-chain/rpc/beacon/attestations_test.go @@ -534,40 +534,42 @@ func TestServer_ListAttestations_Pagination_DefaultPageSize(t *testing.T) { } } -func TestServer_mapAttestationToBlockRoot(t *testing.T) { +func TestServer_mapAttestationToTargetRoot(t *testing.T) { ctx := context.Background() count := uint64(100) atts := make([]*ethpb.Attestation, count, count) - blockRoot1 := bytesutil.ToBytes32([]byte("root1")) - blockRoot2 := bytesutil.ToBytes32([]byte("root2")) + targetRoot1 := bytesutil.ToBytes32([]byte("root1")) + targetRoot2 := bytesutil.ToBytes32([]byte("root2")) for i := uint64(0); i < count; i++ { - var blockRoot [32]byte + var targetRoot [32]byte if i%2 == 0 { - blockRoot = blockRoot1 + targetRoot = targetRoot1 } else { - blockRoot = blockRoot2 + targetRoot = targetRoot2 } atts[i] = ðpb.Attestation{ Data: ðpb.AttestationData{ - BeaconBlockRoot: blockRoot[:], + Target: ðpb.Checkpoint{ + Root: targetRoot[:], + }, }, AggregationBits: bitfield.Bitlist{0b11}, } } - mappedAtts := mapAttestationsByBlockRoot(ctx, atts) + mappedAtts := mapAttestationsByTargetRoot(ctx, atts) wantedMapLen := 2 wantedMapNumberOfElements := 50 if len(mappedAtts) != wantedMapLen { t.Errorf("Expected maped attestation to be of length: %d got: %d", wantedMapLen, len(mappedAtts)) } - if len(mappedAtts[blockRoot1]) != wantedMapNumberOfElements { - t.Errorf("Expected number of attestations per block root to be: %d got: %d", wantedMapNumberOfElements, len(mappedAtts[blockRoot1])) + if len(mappedAtts[targetRoot1]) != wantedMapNumberOfElements { + t.Errorf("Expected number of attestations per block root to be: %d got: %d", wantedMapNumberOfElements, len(mappedAtts[targetRoot1])) } - if len(mappedAtts[blockRoot2]) != wantedMapNumberOfElements { - t.Errorf("Expected maped attestation to be of length: %d got: %d", wantedMapNumberOfElements, len(mappedAtts[blockRoot2])) + if len(mappedAtts[targetRoot2]) != wantedMapNumberOfElements { + t.Errorf("Expected maped attestation to be of length: %d got: %d", wantedMapNumberOfElements, len(mappedAtts[targetRoot2])) } } @@ -612,19 +614,19 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { defer dbTest.TeardownDB(t, db) helpers.ClearCache() ctx := context.Background() - blockRoot1 := bytesutil.ToBytes32([]byte("root")) - blockRoot2 := bytesutil.ToBytes32([]byte("root2")) + targetRoot1 := bytesutil.ToBytes32([]byte("root")) + targetRoot2 := bytesutil.ToBytes32([]byte("root2")) count := params.BeaconConfig().SlotsPerEpoch atts := make([]*ethpb.Attestation, 0, count) atts2 := make([]*ethpb.Attestation, 0, count) for i := uint64(0); i < count; i++ { - var blockRoot [32]byte + var targetRoot [32]byte if i%2 == 0 { - blockRoot = blockRoot1 + targetRoot = targetRoot1 } else { - blockRoot = blockRoot2 + targetRoot = targetRoot2 } blockExample := ðpb.SignedBeaconBlock{ Block: ðpb.BeaconBlock{ @@ -632,9 +634,11 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { Attestations: []*ethpb.Attestation{ { Data: ðpb.AttestationData{ - BeaconBlockRoot: blockRoot[:], - Slot: i, - CommitteeIndex: 0, + Target: ðpb.Checkpoint{ + Root: targetRoot[:], + }, + Slot: i, + CommitteeIndex: 0, }, AggregationBits: bitfield.Bitlist{0b11}, }, @@ -697,25 +701,25 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { StateGen: stategen.New(db, cache.NewStateSummaryCache()), } if err := db.SaveStateSummary(ctx, &pbp2p.StateSummary{ - Root: blockRoot1[:], + Root: targetRoot1[:], Slot: 1, }); err != nil { t.Fatal(err) } if err := db.SaveStateSummary(ctx, &pbp2p.StateSummary{ - Root: blockRoot2[:], + Root: targetRoot2[:], Slot: 2, }); err != nil { t.Fatal(err) } - if err := db.SaveState(ctx, state, bytesutil.ToBytes32(blockRoot1[:])); err != nil { + if err := db.SaveState(ctx, state, bytesutil.ToBytes32(targetRoot1[:])); err != nil { t.Fatal(err) } if err := state.SetSlot(state.Slot() + 1); err != nil { t.Fatal(err) } - if err := db.SaveState(ctx, state, bytesutil.ToBytes32(blockRoot2[:])); err != nil { + if err := db.SaveState(ctx, state, bytesutil.ToBytes32(targetRoot2[:])); err != nil { t.Fatal(err) } res, err := bs.ListIndexedAttestations(ctx, ðpb.ListIndexedAttestationsRequest{