Merge PR #5244: Add debug commands

This commit is contained in:
Marko 2019-11-11 15:54:32 +01:00 committed by Alexander Bezobchuk
parent d81d46192a
commit 92d2dbe3ff
4 changed files with 246 additions and 8 deletions

185
client/debug/main.go Normal file
View File

@ -0,0 +1,185 @@
package debug
import (
"encoding/base64"
"encoding/hex"
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
)
func Cmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "debug",
Short: "Tool for helping with debugging your application",
RunE: client.ValidateCmd,
}
cmd.AddCommand(PubkeyCmd(cdc))
cmd.AddCommand(AddrCmd())
cmd.AddCommand(RawBytesCmd())
return cmd
}
func PubkeyCmd(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "pubkey [pubkey]",
Short: "Decode a pubkey from hex, base64, or bech32",
Long: fmt.Sprintf(`Decode a pubkey from hex, base64, or bech32.
Example:
$ %s debug pubkey TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
$ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
`, version.ClientName, version.ClientName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pubkeyString := args[0]
var pubKeyI crypto.PubKey
// try hex, then base64, then bech32
pubkeyBytes, err := hex.DecodeString(pubkeyString)
if err != nil {
var err2 error
pubkeyBytes, err2 = base64.StdEncoding.DecodeString(pubkeyString)
if err2 != nil {
var err3 error
pubKeyI, err3 = sdk.GetAccPubKeyBech32(pubkeyString)
if err3 != nil {
var err4 error
pubKeyI, err4 = sdk.GetValPubKeyBech32(pubkeyString)
if err4 != nil {
var err5 error
pubKeyI, err5 = sdk.GetConsPubKeyBech32(pubkeyString)
if err5 != nil {
return fmt.Errorf("expected hex, base64, or bech32. Got errors: hex: %v, base64: %v, bech32 Acc: %v, bech32 Val: %v, bech32 Cons: %v",
err, err2, err3, err4, err5)
}
}
}
}
}
var pubKey ed25519.PubKeyEd25519
if pubKeyI == nil {
copy(pubKey[:], pubkeyBytes)
} else {
pubKey = pubKeyI.(ed25519.PubKeyEd25519)
pubkeyBytes = pubKey[:]
}
pubKeyJSONBytes, err := cdc.MarshalJSON(pubKey)
if err != nil {
return err
}
accPub, err := sdk.Bech32ifyAccPub(pubKey)
if err != nil {
return err
}
valPub, err := sdk.Bech32ifyValPub(pubKey)
if err != nil {
return err
}
consenusPub, err := sdk.Bech32ifyConsPub(pubKey)
if err != nil {
return err
}
cmd.Println("Address:", pubKey.Address())
cmd.Printf("Hex: %X\n", pubkeyBytes)
cmd.Println("JSON (base64):", string(pubKeyJSONBytes))
cmd.Println("Bech32 Acc:", accPub)
cmd.Println("Bech32 Validator Operator:", valPub)
cmd.Println("Bech32 Validator Consensus:", consenusPub)
return nil
},
}
}
func AddrCmd() *cobra.Command {
return &cobra.Command{
Use: "addr [address]",
Short: "Convert an address between hex and bech32",
Long: fmt.Sprintf(`Convert an address between hex encoding and bech32.
Example:
$ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
`, version.ClientName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addrString := args[0]
var addr []byte
// try hex, then bech32
var err error
addr, err = hex.DecodeString(addrString)
if err != nil {
var err2 error
addr, err2 = sdk.AccAddressFromBech32(addrString)
if err2 != nil {
var err3 error
addr, err3 = sdk.ValAddressFromBech32(addrString)
if err3 != nil {
return fmt.Errorf("expected hex or bech32. Got errors: hex: %v, bech32 acc: %v, bech32 val: %v", err, err2, err3)
}
}
}
accAddr := sdk.AccAddress(addr)
valAddr := sdk.ValAddress(addr)
cmd.Println("Address:", addr)
cmd.Printf("Address (hex): %X\n", addr)
cmd.Printf("Bech32 Acc: %s\n", accAddr)
cmd.Printf("Bech32 Val: %s\n", valAddr)
return nil
},
}
}
func RawBytesCmd() *cobra.Command {
return &cobra.Command{
Use: "raw-bytes [raw-bytes]",
Short: "Convert raw bytes output (eg. [10 21 13 255]) to hex",
Long: fmt.Sprintf(`Convert raw-bytes to hex.
Example:
$ %s debug raw-bytes [72 101 108 108 111 44 32 112 108 97 121 103 114 111 117 110 100]
`, version.ClientName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
stringBytes := args[0]
stringBytes = strings.Trim(stringBytes, "[")
stringBytes = strings.Trim(stringBytes, "]")
spl := strings.Split(stringBytes, " ")
byteArray := []byte{}
for _, s := range spl {
b, err := strconv.Atoi(s)
if err != nil {
return err
}
byteArray = append(byteArray, byte(b))
}
fmt.Printf("%X\n", byteArray)
return nil
},
}
}

