forked from kilnfi/cosmos-validator-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create debug sub command (exp) (kilnfi#57)
- Loading branch information
Showing
4 changed files
with
174 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |