Skip to content

Commit

Permalink
Merge pull request #243 from cosmos/feature/init-option
Browse files Browse the repository at this point in the history
Feature/init option
  • Loading branch information
ethanfrey authored Sep 8, 2017
2 parents 080d9a1 + 4ac089f commit 83c460a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down Expand Up @@ -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"

Expand Down
35 changes: 31 additions & 4 deletions server/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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",
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions server/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
43 changes: 43 additions & 0 deletions tests/cli/init-server.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 83c460a

Please sign in to comment.