Merge pull request #243 from cosmos/feature/init-option

Feature/init option
This commit is contained in:
Ethan Frey 2017-09-08 20:45:52 +02:00 committed by GitHub
commit 83c460a402
5 changed files with 83 additions and 5 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## CHANGES IN DEVELOP
IMPROVEMENTS:
* initialize options for genesis with `--option` flag on `basecoin init`
## 0.6.2 (July 27, 2017)
IMPROVEMENTS:

View File

@ -3,7 +3,7 @@ GOTOOLS = github.com/mitchellh/gox \
github.com/rigelrozanski/shelldown/cmd/shelldown
TUTORIALS=$(shell find docs/guide -name "*md" -type f)
EXAMPLES := counter eyes basecoin
EXAMPLES := counter eyes basecoin
INSTALL_EXAMPLES := $(addprefix install_,${EXAMPLES})
TEST_EXAMPLES := $(addprefix testex_,${EXAMPLES})
@ -37,6 +37,7 @@ test_unit:
@go test `glide novendor`
test_cli: $(TEST_EXAMPLES)
./tests/cli/init-server.sh
# sudo apt-get install jq
# wget "https://raw.githubusercontent.com/kward/shunit2/master/source/2.1/src/shunit2"

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -25,10 +26,12 @@ var InitCmd = &cobra.Command{
//nolint - flags
var (
FlagChainID = "chain-id" //TODO group with other flags or remove? is this already a flag here?
FlagOption = "option"
)
func init() {
InitCmd.Flags().String(FlagChainID, "test_chain_id", "Chain ID")
InitCmd.Flags().StringSliceP(FlagOption, "p", []string{}, "Genesis option in the format <app>/<option>/<value>")
}
// returns 1 iff it set a file, otherwise 0 (so we can add them)
@ -64,7 +67,31 @@ func initCmd(cmd *cobra.Command, args []string) error {
return errors.New("Address must be 20-bytes in hex")
}
genesis := GetGenesisJSON(viper.GetString(FlagChainID), userAddr)
var optionsStr string
optionsRaw := viper.GetStringSlice(FlagOption)
if len(optionsRaw) > 0 {
var options []string
sep := ",\n "
for i := 0; i < len(optionsRaw); i++ {
s := strings.SplitN(optionsRaw[i], "/", 3)
if len(s) != 3 {
return errors.New("Genesis option must be in the format <app>/<option>/<value>")
}
//Add quotes if the value (s[2]) is not json
if !strings.Contains(s[2], "\"") {
s[2] = `"` + s[2] + `"`
}
option := `"` + s[0] + `/` + s[1] + `", ` + s[2]
options = append(options, option)
}
optionsStr = sep + strings.Join(options[:], sep)
}
genesis := GetGenesisJSON(viper.GetString(FlagChainID), userAddr, optionsStr)
return CreateGenesisValidatorFiles(cfg, genesis, cmd.Root().Name())
}
@ -114,7 +141,7 @@ var PrivValJSON = `{
// GetGenesisJSON returns a new tendermint genesis with Basecoin app_options
// that grant a large amount of "mycoin" to a single address
// TODO: A better UX for generating genesis files
func GetGenesisJSON(chainID, addr string) string {
func GetGenesisJSON(chainID, addr string, options string) string {
return fmt.Sprintf(`{
"app_hash": "",
"chain_id": "%s",
@ -140,8 +167,8 @@ func GetGenesisJSON(chainID, addr string) string {
]
}],
"plugin_options": [
"coin/issuer", {"app": "sigs", "addr": "%s"}
"coin/issuer", {"app": "sigs", "addr": "%s"}%s
]
}
}`, chainID, addr, addr)
}`, chainID, addr, addr, options)
}

View File

@ -35,6 +35,7 @@ func preRunSetup(cmd *cobra.Command, args []string) (err error) {
return nil
}
// SetUpRoot - initialize the root command
func SetUpRoot(cmd *cobra.Command) {
cmd.PersistentPreRunE = preRunSetup
cmd.PersistentFlags().String(FlagLogLevel, defaultLogLevel, "Log level")

43
tests/cli/init-server.sh Executable file
View File

@ -0,0 +1,43 @@
#!/bin/bash
CLIENT_EXE=basecli
SERVER_EXE=basecoin
test01initOption() {
BASE=~/.bc_init_test
rm -rf "$BASE"
mkdir -p "$BASE"
SERVE_DIR="${BASE}/server"
GENESIS_FILE=${SERVE_DIR}/genesis.json
HEX="deadbeef1234deadbeef1234deadbeef1234aaaa"
${SERVER_EXE} init ${HEX} --home="$SERVE_DIR" -p=app1/key1/val1 -p='"app2/key2/{""name"": ""joe"", ""age"": ""100""}"' >/dev/null
if ! assertTrue "line=${LINENO}" $?; then return 1; fi
OPTION1KEY=$(cat ${GENESIS_FILE} | jq '.app_options.plugin_options[2]')
OPTION1VAL=$(cat ${GENESIS_FILE} | jq '.app_options.plugin_options[3]')
OPTION2KEY=$(cat ${GENESIS_FILE} | jq '.app_options.plugin_options[4]')
OPTION2VAL=$(cat ${GENESIS_FILE} | jq '.app_options.plugin_options[5]')
OPTION2VALEXPECTED=$(echo '{"name": "joe", "age": "100"}' | jq '.')
assertEquals "line=${LINENO}" '"app1/key1"' $OPTION1KEY
assertEquals "line=${LINENO}" '"val1"' $OPTION1VAL
assertEquals "line=${LINENO}" '"app2/key2"' $OPTION2KEY
assertEquals "line=${LINENO}" "$OPTION2VALEXPECTED" "$OPTION2VAL"
}
test02runServer() {
# Attempt to begin the server with the custom genesis
SERVER_LOG=$BASE/${SERVER_EXE}.log
startServer $SERVE_DIR $SERVER_LOG
}
oneTimeTearDown() {
quickTearDown
}
# load and run these tests with shunit2!
CLI_DIR=$GOPATH/src/github.com/cosmos/cosmos-sdk/tests/cli
. $CLI_DIR/common.sh
. $CLI_DIR/shunit2