prysm-pulse/slasher/beaconclient/chain_data.go
Raul Jordan d143187b7e
Request All Indexed Attestations Since Genesis in Slasher on Startup (#4894)
* include fixes

* rev

* logrus

* tests for query sync status and chain head

* begin tests for indexed atts

* test passing for requesting historical atts

* Update slasher/beaconclient/chain_data_test.go

* Update slasher/beaconclient/historical_data_retrieval.go

* lint

* fixed up wanted vs receied

* fix mock

* gazelle

* fix broken build

* tests pass

* dep

* gaz

* add dep

* tests pass

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-02-19 16:26:14 -06:00

61 lines
1.7 KiB
Go

package beaconclient
import (
"context"
"time"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/params"
"go.opencensus.io/trace"
)
var syncStatusPollingInterval = time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second
// ChainHead requests the latest beacon chain head
// from a beacon node via gRPC.
func (bs *Service) ChainHead(
ctx context.Context,
) (*ethpb.ChainHead, error) {
ctx, span := trace.StartSpan(ctx, "beaconclient.ChainHead")
defer span.End()
res, err := bs.beaconClient.GetChainHead(ctx, &ptypes.Empty{})
if err != nil {
return nil, errors.Wrap(err, "Could not retrieve chain head")
}
return res, nil
}
// Poll the beacon node every syncStatusPollingInterval until the node
// is no longer syncing.
func (bs *Service) querySyncStatus(ctx context.Context) {
status, err := bs.nodeClient.GetSyncStatus(ctx, &ptypes.Empty{})
if err != nil {
log.WithError(err).Error("Could not fetch sync status")
}
if !status.Syncing {
log.Info("Beacon node is fully synced, starting slashing detection")
return
}
ticker := time.NewTicker(syncStatusPollingInterval)
log.Info("Waiting for beacon node to be fully synced...")
for {
select {
case <-ticker.C:
status, err := bs.nodeClient.GetSyncStatus(ctx, &ptypes.Empty{})
if err != nil {
log.WithError(err).Error("Could not fetch sync status")
}
if !status.Syncing {
log.Info("Beacon node is fully synced, starting slashing detection")
return
}
log.Info("Waiting for beacon node to be fully synced...")
case <-ctx.Done():
log.Debug("Context closed, exiting routine")
return
}
}
}