cosmos-sdk/x/bank/app_test.go

208 lines
5.1 KiB
Go
Raw Normal View History

2018-06-07 16:13:11 -07:00
package bank
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
2018-06-07 20:55:14 -07:00
"github.com/cosmos/cosmos-sdk/x/auth/mock"
2018-06-07 16:13:11 -07:00
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
)
// test bank module in a mock application
var (
priv1 = crypto.GenPrivKeyEd25519()
addr1 = priv1.PubKey().Address()
priv2 = crypto.GenPrivKeyEd25519()
addr2 = priv2.PubKey().Address()
addr3 = crypto.GenPrivKeyEd25519().PubKey().Address()
priv4 = crypto.GenPrivKeyEd25519()
addr4 = priv4.PubKey().Address()
coins = sdk.Coins{{"foocoin", 10}}
halfCoins = sdk.Coins{{"foocoin", 5}}
manyCoins = sdk.Coins{{"foocoin", 1}, {"barcoin", 1}}
2018-06-07 17:20:35 -07:00
freeFee = auth.StdFee{ // no fees for a buncha gas
2018-06-07 16:13:11 -07:00
sdk.Coins{{"foocoin", 0}},
100000,
}
sendMsg1 = MsgSend{
Inputs: []Input{NewInput(addr1, coins)},
Outputs: []Output{NewOutput(addr2, coins)},
}
2018-06-07 17:20:35 -07:00
sendMsg2 = MsgSend{
Inputs: []Input{NewInput(addr1, coins)},
Outputs: []Output{
NewOutput(addr2, halfCoins),
NewOutput(addr3, halfCoins),
},
}
sendMsg3 = MsgSend{
Inputs: []Input{
NewInput(addr1, coins),
NewInput(addr4, coins),
},
Outputs: []Output{
NewOutput(addr2, coins),
NewOutput(addr3, coins),
},
}
2018-06-07 16:13:11 -07:00
2018-06-07 17:20:35 -07:00
sendMsg4 = MsgSend{
Inputs: []Input{
NewInput(addr2, coins),
},
Outputs: []Output{
NewOutput(addr1, coins),
},
}
sendMsg5 = MsgSend{
Inputs: []Input{
NewInput(addr1, manyCoins),
},
Outputs: []Output{
NewOutput(addr2, manyCoins),
},
}
)
// initialize the mock application for this module
func getMockApp(t *testing.T) *mock.App {
2018-06-07 16:13:11 -07:00
mapp := mock.NewApp()
RegisterWire(mapp.Cdc)
coinKeeper := NewKeeper(mapp.AccountMapper)
mapp.Router().AddRoute("bank", NewHandler(coinKeeper))
mapp.CompleteSetup(t, []*sdk.KVStoreKey{})
2018-06-07 17:20:35 -07:00
return mapp
}
2018-06-07 16:13:11 -07:00
2018-06-07 17:20:35 -07:00
func TestMsgSendWithAccounts(t *testing.T) {
mapp := getMockApp(t)
2018-06-07 16:13:11 -07:00
2018-06-07 17:20:35 -07:00
// Add an account at genesis
acc := &auth.BaseAccount{
2018-06-07 16:13:11 -07:00
Address: addr1,
2018-06-07 17:20:35 -07:00
Coins: sdk.Coins{{"foocoin", 67}},
2018-06-07 16:13:11 -07:00
}
2018-06-07 17:20:35 -07:00
accs := []auth.Account{acc}
2018-06-07 16:13:11 -07:00
// Construct genesis state
2018-06-07 17:20:35 -07:00
mock.SetGenesis(mapp, accs)
2018-06-07 16:13:11 -07:00
// A checkTx context (true)
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{})
res1 := mapp.AccountMapper.GetAccount(ctxCheck, addr1)
require.NotNil(t, res1)
2018-06-07 17:20:35 -07:00
assert.Equal(t, acc, res1.(*auth.BaseAccount))
2018-06-07 16:13:11 -07:00
// Run a CheckDeliver
2018-06-07 20:55:14 -07:00
mock.SignCheckDeliver(t, mapp.BaseApp, sendMsg1, []int64{0}, true, priv1)
2018-06-07 16:13:11 -07:00
// Check balances
2018-06-07 17:20:35 -07:00
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 57}})
2018-06-07 16:13:11 -07:00
mock.CheckBalance(t, mapp, addr2, sdk.Coins{{"foocoin", 10}})
// Delivering again should cause replay error
2018-06-07 20:55:14 -07:00
mock.SignCheckDeliver(t, mapp.BaseApp, sendMsg1, []int64{0}, false, priv1)
2018-06-07 16:13:11 -07:00
// bumping the txnonce number without resigning should be an auth error
tx := mock.GenTx(sendMsg1, []int64{0}, priv1)
tx.Signatures[0].Sequence = 1
res := mapp.Deliver(tx)
assert.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnauthorized), res.Code, res.Log)
// resigning the tx with the bumped sequence should work
2018-06-07 20:55:14 -07:00
mock.SignCheckDeliver(t, mapp.BaseApp, sendMsg1, []int64{1}, true, priv1)
2018-06-07 16:13:11 -07:00
}
2018-06-07 17:20:35 -07:00
func TestMsgSendMultipleOut(t *testing.T) {
mapp := getMockApp(t)
acc1 := &auth.BaseAccount{
Address: addr1,
Coins: sdk.Coins{{"foocoin", 42}},
}
acc2 := &auth.BaseAccount{
Address: addr2,
Coins: sdk.Coins{{"foocoin", 42}},
}
accs := []auth.Account{acc1, acc2}
mock.SetGenesis(mapp, accs)
// Simulate a Block
2018-06-07 20:55:14 -07:00
mock.SignCheckDeliver(t, mapp.BaseApp, sendMsg2, []int64{0}, true, priv1)
2018-06-07 17:20:35 -07:00
// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 32}})
mock.CheckBalance(t, mapp, addr2, sdk.Coins{{"foocoin", 47}})
mock.CheckBalance(t, mapp, addr3, sdk.Coins{{"foocoin", 5}})
}
func TestSengMsgMultipleInOut(t *testing.T) {
mapp := getMockApp(t)
acc1 := &auth.BaseAccount{
Address: addr1,
Coins: sdk.Coins{{"foocoin", 42}},
}
acc2 := &auth.BaseAccount{
Address: addr2,
Coins: sdk.Coins{{"foocoin", 42}},
}
acc4 := &auth.BaseAccount{
Address: addr4,
Coins: sdk.Coins{{"foocoin", 42}},
}
accs := []auth.Account{acc1, acc2, acc4}
mock.SetGenesis(mapp, accs)
// CheckDeliver
2018-06-07 20:55:14 -07:00
mock.SignCheckDeliver(t, mapp.BaseApp, sendMsg3, []int64{0, 0}, true, priv1, priv4)
2018-06-07 17:20:35 -07:00
// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 32}})
mock.CheckBalance(t, mapp, addr4, sdk.Coins{{"foocoin", 32}})
mock.CheckBalance(t, mapp, addr2, sdk.Coins{{"foocoin", 52}})
mock.CheckBalance(t, mapp, addr3, sdk.Coins{{"foocoin", 10}})
}
func TestMsgSendDependent(t *testing.T) {
mapp := getMockApp(t)
acc1 := &auth.BaseAccount{
Address: addr1,
Coins: sdk.Coins{{"foocoin", 42}},
}
accs := []auth.Account{acc1}
mock.SetGenesis(mapp, accs)
// CheckDeliver
2018-06-07 20:55:14 -07:00
mock.SignCheckDeliver(t, mapp.BaseApp, sendMsg1, []int64{0}, true, priv1)
2018-06-07 17:20:35 -07:00
// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 32}})
mock.CheckBalance(t, mapp, addr2, sdk.Coins{{"foocoin", 10}})
// Simulate a Block
2018-06-07 20:55:14 -07:00
mock.SignCheckDeliver(t, mapp.BaseApp, sendMsg4, []int64{0}, true, priv2)
2018-06-07 17:20:35 -07:00
// Check balances
mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 42}})
}