From 26cad5b2a7732ce83549a66e6317fe34801e471f Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Fri, 4 Jun 2021 09:47:23 +0000 Subject: [PATCH] go/oasis-node/cmd/debug/beacon: Initial import --- .changelog/3897.feature.md | 4 + go/oasis-node/cmd/debug/beacon/beacon.go | 102 +++++++++++++++++++++++ go/oasis-node/cmd/debug/debug.go | 2 + 3 files changed, 108 insertions(+) create mode 100644 .changelog/3897.feature.md create mode 100644 go/oasis-node/cmd/debug/beacon/beacon.go diff --git a/.changelog/3897.feature.md b/.changelog/3897.feature.md new file mode 100644 index 00000000000..cd26ee0ba5c --- /dev/null +++ b/.changelog/3897.feature.md @@ -0,0 +1,4 @@ +go/oasis-node/cmd/debug/beacon: Initial import + +Add a `debug beacon status` command which queries the current beacon and +PVSS backend state. diff --git a/go/oasis-node/cmd/debug/beacon/beacon.go b/go/oasis-node/cmd/debug/beacon/beacon.go new file mode 100644 index 00000000000..5b6b6cc02ed --- /dev/null +++ b/go/oasis-node/cmd/debug/beacon/beacon.go @@ -0,0 +1,102 @@ +// Package beacon implements the beacon introspection debug sub-commands. +package beacon + +import ( + "context" + "encoding/json" + "fmt" + "os" + + "github.com/spf13/cobra" + "google.golang.org/grpc" + + beacon "github.com/oasisprotocol/oasis-core/go/beacon/api" + "github.com/oasisprotocol/oasis-core/go/common/logging" + consensus "github.com/oasisprotocol/oasis-core/go/consensus/api" + cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common" + cmdGrpc "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/grpc" +) + +var ( + beaconCmd = &cobra.Command{ + Use: "beacon", + Short: "debug the random beacon", + } + + beaconStatusCmd = &cobra.Command{ + Use: "status", + Short: "query beacon status", + Run: doBeaconStatus, + } + + logger = logging.GetLogger("cmd/debug/beacon") +) + +func doConnect(cmd *cobra.Command) (*grpc.ClientConn, beacon.Backend) { + if err := cmdCommon.Init(); err != nil { + cmdCommon.EarlyLogAndExit(err) + } + + conn, err := cmdGrpc.NewClient(cmd) + if err != nil { + logger.Error("failed to establish connection with node", + "err", err, + ) + os.Exit(1) + } + + client := beacon.NewBeaconClient(conn) + + return conn, client +} + +func doBeaconStatus(cmd *cobra.Command, args []string) { + conn, client := doConnect(cmd) + defer conn.Close() + + logger.Info("querying latest beacon") + + b, err := client.GetBeacon(context.Background(), consensus.HeightLatest) + if err != nil { + logger.Error("failed to query beacon", + "err", err, + ) + os.Exit(1) + } + + // I'm going to be sad if people use this as a way to programatically + // scrape the beacon. Oh well. + prettyOut := struct { + Beacon []byte + State *beacon.PVSSState + }{ + Beacon: b, + } + + pvssClient, ok := client.(beacon.PVSSBackend) + if ok { + prettyOut.State, err = pvssClient.GetPVSSState(context.Background(), consensus.HeightLatest) + if err != nil { + logger.Error("failed to query PVSS backend state", + "err", err, + ) + os.Exit(1) + } + } + + formatted, err := json.MarshalIndent(prettyOut, "", " ") + if err != nil { + logger.Error("failed to format state", + "err", err, + ) + os.Exit(1) + } + fmt.Println(string(formatted)) +} + +// Register registers the beacon sub-command and all of it's children. +func Register(parentCmd *cobra.Command) { + beaconCmd.PersistentFlags().AddFlagSet(cmdGrpc.ClientFlags) + + beaconCmd.AddCommand(beaconStatusCmd) +} diff --git a/go/oasis-node/cmd/debug/debug.go b/go/oasis-node/cmd/debug/debug.go index 67249a2ce3a..d58239d7c61 100644 --- a/go/oasis-node/cmd/debug/debug.go +++ b/go/oasis-node/cmd/debug/debug.go @@ -4,6 +4,7 @@ package debug import ( "github.com/spf13/cobra" + "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/beacon" "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/byzantine" "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/consim" "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/debug/control" @@ -27,6 +28,7 @@ func Register(parentCmd *cobra.Command) { control.Register(debugCmd) consim.Register(debugCmd) dumpdb.Register(debugCmd) + beacon.Register(debugCmd) parentCmd.AddCommand(debugCmd) }