Merge pull request #884 from cosmos/rigel/883-show-validator

show_validator displays HEX
This commit is contained in:
Christopher Goes 2018-04-20 12:25:11 +02:00 committed by GitHub
commit 45a97e1206
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 91 deletions

View File

@ -40,8 +40,8 @@ func TestGaiaCLISend(t *testing.T) {
executeWrite(t, "gaiacli keys add foo --recover", pass, masterKey)
executeWrite(t, "gaiacli keys add bar", pass)
fooAddr, _ := executeGetAddr(t, "gaiacli keys show foo --output=json")
barAddr, _ := executeGetAddr(t, "gaiacli keys show bar --output=json")
fooAddr, _ := executeGetAddrPK(t, "gaiacli keys show foo --output=json")
barAddr, _ := executeGetAddrPK(t, "gaiacli keys show bar --output=json")
fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooAddr, flags))
assert.Equal(t, int64(100000), fooAcc.GetCoins().AmountOf("fermion"))
@ -71,7 +71,7 @@ func TestGaiaCLIDeclareCandidacy(t *testing.T) {
defer cmd.Process.Kill()
executeWrite(t, "gaiacli keys add foo --recover", pass, masterKey)
fooAddr, fooPubKey := executeGetAddr(t, "gaiacli keys show foo --output=json")
fooAddr, fooPubKey := executeGetAddrPK(t, "gaiacli keys show foo --output=json")
fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", fooAddr, flags))
assert.Equal(t, int64(100000), fooAcc.GetCoins().AmountOf("fermion"))
@ -91,7 +91,7 @@ func TestGaiaCLIDeclareCandidacy(t *testing.T) {
assert.Equal(t, candidate.Address.String(), fooAddr)
assert.Equal(t, int64(3), candidate.Assets.Evaluate())
// TODO figure out why this times out with connection refused errors in go-bash
// TODO timeout issues if not connected to the internet
// unbond a single share
unbondStr := fmt.Sprintf("gaiacli unbond %v", flags)
unbondStr += fmt.Sprintf(" --name=%v", "foo")
@ -146,19 +146,12 @@ func executeInit(t *testing.T, cmdStr string) (masterKey, chainID string) {
return
}
func executeGetAddr(t *testing.T, cmdStr string) (addr, pubKey string) {
func executeGetAddrPK(t *testing.T, cmdStr string) (addr, pubKey string) {
out := tests.ExecuteT(t, cmdStr, 2)
var info crkeys.Info
keys.UnmarshalJSON([]byte(out), &info)
pubKey = hex.EncodeToString(info.PubKey.(crypto.PubKeyEd25519).Bytes())
// TODO this is really wierd, also error that not 64 characters!
pubKey = strings.TrimLeft(pubKey, "1624de6220")
pubKey = fmt.Sprintf("%064v", pubKey)
fmt.Printf("debug pubKey: %v\n", pubKey)
addr = info.PubKey.Address().String()
fmt.Printf("debug addr: %v\n", addr)
return
}

View File

@ -1,9 +1,11 @@
package server
import (
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/p2p"
@ -12,73 +14,59 @@ import (
// ShowNodeIDCmd - ported from Tendermint, dump node ID to stdout
func ShowNodeIDCmd(ctx *Context) *cobra.Command {
cmd := showNodeID{ctx}
return &cobra.Command{
Use: "show_node_id",
Short: "Show this node's ID",
RunE: cmd.run,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ctx.Config
nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
return err
}
fmt.Println(nodeKey.ID())
return nil
},
}
}
type showNodeID struct {
context *Context
}
func (s showNodeID) run(cmd *cobra.Command, args []string) error {
cfg := s.context.Config
nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
return err
}
fmt.Println(nodeKey.ID())
return nil
}
//--------------------------------------------------------------------------------
// ShowValidator - ported from Tendermint, show this node's validator info
func ShowValidatorCmd(ctx *Context) *cobra.Command {
cmd := showValidator{ctx}
return &cobra.Command{
flagJSON := "json"
cmd := cobra.Command{
Use: "show_validator",
Short: "Show this node's validator info",
RunE: cmd.run,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ctx.Config
privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorFile())
pubKey := privValidator.PubKey
if viper.GetBool(flagJSON) {
pubKeyJSONBytes, err := cdc.MarshalJSON(pubKey)
if err != nil {
return err
}
fmt.Println(string(pubKeyJSONBytes))
return nil
}
pubKeyHex := hex.EncodeToString(pubKey.Bytes())
fmt.Println(pubKeyHex)
return nil
},
}
cmd.Flags().Bool(flagJSON, false, "get machine parseable output")
return &cmd
}
type showValidator struct {
context *Context
}
func (s showValidator) run(cmd *cobra.Command, args []string) error {
cfg := s.context.Config
privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorFile())
pubKeyJSONBytes, err := cdc.MarshalJSON(privValidator.PubKey)
if err != nil {
return err
}
fmt.Println(string(pubKeyJSONBytes))
return nil
}
//------------------------------------------------------------------------------
// UnsafeResetAllCmd - extension of the tendermint command, resets initialization
func UnsafeResetAllCmd(ctx *Context) *cobra.Command {
cmd := resetAll{ctx}
return &cobra.Command{
Use: "unsafe_reset_all",
Short: "Reset all blockchain data",
RunE: cmd.run,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ctx.Config
tcmd.ResetAll(cfg.DBDir(), cfg.PrivValidatorFile(), ctx.Logger)
return nil
},
}
}
type resetAll struct {
context *Context
}
func (r resetAll) run(cmd *cobra.Command, args []string) error {
cfg := r.context.Config
tcmd.ResetAll(cfg.DBDir(), cfg.PrivValidatorFile(), r.context.Logger)
return nil
}

