Skip to content

Commit

Permalink
Fix listindexed attestations and detect historic attestations (#5321)
Browse files Browse the repository at this point in the history
* fix list indexed attestations

* fix tests

* goimports

* names
  • Loading branch information
shayzluf authored Apr 6, 2020
1 parent a0cafff commit 35a6df7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
50 changes: 30 additions & 20 deletions beacon-chain/rpc/beacon/attestations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
"github.com/prysmaticlabs/prysm/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/shared/attestationutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/pagination"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -102,7 +103,6 @@ func (bs *Server) ListIndexedAttestations(
) (*ethpb.ListIndexedAttestationsResponse, error) {
blocks := make([]*ethpb.SignedBeaconBlock, 0)
var err error
epoch := helpers.SlotToEpoch(bs.GenesisTimeFetcher.CurrentSlot())
switch q := req.QueryFilter.(type) {
case *ethpb.ListIndexedAttestationsRequest_GenesisEpoch:
blocks, err = bs.BeaconDB.Blocks(ctx, filters.NewFilter().SetStartEpoch(0).SetEndEpoch(0))
Expand Down Expand Up @@ -135,30 +135,40 @@ func (bs *Server) ListIndexedAttestations(
}, nil
}

committeesBySlot, _, err := bs.retrieveCommitteesForEpoch(ctx, epoch)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not retrieve committees for epoch %d: %v",
epoch,
err,
)
}

// We use the retrieved committees for the epoch to convert all attestations
// into indexed form effectively.
indexedAtts := make([]*ethpb.IndexedAttestation, numAttestations, numAttestations)
startSlot := helpers.StartSlot(epoch)
endSlot := startSlot + params.BeaconConfig().SlotsPerEpoch
for i := 0; i < len(indexedAtts); i++ {
for i := 0; i < len(atts); i++ {
att := atts[i]
// Out of range check, the attestation slot cannot be greater
// the last slot of the requested epoch or smaller than its start slot
// given committees are accessed as a map of slot -> commitees list, where there are
// SLOTS_PER_EPOCH keys in the map.
if att.Data.Slot < startSlot || att.Data.Slot > endSlot {
continue
epoch := helpers.SlotToEpoch(att.Data.Slot)
attState, err := bs.BeaconDB.State(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot))
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not retrieve state for attestation data block root %v: %v",
att.Data.BeaconBlockRoot,
err,
)
}
activeIndices, err := helpers.ActiveValidatorIndices(attState, epoch)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not retrieve active validator indices for epoch %d: %v",
epoch,
err,
)
}
seed, err := helpers.Seed(attState, epoch, params.BeaconConfig().DomainBeaconAttester)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not seed for epoch %d: %v",
epoch,
err,
)
}
committeesBySlot, err := computeCommittees(helpers.StartSlot(epoch), activeIndices, seed)
committee := committeesBySlot[att.Data.Slot].Committees[att.Data.CommitteeIndex]
idxAtt := attestationutil.ConvertToIndexed(ctx, atts[i], committee.ValidatorIndices)
indexedAtts[i] = idxAtt
Expand Down
29 changes: 11 additions & 18 deletions beacon-chain/rpc/beacon/attestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,30 +563,29 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) {

// We setup 128 validators.
numValidators := 128
headState := setupActiveValidators(t, db, numValidators)
state := setupActiveValidators(t, db, numValidators)

randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
for i := 0; i < len(randaoMixes); i++ {
randaoMixes[i] = make([]byte, 32)
}
if err := headState.SetRandaoMixes(randaoMixes); err != nil {
if err := state.SetRandaoMixes(randaoMixes); err != nil {
t.Fatal(err)
}

activeIndices, err := helpers.ActiveValidatorIndices(headState, 0)
activeIndices, err := helpers.ActiveValidatorIndices(state, 0)
if err != nil {
t.Fatal(err)
}
epoch := uint64(0)
attesterSeed, err := helpers.Seed(headState, epoch, params.BeaconConfig().DomainBeaconAttester)
attesterSeed, err := helpers.Seed(state, epoch, params.BeaconConfig().DomainBeaconAttester)
if err != nil {
t.Fatal(err)
}
committees, err := computeCommittees(helpers.StartSlot(epoch), activeIndices, attesterSeed)
if err != nil {
t.Fatal(err)
}

// Next up we convert the test attestations to indexed form:
indexedAtts := make([]*ethpb.IndexedAttestation, len(atts), len(atts))
for i := 0; i < len(indexedAtts); i++ {
Expand All @@ -601,14 +600,11 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) {

bs := &Server{
BeaconDB: db,
HeadFetcher: &mock.ChainService{
State: headState,
},
GenesisTimeFetcher: &mock.ChainService{
Genesis: time.Now(),
},
}

db.SaveState(ctx, state, bytesutil.ToBytes32([]byte("root")))
res, err := bs.ListIndexedAttestations(ctx, &ethpb.ListIndexedAttestationsRequest{
QueryFilter: &ethpb.ListIndexedAttestationsRequest_GenesisEpoch{
GenesisEpoch: true,
Expand Down Expand Up @@ -666,24 +662,24 @@ func TestServer_ListIndexedAttestations_ArchivedEpoch(t *testing.T) {

// We setup 128 validators.
numValidators := 128
headState := setupActiveValidators(t, db, numValidators)
state := setupActiveValidators(t, db, numValidators)

randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
for i := 0; i < len(randaoMixes); i++ {
randaoMixes[i] = make([]byte, 32)
}
if err := headState.SetRandaoMixes(randaoMixes); err != nil {
if err := state.SetRandaoMixes(randaoMixes); err != nil {
t.Fatal(err)
}
if err := headState.SetSlot(startSlot); err != nil {
if err := state.SetSlot(startSlot); err != nil {
t.Fatal(err)
}

activeIndices, err := helpers.ActiveValidatorIndices(headState, epoch)
activeIndices, err := helpers.ActiveValidatorIndices(state, epoch)
if err != nil {
t.Fatal(err)
}
attesterSeed, err := helpers.Seed(headState, epoch, params.BeaconConfig().DomainBeaconAttester)
attesterSeed, err := helpers.Seed(state, epoch, params.BeaconConfig().DomainBeaconAttester)
if err != nil {
t.Fatal(err)
}
Expand All @@ -706,14 +702,11 @@ func TestServer_ListIndexedAttestations_ArchivedEpoch(t *testing.T) {

bs := &Server{
BeaconDB: db,
HeadFetcher: &mock.ChainService{
State: headState,
},
GenesisTimeFetcher: &mock.ChainService{
Genesis: time.Now(),
},
}

db.SaveState(ctx, state, bytesutil.ToBytes32([]byte("root")))
res, err := bs.ListIndexedAttestations(ctx, &ethpb.ListIndexedAttestationsRequest{
QueryFilter: &ethpb.ListIndexedAttestationsRequest_Epoch{
Epoch: epoch,
Expand Down
1 change: 1 addition & 0 deletions slasher/detection/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (ds *Service) Start() {
// our gRPC client to keep detecting slashable offenses.
go ds.detectIncomingBlocks(ds.ctx, ds.blocksChan)
go ds.detectIncomingAttestations(ds.ctx, ds.attsChan)
go ds.detectHistoricalChainData(ds.ctx)
}

func (ds *Service) detectHistoricalChainData(ctx context.Context) {
Expand Down

0 comments on commit 35a6df7

Please sign in to comment.