323 lines
9.0 KiB
Go
323 lines
9.0 KiB
Go
package bank_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
|
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
|
supplytypes "github.com/cosmos/cosmos-sdk/x/supply/types"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
"github.com/tendermint/tendermint/crypto"
|
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
|
)
|
|
|
|
type (
|
|
expectedBalance struct {
|
|
addr sdk.AccAddress
|
|
coins sdk.Coins
|
|
}
|
|
|
|
appTestCase struct {
|
|
expSimPass bool
|
|
expPass bool
|
|
msgs []sdk.Msg
|
|
accNums []uint64
|
|
accSeqs []uint64
|
|
privKeys []crypto.PrivKey
|
|
expectedBalances []expectedBalance
|
|
}
|
|
)
|
|
|
|
var (
|
|
priv1 = secp256k1.GenPrivKey()
|
|
addr1 = sdk.AccAddress(priv1.PubKey().Address())
|
|
priv2 = secp256k1.GenPrivKey()
|
|
addr2 = sdk.AccAddress(priv2.PubKey().Address())
|
|
addr3 = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
|
|
priv4 = secp256k1.GenPrivKey()
|
|
addr4 = sdk.AccAddress(priv4.PubKey().Address())
|
|
|
|
coins = sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}
|
|
halfCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 5)}
|
|
manyCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 1), sdk.NewInt64Coin("barcoin", 1)}
|
|
freeFee = auth.NewStdFee(100000, sdk.Coins{sdk.NewInt64Coin("foocoin", 0)})
|
|
|
|
sendMsg1 = types.NewMsgSend(addr1, addr2, coins)
|
|
|
|
multiSendMsg1 = types.MsgMultiSend{
|
|
Inputs: []types.Input{types.NewInput(addr1, coins)},
|
|
Outputs: []types.Output{types.NewOutput(addr2, coins)},
|
|
}
|
|
multiSendMsg2 = types.MsgMultiSend{
|
|
Inputs: []types.Input{types.NewInput(addr1, coins)},
|
|
Outputs: []types.Output{
|
|
types.NewOutput(addr2, halfCoins),
|
|
types.NewOutput(addr3, halfCoins),
|
|
},
|
|
}
|
|
multiSendMsg3 = types.MsgMultiSend{
|
|
Inputs: []types.Input{
|
|
types.NewInput(addr1, coins),
|
|
types.NewInput(addr4, coins),
|
|
},
|
|
Outputs: []types.Output{
|
|
types.NewOutput(addr2, coins),
|
|
types.NewOutput(addr3, coins),
|
|
},
|
|
}
|
|
multiSendMsg4 = types.MsgMultiSend{
|
|
Inputs: []types.Input{
|
|
types.NewInput(addr2, coins),
|
|
},
|
|
Outputs: []types.Output{
|
|
types.NewOutput(addr1, coins),
|
|
},
|
|
}
|
|
multiSendMsg5 = types.MsgMultiSend{
|
|
Inputs: []types.Input{
|
|
types.NewInput(addr1, manyCoins),
|
|
},
|
|
Outputs: []types.Output{
|
|
types.NewOutput(addr2, manyCoins),
|
|
},
|
|
}
|
|
)
|
|
|
|
// initialize the mock application for this module
|
|
func getMockApp(t *testing.T) *mock.App {
|
|
mapp, err := getBenchmarkMockApp()
|
|
supplytypes.RegisterCodec(mapp.Cdc)
|
|
require.NoError(t, err)
|
|
return mapp
|
|
}
|
|
|
|
// overwrite the mock init chainer
|
|
func getInitChainer(mapp *mock.App, keeper keeper.BaseKeeper) sdk.InitChainer {
|
|
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
|
mapp.InitChainer(ctx, req)
|
|
bankGenesis := bank.DefaultGenesisState()
|
|
bank.InitGenesis(ctx, keeper, bankGenesis)
|
|
|
|
return abci.ResponseInitChain{}
|
|
}
|
|
}
|
|
|
|
func TestSendNotEnoughBalance(t *testing.T) {
|
|
mapp := getMockApp(t)
|
|
acc := &auth.BaseAccount{
|
|
Address: addr1,
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 67)},
|
|
}
|
|
|
|
mock.SetGenesis(mapp, []auth.Account{acc})
|
|
|
|
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{})
|
|
|
|
res1 := mapp.AccountKeeper.GetAccount(ctxCheck, addr1)
|
|
require.NotNil(t, res1)
|
|
require.Equal(t, acc, res1.(*auth.BaseAccount))
|
|
|
|
origAccNum := res1.GetAccountNumber()
|
|
origSeq := res1.GetSequence()
|
|
|
|
sendMsg := types.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 100)})
|
|
header := abci.Header{Height: mapp.LastBlockHeight() + 1}
|
|
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{sendMsg}, []uint64{origAccNum}, []uint64{origSeq}, false, false, priv1)
|
|
|
|
mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 67)})
|
|
|
|
res2 := mapp.AccountKeeper.GetAccount(mapp.NewContext(true, abci.Header{}), addr1)
|
|
require.NotNil(t, res2)
|
|
|
|
require.True(t, res2.GetAccountNumber() == origAccNum)
|
|
require.True(t, res2.GetSequence() == origSeq+1)
|
|
}
|
|
|
|
func TestMsgMultiSendWithAccounts(t *testing.T) {
|
|
mapp := getMockApp(t)
|
|
acc := &auth.BaseAccount{
|
|
Address: addr1,
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 67)},
|
|
}
|
|
|
|
mock.SetGenesis(mapp, []auth.Account{acc})
|
|
|
|
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{})
|
|
|
|
res1 := mapp.AccountKeeper.GetAccount(ctxCheck, addr1)
|
|
require.NotNil(t, res1)
|
|
require.Equal(t, acc, res1.(*auth.BaseAccount))
|
|
|
|
testCases := []appTestCase{
|
|
{
|
|
msgs: []sdk.Msg{multiSendMsg1},
|
|
accNums: []uint64{0},
|
|
accSeqs: []uint64{0},
|
|
expSimPass: true,
|
|
expPass: true,
|
|
privKeys: []crypto.PrivKey{priv1},
|
|
expectedBalances: []expectedBalance{
|
|
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 57)}},
|
|
{addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}},
|
|
},
|
|
},
|
|
{
|
|
msgs: []sdk.Msg{multiSendMsg1, multiSendMsg2},
|
|
accNums: []uint64{0},
|
|
accSeqs: []uint64{0},
|
|
expSimPass: true, // doesn't check signature
|
|
expPass: false,
|
|
privKeys: []crypto.PrivKey{priv1},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
header := abci.Header{Height: mapp.LastBlockHeight() + 1}
|
|
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
|
|
|
|
for _, eb := range tc.expectedBalances {
|
|
mock.CheckBalance(t, mapp, eb.addr, eb.coins)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMsgMultiSendMultipleOut(t *testing.T) {
|
|
mapp := getMockApp(t)
|
|
|
|
acc1 := &auth.BaseAccount{
|
|
Address: addr1,
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)},
|
|
}
|
|
acc2 := &auth.BaseAccount{
|
|
Address: addr2,
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)},
|
|
}
|
|
|
|
mock.SetGenesis(mapp, []auth.Account{acc1, acc2})
|
|
|
|
testCases := []appTestCase{
|
|
{
|
|
msgs: []sdk.Msg{multiSendMsg2},
|
|
accNums: []uint64{0},
|
|
accSeqs: []uint64{0},
|
|
expSimPass: true,
|
|
expPass: true,
|
|
privKeys: []crypto.PrivKey{priv1},
|
|
expectedBalances: []expectedBalance{
|
|
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 32)}},
|
|
{addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 47)}},
|
|
{addr3, sdk.Coins{sdk.NewInt64Coin("foocoin", 5)}},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
header := abci.Header{Height: mapp.LastBlockHeight() + 1}
|
|
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
|
|
|
|
for _, eb := range tc.expectedBalances {
|
|
mock.CheckBalance(t, mapp, eb.addr, eb.coins)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMsgMultiSendMultipleInOut(t *testing.T) {
|
|
mapp := getMockApp(t)
|
|
|
|
acc1 := &auth.BaseAccount{
|
|
Address: addr1,
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)},
|
|
}
|
|
acc2 := &auth.BaseAccount{
|
|
Address: addr2,
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)},
|
|
}
|
|
acc4 := &auth.BaseAccount{
|
|
Address: addr4,
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)},
|
|
}
|
|
|
|
mock.SetGenesis(mapp, []auth.Account{acc1, acc2, acc4})
|
|
|
|
testCases := []appTestCase{
|
|
{
|
|
msgs: []sdk.Msg{multiSendMsg3},
|
|
accNums: []uint64{0, 2},
|
|
accSeqs: []uint64{0, 0},
|
|
expSimPass: true,
|
|
expPass: true,
|
|
privKeys: []crypto.PrivKey{priv1, priv4},
|
|
expectedBalances: []expectedBalance{
|
|
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 32)}},
|
|
{addr4, sdk.Coins{sdk.NewInt64Coin("foocoin", 32)}},
|
|
{addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 52)}},
|
|
{addr3, sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
header := abci.Header{Height: mapp.LastBlockHeight() + 1}
|
|
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
|
|
|
|
for _, eb := range tc.expectedBalances {
|
|
mock.CheckBalance(t, mapp, eb.addr, eb.coins)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMsgMultiSendDependent(t *testing.T) {
|
|
mapp := getMockApp(t)
|
|
|
|
acc1 := auth.NewBaseAccountWithAddress(addr1)
|
|
acc2 := auth.NewBaseAccountWithAddress(addr2)
|
|
err := acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))
|
|
require.NoError(t, err)
|
|
err = acc2.SetAccountNumber(1)
|
|
require.NoError(t, err)
|
|
|
|
mock.SetGenesis(mapp, []auth.Account{&acc1, &acc2})
|
|
|
|
testCases := []appTestCase{
|
|
{
|
|
msgs: []sdk.Msg{multiSendMsg1},
|
|
accNums: []uint64{0},
|
|
accSeqs: []uint64{0},
|
|
expSimPass: true,
|
|
expPass: true,
|
|
privKeys: []crypto.PrivKey{priv1},
|
|
expectedBalances: []expectedBalance{
|
|
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 32)}},
|
|
{addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}},
|
|
},
|
|
},
|
|
{
|
|
msgs: []sdk.Msg{multiSendMsg4},
|
|
accNums: []uint64{1},
|
|
accSeqs: []uint64{0},
|
|
expSimPass: true,
|
|
expPass: true,
|
|
privKeys: []crypto.PrivKey{priv2},
|
|
expectedBalances: []expectedBalance{
|
|
{addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
header := abci.Header{Height: mapp.LastBlockHeight() + 1}
|
|
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
|
|
|
|
for _, eb := range tc.expectedBalances {
|
|
mock.CheckBalance(t, mapp, eb.addr, eb.coins)
|
|
}
|
|
}
|
|
}
|