Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table-Driven Bank Module Unit Tests #1764

Merged
merged 2 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ IMPROVEMENTS
* [tools] Remove `rm -rf vendor/` from `make get_vendor_deps`
* [x/stake] Add revoked to human-readable validator
* [x/gov] Votes on a proposal can now be queried
* [x/bank] Unit tests are now table-driven

BUG FIXES
* \#1666 Add intra-tx counter to the genesis validators
199 changes: 134 additions & 65 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,57 @@ package bank
import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/mock"

"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
)

// test bank module in a mock application
type (
expectedBalance struct {
addr sdk.AccAddress
coins sdk.Coins
}

appTestCase struct {
expPass bool
msgs []sdk.Msg
accNums []int64
accSeqs []int64
privKeys []crypto.PrivKey
expectedBalances []expectedBalance
}
)

var (
priv1 = crypto.GenPrivKeyEd25519()
addr1 = sdk.AccAddress(priv1.PubKey().Address())
priv2 = crypto.GenPrivKeyEd25519()
addr2 = sdk.AccAddress(priv2.PubKey().Address())
addr3 = sdk.AccAddress(crypto.GenPrivKeyEd25519().PubKey().Address())
priv4 = crypto.GenPrivKeyEd25519()
addr4 = sdk.AccAddress(priv4.PubKey().Address())
priv1 = crypto.GenPrivKeyEd25519()
addr1 = sdk.AccAddress(priv1.PubKey().Address())
priv2 = crypto.GenPrivKeyEd25519()
addr2 = sdk.AccAddress(priv2.PubKey().Address())
addr3 = sdk.AccAddress(crypto.GenPrivKeyEd25519().PubKey().Address())
priv4 = crypto.GenPrivKeyEd25519()
addr4 = sdk.AccAddress(priv4.PubKey().Address())

coins = sdk.Coins{sdk.NewCoin("foocoin", 10)}
halfCoins = sdk.Coins{sdk.NewCoin("foocoin", 5)}
manyCoins = sdk.Coins{sdk.NewCoin("foocoin", 1), sdk.NewCoin("barcoin", 1)}

freeFee = auth.StdFee{ // no fees for a buncha gas
sdk.Coins{sdk.NewCoin("foocoin", 0)},
100000,
}
freeFee = auth.NewStdFee(100000, sdk.Coins{sdk.NewCoin("foocoin", 0)}...)

sendMsg1 = MsgSend{
Inputs: []Input{NewInput(addr1, coins)},
Outputs: []Output{NewOutput(addr2, coins)},
}

sendMsg2 = MsgSend{
Inputs: []Input{NewInput(addr1, coins)},
Outputs: []Output{
NewOutput(addr2, halfCoins),
NewOutput(addr3, halfCoins),
},
}

sendMsg3 = MsgSend{
Inputs: []Input{
NewInput(addr1, coins),
Expand All @@ -54,7 +64,6 @@ var (
NewOutput(addr3, coins),
},
}

sendMsg4 = MsgSend{
Inputs: []Input{
NewInput(addr2, coins),
Expand All @@ -63,7 +72,6 @@ var (
NewOutput(addr1, coins),
},
}

sendMsg5 = MsgSend{
Inputs: []Input{
NewInput(addr1, manyCoins),
Expand All @@ -83,39 +91,55 @@ func getMockApp(t *testing.T) *mock.App {

func TestMsgSendWithAccounts(t *testing.T) {
mapp := getMockApp(t)

// Add an account at genesis
acc := &auth.BaseAccount{
Address: addr1,
Coins: sdk.Coins{sdk.NewCoin("foocoin", 67)},
}
accs := []auth.Account{acc}

// Construct genesis state
mock.SetGenesis(mapp, accs)
mock.SetGenesis(mapp, []auth.Account{acc})

// A checkTx context (true)
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{})

res1 := mapp.AccountMapper.GetAccount(ctxCheck, addr1)
require.NotNil(t, res1)
require.Equal(t, acc, res1.(*auth.BaseAccount))

// Run a CheckDeliver
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{sendMsg1}, []int64{0}, []int64{0}, true, priv1)
testCases := []appTestCase{
{
msgs: []sdk.Msg{sendMsg1},
accNums: []int64{0},
accSeqs: []int64{0},
expPass: true,
privKeys: []crypto.PrivKey{priv1},
expectedBalances: []expectedBalance{
expectedBalance{addr1, sdk.Coins{sdk.NewCoin("foocoin", 57)}},
expectedBalance{addr2, sdk.Coins{sdk.NewCoin("foocoin", 10)}},
},
},
{
msgs: []sdk.Msg{sendMsg1, sendMsg2},
accNums: []int64{0},
accSeqs: []int64{0},
expPass: false,
privKeys: []crypto.PrivKey{priv1},
},
}

// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("foocoin", 57)})
mock.CheckBalance(t, mapp, addr2, sdk.Coins{sdk.NewCoin("foocoin", 10)})
for _, tc := range testCases {
mock.SignCheckDeliver(t, mapp.BaseApp, tc.msgs, tc.accNums, tc.accSeqs, tc.expPass, tc.privKeys...)

// Delivering again should cause replay error
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{sendMsg1, sendMsg2}, []int64{0}, []int64{0}, false, priv1)
for _, eb := range tc.expectedBalances {
mock.CheckBalance(t, mapp, eb.addr, eb.coins)
}
}

// bumping the txnonce number without resigning should be an auth error
// bumping the tx nonce number without resigning should be an auth error
mapp.BeginBlock(abci.RequestBeginBlock{})

tx := mock.GenTx([]sdk.Msg{sendMsg1}, []int64{0}, []int64{0}, priv1)
tx.Signatures[0].Sequence = 1
res := mapp.Deliver(tx)

res := mapp.Deliver(tx)
require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnauthorized), res.Code, res.Log)

// resigning the tx with the bumped sequence should work
Expand All @@ -129,22 +153,35 @@ func TestMsgSendMultipleOut(t *testing.T) {
Address: addr1,
Coins: sdk.Coins{sdk.NewCoin("foocoin", 42)},
}

acc2 := &auth.BaseAccount{
Address: addr2,
Coins: sdk.Coins{sdk.NewCoin("foocoin", 42)},
}
accs := []auth.Account{acc1, acc2}

mock.SetGenesis(mapp, accs)
mock.SetGenesis(mapp, []auth.Account{acc1, acc2})

testCases := []appTestCase{
{
msgs: []sdk.Msg{sendMsg2},
accNums: []int64{0},
accSeqs: []int64{0},
expPass: true,
privKeys: []crypto.PrivKey{priv1},
expectedBalances: []expectedBalance{
expectedBalance{addr1, sdk.Coins{sdk.NewCoin("foocoin", 32)}},
expectedBalance{addr2, sdk.Coins{sdk.NewCoin("foocoin", 47)}},
expectedBalance{addr3, sdk.Coins{sdk.NewCoin("foocoin", 5)}},
},
},
}

// Simulate a Block
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{sendMsg2}, []int64{0}, []int64{0}, true, priv1)
for _, tc := range testCases {
mock.SignCheckDeliver(t, mapp.BaseApp, tc.msgs, tc.accNums, tc.accSeqs, tc.expPass, tc.privKeys...)

// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("foocoin", 32)})
mock.CheckBalance(t, mapp, addr2, sdk.Coins{sdk.NewCoin("foocoin", 47)})
mock.CheckBalance(t, mapp, addr3, sdk.Coins{sdk.NewCoin("foocoin", 5)})
for _, eb := range tc.expectedBalances {
mock.CheckBalance(t, mapp, eb.addr, eb.coins)
}
}
}

