Skip to content

Commit

Permalink
feat: create debug sub command (exp) (kilnfi#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo authored Jan 18, 2024
1 parent 1c6b221 commit 19c4477
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 6 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ This app is using the [CometBFT library](https://github.com/cometbft/cometbft/)

### How to get your validator pubkey address?

Use `tendermint show-validator` to get the pubkey and `debug pubkey` to convert to hex format.
**Option 1**: use `tendermint show-validator` to get the pubkey and `debug pubkey` to convert to hex format.

```bash
CLI_NAME=gaiad
Expand All @@ -147,6 +147,18 @@ echo "${ADDRESS^^}"

(replace `gaiad` by the binary name or the desired chain, eg. `evmosd`, `strided`, `injectived`, …).

**Option 2**: use the `cosmos-validator-watcher debug consensus-key` sub command:

```bash
cosmos-validator-watcher debug validator \
--node https://cosmos-rpc.publicnode.com:443 \
cosmosvaloper1uxlf7mvr8nep3gm7udf2u9remms2jyjqvwdul2
```

Notes:
- the `--node` flag must be placed before the validator address)
- this doesns't work for consumer chains (neutron, stride) since they don't rely on the `staking` module


## 📃 License

Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var Version = "v0.0.0-dev" // generated at build time

func main() {
app := &cli.App{
Name: "cosmos-validator-watcher",
Usage: "Real-time Cosmos-based chains monitoring tool",
Flags: app.Flags,
Action: app.RunFunc,
Version: Version,
Name: "cosmos-validator-watcher",
Usage: "Real-time Cosmos-based chains monitoring tool",
Flags: app.Flags,
Action: app.RunFunc,
Commands: app.Commands,
Version: Version,
}

if err := app.Run(os.Args); err != nil && !errors.Is(err, context.Canceled) {
Expand Down
27 changes: 27 additions & 0 deletions pkg/app/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app

import "github.com/urfave/cli/v2"

var Commands = []*cli.Command{
{
Name: "debug",
Usage: "Debug utilities",
Subcommands: []*cli.Command{
{
Name: "consensus-key",
Action: DebugConsensusKeyRun,
Flags: Flags,
},
{
Name: "denom",
Action: DebugDenomRun,
Flags: Flags,
},
{
Name: "validator",
Action: DebugValidatorRun,
Flags: Flags,
},
},
},
}
128 changes: 128 additions & 0 deletions pkg/app/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package app

import (
"fmt"

"github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/urfave/cli/v2"
)

func DebugDenomRun(cCtx *cli.Context) error {
var (
ctx = cCtx.Context
nodes = cCtx.StringSlice("node")
)

if len(nodes) < 1 {
return cli.Exit("at least one node must be specified", 1)
}

endpoint := nodes[0]

rpcClient, err := http.New(endpoint, "/websocket")
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}

clientCtx := (client.Context{}).WithClient(rpcClient)
queryClient := bank.NewQueryClient(clientCtx)

resp, err := queryClient.DenomsMetadata(ctx, &bank.QueryDenomsMetadataRequest{})
if err != nil {
return err
}

cdc := codec.NewLegacyAmino()
j, err := cdc.MarshalJSONIndent(resp.Metadatas, "", " ")
fmt.Println(string(j))

return nil
}

func DebugValidatorRun(cCtx *cli.Context) error {
var (
ctx = cCtx.Context
nodes = cCtx.StringSlice("node")
)

if cCtx.Args().Len() < 1 {
return cli.Exit("validator address must be specified", 1)
}
if len(nodes) < 1 {
return cli.Exit("at least one node must be specified", 1)
}

endpoint := nodes[0]
validatorAddr := cCtx.Args().First()

rpcClient, err := http.New(endpoint, "/websocket")
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}

clientCtx := (client.Context{}).WithClient(rpcClient)
queryClient := staking.NewQueryClient(clientCtx)

resp, err := queryClient.Validator(ctx, &staking.QueryValidatorRequest{
ValidatorAddr: validatorAddr,
})
if err != nil {
return err
}

val := resp.Validator

cdc := codec.NewLegacyAmino()
j, err := cdc.MarshalJSONIndent(val, "", " ")
if err != nil {
return err
}
fmt.Println(string(j))

return nil
}

func DebugConsensusKeyRun(cCtx *cli.Context) error {
var (
ctx = cCtx.Context
nodes = cCtx.StringSlice("node")
)

if cCtx.Args().Len() < 1 {
return cli.Exit("validator address must be specified", 1)
}
if len(nodes) < 1 {
return cli.Exit("at least one node must be specified", 1)
}

endpoint := nodes[0]
validatorAddr := cCtx.Args().First()

rpcClient, err := http.New(endpoint, "/websocket")
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}

clientCtx := (client.Context{}).WithClient(rpcClient)
queryClient := staking.NewQueryClient(clientCtx)

resp, err := queryClient.Validator(ctx, &staking.QueryValidatorRequest{
ValidatorAddr: validatorAddr,
})
if err != nil {
return err
}

val := resp.Validator
pubkey := ed25519.PubKey{Key: val.ConsensusPubkey.Value[2:]}
address := pubkey.Address().String()

fmt.Println(address)

return nil
}

0 comments on commit 19c4477

Please sign in to comment.