cosmos-sdk/plugins/counter/counter_test.go

105 lines
3.6 KiB
Go
Raw Normal View History

package counter
import (
2017-02-24 14:25:48 -08:00
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
2017-02-24 14:25:48 -08:00
"github.com/stretchr/testify/require"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/basecoin/app"
"github.com/tendermint/basecoin/types"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
eyescli "github.com/tendermint/merkleeyes/client"
)
func TestCounterPlugin(t *testing.T) {
2017-03-23 17:51:50 -07:00
assert := assert.New(t)
// Basecoin initialization
2017-01-28 09:29:32 -08:00
eyesCli := eyescli.NewLocalClient("", 0)
chainID := "test_chain_id"
bcApp := app.NewBasecoin(eyesCli)
2017-03-14 10:55:46 -07:00
bcApp.SetOption("base/chain_id", chainID)
2017-02-24 14:25:48 -08:00
// t.Log(bcApp.Info())
// Add Counter plugin
2017-02-12 19:01:17 -08:00
counterPlugin := New()
bcApp.RegisterPlugin(counterPlugin)
// Account initialization
test1PrivAcc := types.PrivAccountFromSecret("test1")
// Seed Basecoin with account
test1Acc := test1PrivAcc.Account
test1Acc.Balance = types.Coins{{"", 1000}, {"gold", 1000}}
2017-02-24 14:25:48 -08:00
accOpt, err := json.Marshal(test1Acc)
require.Nil(t, err)
bcApp.SetOption("base/account", string(accOpt))
// Deliver a CounterTx
2017-01-28 17:01:07 -08:00
DeliverCounterTx := func(gas int64, fee types.Coin, inputCoins types.Coins, inputSequence int, appFee types.Coins) abci.Result {
// Construct an AppTx signature
tx := &types.AppTx{
Gas: gas,
Fee: fee,
Name: counterPlugin.Name(),
Input: types.NewTxInput(test1Acc.PubKey, inputCoins, inputSequence),
2017-01-28 17:01:07 -08:00
Data: wire.BinaryBytes(CounterTx{Valid: true, Fee: appFee}),
}
// Sign request
signBytes := tx.SignBytes(chainID)
2017-02-24 14:25:48 -08:00
// t.Logf("Sign bytes: %X\n", signBytes)
sig := test1PrivAcc.Sign(signBytes)
tx.Input.Signature = crypto.SignatureS{sig}
2017-02-24 14:25:48 -08:00
// t.Logf("Signed TX bytes: %X\n", wire.BinaryBytes(struct{ types.Tx }{tx}))
// Write request
txBytes := wire.BinaryBytes(struct{ types.Tx }{tx})
return bcApp.DeliverTx(txBytes)
}
2017-01-28 17:01:07 -08:00
// REF: DeliverCounterTx(gas, fee, inputCoins, inputSequence, appFee) {
// Test a basic send, no fee
res := DeliverCounterTx(0, types.Coin{}, types.Coins{{"", 1}}, 1, types.Coins{})
2017-03-23 17:51:50 -07:00
assert.True(res.IsOK(), res.String())
// Test fee prevented transaction
res = DeliverCounterTx(0, types.Coin{"", 2}, types.Coins{{"", 1}}, 2, types.Coins{})
2017-03-23 17:51:50 -07:00
assert.True(res.IsErr(), res.String())
// Test input equals fee
res = DeliverCounterTx(0, types.Coin{"", 2}, types.Coins{{"", 2}}, 2, types.Coins{})
2017-03-23 17:51:50 -07:00
assert.True(res.IsOK(), res.String())
// Test more input than fee
res = DeliverCounterTx(0, types.Coin{"", 2}, types.Coins{{"", 3}}, 3, types.Coins{})
2017-03-23 17:51:50 -07:00
assert.True(res.IsOK(), res.String())
2017-01-28 17:01:07 -08:00
// Test input equals fee+appFee
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 3}, {"gold", 1}}, 4, types.Coins{{"", 2}, {"gold", 1}})
2017-03-23 17:51:50 -07:00
assert.True(res.IsOK(), res.String())
2017-01-28 17:01:07 -08:00
// Test fee+appFee prevented transaction, not enough ""
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 2}, {"gold", 1}}, 5, types.Coins{{"", 2}, {"gold", 1}})
2017-03-23 17:51:50 -07:00
assert.True(res.IsErr(), res.String())
2017-01-28 17:01:07 -08:00
// Test fee+appFee prevented transaction, not enough "gold"
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 3}, {"gold", 1}}, 5, types.Coins{{"", 2}, {"gold", 2}})
2017-03-23 17:51:50 -07:00
assert.True(res.IsErr(), res.String())
// Test more input than fee, more ""
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 4}, {"gold", 1}}, 6, types.Coins{{"", 2}, {"gold", 1}})
2017-03-23 17:51:50 -07:00
assert.True(res.IsOK(), res.String())
// Test more input than fee, more "gold"
res = DeliverCounterTx(0, types.Coin{"", 1}, types.Coins{{"", 3}, {"gold", 2}}, 7, types.Coins{{"", 2}, {"gold", 1}})
2017-03-23 17:51:50 -07:00
assert.True(res.IsOK(), res.String())
// REF: DeliverCounterTx(gas, fee, inputCoins, inputSequence, appFee) {w
}