View File

@ -1,13 +1,20 @@
package cli
import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/tendermint/go-amino"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
@ -38,3 +45,54 @@ func GetDecodeCommand(codec *amino.Codec) *cobra.Command {
return client.PostCommands(cmd)[0]
}
// GetDecodeTxCmd - returns the command to decode a tx from hex or base64
func GetDecodeTxCmd(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "decode-tx [tx]",
Short: "Decode a tx from hex or base64",
Long: fmt.Sprintf(`Decode a tx from hex, base64.
Example:
$ %s tx decode-tx TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
`, version.ClientName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
txString := args[0]
// try hex, then base64
txBytes, err := hex.DecodeString(txString)
if err != nil {
var err2 error
txBytes, err2 = base64.StdEncoding.DecodeString(txString)
if err2 != nil {
return fmt.Errorf(`expected hex or base64. Got errors:
hex: %v,
base64: %v
`, err, err2)
}
}
var tx = types.StdTx{}
err = cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
if err != nil {
return err
}
bz, err := cdc.MarshalJSON(tx)
if err != nil {
return err
}
buf := bytes.NewBuffer([]byte{})
if err = json.Indent(buf, bz, "", " "); err != nil {
return err
}
fmt.Println(buf.String())
return nil
},
}
}

View File

@ -2,16 +2,11 @@ package types
import (
"fmt"
"regexp"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
_ Router = (*router)(nil)
isAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString
)
var _ Router = (*router)(nil)
// Router implements a governance Handler router.
//

View File

@ -272,7 +272,7 @@ func SimulateMsgUndelegate(ak types.AccountKeeper, k keeper.Keeper) simulation.O
}
// if simaccount.PrivKey == nil, delegation address does not exist in accs. Return error
if simAccount.PrivKey == nil {
return simulation.NoOpMsg(types.ModuleName), nil, fmt.Errorf("Delegation addr: %s does not exist in simulation accounts", delAddr)
return simulation.NoOpMsg(types.ModuleName), nil, fmt.Errorf("delegation addr: %s does not exist in simulation accounts", delAddr)
}
account := ak.GetAccount(ctx, delAddr)
@ -371,7 +371,7 @@ func SimulateMsgBeginRedelegate(ak types.AccountKeeper, k keeper.Keeper) simulat
}
// if simaccount.PrivKey == nil, delegation address does not exist in accs. Return error
if simAccount.PrivKey == nil {
return simulation.NoOpMsg(types.ModuleName), nil, fmt.Errorf("Delegation addr: %s does not exist in simulation accounts", delAddr)
return simulation.NoOpMsg(types.ModuleName), nil, fmt.Errorf("delegation addr: %s does not exist in simulation accounts", delAddr)
}
// get tx fees