prysm-pulse/tools/forkchecker/forkchecker.go

150 lines
4.1 KiB
Go
Raw Normal View History

2019-08-26 20:17:47 +00:00
/**
* Fork choice checker
*
* A gRPC client that polls beacon node at every slot to log or compare nodes current head.
*
* Example: 2 beacon nodes with 2 gRPC end points, 127.0.0.1:4000 and 127.0.0.1:4001
* For logging heads: forkchecker --endpoint 127.0.0.1:4000 --endpoint 127.0.0.1:4001
* For comparing heads: forkchecker --endpoint 127.0.0.1:4000 --endpoint 127.0.0.1:4001 --compare
*/
package main
import (
"context"
"encoding/hex"
"flag"
"reflect"
"time"
"github.com/prysmaticlabs/prysm/config/params"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
pb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
2019-08-26 20:17:47 +00:00
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
Change gogoproto compiler to protoc-gen-go-cast (#8697) * Remove gogoproto compiler * Remove more gogoproto * Improvements * Fix gengo * More scripts * Gazelle, fix deps * Fix version and errors * Fix gocast for arrays * Fix ethapis * Fixes * Fix compile errors * fix go.mod * //proto/... builds * Update for protov2 * temp fix compilation to move on * Change everything to emptypb.empty * Add grpc to proto/slashings * Fix almost all build failures * Oher build problems * FIX THIS FUCKING THING * gaz literally every .bazel * Final touches * Final final touches * Fix proto * Begin moving proto.Marshal to native * Fix site_data * Fixes * Fix duplicate gateway * Fix gateway target * Fix ethapis * Fixes from review * Update * Fix * Fix status test * Fix fuzz * Add isprotoslice to fun * Change DeepEqual to DeepSSZEqual for proto arrays * Fix build * Fix gaz * Update go * Fixes * Fixes * Add case for nil validators after copy * Fix cast * Fix test * Fix imports * Go mod * Only use extension where needed * Fixes * Split gateway from gengo * gaz * go mod * Add back hydrated state * fix hydrate * Fix proto.clone * Fies * Revert "Split gateway from gengo" This reverts commit 7298bb2054d446e427d9af97e13b8fabe8695085. * Revert "gaz" This reverts commit ca952565701a88727e22302d6c8d60ac48d97255. * Merge all gateway into one target * go mod * Gaz * Add generate v1_gateway files * run pb again * goimports * gaz * Fix comments * Fix protos * Fix PR * Fix protos * Update grpc-gateway and ethapis * Update ethapis and gen-go-cast * Go tidy * Reorder * Fix ethapis * fix spec tests * Fix script * Remove unused import * Fix fuzz * Fix gomod * Update version * Error if the cloned result is nil * Handle optional slots * ADd more empty checks to clone * Undo fuzz changes * Fix build.bazel * Gaz * Redo fuzz changes * Undo some eth1data changes * Update go.mod Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Undo clone beacon state * Remove gogo proto more and unused v1_gateway * Add manual fix for nil vals * Fix gaz * tidy * Tidy again * Add detailed error * Revert "Add detailed error" This reverts commit 59bc053dcd59569a54c95b07739d5a379665ec5d. * Undo varint changes * Fix nil validators in deposit test * Commit * Undo Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-05-17 18:32:04 +00:00
"google.golang.org/protobuf/types/known/emptypb"
2019-08-26 20:17:47 +00:00
)
var log = logrus.WithField("prefix", "forkchoice_checker")
type endpoint []string
func (_ *endpoint) String() string {
2019-08-26 20:17:47 +00:00
return "gRPC endpoints"
}
// Set adds endpoint value to list.
2019-08-26 20:17:47 +00:00
func (e *endpoint) Set(value string) error {
*e = append(*e, value)
return nil
}
func main() {
var endpts endpoint
clients := make(map[string]pb.BeaconChainClient)
flag.Var(&endpts, "endpoint", "Specify gRPC end points for beacon node")
compare := flag.Bool("compare", false, "Enable head comparisons between all end points")
flag.Parse()
for _, endpt := range endpts {
conn, err := grpc.Dial(endpt, grpc.WithInsecure())
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
clients[endpt] = pb.NewBeaconChainClient(conn)
}
ticker := time.NewTicker(time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second)
go func() {
for range ticker.C {
if *compare {
compareHeads(clients)
} else {
displayHeads(clients)
2019-08-26 20:17:47 +00:00
}
}
}()
select {}
}
// log heads for all RPC end points
func displayHeads(clients map[string]pb.BeaconChainClient) {
for endpt, client := range clients {
Change gogoproto compiler to protoc-gen-go-cast (#8697) * Remove gogoproto compiler * Remove more gogoproto * Improvements * Fix gengo * More scripts * Gazelle, fix deps * Fix version and errors * Fix gocast for arrays * Fix ethapis * Fixes * Fix compile errors * fix go.mod * //proto/... builds * Update for protov2 * temp fix compilation to move on * Change everything to emptypb.empty * Add grpc to proto/slashings * Fix almost all build failures * Oher build problems * FIX THIS FUCKING THING * gaz literally every .bazel * Final touches * Final final touches * Fix proto * Begin moving proto.Marshal to native * Fix site_data * Fixes * Fix duplicate gateway * Fix gateway target * Fix ethapis * Fixes from review * Update * Fix * Fix status test * Fix fuzz * Add isprotoslice to fun * Change DeepEqual to DeepSSZEqual for proto arrays * Fix build * Fix gaz * Update go * Fixes * Fixes * Add case for nil validators after copy * Fix cast * Fix test * Fix imports * Go mod * Only use extension where needed * Fixes * Split gateway from gengo * gaz * go mod * Add back hydrated state * fix hydrate * Fix proto.clone * Fies * Revert "Split gateway from gengo" This reverts commit 7298bb2054d446e427d9af97e13b8fabe8695085. * Revert "gaz" This reverts commit ca952565701a88727e22302d6c8d60ac48d97255. * Merge all gateway into one target * go mod * Gaz * Add generate v1_gateway files * run pb again * goimports * gaz * Fix comments * Fix protos * Fix PR * Fix protos * Update grpc-gateway and ethapis * Update ethapis and gen-go-cast * Go tidy * Reorder * Fix ethapis * fix spec tests * Fix script * Remove unused import * Fix fuzz * Fix gomod * Update version * Error if the cloned result is nil * Handle optional slots * ADd more empty checks to clone * Undo fuzz changes * Fix build.bazel * Gaz * Redo fuzz changes * Undo some eth1data changes * Update go.mod Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Undo clone beacon state * Remove gogo proto more and unused v1_gateway * Add manual fix for nil vals * Fix gaz * tidy * Tidy again * Add detailed error * Revert "Add detailed error" This reverts commit 59bc053dcd59569a54c95b07739d5a379665ec5d. * Undo varint changes * Fix nil validators in deposit test * Commit * Undo Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-05-17 18:32:04 +00:00
head, err := client.GetChainHead(context.Background(), &emptypb.Empty{})
2019-08-26 20:17:47 +00:00
if err != nil {
log.Fatal(err)
}
logHead(endpt, head)
}
}
// compare heads between all RPC end points, log the missmatch if there's one.
func compareHeads(clients map[string]pb.BeaconChainClient) {
endpt1 := randomEndpt(clients)
Change gogoproto compiler to protoc-gen-go-cast (#8697) * Remove gogoproto compiler * Remove more gogoproto * Improvements * Fix gengo * More scripts * Gazelle, fix deps * Fix version and errors * Fix gocast for arrays * Fix ethapis * Fixes * Fix compile errors * fix go.mod * //proto/... builds * Update for protov2 * temp fix compilation to move on * Change everything to emptypb.empty * Add grpc to proto/slashings * Fix almost all build failures * Oher build problems * FIX THIS FUCKING THING * gaz literally every .bazel * Final touches * Final final touches * Fix proto * Begin moving proto.Marshal to native * Fix site_data * Fixes * Fix duplicate gateway * Fix gateway target * Fix ethapis * Fixes from review * Update * Fix * Fix status test * Fix fuzz * Add isprotoslice to fun * Change DeepEqual to DeepSSZEqual for proto arrays * Fix build * Fix gaz * Update go * Fixes * Fixes * Add case for nil validators after copy * Fix cast * Fix test * Fix imports * Go mod * Only use extension where needed * Fixes * Split gateway from gengo * gaz * go mod * Add back hydrated state * fix hydrate * Fix proto.clone * Fies * Revert "Split gateway from gengo" This reverts commit 7298bb2054d446e427d9af97e13b8fabe8695085. * Revert "gaz" This reverts commit ca952565701a88727e22302d6c8d60ac48d97255. * Merge all gateway into one target * go mod * Gaz * Add generate v1_gateway files * run pb again * goimports * gaz * Fix comments * Fix protos * Fix PR * Fix protos * Update grpc-gateway and ethapis * Update ethapis and gen-go-cast * Go tidy * Reorder * Fix ethapis * fix spec tests * Fix script * Remove unused import * Fix fuzz * Fix gomod * Update version * Error if the cloned result is nil * Handle optional slots * ADd more empty checks to clone * Undo fuzz changes * Fix build.bazel * Gaz * Redo fuzz changes * Undo some eth1data changes * Update go.mod Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Undo clone beacon state * Remove gogo proto more and unused v1_gateway * Add manual fix for nil vals * Fix gaz * tidy * Tidy again * Add detailed error * Revert "Add detailed error" This reverts commit 59bc053dcd59569a54c95b07739d5a379665ec5d. * Undo varint changes * Fix nil validators in deposit test * Commit * Undo Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-05-17 18:32:04 +00:00
head1, err := clients[endpt1].GetChainHead(context.Background(), &emptypb.Empty{})
2019-08-26 20:17:47 +00:00
if err != nil {
log.Fatal(err)
}
log.Infof("Comparing all heads for head slot :%d", head1.HeadSlot)
if (head1.HeadSlot+1)%params.BeaconConfig().SlotsPerEpoch == 0 {
p, err := clients[endpt1].GetValidatorParticipation(context.Background(), &pb.GetValidatorParticipationRequest{})
2019-08-26 20:17:47 +00:00
if err != nil {
log.Fatal(err)
}
logParticipation(endpt1, p.Participation)
2019-08-26 20:17:47 +00:00
}
for endpt2, client := range clients {
Change gogoproto compiler to protoc-gen-go-cast (#8697) * Remove gogoproto compiler * Remove more gogoproto * Improvements * Fix gengo * More scripts * Gazelle, fix deps * Fix version and errors * Fix gocast for arrays * Fix ethapis * Fixes * Fix compile errors * fix go.mod * //proto/... builds * Update for protov2 * temp fix compilation to move on * Change everything to emptypb.empty * Add grpc to proto/slashings * Fix almost all build failures * Oher build problems * FIX THIS FUCKING THING * gaz literally every .bazel * Final touches * Final final touches * Fix proto * Begin moving proto.Marshal to native * Fix site_data * Fixes * Fix duplicate gateway * Fix gateway target * Fix ethapis * Fixes from review * Update * Fix * Fix status test * Fix fuzz * Add isprotoslice to fun * Change DeepEqual to DeepSSZEqual for proto arrays * Fix build * Fix gaz * Update go * Fixes * Fixes * Add case for nil validators after copy * Fix cast * Fix test * Fix imports * Go mod * Only use extension where needed * Fixes * Split gateway from gengo * gaz * go mod * Add back hydrated state * fix hydrate * Fix proto.clone * Fies * Revert "Split gateway from gengo" This reverts commit 7298bb2054d446e427d9af97e13b8fabe8695085. * Revert "gaz" This reverts commit ca952565701a88727e22302d6c8d60ac48d97255. * Merge all gateway into one target * go mod * Gaz * Add generate v1_gateway files * run pb again * goimports * gaz * Fix comments * Fix protos * Fix PR * Fix protos * Update grpc-gateway and ethapis * Update ethapis and gen-go-cast * Go tidy * Reorder * Fix ethapis * fix spec tests * Fix script * Remove unused import * Fix fuzz * Fix gomod * Update version * Error if the cloned result is nil * Handle optional slots * ADd more empty checks to clone * Undo fuzz changes * Fix build.bazel * Gaz * Redo fuzz changes * Undo some eth1data changes * Update go.mod Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Undo clone beacon state * Remove gogo proto more and unused v1_gateway * Add manual fix for nil vals * Fix gaz * tidy * Tidy again * Add detailed error * Revert "Add detailed error" This reverts commit 59bc053dcd59569a54c95b07739d5a379665ec5d. * Undo varint changes * Fix nil validators in deposit test * Commit * Undo Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-05-17 18:32:04 +00:00
head2, err := client.GetChainHead(context.Background(), &emptypb.Empty{})
2019-08-26 20:17:47 +00:00
if err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(head1, head2) {
log.Error("Uh oh! Heads missmatched!")
2019-08-26 20:17:47 +00:00
logHead(endpt1, head1)
logHead(endpt2, head2)
if (head1.HeadSlot+1)%params.BeaconConfig().SlotsPerEpoch == 0 {
p, err := clients[endpt2].GetValidatorParticipation(context.Background(), &pb.GetValidatorParticipationRequest{
QueryFilter: &pb.GetValidatorParticipationRequest_Epoch{
Epoch: types.Epoch(head2.HeadSlot / params.BeaconConfig().SlotsPerEpoch),
},
})
2019-08-26 20:17:47 +00:00
if err != nil {
log.Fatal(err)
}
logParticipation(endpt2, p.Participation)
2019-08-26 20:17:47 +00:00
}
}
}
}
func logHead(endpt string, head *pb.ChainHead) {
log.WithFields(
logrus.Fields{
"HeadSlot": head.HeadSlot,
"HeadRoot": hex.EncodeToString(head.HeadBlockRoot),
"JustifiedEpoch": head.JustifiedEpoch,
"JustifiedRoot": hex.EncodeToString(head.JustifiedBlockRoot),
"FinalizedEpoch": head.FinalizedEpoch,
"FinalizedRoot": hex.EncodeToString(head.FinalizedBlockRoot),
2019-08-26 20:17:47 +00:00
}).Info("Head from beacon node ", endpt)
}
func logParticipation(endpt string, p *pb.ValidatorParticipation) {
log.WithFields(
logrus.Fields{
"VotedEther": p.VotedEther,
"TotalEther": p.EligibleEther,
"ParticipationRate": p.GlobalParticipationRate,
}).Info("Participation rate from beacon node ", endpt)
}
func randomEndpt(clients map[string]pb.BeaconChainClient) string {
for endpt := range clients {
return endpt
}
return ""
}