From d6d1655ab1a2df07a423616327fec40f074a25b2 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 12 Jul 2017 21:04:34 +0200 Subject: [PATCH] Add support for --fee in countercli --- docs/guide/counter/cmd/counter/main.go | 2 +- .../cmd/countercli/commands/counter.go | 13 ++++++---- docs/guide/counter/plugins/counter/counter.go | 8 ++++--- .../counter/plugins/counter/counter_test.go | 2 +- tests/cli/basictx.sh | 2 +- tests/cli/common.sh | 24 +++++++++++++++++++ tests/cli/counter.sh | 10 ++++++++ 7 files changed, 51 insertions(+), 10 deletions(-) diff --git a/docs/guide/counter/cmd/counter/main.go b/docs/guide/counter/cmd/counter/main.go index b7ba4c48ff..37816b0421 100644 --- a/docs/guide/counter/cmd/counter/main.go +++ b/docs/guide/counter/cmd/counter/main.go @@ -18,7 +18,7 @@ func main() { } // TODO: register the counter here - commands.Handler = counter.NewHandler() + commands.Handler = counter.NewHandler("mycoin") RootCmd.AddCommand( commands.InitCmd, diff --git a/docs/guide/counter/cmd/countercli/commands/counter.go b/docs/guide/counter/cmd/countercli/commands/counter.go index 326b62b19c..9e420b008c 100644 --- a/docs/guide/counter/cmd/countercli/commands/counter.go +++ b/docs/guide/counter/cmd/countercli/commands/counter.go @@ -28,14 +28,15 @@ You must pass --valid for it to count and the countfee will be added to the coun const ( FlagCountFee = "countfee" FlagValid = "valid" - FlagSequence = "sequence" // FIXME: currently not supported... ) func init() { fs := CounterTxCmd.Flags() fs.String(FlagCountFee, "", "Coins to send in the format ,...") fs.Bool(FlagValid, false, "Is count valid?") - fs.Int(FlagSequence, -1, "Sequence number for this transaction") + + fs.String(bcmd.FlagFee, "0mycoin", "Coins for the transaction fee of the format ") + fs.Int(bcmd.FlagSequence, -1, "Sequence number for this transaction") } // TODO: counterTx is very similar to the sendtx one, @@ -55,11 +56,15 @@ func counterTx(cmd *cobra.Command, args []string) error { } // TODO: make this more flexible for middleware - // add the chain info + tx, err = bcmd.WrapFeeTx(tx) + if err != nil { + return err + } tx, err = bcmd.WrapChainTx(tx) if err != nil { return err } + stx := auth.NewSig(tx) // Sign if needed and post. This it the work-horse @@ -78,6 +83,6 @@ func readCounterTxFlags() (tx basecoin.Tx, err error) { return tx, err } - tx = counter.NewTx(viper.GetBool(FlagValid), feeCoins, viper.GetInt(FlagSequence)) + tx = counter.NewTx(viper.GetBool(FlagValid), feeCoins, viper.GetInt(bcmd.FlagSequence)) return tx, nil } diff --git a/docs/guide/counter/plugins/counter/counter.go b/docs/guide/counter/plugins/counter/counter.go index da30a798c3..b1a7b86088 100644 --- a/docs/guide/counter/plugins/counter/counter.go +++ b/docs/guide/counter/plugins/counter/counter.go @@ -11,6 +11,7 @@ import ( "github.com/tendermint/basecoin/modules/auth" "github.com/tendermint/basecoin/modules/base" "github.com/tendermint/basecoin/modules/coin" + "github.com/tendermint/basecoin/modules/fee" "github.com/tendermint/basecoin/stack" "github.com/tendermint/basecoin/state" ) @@ -89,12 +90,12 @@ func ErrDecoding() error { //-------------------------------------------------------------------------------- // NewHandler returns a new counter transaction processing handler -func NewHandler() basecoin.Handler { +func NewHandler(feeDenom string) basecoin.Handler { // use the default stack - coin := coin.NewHandler() + ch := coin.NewHandler() counter := Handler{} dispatcher := stack.NewDispatcher( - stack.WrapHandler(coin), + stack.WrapHandler(ch), counter, ) return stack.New( @@ -102,6 +103,7 @@ func NewHandler() basecoin.Handler { stack.Recovery{}, auth.Signatures{}, 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 2de91d62a7..60c5101fd1 100644 --- a/docs/guide/counter/plugins/counter/counter_test.go +++ b/docs/guide/counter/plugins/counter/counter_test.go @@ -27,7 +27,7 @@ func TestCounterPlugin(t *testing.T) { logger := log.NewTMLogger(os.Stdout).With("module", "app") // logger = log.NewTracingLogger(logger) bcApp := app.NewBasecoin( - NewHandler(), + NewHandler("gold"), eyesCli, logger, ) diff --git a/tests/cli/basictx.sh b/tests/cli/basictx.sh index 46ef2a3ee3..0ecc4d9711 100755 --- a/tests/cli/basictx.sh +++ b/tests/cli/basictx.sh @@ -63,7 +63,7 @@ test02SendTxWithFee() { checkAccount $RECV "1082" # Make sure tx is indexed - # checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" + checkSendFeeTx $HASH $TX_HEIGHT $SENDER "90" "10" } diff --git a/tests/cli/common.sh b/tests/cli/common.sh index ef9eee87a3..c361c1df84 100644 --- a/tests/cli/common.sh +++ b/tests/cli/common.sh @@ -169,6 +169,30 @@ checkSendTx() { return $? } +# XXX Ex Usage: checkSendFeeTx $HASH $HEIGHT $SENDER $AMOUNT $FEE +# Desc: This is like checkSendTx, but asserts a feetx wrapper with $FEE value. +# This looks up the tx by hash, and makes sure the height and type match +# and that the first input was from this sender for this amount +checkSendFeeTx() { + TX=$(${CLIENT_EXE} query tx $1) + 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) + 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) + 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) + return $? +} + + # XXX Ex Usage: waitForBlock $port # Desc: Waits until the block height on that node increases by one waitForBlock() { diff --git a/tests/cli/counter.sh b/tests/cli/counter.sh index cf2f6b391c..30fb29ab40 100755 --- a/tests/cli/counter.sh +++ b/tests/cli/counter.sh @@ -89,6 +89,16 @@ test03AddCount() { assertEquals "type=cntr/count" '"cntr/count"' $(echo $CNTX | jq .type) assertEquals "proper fee" "10" $(echo $CNTX | jq .data.fee[0].amount) fi + + # test again with fees... + TX=$(echo qwertyuiop | ${CLIENT_EXE} tx counter --countfee=7mycoin --fee=4mycoin --sequence=3 --name=${RICH} --valid) + txSucceeded $? "$TX" "counter" + + # make sure the counter was updated, added 7 + checkCounter "2" "17" + + # make sure the account was debited 11 + checkAccount $SENDER "9007199254739979" } # Load common then run these tests with shunit2!