Fix GetChainHead for RPC (#3352)

* Fix ChainHeadQuery

* Fixed test
This commit is contained in:
terence tsao
2019-08-29 08:17:21 -07:00
committed by Raul Jordan
parent 206222c5bc
commit d8fd7e502a
4 changed files with 37 additions and 42 deletions

View File

@@ -5,6 +5,7 @@ import (
"sort"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
@@ -12,6 +13,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
"github.com/prysmaticlabs/prysm/beacon-chain/operations"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/pagination"
@@ -25,7 +27,7 @@ import (
// beacon chain.
type BeaconChainServer struct {
beaconDB db.Database
head blockchain.HeadRetriever
head interface{}
pool operations.Pool
}
@@ -223,13 +225,13 @@ func (bs *BeaconChainServer) ListBlocks(
// This includes the head block slot and root as well as information about
// the most recent finalized and justified slots.
func (bs *BeaconChainServer) GetChainHead(ctx context.Context, _ *ptypes.Empty) (*ethpb.ChainHead, error) {
finalizedCheckpoint := bs.head.HeadState().FinalizedCheckpoint
justifiedCheckpoint := bs.head.HeadState().CurrentJustifiedCheckpoint
prevJustifiedCheckpoint := bs.head.HeadState().PreviousJustifiedCheckpoint
finalizedCheckpoint := bs.head.(blockchain.HeadRetriever).HeadState().FinalizedCheckpoint
justifiedCheckpoint := bs.head.(blockchain.HeadRetriever).HeadState().CurrentJustifiedCheckpoint
prevJustifiedCheckpoint := bs.head.(blockchain.HeadRetriever).HeadState().PreviousJustifiedCheckpoint
return &ethpb.ChainHead{
BlockRoot: bs.head.HeadRoot(),
BlockSlot: bs.head.HeadSlot(),
BlockRoot: bs.head.(blockchain.HeadRetriever).HeadRoot(),
BlockSlot: bs.head.(blockchain.HeadRetriever).HeadSlot(),
FinalizedBlockRoot: finalizedCheckpoint.Root,
FinalizedSlot: finalizedCheckpoint.Epoch * params.BeaconConfig().SlotsPerEpoch,
JustifiedBlockRoot: justifiedCheckpoint.Root,
@@ -503,24 +505,31 @@ func (bs *BeaconChainServer) ListValidatorAssignments(
func (bs *BeaconChainServer) GetValidatorParticipation(
ctx context.Context, req *ethpb.GetValidatorParticipationRequest,
) (*ethpb.ValidatorParticipation, error) {
s, err := bs.beaconDB.HeadState(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not retrieve current state: %v", err)
var headState *pbp2p.BeaconState
var err error
if _, isLegacyDB := bs.beaconDB.(*db.BeaconDB); isLegacyDB {
headState, err = bs.beaconDB.HeadState(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not retrieve beacon state")
}
} else {
headState = bs.head.(blockchain.HeadRetriever).HeadState()
}
currentEpoch := helpers.SlotToEpoch(s.Slot)
finalized := currentEpoch == s.FinalizedCheckpoint.Epoch
currentEpoch := helpers.SlotToEpoch(headState.Slot)
finalized := currentEpoch == headState.FinalizedCheckpoint.Epoch
atts, err := epoch.MatchAttestations(s, currentEpoch)
atts, err := epoch.MatchAttestations(headState, currentEpoch)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not retrieve head attestations: %v", err)
}
attestedBalances, err := epoch.AttestingBalance(s, atts.Target)
attestedBalances, err := epoch.AttestingBalance(headState, atts.Target)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not retrieve attested balances: %v", err)
}
totalBalances, err := helpers.TotalActiveBalance(s)
totalBalances, err := helpers.TotalActiveBalance(headState)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not retrieve total balances: %v", err)
}

View File

@@ -807,20 +807,7 @@ func TestBeaconChainServer_GetValidatorsParticipation(t *testing.T) {
bs := &BeaconChainServer{
beaconDB: db,
}
block := &ethpb.BeaconBlock{
Slot: 1,
}
blockRoot, err := ssz.SigningRoot(block)
if err != nil {
t.Fatal(err)
}
if err := bs.beaconDB.SaveHeadBlockRoot(ctx, blockRoot); err != nil {
t.Fatal(err)
}
if err := bs.beaconDB.SaveState(ctx, s, blockRoot); err != nil {
t.Fatal(err)
head: &mock.ChainService{State: s},
}
res, err := bs.GetValidatorParticipation(ctx, &ethpb.GetValidatorParticipationRequest{Epoch: epoch})

View File

@@ -193,6 +193,7 @@ func (s *Service) Start() {
beaconChainServer := &BeaconChainServer{
beaconDB: s.beaconDB,
pool: s.operationService,
head: s.chainService,
}
pb.RegisterBeaconServiceServer(s.grpcServer, beaconServer)
pb.RegisterProposerServiceServer(s.grpcServer, proposerServer)

View File

@@ -89,10 +89,9 @@ func compareHeads(clients map[string]pb.BeaconChainClient) {
log.Fatal(err)
}
log.Infof("Compare all heads for head slot :%d", head1.BlockSlot)
if head1.BlockSlot%params.BeaconConfig().SlotsPerEpoch == 0 {
p, err := clients[endpt1].GetValidatorParticipation(context.Background(), &pb.GetValidatorParticipationRequest{
Epoch: (head1.BlockSlot / params.BeaconConfig().SlotsPerEpoch) - 1})
log.Infof("Comparing all heads for head slot :%d", head1.BlockSlot)
if (head1.BlockSlot+1)%params.BeaconConfig().SlotsPerEpoch == 0 {
p, err := clients[endpt1].GetValidatorParticipation(context.Background(), &pb.GetValidatorParticipationRequest{})
if err != nil {
log.Fatal(err)
}
@@ -105,13 +104,12 @@ func compareHeads(clients map[string]pb.BeaconChainClient) {
log.Fatal(err)
}
if !reflect.DeepEqual(head1, head2) {
log.Error("Uh oh! Head miss-matched!")
log.Error("Uh oh! Heads missmatched!")
logHead(endpt1, head1)
logHead(endpt2, head2)
if head1.BlockSlot%params.BeaconConfig().SlotsPerEpoch == 0 {
p, err := clients[endpt2].GetValidatorParticipation(context.Background(), &pb.GetValidatorParticipationRequest{
Epoch: (head1.BlockSlot / params.BeaconConfig().SlotsPerEpoch) - 1})
if (head1.BlockSlot+1)%params.BeaconConfig().SlotsPerEpoch == 0 {
p, err := clients[endpt2].GetValidatorParticipation(context.Background(), &pb.GetValidatorParticipationRequest{})
if err != nil {
log.Fatal(err)
}
@@ -124,12 +122,12 @@ func compareHeads(clients map[string]pb.BeaconChainClient) {
func logHead(endpt string, head *pb.ChainHead) {
log.WithFields(
logrus.Fields{
"HeadSlot": head.BlockSlot,
"HeadRoot": hex.EncodeToString(head.BlockRoot),
"JustifiedSlot": head.JustifiedSlot,
"JustifiedRoot": hex.EncodeToString(head.JustifiedBlockRoot),
"FinalizedSlot": head.FinalizedSlot,
"Finalizedroot": hex.EncodeToString(head.FinalizedBlockRoot),
"HeadSlot": head.BlockSlot,
"HeadRoot": hex.EncodeToString(head.BlockRoot),
"JustifiedEpoch": head.JustifiedSlot / params.BeaconConfig().SlotsPerEpoch,
"JustifiedRoot": hex.EncodeToString(head.JustifiedBlockRoot),
"FinalizedEpoch": head.FinalizedSlot / params.BeaconConfig().SlotsPerEpoch,
"FinalizedRoot": hex.EncodeToString(head.FinalizedBlockRoot),
}).Info("Head from beacon node ", endpt)
}