prysm-pulse/slasher/beaconclient/historical_data_retrieval.go
Raul Jordan 14b3181e67
Plug-In Attester Slashing Detection Into Slasher Runtime (#4937)
* more spanner additions

* implement iface

* begin implement

* wrapped up spanner functions

* rem interface

* added in necessary comments

* comments on enums

* begin adding tests

* plug in surround vote detection

* saved indexed db implementation

* finally plugin slashing for historical data

* Small fixes

* add in all gazelle

* save incoming new functions

* resolve todo

* fix broken test channel item

* tests passing when fixing certain arguments and setups

* Add comment and change unimplemented

* find surround

* added in gazelle

* gazz

* feedback from shay

* fixed up naming

* Update

* Add tests for detectSurroundVotes

* Remove logs

* Fix slasher test

* formatting

* Remove unneeded condition

* Test indices better

* fixing broken build

* pass tests

* skip tests

* imports

* Update slasher/detection/attestations/attestations_test.go

* Update slasher/beaconclient/historical_data_retrieval_test.go

* Address comments

* Rename function

* Add comment for future optimization

* Fix comment

Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com>
2020-02-27 12:22:39 -05:00

50 lines
1.5 KiB
Go

package beaconclient
import (
"context"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/params"
"go.opencensus.io/trace"
)
// RequestHistoricalAttestations requests all indexed attestations for a
// given epoch from a beacon node via gRPC.
func (bs *Service) RequestHistoricalAttestations(
ctx context.Context,
epoch uint64,
) ([]*ethpb.IndexedAttestation, error) {
ctx, span := trace.StartSpan(ctx, "beaconclient.RequestHistoricalAttestations")
defer span.End()
indexedAtts := make([]*ethpb.IndexedAttestation, 0)
res := &ethpb.ListIndexedAttestationsResponse{}
var err error
for {
res, err = bs.beaconClient.ListIndexedAttestations(ctx, &ethpb.ListIndexedAttestationsRequest{
QueryFilter: &ethpb.ListIndexedAttestationsRequest_TargetEpoch{
TargetEpoch: epoch,
},
PageSize: int32(params.BeaconConfig().DefaultPageSize),
PageToken: res.NextPageToken,
})
if err != nil {
return nil, errors.Wrapf(err, "could not request indexed attestations for epoch: %d", epoch)
}
indexedAtts = append(indexedAtts, res.IndexedAttestations...)
log.Infof(
"Retrieved %d/%d indexed attestations for epoch %d",
len(indexedAtts),
res.TotalSize,
epoch,
)
if res.NextPageToken == "" || res.TotalSize == 0 || len(indexedAtts) == int(res.TotalSize) {
break
}
}
if err := bs.slasherDB.SaveIncomingIndexedAttestationsByEpoch(ctx, indexedAtts); err != nil {
return nil, errors.Wrap(err, "could not save indexed attestations")
}
return indexedAtts, nil
}