View File

@ -29,7 +29,7 @@ var (
)
func init() {
fsPk.String(FlagPubKey, "", "PubKey of the validator-candidate")
fsPk.String(FlagPubKey, "", "Go-Amino encoded hex PubKey of the validator-candidate. For Ed25519 the go-amino prepend hex is 1624de6220")
fsAmount.String(FlagAmount, "1fermion", "Amount of coins to bond")
fsShares.String(FlagShares, "", "Amount of shares to unbond, either in decimal or keyword MAX (ex. 1.23456789, 99, MAX)")
fsDescription.String(FlagMoniker, "", "validator-candidate name")

View File

@ -30,10 +30,20 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
if err != nil {
return err
}
pk, err := GetPubKey(viper.GetString(FlagPubKey))
pkStr := viper.GetString(FlagPubKey)
if len(pkStr) == 0 {
return fmt.Errorf("must use --pubkey flag")
}
pkBytes, err := hex.DecodeString(pkStr)
if err != nil {
return err
}
pk, err := crypto.PubKeyFromBytes(pkBytes)
if err != nil {
return err
}
if viper.GetString(FlagMoniker) == "" {
return fmt.Errorf("please enter a moniker for the validator-candidate using --moniker")
}
@ -187,30 +197,3 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
cmd.Flags().AddFlagSet(fsCandidate)
return cmd
}
//______________________________________________________________________________________
// create the pubkey from a pubkey string
// TODO move to a better reusable place
func GetPubKey(pubKeyStr string) (pk crypto.PubKey, err error) {
if len(pubKeyStr) == 0 {
err = fmt.Errorf("must use --pubkey flag")
return
}
if len(pubKeyStr) != 64 { //if len(pkBytes) != 32 {
err = fmt.Errorf("pubkey must be Ed25519 hex encoded string which is 64 characters, this pubkey is %v characters", len(pubKeyStr))
return
}
// TODO: bech32 ...
var pkBytes []byte
pkBytes, err = hex.DecodeString(pubKeyStr)
if err != nil {
return
}
var pkEd crypto.PubKeyEd25519
copy(pkEd[:], pkBytes[:])
pk = pkEd
return
}