2021-10-07 18:50:03 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
|
2022-03-25 16:31:50 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-10-07 18:50:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/api/grpc"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
2022-03-25 16:31:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2021-10-07 18:50:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/sync"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateSync checks whether the node is currently syncing and returns an error if it is.
|
|
|
|
// It also appends syncing info to gRPC headers.
|
|
|
|
func ValidateSync(ctx context.Context, syncChecker sync.Checker, headFetcher blockchain.HeadFetcher, timeFetcher blockchain.TimeFetcher) error {
|
|
|
|
if !syncChecker.Syncing() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
headSlot := headFetcher.HeadSlot()
|
|
|
|
syncDetailsContainer := &SyncDetailsContainer{
|
|
|
|
SyncDetails: &SyncDetails{
|
|
|
|
HeadSlot: strconv.FormatUint(uint64(headSlot), 10),
|
|
|
|
SyncDistance: strconv.FormatUint(uint64(timeFetcher.CurrentSlot()-headSlot), 10),
|
|
|
|
IsSyncing: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := grpc.AppendCustomErrorHeader(ctx, syncDetailsContainer)
|
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(
|
2022-03-24 18:32:39 +00:00
|
|
|
codes.Internal,
|
2021-10-07 18:50:03 +00:00
|
|
|
"Syncing to latest head, not ready to respond. Could not prepare sync details: %v",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return status.Error(codes.Unavailable, "Syncing to latest head, not ready to respond")
|
|
|
|
}
|
2022-03-25 16:31:50 +00:00
|
|
|
|
|
|
|
// IsOptimistic checks whether the latest block header of the passed in beacon state is the header of an optimistic block.
|
2022-05-12 17:23:45 +00:00
|
|
|
func IsOptimistic(ctx context.Context, st state.BeaconState, optimisticSyncFetcher blockchain.OptimisticModeFetcher) (bool, error) {
|
2022-03-25 16:31:50 +00:00
|
|
|
root, err := st.HashTreeRoot(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "could not get state root")
|
|
|
|
}
|
|
|
|
header := st.LatestBlockHeader()
|
|
|
|
header.StateRoot = root[:]
|
|
|
|
headRoot, err := header.HashTreeRoot()
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "could not get header root")
|
|
|
|
}
|
2022-05-12 17:23:45 +00:00
|
|
|
isOptimistic, err := optimisticSyncFetcher.IsOptimisticForRoot(ctx, headRoot)
|
2022-03-25 16:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "could not check if block is optimistic")
|
|
|
|
}
|
|
|
|
return isOptimistic, nil
|
|
|
|
}
|