diff --git a/client/tx/query.go b/client/tx/query.go index 0583f94f5b..2078b78831 100644 --- a/client/tx/query.go +++ b/client/tx/query.go @@ -8,7 +8,6 @@ import ( "strconv" "github.com/gorilla/mux" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" abci "github.com/tendermint/abci/types" @@ -25,10 +24,8 @@ func QueryTxCmd(cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "tx [hash]", Short: "Matches this txhash over all committed blocks", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide a tx hash") - } // find the key to look up the account hashHexStr := args[0] diff --git a/server/init.go b/server/init.go index de2bc94282..0ce6ad50b2 100644 --- a/server/init.go +++ b/server/init.go @@ -31,6 +31,7 @@ func InitCmd(ctx *Context, cdc *wire.Codec, gen GenAppParams, appendState Append cmd := &cobra.Command{ Use: "init", Short: "Initialize genesis config, priv-validator file, and p2p-node file", + Args: cobra.NoArgs, RunE: func(_ *cobra.Command, _ []string) error { config := ctx.Config @@ -74,10 +75,11 @@ func InitCmd(ctx *Context, cdc *wire.Codec, gen GenAppParams, appendState Append return nil }, } - - cmd.AddCommand(FromPiecesCmd(ctx, cdc, appendState)) + if appendState != nil { + cmd.AddCommand(FromPiecesCmd(ctx, cdc, appendState)) + cmd.Flags().BoolP(flagAppendFile, "a", false, "create an append file for others to import") + } cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the config file") - cmd.Flags().BoolP(flagAppendFile, "a", false, "create an append file for others to import") return cmd } @@ -102,16 +104,16 @@ func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appendState AppendAppState) *c config := ctx.Config privValFile := config.PrivValidatorFile() nodeKeyFile := config.NodeKeyFile() - genFile := config.GenesisFile() if !cmn.FileExists(privValFile) { return fmt.Errorf("privVal file must already exist, please initialize with init cmd: %v", privValFile) } if !cmn.FileExists(nodeKeyFile) { return fmt.Errorf("nodeKey file must already exist, please initialize with init cmd: %v", nodeKeyFile) } - cmn.FileExists(genFile) - // XXX remove the existing gen config file + // remove genFile for creation + genFile := config.GenesisFile() + os.Remove(genFile) // deterministically walk the directory for genesis-piece files to import filepath.Walk(pieceDir, appendPiece(cdc, appendState, nodeKeyFile, genFile)) @@ -123,16 +125,19 @@ func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appendState AppendAppState) *c // append a genesis-piece func appendPiece(cdc *wire.Codec, appendState AppendAppState, nodeKeyFile, genFile string) filepath.WalkFunc { - return func(filePath string, _ os.FileInfo, err error) error { + return func(pieceFile string, _ os.FileInfo, err error) error { if err != nil { return err } - if path.Ext(filePath) != "json" { + if path.Ext(pieceFile) != "json" { return nil } - // XXX get the file bytes - var bz []byte + // get the piece file bytes + bz, err := ioutil.ReadFile(pieceFile) + if err != nil { + return err + } // get the piece var piece GenesisPiece @@ -143,25 +148,44 @@ func appendPiece(cdc *wire.Codec, appendState AppendAppState, nodeKeyFile, genFi // if the first file, create the genesis from scratch with piece inputs if !cmn.FileExists(genFile) { - return WriteGenesisFile(cdc, genFile, piece.chainID, piece.validators, piece.appState) + return WriteGenesisFile(cdc, genFile, piece.ChainID, piece.Validators, piece.AppState) } - // XXX read in the genFile + // read in the genFile + bz, err = ioutil.ReadFile(genFile) + if err != nil { + return err + } + var genMap map[string]json.RawMessage + err = cdc.UnmarshalJSON(bz, &genMap) + if err != nil { + return err + } + appState := genMap["app_state"] - // XXX verify chain-ids are the same - // XXX combine the validator set + // verify chain-ids are the same + if piece.ChainID != string(genMap["chain_id"]) { + return fmt.Errorf("piece chain id's are mismatched, %s != %s", piece.ChainID, genMap["chain_id"]) + } + + // combine the validator set var validators []tmtypes.GenesisValidator + err = cdc.UnmarshalJSON(genMap["validators"], &validators) + if err != nil { + return err + } + validators = append(validators, piece.Validators...) // combine the app state - appState, err := appendState(appState, piece.AppState) + appState, err = appendState(cdc, appState, piece.AppState) if err != nil { return err } // write the appended genesis file - return WriteGenesisFile(cdc, genFile, piece.chainID, validators, appState) + return WriteGenesisFile(cdc, genFile, piece.ChainID, validators, appState) - // XXX read in nodeKey and combine new nodeID file + // XXX XXX XXX read in configTOMBL and combine new nodeID file return nil } @@ -218,7 +242,7 @@ func addAppStateToGenesis(cdc *wire.Codec, genesisConfigPath string, appState js type GenAppParams func(*wire.Codec, crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, cliPrint json.RawMessage, err error) // append appState1 with appState2 -type AppendAppState func(cdc *wire.Codec, appState1, appState2 json.RawMesssage) (appState json.RawMessage, err error) +type AppendAppState func(cdc *wire.Codec, appState1, appState2 json.RawMessage) (appState json.RawMessage, err error) // Create one account with a whole bunch of mycoin in it func SimpleGenAppParams(cdc *wire.Codec, pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, cliPrint json.RawMessage, err error) { diff --git a/server/util.go b/server/util.go index 1d60428b58..4b34d0383f 100644 --- a/server/util.go +++ b/server/util.go @@ -71,7 +71,7 @@ func AddCommands( rootCmd.PersistentFlags().String("log_level", ctx.Config.LogLevel, "Log level") rootCmd.AddCommand( - InitCmd(ctx, cdc, appState), + InitCmd(ctx, cdc, appState, nil), StartCmd(ctx, appCreator), UnsafeResetAllCmd(ctx), ShowNodeIDCmd(ctx), diff --git a/x/auth/client/cli/account.go b/x/auth/client/cli/account.go index c6c8c6c548..b45cb12ddf 100644 --- a/x/auth/client/cli/account.go +++ b/x/auth/client/cli/account.go @@ -4,7 +4,6 @@ import ( "encoding/hex" "fmt" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/context" @@ -32,56 +31,40 @@ func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder { // GetAccountCmd returns a query account that will display the // state of the account at a given address func GetAccountCmd(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder) *cobra.Command { - cmdr := commander{ - storeName, - cdc, - decoder, - } return &cobra.Command{ - Use: "account
", + Use: "account [address]", Short: "Query account balance", - RunE: cmdr.getAccountCmd, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + + // find the key to look up the account + addr := args[0] + bz, err := hex.DecodeString(addr) + if err != nil { + return err + } + key := sdk.Address(bz) + + // perform query + ctx := context.NewCoreContextFromViper() + res, err := ctx.Query(key, storeName) + if err != nil { + return err + } + + // decode the value + account, err := decoder(res) + if err != nil { + return err + } + + // print out whole account + output, err := wire.MarshalJSONIndent(cdc, account) + if err != nil { + return err + } + fmt.Println(string(output)) + return nil + }, } } - -type commander struct { - storeName string - cdc *wire.Codec - decoder sdk.AccountDecoder -} - -func (c commander) getAccountCmd(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide an account name") - } - - // find the key to look up the account - addr := args[0] - bz, err := hex.DecodeString(addr) - if err != nil { - return err - } - key := sdk.Address(bz) - - ctx := context.NewCoreContextFromViper() - - res, err := ctx.Query(key, c.storeName) - if err != nil { - return err - } - - // decode the value - account, err := c.decoder(res) - if err != nil { - return err - } - - // print out whole account - output, err := wire.MarshalJSONIndent(c.cdc, account) - if err != nil { - return err - } - fmt.Println(string(output)) - - return nil -}