cosmos-sdk/app/app_test.go

319 lines
9.0 KiB
Go
Raw Normal View History

package app
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
2017-03-23 17:51:50 -07:00
"github.com/stretchr/testify/require"
2017-07-13 13:58:13 -07:00
abci "github.com/tendermint/abci/types"
2017-07-03 12:34:08 -07:00
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/base"
2017-07-03 12:34:08 -07:00
"github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/modules/fee"
2017-07-21 09:25:30 -07:00
"github.com/tendermint/basecoin/modules/ibc"
2017-07-12 02:02:16 -07:00
"github.com/tendermint/basecoin/modules/nonce"
2017-07-21 09:25:30 -07:00
"github.com/tendermint/basecoin/modules/roles"
2017-07-11 06:21:02 -07:00
"github.com/tendermint/basecoin/stack"
2017-07-06 05:23:38 -07:00
"github.com/tendermint/basecoin/state"
2017-05-01 07:03:54 -07:00
wire "github.com/tendermint/go-wire"
"github.com/tendermint/tmlibs/log"
)
2017-07-21 09:25:30 -07:00
// DefaultHandler for the tests (coin, roles, ibc)
func DefaultHandler(feeDenom string) basecoin.Handler {
// use the default stack
r := roles.NewHandler()
i := ibc.NewHandler()
return stack.New(
base.Logger{},
stack.Recovery{},
auth.Signatures{},
base.Chain{},
stack.Checkpoint{OnCheck: true},
nonce.ReplayCheck{},
).
IBC(ibc.NewMiddleware()).
Apps(
roles.NewMiddleware(),
fee.NewSimpleFeeMiddleware(coin.Coin{feeDenom, 0}, fee.Bank),
stack.Checkpoint{OnDeliver: true},
).
Dispatch(
coin.NewHandler(),
2017-07-21 09:25:30 -07:00
stack.WrapHandler(r),
stack.WrapHandler(i),
)
}
2017-04-13 18:33:39 -07:00
//--------------------------------------------------------
// test environment is a list of input and output accounts
type appTest struct {
t *testing.T
chainID string
app *Basecoin
2017-07-06 04:40:02 -07:00
acctIn *coin.AccountWithKey
acctOut *coin.AccountWithKey
2017-04-13 18:33:39 -07:00
}
func newAppTest(t *testing.T) *appTest {
at := &appTest{
t: t,
chainID: "test_chain_id",
}
at.reset()
return at
}
// baseTx is the
func (at *appTest) baseTx(coins coin.Coins) basecoin.Tx {
2017-07-12 09:54:07 -07:00
in := []coin.TxInput{{Address: at.acctIn.Actor(), Coins: coins}}
2017-07-06 04:40:02 -07:00
out := []coin.TxOutput{{Address: at.acctOut.Actor(), Coins: coins}}
2017-07-03 12:34:08 -07:00
tx := coin.NewSendTx(in, out)
return tx
}
func (at *appTest) signTx(tx basecoin.Tx) basecoin.Tx {
stx := auth.NewMulti(tx)
auth.Sign(stx, at.acctIn.Key)
2017-07-03 12:34:08 -07:00
return stx.Wrap()
2017-04-13 18:33:39 -07:00
}
2017-07-14 13:29:43 -07:00
func (at *appTest) getTx(coins coin.Coins, sequence uint32) basecoin.Tx {
tx := at.baseTx(coins)
2017-07-14 13:29:43 -07:00
tx = nonce.NewTx(sequence, []basecoin.Actor{at.acctIn.Actor()}, tx)
tx = base.NewChainTx(at.chainID, 0, tx)
return at.signTx(tx)
}
2017-07-14 13:29:43 -07:00
func (at *appTest) feeTx(coins coin.Coins, toll coin.Coin, sequence uint32) basecoin.Tx {
tx := at.baseTx(coins)
tx = fee.NewFee(tx, toll, at.acctIn.Actor())
2017-07-14 13:29:43 -07:00
tx = nonce.NewTx(sequence, []basecoin.Actor{at.acctIn.Actor()}, tx)
tx = base.NewChainTx(at.chainID, 0, tx)
return at.signTx(tx)
}
2017-07-30 09:57:48 -07:00
// set the account on the app through InitState
2017-07-06 04:40:02 -07:00
func (at *appTest) initAccount(acct *coin.AccountWithKey) {
2017-07-30 09:57:48 -07:00
res := at.app.InitState("coin/account", acct.MakeOption())
2017-04-13 18:33:39 -07:00
require.EqualValues(at.t, res, "Success")
}
// reset the in and out accs to be one account each with 7mycoin
func (at *appTest) reset() {
2017-07-06 05:59:45 -07:00
at.acctIn = coin.NewAccountWithKey(coin.Coins{{"mycoin", 7}})
at.acctOut = coin.NewAccountWithKey(coin.Coins{{"mycoin", 7}})
2017-04-13 18:33:39 -07:00
// Note: switch logger if you want to get more info
logger := log.TestingLogger()
// logger := log.NewTracingLogger(log.NewTMLogger(os.Stdout))
2017-07-27 08:17:22 -07:00
store, err := NewStore("", 0, logger.With("module", "store"))
require.Nil(at.t, err, "%+v", err)
at.app = NewBasecoin(
DefaultHandler("mycoin"),
store,
logger.With("module", "app"),
)
2017-04-13 18:33:39 -07:00
2017-07-30 09:57:48 -07:00
res := at.app.InitState("base/chain_id", at.chainID)
2017-04-13 18:33:39 -07:00
require.EqualValues(at.t, res, "Success")
2017-07-06 04:40:02 -07:00
at.initAccount(at.acctIn)
at.initAccount(at.acctOut)
2017-04-13 18:33:39 -07:00
resabci := at.app.Commit()
require.True(at.t, resabci.IsOK(), resabci)
}
func getBalance(key basecoin.Actor, store state.SimpleDB) (coin.Coins, error) {
cspace := stack.PrefixedStore(coin.NameCoin, store)
acct, err := coin.GetAccount(cspace, key)
return acct.Coins, err
2017-07-03 12:34:08 -07:00
}
func getAddr(addr []byte, state state.SimpleDB) (coin.Coins, error) {
actor := auth.SigPerm(addr)
2017-07-06 04:40:02 -07:00
return getBalance(actor, state)
2017-07-03 12:34:08 -07:00
}
2017-04-13 18:33:39 -07:00
// returns the final balance and expected balance for input and output accounts
2017-07-06 05:59:45 -07:00
func (at *appTest) exec(t *testing.T, tx basecoin.Tx, checkTx bool) (res abci.Result, diffIn, diffOut coin.Coins) {
2017-07-03 12:34:08 -07:00
require := require.New(t)
2017-04-13 18:33:39 -07:00
2017-07-06 04:40:02 -07:00
initBalIn, err := getBalance(at.acctIn.Actor(), at.app.GetState())
2017-07-03 12:34:08 -07:00
require.Nil(err, "%+v", err)
2017-07-06 04:40:02 -07:00
initBalOut, err := getBalance(at.acctOut.Actor(), at.app.GetState())
2017-07-03 12:34:08 -07:00
require.Nil(err, "%+v", err)
2017-04-13 18:33:39 -07:00
2017-07-03 12:34:08 -07:00
txBytes := wire.BinaryBytes(tx)
2017-04-13 18:33:39 -07:00
if checkTx {
res = at.app.CheckTx(txBytes)
} else {
res = at.app.DeliverTx(txBytes)
}
2017-07-06 04:40:02 -07:00
endBalIn, err := getBalance(at.acctIn.Actor(), at.app.GetState())
2017-07-03 12:34:08 -07:00
require.Nil(err, "%+v", err)
2017-07-06 04:40:02 -07:00
endBalOut, err := getBalance(at.acctOut.Actor(), at.app.GetState())
2017-07-03 12:34:08 -07:00
require.Nil(err, "%+v", err)
return res, endBalIn.Minus(initBalIn), endBalOut.Minus(initBalOut)
2017-04-13 18:33:39 -07:00
}
//--------------------------------------------------------
2017-07-30 09:57:48 -07:00
func TestInitState(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
logger := log.TestingLogger()
2017-07-27 08:17:22 -07:00
store, err := NewStore("", 0, logger.With("module", "store"))
require.Nil(err, "%+v", err)
2017-07-03 12:34:08 -07:00
app := NewBasecoin(
DefaultHandler("atom"),
store,
logger.With("module", "app"),
2017-07-03 12:34:08 -07:00
)
//testing ChainID
2017-03-28 13:32:55 -07:00
chainID := "testChain"
2017-07-30 09:57:48 -07:00
res := app.InitState("base/chain_id", chainID)
assert.EqualValues(app.GetChainID(), chainID)
assert.EqualValues(res, "Success")
// make a nice account...
2017-07-06 05:59:45 -07:00
bal := coin.Coins{{"atom", 77}, {"eth", 12}}
2017-07-06 04:40:02 -07:00
acct := coin.NewAccountWithKey(bal)
2017-07-30 09:57:48 -07:00
res = app.InitState("coin/account", acct.MakeOption())
require.EqualValues(res, "Success")
2017-07-03 12:34:08 -07:00
// make sure it is set correctly, with some balance
coins, err := getBalance(acct.Actor(), app.GetState())
2017-07-03 12:34:08 -07:00
require.Nil(err)
2017-07-06 04:40:02 -07:00
assert.Equal(bal, coins)
// let's parse an account with badly sorted coins...
unsortAddr, err := hex.DecodeString("C471FB670E44D219EE6DF2FC284BE38793ACBCE1")
require.Nil(err)
2017-07-06 05:59:45 -07:00
unsortCoins := coin.Coins{{"BTC", 789}, {"eth", 123}}
unsortAcc := `{
"pub_key": {
"type": "ed25519",
"data": "AD084F0572C116D618B36F2EB08240D1BAB4B51716CCE0E7734B89C8936DCE9A"
},
"coins": [
{
"denom": "eth",
"amount": 123
},
{
"denom": "BTC",
"amount": 789
}
]
}`
2017-07-30 09:57:48 -07:00
res = app.InitState("coin/account", unsortAcc)
require.EqualValues(res, "Success")
2017-07-03 12:34:08 -07:00
coins, err = getAddr(unsortAddr, app.GetState())
2017-07-03 12:34:08 -07:00
require.Nil(err)
assert.True(coins.IsValid())
assert.Equal(unsortCoins, coins)
2017-07-30 09:57:48 -07:00
res = app.InitState("base/dslfkgjdas", "")
assert.NotEqual(res, "Success")
2017-07-30 09:57:48 -07:00
res = app.InitState("dslfkgjdas", "")
assert.NotEqual(res, "Success")
2017-07-30 09:57:48 -07:00
res = app.InitState("dslfkgjdas/szfdjzs", "")
assert.NotEqual(res, "Success")
}
2017-04-13 18:33:39 -07:00
// Test CheckTx and DeliverTx with insufficient and sufficient balance
2017-03-23 17:51:50 -07:00
func TestTx(t *testing.T) {
assert := assert.New(t)
2017-03-28 13:32:55 -07:00
at := newAppTest(t)
2017-03-23 17:51:50 -07:00
//Bad Balance
2017-07-06 05:59:45 -07:00
at.acctIn.Coins = coin.Coins{{"mycoin", 2}}
2017-07-06 04:40:02 -07:00
at.initAccount(at.acctIn)
at.app.Commit()
2017-07-14 13:29:43 -07:00
res, _, _ := at.exec(t, at.getTx(coin.Coins{{"mycoin", 5}}, 1), true)
2017-04-17 16:53:06 -07:00
assert.True(res.IsErr(), "ExecTx/Bad CheckTx: Expected error return from ExecTx, returned: %v", res)
2017-07-14 13:29:43 -07:00
res, diffIn, diffOut := at.exec(t, at.getTx(coin.Coins{{"mycoin", 5}}, 1), false)
2017-04-17 16:53:06 -07:00
assert.True(res.IsErr(), "ExecTx/Bad DeliverTx: Expected error return from ExecTx, returned: %v", res)
2017-07-03 12:34:08 -07:00
assert.True(diffIn.IsZero())
assert.True(diffOut.IsZero())
//Regular CheckTx
2017-03-28 13:32:55 -07:00
at.reset()
2017-07-14 13:29:43 -07:00
res, _, _ = at.exec(t, at.getTx(coin.Coins{{"mycoin", 5}}, 1), true)
2017-04-17 16:53:06 -07:00
assert.True(res.IsOK(), "ExecTx/Good CheckTx: Expected OK return from ExecTx, Error: %v", res)
//Regular DeliverTx
2017-03-28 13:32:55 -07:00
at.reset()
2017-07-06 05:59:45 -07:00
amt := coin.Coins{{"mycoin", 3}}
2017-07-14 13:29:43 -07:00
res, diffIn, diffOut = at.exec(t, at.getTx(amt, 1), false)
2017-04-17 16:53:06 -07:00
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
2017-07-03 12:34:08 -07:00
assert.Equal(amt.Negative(), diffIn)
assert.Equal(amt, diffOut)
//DeliverTx with fee.... 4 get to recipient, 1 extra taxed
at.reset()
amt = coin.Coins{{"mycoin", 4}}
toll := coin.Coin{"mycoin", 1}
2017-07-14 13:29:43 -07:00
res, diffIn, diffOut = at.exec(t, at.feeTx(amt, toll, 1), false)
assert.True(res.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", res)
payment := amt.Plus(coin.Coins{toll}).Negative()
assert.Equal(payment, diffIn)
assert.Equal(amt, diffOut)
2017-03-23 17:51:50 -07:00
}
2017-03-23 17:51:50 -07:00
func TestQuery(t *testing.T) {
assert := assert.New(t)
2017-03-28 13:32:55 -07:00
at := newAppTest(t)
2017-07-14 13:29:43 -07:00
res, _, _ := at.exec(t, at.getTx(coin.Coins{{"mycoin", 5}}, 1), false)
2017-04-17 16:53:06 -07:00
assert.True(res.IsOK(), "Commit, DeliverTx: Expected OK return from DeliverTx, Error: %v", res)
2017-03-28 13:32:55 -07:00
resQueryPreCommit := at.app.Query(abci.RequestQuery{
Path: "/account",
2017-07-06 04:40:02 -07:00
Data: at.acctIn.Address(),
})
2017-03-28 13:32:55 -07:00
res = at.app.Commit()
assert.True(res.IsOK(), res)
2017-03-28 13:32:55 -07:00
resQueryPostCommit := at.app.Query(abci.RequestQuery{
Path: "/account",
2017-07-06 04:40:02 -07:00
Data: at.acctIn.Address(),
})
assert.NotEqual(resQueryPreCommit, resQueryPostCommit, "Query should change before/after commit")
2017-03-23 17:51:50 -07:00
}
func TestSplitKey(t *testing.T) {
assert := assert.New(t)
prefix, suffix := splitKey("foo/bar")
assert.EqualValues("foo", prefix)
assert.EqualValues("bar", suffix)
prefix, suffix = splitKey("foobar")
assert.EqualValues("base", prefix)
assert.EqualValues("foobar", suffix)
prefix, suffix = splitKey("some/complex/issue")
assert.EqualValues("some", prefix)
assert.EqualValues("complex/issue", suffix)
}