func TestSengMsgMultipleInOut(t *testing.T) {
Expand All @@ -162,18 +199,32 @@ func TestSengMsgMultipleInOut(t *testing.T) {
Address: addr4,
Coins: sdk.Coins{sdk.NewCoin("foocoin", 42)},
}
accs := []auth.Account{acc1, acc2, acc4}

mock.SetGenesis(mapp, accs)
mock.SetGenesis(mapp, []auth.Account{acc1, acc2, acc4})

testCases := []appTestCase{
{
msgs: []sdk.Msg{sendMsg3},
accNums: []int64{0, 2},
accSeqs: []int64{0, 0},
expPass: true,
privKeys: []crypto.PrivKey{priv1, priv4},
expectedBalances: []expectedBalance{
expectedBalance{addr1, sdk.Coins{sdk.NewCoin("foocoin", 32)}},
expectedBalance{addr4, sdk.Coins{sdk.NewCoin("foocoin", 32)}},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nitpick, can the addresses be sorted? i.e. addr1, addr2, addr3 then 4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I had them originally as they were implemented, but I can certainly order them by address.

expectedBalance{addr2, sdk.Coins{sdk.NewCoin("foocoin", 52)}},
expectedBalance{addr3, sdk.Coins{sdk.NewCoin("foocoin", 10)}},
},
},
}

// CheckDeliver
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{sendMsg3}, []int64{0, 2}, []int64{0, 0}, true, priv1, priv4)
for _, tc := range testCases {
mock.SignCheckDeliver(t, mapp.BaseApp, tc.msgs, tc.accNums, tc.accSeqs, tc.expPass, tc.privKeys...)

// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("foocoin", 32)})
mock.CheckBalance(t, mapp, addr4, sdk.Coins{sdk.NewCoin("foocoin", 32)})
mock.CheckBalance(t, mapp, addr2, sdk.Coins{sdk.NewCoin("foocoin", 52)})
mock.CheckBalance(t, mapp, addr3, sdk.Coins{sdk.NewCoin("foocoin", 10)})
for _, eb := range tc.expectedBalances {
mock.CheckBalance(t, mapp, eb.addr, eb.coins)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be left for a seperate PR, but perhaps mock.CheckBalance should take in a message parameter, so that we can have error messages as described in https://github.com/cosmos/cosmos-sdk/blob/develop/CONTRIBUTING.md#testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely worthy for another PR :-)

}
}
}

