role cli uses hex for consistency

This commit is contained in:
Ethan Frey 2017-08-04 14:44:16 +02:00
parent 2113c9f884
commit f2adf3645a
3 changed files with 30 additions and 25 deletions

View File

@ -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"
)
@ -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
}

View File

@ -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 $?
}
@ -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
@ -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 $?
}

View File

@ -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
@ -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)
@ -60,7 +61,7 @@ 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"
@ -68,12 +69,12 @@ test03SendMultiFromRole() {
# 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)