diff --git a/beacon-chain/rpc/beacon_chain_server.go b/beacon-chain/rpc/beacon_chain_server.go index 51c499ca50..b1c7b56bec 100644 --- a/beacon-chain/rpc/beacon_chain_server.go +++ b/beacon-chain/rpc/beacon_chain_server.go @@ -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 ðpb.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) } diff --git a/beacon-chain/rpc/beacon_chain_server_test.go b/beacon-chain/rpc/beacon_chain_server_test.go index e4fe22203a..a234bacb5e 100644 --- a/beacon-chain/rpc/beacon_chain_server_test.go +++ b/beacon-chain/rpc/beacon_chain_server_test.go @@ -807,20 +807,7 @@ func TestBeaconChainServer_GetValidatorsParticipation(t *testing.T) { bs := &BeaconChainServer{ beaconDB: db, - } - - block := ðpb.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, ðpb.GetValidatorParticipationRequest{Epoch: epoch}) diff --git a/beacon-chain/rpc/service.go b/beacon-chain/rpc/service.go index 5bb75dcec3..03e803858e 100644 --- a/beacon-chain/rpc/service.go +++ b/beacon-chain/rpc/service.go @@ -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) diff --git a/tools/forkchecker/forkchecker.go b/tools/forkchecker/forkchecker.go index 1d99a6fdcd..302d880d83 100644 --- a/tools/forkchecker/forkchecker.go +++ b/tools/forkchecker/forkchecker.go @@ -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) }