Skip to content

Commit

Permalink
go/oasis-node/cmd/debug/beacon: Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Yawning committed Jun 7, 2021
1 parent ae99fb3 commit 26cad5b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .changelog/3897.feature.md
Original file line number Diff line number Diff line change
@@ -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.
102 changes: 102 additions & 0 deletions go/oasis-node/cmd/debug/beacon/beacon.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions go/oasis-node/cmd/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,6 +28,7 @@ func Register(parentCmd *cobra.Command) {
control.Register(debugCmd)
consim.Register(debugCmd)
dumpdb.Register(debugCmd)
beacon.Register(debugCmd)

parentCmd.AddCommand(debugCmd)
}

0 comments on commit 26cad5b

Please sign in to comment.