github.com/prysmaticlabs/prysm@v1.4.4/slasher/beaconclient/historical_data_retrieval.go (about)

     1  package beaconclient
     2  
     3  import (
     4  	"context"
     5  
     6  	types "github.com/prysmaticlabs/eth2-types"
     7  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     8  	"github.com/prysmaticlabs/prysm/shared/cmd"
     9  	"go.opencensus.io/trace"
    10  )
    11  
    12  // RequestHistoricalAttestations requests all indexed attestations for a
    13  // given epoch from a beacon node via gRPC.
    14  func (s *Service) RequestHistoricalAttestations(
    15  	ctx context.Context,
    16  	epoch types.Epoch,
    17  ) ([]*ethpb.IndexedAttestation, error) {
    18  	ctx, span := trace.StartSpan(ctx, "beaconclient.RequestHistoricalAttestations")
    19  	defer span.End()
    20  	indexedAtts := make([]*ethpb.IndexedAttestation, 0)
    21  	res := &ethpb.ListIndexedAttestationsResponse{}
    22  	var err error
    23  	for {
    24  		if ctx.Err() != nil {
    25  			return nil, ctx.Err()
    26  		}
    27  		if res == nil {
    28  			res = &ethpb.ListIndexedAttestationsResponse{}
    29  		}
    30  		res, err = s.cfg.BeaconClient.ListIndexedAttestations(ctx, &ethpb.ListIndexedAttestationsRequest{
    31  			QueryFilter: &ethpb.ListIndexedAttestationsRequest_Epoch{
    32  				Epoch: epoch,
    33  			},
    34  			PageSize:  int32(cmd.Get().MaxRPCPageSize),
    35  			PageToken: res.NextPageToken,
    36  		})
    37  		if err != nil {
    38  			log.WithError(err).Errorf("could not request indexed attestations for epoch: %d", epoch)
    39  			break
    40  		}
    41  		indexedAtts = append(indexedAtts, res.IndexedAttestations...)
    42  		log.Infof(
    43  			"Retrieved %d/%d indexed attestations for epoch %d",
    44  			len(indexedAtts),
    45  			res.TotalSize,
    46  			epoch,
    47  		)
    48  		if res.NextPageToken == "" || res.TotalSize == 0 || len(indexedAtts) == int(res.TotalSize) {
    49  			break
    50  		}
    51  	}
    52  	return indexedAtts, nil
    53  }