Skip to content

Commit

Permalink
role cli uses hex for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Aug 4, 2017
1 parent 2113c9f commit f2adf36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
13 changes: 12 additions & 1 deletion modules/roles/commands/wrap.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package commands

import (
"encoding/hex"

"github.com/spf13/pflag"
"github.com/spf13/viper"

abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"

"github.com/tendermint/basecoin"
txcmd "github.com/tendermint/basecoin/client/commands/txs"
"github.com/tendermint/basecoin/errors"
"github.com/tendermint/basecoin/modules/roles"
)

Expand Down Expand Up @@ -44,5 +50,10 @@ func (RoleWrapper) Register(fs *pflag.FlagSet) {

// parse role turns the string->byte... todo: support hex?
func parseRole(role string) ([]byte, error) {
return []byte(role), nil
res, err := hex.DecodeString(cmn.StripHex(role))
if err != nil {
err = errors.WithMessage("Address is invalid hex", err,
abci.CodeType_EncodingError)
}
return res, err
}
19 changes: 6 additions & 13 deletions tests/cli/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ checkAccount() {
# Desc: Ensures this named role exists, and has the number of members and required signatures as above
checkRole() {
# make sure sender goes down
ROLE=$(${CLIENT_EXE} query role $1)
QROLE=$(${CLIENT_EXE} query role $1)
if ! assertTrue "line=${LINENO}, role must exist" $?; then
return 1
fi

if [ -n "$DEBUG" ]; then echo $ROLE; echo; fi
assertEquals "line=${LINENO}, proper sigs" "$2" $(echo $ROLE | jq .data.min_sigs)
assertEquals "line=${LINENO}, proper app" '"sigs"' $(echo $ROLE | jq '.data.signers[0].app' )
assertEquals "line=${LINENO}, proper signers" "$3" $(echo $ROLE | jq '.data.signers | length')
if [ -n "$DEBUG" ]; then echo $QROLE; echo; fi
assertEquals "line=${LINENO}, proper sigs" "$2" $(echo $QROLE | jq .data.min_sigs)
assertEquals "line=${LINENO}, proper app" '"sigs"' $(echo $QROLE | jq '.data.signers[0].app' )
assertEquals "line=${LINENO}, proper signers" "$3" $(echo $QROLE | jq '.data.signers | length')
return $?
}

Expand Down Expand Up @@ -188,12 +188,6 @@ checkSendTx() {
return $?
}

# XXX Ex Usage: toHex "my-name"
# converts the string into the hex representation of the bytes
toHex() {
echo -n $1 | od -A n -t x1 | sed 's/ //g' | tr 'a-f' 'A-F'
}

# XXX Ex Usage: checkRoleTx $HASH $HEIGHT $NAME $NUM_SIGNERS
# Desc: This looks up the tx by hash, and makes sure the height and type match
# and that the it refers to the proper role
Expand All @@ -211,8 +205,7 @@ checkRoleTx() {
assertEquals "line=${LINENO}, type=nonce" '"nonce"' $(echo $NTX | jq .type)
RTX=$(echo $NTX | jq .data.tx)
assertEquals "line=${LINENO}, type=role/create" '"role/create"' $(echo $RTX | jq .type)
HEXNAME=$(toHex $3)
assertEquals "line=${LINENO}, proper name" "\"$HEXNAME\"" $(echo $RTX | jq .data.role)
assertEquals "line=${LINENO}, proper name" "\"$3\"" $(echo $RTX | jq .data.role)
assertEquals "line=${LINENO}, proper num signers" "$4" $(echo $RTX | jq '.data.signers | length')
return $?
}
Expand Down
23 changes: 12 additions & 11 deletions tests/cli/roles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ACCOUNTS=(jae ethan bucky rigel igor)
RICH=${ACCOUNTS[0]}
POOR=${ACCOUNTS[4]}
DUDE=${ACCOUNTS[2]}
ROLE="10CAFE4E"

oneTimeSetUp() {
if ! quickSetup .basecoin_test_roles roles-chain; then
Expand All @@ -26,26 +27,26 @@ test01SetupRole() {

SIGS=2

assertFalse "line=${LINENO}, missing min-sigs" "echo qwertyuiop | ${CLIENT_EXE} tx create-role --role=bank --members=${MEMBERS} --sequence=1 --name=$RICH"
assertFalse "line=${LINENO}, missing members" "echo qwertyuiop | ${CLIENT_EXE} tx create-role --role=bank --min-sigs=2 --sequence=1 --name=$RICH"
assertFalse "line=${LINENO}, missing min-sigs" "echo qwertyuiop | ${CLIENT_EXE} tx create-role --role=${ROLE} --members=${MEMBERS} --sequence=1 --name=$RICH"
assertFalse "line=${LINENO}, missing members" "echo qwertyuiop | ${CLIENT_EXE} tx create-role --role=${ROLE} --min-sigs=2 --sequence=1 --name=$RICH"
assertFalse "line=${LINENO}, missing role" "echo qwertyuiop | ${CLIENT_EXE} tx create-role --min-sigs=2 --members=${MEMBERS} --sequence=1 --name=$RICH"
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx create-role --role=bank --min-sigs=$SIGS --members=${MEMBERS} --sequence=1 --name=$RICH)
txSucceeded $? "$TX" "bank"
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx create-role --role=${ROLE} --min-sigs=$SIGS --members=${MEMBERS} --sequence=1 --name=$RICH)
txSucceeded $? "$TX" "${ROLE}"
HASH=$(echo $TX | jq .hash | tr -d \")
TX_HEIGHT=$(echo $TX | jq .height)

checkRole bank $SIGS 3
checkRole "${ROLE}" $SIGS 3

# Make sure tx is indexed
checkRoleTx $HASH $TX_HEIGHT "bank" 3
checkRoleTx $HASH $TX_HEIGHT "${ROLE}" 3
}

test02SendTxToRole() {
SENDER=$(getAddr $RICH)
RECV=role:$(toHex bank)
RECV=role:${ROLE}

TX=$(echo qwertyuiop | ${CLIENT_EXE} tx send --fee=90mycoin --amount=10000mycoin --to=$RECV --sequence=2 --name=$RICH)
txSucceeded $? "$TX" "bank"
txSucceeded $? "$TX" "${ROLE}"
HASH=$(echo $TX | jq .hash | tr -d \")
TX_HEIGHT=$(echo $TX | jq .height)

Expand All @@ -60,20 +61,20 @@ test03SendMultiFromRole() {
ONE=$(getAddr $RICH)
TWO=$(getAddr $POOR)
THREE=$(getAddr $DUDE)
BANK=role:$(toHex bank)
BANK=role:${ROLE}

# no money to start mr. poor...
assertFalse "line=${LINENO}, has no money yet" "${CLIENT_EXE} query account $TWO 2>/dev/null"

# let's try to send money from the role directly without multisig
FAIL=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=6000mycoin --from=$BANK --to=$TWO --sequence=1 --name=$POOR 2>/dev/null)
assertFalse "need to assume role" $?
FAIL=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=6000mycoin --from=$BANK --to=$TWO --sequence=2 --assume-role=bank --name=$POOR 2>/dev/null)
FAIL=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=6000mycoin --from=$BANK --to=$TWO --sequence=2 --assume-role=${ROLE} --name=$POOR 2>/dev/null)
assertFalse "need two signatures" $?

# okay, begin a multisig transaction mr. poor...
TX_FILE=$BASE_DIR/tx.json
echo qwertyuiop | ${CLIENT_EXE} tx send --amount=6000mycoin --from=$BANK --to=$TWO --sequence=1 --assume-role=bank --name=$POOR --multi --prepare=$TX_FILE
echo qwertyuiop | ${CLIENT_EXE} tx send --amount=6000mycoin --from=$BANK --to=$TWO --sequence=1 --assume-role=${ROLE} --name=$POOR --multi --prepare=$TX_FILE
assertTrue "line=${LINENO}, successfully prepare tx" $?
# and get some dude to sign it
# FAIL=$(echo qwertyuiop | ${CLIENT_EXE} tx --in=$TX_FILE --name=$POOR 2>/dev/null)
Expand Down

0 comments on commit f2adf36

Please sign in to comment.