From ad0c776d943f90e8acd52a7367cc992b98c85ef9 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 19 Apr 2018 15:18:31 -0400 Subject: [PATCH] show_validator now displays HEX by default, declare-candidacy also takes go-amino encoded --- cmd/gaia/cli_test/cli_test.go | 17 ++----- server/tm_cmds.go | 88 ++++++++++++++++------------------- x/stake/commands/tx.go | 39 +++++----------- 3 files changed, 56 insertions(+), 88 deletions(-) diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index 0d9a89578b..6bda6894b4 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -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 } diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 67504fa6b4..dd826ba8c8 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -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,63 @@ 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 -} diff --git a/x/stake/commands/tx.go b/x/stake/commands/tx.go index e04a818855..69fedba54f 100644 --- a/x/stake/commands/tx.go +++ b/x/stake/commands/tx.go @@ -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 -}