func TestMsgSendDependent(t *testing.T) {
Expand All @@ -183,20 +234,38 @@ func TestMsgSendDependent(t *testing.T) {
Address: addr1,
Coins: sdk.Coins{sdk.NewCoin("foocoin", 42)},
}
accs := []auth.Account{acc1}

mock.SetGenesis(mapp, accs)

// CheckDeliver
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{sendMsg1}, []int64{0}, []int64{0}, true, priv1)

// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("foocoin", 32)})
mock.CheckBalance(t, mapp, addr2, sdk.Coins{sdk.NewCoin("foocoin", 10)})
mock.SetGenesis(mapp, []auth.Account{acc1})

testCases := []appTestCase{
{
msgs: []sdk.Msg{sendMsg1},
accNums: []int64{0},
accSeqs: []int64{0},
expPass: true,
privKeys: []crypto.PrivKey{priv1},
expectedBalances: []expectedBalance{
expectedBalance{addr1, sdk.Coins{sdk.NewCoin("foocoin", 32)}},
expectedBalance{addr2, sdk.Coins{sdk.NewCoin("foocoin", 10)}},
},
},
{
msgs: []sdk.Msg{sendMsg4},
accNums: []int64{1},
accSeqs: []int64{0},
expPass: true,
privKeys: []crypto.PrivKey{priv2},
expectedBalances: []expectedBalance{
expectedBalance{addr1, sdk.Coins{sdk.NewCoin("foocoin", 42)}},
},
},
}

// Simulate a Block
mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{sendMsg4}, []int64{1}, []int64{0}, true, priv2)
for _, tc := range testCases {
mock.SignCheckDeliver(t, mapp.BaseApp, tc.msgs, tc.accNums, tc.accSeqs, tc.expPass, tc.privKeys...)

// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewCoin("foocoin", 42)})
for _, eb := range tc.expectedBalances {
mock.CheckBalance(t, mapp, eb.addr, eb.coins)
}
}
}