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

Change ListIndexedAttestations to convert attestations from target root #5548

Merged
merged 1 commit into from
Apr 21, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions beacon-chain/rpc/beacon/attestations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
)
}
Expand Down
52 changes: 28 additions & 24 deletions beacon-chain/rpc/beacon/attestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] = &ethpb.Attestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: blockRoot[:],
Target: &ethpb.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]))
}
}

Expand Down Expand Up @@ -612,29 +614,31 @@ 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 := &ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{
Body: &ethpb.BeaconBlockBody{
Attestations: []*ethpb.Attestation{
{
Data: &ethpb.AttestationData{
BeaconBlockRoot: blockRoot[:],
Slot: i,
CommitteeIndex: 0,
Target: &ethpb.Checkpoint{
Root: targetRoot[:],
},
Slot: i,
CommitteeIndex: 0,
},
AggregationBits: bitfield.Bitlist{0b11},
},
Expand Down Expand Up @@ -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, &ethpb.ListIndexedAttestationsRequest{
Expand Down