From 23615c5d37d221729f600fa72b9539b1aa41dbf2 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Thu, 13 Jul 2017 19:29:53 -0400 Subject: [PATCH] almost done! --- cmd/basecli/commands/cmds.go | 1 + .../cmd/countercli/commands/counter.go | 14 +++++ docs/guide/counter/plugins/counter/counter.go | 2 + .../counter/plugins/counter/counter_test.go | 13 ++-- tests/cli/common.sh | 63 ++++++++++--------- tests/cli/counter.sh | 36 +++++------ 6 files changed, 76 insertions(+), 53 deletions(-) diff --git a/cmd/basecli/commands/cmds.go b/cmd/basecli/commands/cmds.go index 3bd6e26a6..832151ca1 100644 --- a/cmd/basecli/commands/cmds.go +++ b/cmd/basecli/commands/cmds.go @@ -66,6 +66,7 @@ func doSendTx(cmd *cobra.Command, args []string) error { return err } + //get the nonce accounts sendTx, ok := tx.Unwrap().(coin.SendTx) if !ok { return errors.New("tx not SendTx") diff --git a/docs/guide/counter/cmd/countercli/commands/counter.go b/docs/guide/counter/cmd/countercli/commands/counter.go index 8364f41cb..3ab9e8e26 100644 --- a/docs/guide/counter/cmd/countercli/commands/counter.go +++ b/docs/guide/counter/cmd/countercli/commands/counter.go @@ -1,6 +1,8 @@ package commands import ( + "fmt" + "github.com/spf13/cobra" "github.com/spf13/viper" @@ -11,6 +13,7 @@ import ( "github.com/tendermint/basecoin/docs/guide/counter/plugins/counter" "github.com/tendermint/basecoin/modules/auth" "github.com/tendermint/basecoin/modules/coin" + "github.com/tendermint/basecoin/modules/nonce" ) //CounterTxCmd is the CLI command to execute the counter @@ -55,6 +58,10 @@ func counterTx(cmd *cobra.Command, args []string) error { return err } + //get the nonce accounts + var addr []byte + nonceAccount := []basecoin.Actor{basecoin.NewActor(counter.NameCounter, addr)} + // TODO: make this more flexible for middleware tx, err = bcmd.WrapFeeTx(tx) if err != nil { @@ -65,6 +72,13 @@ func counterTx(cmd *cobra.Command, args []string) error { return err } + //add the nonce tx layer to the tx + seq := viper.GetInt(FlagSequence) + if seq < 0 { + return fmt.Errorf("sequence must be greater than 0") + } + tx = nonce.NewTx(uint32(seq), nonceAccount, tx) + stx := auth.NewSig(tx) // Sign if needed and post. This it the work-horse diff --git a/docs/guide/counter/plugins/counter/counter.go b/docs/guide/counter/plugins/counter/counter.go index 7a4eb9e4c..d00f0fa90 100644 --- a/docs/guide/counter/plugins/counter/counter.go +++ b/docs/guide/counter/plugins/counter/counter.go @@ -12,6 +12,7 @@ import ( "github.com/tendermint/basecoin/modules/base" "github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/modules/fee" + "github.com/tendermint/basecoin/modules/nonce" "github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/state" ) @@ -101,6 +102,7 @@ func NewHandler(feeDenom string) basecoin.Handler { base.Logger{}, stack.Recovery{}, auth.Signatures{}, + nonce.ReplayCheck{}, base.Chain{}, fee.NewSimpleFeeMiddleware(coin.Coin{feeDenom, 0}, fee.Bank), ).Use(dispatcher) diff --git a/docs/guide/counter/plugins/counter/counter_test.go b/docs/guide/counter/plugins/counter/counter_test.go index 919c42dc7..b8b79a743 100644 --- a/docs/guide/counter/plugins/counter/counter_test.go +++ b/docs/guide/counter/plugins/counter/counter_test.go @@ -7,10 +7,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" abci "github.com/tendermint/abci/types" + "github.com/tendermint/basecoin" "github.com/tendermint/basecoin/app" "github.com/tendermint/basecoin/modules/auth" "github.com/tendermint/basecoin/modules/base" "github.com/tendermint/basecoin/modules/coin" + "github.com/tendermint/basecoin/modules/nonce" "github.com/tendermint/go-wire" eyescli "github.com/tendermint/merkleeyes/client" "github.com/tendermint/tmlibs/log" @@ -40,9 +42,10 @@ func TestCounterPlugin(t *testing.T) { require.Equal(t, "Success", log) // Deliver a CounterTx - DeliverCounterTx := func(valid bool, counterFee coin.Coins) abci.Result { + DeliverCounterTx := func(valid bool, counterFee coin.Coins, sequence uint32) abci.Result { tx := NewTx(valid, counterFee) tx = base.NewChainTx(chainID, 0, tx) + tx = nonce.NewTx(sequence, []basecoin.Actor{acct.Actor()}, tx) stx := auth.NewSig(tx) auth.Sign(stx, acct.Key) txBytes := wire.BinaryBytes(stx.Wrap()) @@ -50,18 +53,18 @@ func TestCounterPlugin(t *testing.T) { } // Test a basic send, no fee - res := DeliverCounterTx(true, nil) + res := DeliverCounterTx(true, nil, 1) assert.True(res.IsOK(), res.String()) // Test an invalid send, no fee - res = DeliverCounterTx(false, nil) + res = DeliverCounterTx(false, nil, 2) assert.True(res.IsErr(), res.String()) // Test an invalid send, with supported fee - res = DeliverCounterTx(true, coin.Coins{{"gold", 100}}) + res = DeliverCounterTx(true, coin.Coins{{"gold", 100}}, 2) assert.True(res.IsOK(), res.String()) // Test unsupported fee - res = DeliverCounterTx(true, coin.Coins{{"silver", 100}}) + res = DeliverCounterTx(true, coin.Coins{{"silver", 100}}, 3) assert.True(res.IsErr(), res.String()) } diff --git a/tests/cli/common.sh b/tests/cli/common.sh index d186db220..6242086f0 100644 --- a/tests/cli/common.sh +++ b/tests/cli/common.sh @@ -46,7 +46,7 @@ quickTearDown() { prepareClient() { echo "Preparing client keys..." ${CLIENT_EXE} reset_all - assertTrue $? + assertTrue "line=${LINENO}, prepare client" $? for i in "${!ACCOUNTS[@]}"; do newKey ${ACCOUNTS[$i]} @@ -60,7 +60,7 @@ prepareClient() { initServer() { echo "Setting up genesis..." SERVE_DIR=$1/server - assertNotNull "no chain" $2 + assertNotNull "line=${LINENO}, no chain" $2 CHAIN=$2 SERVER_LOG=$1/${SERVER_EXE}.log @@ -100,26 +100,26 @@ initClient() { PORT=${2:-46657} # hard-code the expected validator hash ${CLIENT_EXE} init --chain-id=$1 --node=tcp://localhost:${PORT} --valhash=EB168E17E45BAEB194D4C79067FFECF345C64DE6 - assertTrue "initialized light-client" $? + assertTrue "line=${LINENO}, initialized light-client" $? } # XXX Ex Usage1: newKey $NAME # XXX Ex Usage2: newKey $NAME $PASSWORD # Desc: Generates key for given username and password newKey(){ - assertNotNull "keyname required" "$1" + assertNotNull "line=${LINENO}, keyname required" "$1" KEYPASS=${2:-qwertyuiop} (echo $KEYPASS; echo $KEYPASS) | ${CLIENT_EXE} keys new $1 >/dev/null 2>/dev/null - assertTrue "created $1" $? - assertTrue "$1 doesn't exist" "${CLIENT_EXE} keys get $1" + assertTrue "line=${LINENO}, created $1" $? + assertTrue "line=${LINENO}, $1 doesn't exist" "${CLIENT_EXE} keys get $1" } # XXX Ex Usage: getAddr $NAME # Desc: Gets the address for a key name getAddr() { - assertNotNull "keyname required" "$1" + assertNotNull "line=${LINENO}, keyname required" "$1" RAW=$(${CLIENT_EXE} keys get $1) - assertTrue "no key for $1" $? + assertTrue "line=${LINENO}, no key for $1" $? # print the addr echo $RAW | cut -d' ' -f2 } @@ -129,12 +129,12 @@ getAddr() { checkAccount() { # make sure sender goes down ACCT=$(${CLIENT_EXE} query account $1) - if ! assertTrue "account must exist" $?; then + if ! assertTrue "line=${LINENO}, account must exist" $?; then return 1 fi if [ -n "$DEBUG" ]; then echo $ACCT; echo; fi - assertEquals "proper money" "$2" $(echo $ACCT | jq .data.coins[0].amount) + assertEquals "line=${LINENO}, proper money" "$2" $(echo $ACCT | jq .data.coins[0].amount) return $? } @@ -143,8 +143,8 @@ checkAccount() { txSucceeded() { if (assertTrue "sent tx ($3): $2" $1); then TX=$2 - assertEquals "good check ($3): $TX" "0" $(echo $TX | jq .check_tx.code) - assertEquals "good deliver ($3): $TX" "0" $(echo $TX | jq .deliver_tx.code) + assertEquals "line=${LINENO}, good check ($3): $TX" "0" $(echo $TX | jq .check_tx.code) + assertEquals "line=${LINENO}, good deliver ($3): $TX" "0" $(echo $TX | jq .deliver_tx.code) else return 1 fi @@ -155,18 +155,19 @@ txSucceeded() { # and that the first input was from this sender for this amount checkSendTx() { TX=$(${CLIENT_EXE} query tx $1) - assertTrue "found tx" $? + assertTrue "line=${LINENO}, found tx" $? if [ -n "$DEBUG" ]; then echo $TX; echo; fi - assertEquals "proper height" $2 $(echo $TX | jq .height) - assertEquals "type=sigs/one" '"sigs/one"' $(echo $TX | jq .data.type) - CTX=$(echo $TX | jq .data.data.tx) - assertEquals "type=nonce" '"nonce"' $(echo $CTX | jq .type) - assertEquals "type=chain/tx" '"chain/tx"' $(echo $CTX | jq .type) + assertEquals "line=${LINENO}, proper height" $2 $(echo $TX | jq .height) + assertEquals "line=${LINENO}, type=sigs/one" '"sigs/one"' $(echo $TX | jq .data.type) + NTX=$(echo $TX | jq .data.data.tx) + assertEquals "line=${LINENO}, type=nonce" '"nonce"' $(echo $NTX | jq .type) + CTX=$(echo $NTX | jq .data.tx) + assertEquals "line=${LINENO}, type=chain/tx" '"chain/tx"' $(echo $CTX | jq .type) STX=$(echo $CTX | jq .data.tx) - assertEquals "type=coin/send" '"coin/send"' $(echo $STX | jq .type) - assertEquals "proper sender" "\"$3\"" $(echo $STX | jq .data.inputs[0].address.addr) - assertEquals "proper out amount" "$4" $(echo $STX | jq .data.outputs[0].coins[0].amount) + assertEquals "line=${LINENO}, type=coin/send" '"coin/send"' $(echo $STX | jq .type) + assertEquals "line=${LINENO}, proper sender" "\"$3\"" $(echo $STX | jq .data.inputs[0].address.addr) + assertEquals "line=${LINENO}, proper out amount" "$4" $(echo $STX | jq .data.outputs[0].coins[0].amount) return $? } @@ -179,17 +180,19 @@ checkSendFeeTx() { assertTrue "found tx" $? if [ -n "$DEBUG" ]; then echo $TX; echo; fi - assertEquals "proper height" $2 $(echo $TX | jq .height) - assertEquals "type=sigs/one" '"sigs/one"' $(echo $TX | jq .data.type) - CTX=$(echo $TX | jq .data.data.tx) - assertEquals "type=chain/tx" '"chain/tx"' $(echo $CTX | jq .type) + assertEquals "line=${LINENO}, proper height" $2 $(echo $TX | jq .height) + assertEquals "line=${LINENO}, type=sigs/one" '"sigs/one"' $(echo $TX | jq .data.type) + NTX=$(echo $TX | jq .data.data.tx) + assertEquals "line=${LINENO}, type=nonce" '"nonce"' $(echo $NTX | jq .type) + CTX=$(echo $NTX | jq .data.tx) + assertEquals "line=${LINENO}, type=chain/tx" '"chain/tx"' $(echo $CTX | jq .type) FTX=$(echo $CTX | jq .data.tx) - assertEquals "type=fee/tx" '"fee/tx"' $(echo $FTX | jq .type) - assertEquals "proper fee" "$5" $(echo $FTX | jq .data.fee.amount) + assertEquals "line=${LINENO}, type=fee/tx" '"fee/tx"' $(echo $FTX | jq .type) + assertEquals "line=${LINENO}, proper fee" "$5" $(echo $FTX | jq .data.fee.amount) STX=$(echo $FTX | jq .data.tx) - assertEquals "type=coin/send" '"coin/send"' $(echo $STX | jq .type) - assertEquals "proper sender" "\"$3\"" $(echo $STX | jq .data.inputs[0].address.addr) - assertEquals "proper out amount" "$4" $(echo $STX | jq .data.outputs[0].coins[0].amount) + assertEquals "line=${LINENO}, type=coin/send" '"coin/send"' $(echo $STX | jq .type) + assertEquals "line=${LINENO}, proper sender" "\"$3\"" $(echo $STX | jq .data.inputs[0].address.addr) + assertEquals "line=${LINENO}, proper out amount" "$4" $(echo $STX | jq .data.outputs[0].coins[0].amount) return $? } diff --git a/tests/cli/counter.sh b/tests/cli/counter.sh index 30fb29ab4..1cd62c03d 100755 --- a/tests/cli/counter.sh +++ b/tests/cli/counter.sh @@ -21,21 +21,21 @@ test00GetAccount() { SENDER=$(getAddr $RICH) RECV=$(getAddr $POOR) - assertFalse "requires arg" "${CLIENT_EXE} query account" + assertFalse "Line=${LINENO}, requires arg" "${CLIENT_EXE} query account" checkAccount $SENDER "9007199254740992" ACCT2=$(${CLIENT_EXE} query account $RECV 2>/dev/null) - assertFalse "has no genesis account" $? + assertFalse "Line=${LINENO}, has no genesis account" $? } test01SendTx() { SENDER=$(getAddr $RICH) RECV=$(getAddr $POOR) - assertFalse "missing dest" "${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 2>/dev/null" - assertFalse "bad password" "echo foo | ${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 --to=$RECV --name=$RICH 2>/dev/null" - TX=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 --to=$RECV --name=$RICH 2>/dev/null) + assertFalse "Line=${LINENO}, missing dest" "${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 2>/dev/null" + assertFalse "Line=${LINENO}, bad password" "echo foo | ${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 --to=$RECV --name=$RICH 2>/dev/null" + TX=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 --to=$RECV --name=$RICH) txSucceeded $? "$TX" "$RECV" HASH=$(echo $TX | jq .hash | tr -d \") TX_HEIGHT=$(echo $TX | jq .height) @@ -49,7 +49,7 @@ test01SendTx() { test02GetCounter() { COUNT=$(${CLIENT_EXE} query counter 2>/dev/null) - assertFalse "no default count" $? + assertFalse "Line=${LINENO}, no default count" $? } # checkCounter $COUNT $BALANCE @@ -57,15 +57,15 @@ test02GetCounter() { checkCounter() { # make sure sender goes down ACCT=$(${CLIENT_EXE} query counter) - if assertTrue "count is set" $?; then - assertEquals "proper count" "$1" $(echo $ACCT | jq .data.counter) - assertEquals "proper money" "$2" $(echo $ACCT | jq .data.total_fees[0].amount) + if assertTrue "Line=${LINENO}, count is set" $?; then + assertEquals "Line=${LINENO}, proper count" "$1" $(echo $ACCT | jq .data.counter) + assertEquals "Line=${LINENO}, proper money" "$2" $(echo $ACCT | jq .data.total_fees[0].amount) fi } test03AddCount() { SENDER=$(getAddr $RICH) - assertFalse "bad password" "echo hi | ${CLIENT_EXE} tx counter --countfee=100mycoin --sequence=2 --name=${RICH} 2>/dev/null" + assertFalse "Line=${LINENO}, bad password" "echo hi | ${CLIENT_EXE} tx counter --countfee=100mycoin --sequence=2 --name=${RICH} 2>/dev/null" TX=$(echo qwertyuiop | ${CLIENT_EXE} tx counter --countfee=10mycoin --sequence=2 --name=${RICH} --valid) txSucceeded $? "$TX" "counter" @@ -80,14 +80,14 @@ test03AddCount() { # make sure tx is indexed TX=$(${CLIENT_EXE} query tx $HASH --trace) - if assertTrue "found tx" $?; then - assertEquals "proper height" $TX_HEIGHT $(echo $TX | jq .height) - assertEquals "type=sigs/one" '"sigs/one"' $(echo $TX | jq .data.type) - CTX=$(echo $TX | jq .data.data.tx) - assertEquals "type=chain/tx" '"chain/tx"' $(echo $CTX | jq .type) - CNTX=$(echo $CTX | jq .data.tx) - assertEquals "type=cntr/count" '"cntr/count"' $(echo $CNTX | jq .type) - assertEquals "proper fee" "10" $(echo $CNTX | jq .data.fee[0].amount) + if assertTrue "Line=${LINENO}, found tx" $?; then + assertEquals "Line=${LINENO}, proper height" $TX_HEIGHT $(echo $TX | jq .height) + assertEquals "Line=${LINENO}, type=sigs/one" '"sigs/one"' $(echo $TX | jq .data.type) + CTX=$(echo $TX | jq .data.data.tx) + assertEquals "Line=${LINENO}, type=chain/tx" '"chain/tx"' $(echo $CTX | jq .type) + CNTX=$(echo $CTX | jq .data.tx) + assertEquals "Line=${LINENO}, type=cntr/count" '"cntr/count"' $(echo $CNTX | jq .type) + assertEquals "Line=${LINENO}, proper fee" "10" $(echo $CNTX | jq .data.fee[0].amount) fi # test again with fees...