move ibc_test code to basecoin/app_test
This commit is contained in:
parent
5c2805dbec
commit
8a99041512
|
@ -61,14 +61,14 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
|
|||
AddRoute("bank", bank.NewHandler(coinKeeper)).
|
||||
AddRoute("cool", cool.NewHandler(coinKeeper, coolMapper)).
|
||||
AddRoute("sketchy", sketchy.NewHandler()).
|
||||
AddRoute("ibc", ibc.NewHandler(ibcMapper))
|
||||
AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper))
|
||||
|
||||
// initialize BaseApp
|
||||
app.SetTxDecoder(app.txDecoder)
|
||||
app.SetInitChainer(app.initChainer)
|
||||
// TODO: mounting multiple stores is broken
|
||||
// https://github.com/cosmos/cosmos-sdk/issues/532
|
||||
app.MountStoresIAVL(app.capKeyMainStore) // , app.capKeyIBCStore)
|
||||
app.MountStoresIAVL(app.capKeyMainStore, app.capKeyIBCStore)
|
||||
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper))
|
||||
err := app.LoadLatestVersion(app.capKeyMainStore)
|
||||
if err != nil {
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
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/ibc"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
|
@ -267,6 +268,58 @@ func TestQuizMsg(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
bapp := newBasecoinApp()
|
||||
|
||||
chainid := "ibcchain"
|
||||
|
||||
vals := []abci.Validator{}
|
||||
baseAcc := auth.BaseAccount{
|
||||
Address: addr1,
|
||||
Coins: coins,
|
||||
}
|
||||
acc1 := &types.AppAccount{baseAcc, "foobart"}
|
||||
genesisState := types.GenesisState{
|
||||
Accounts: []*types.GenesisAccount{
|
||||
types.NewGenesisAccount(acc1),
|
||||
},
|
||||
}
|
||||
stateBytes, err := json.MarshalIndent(genesisState, "", "\t")
|
||||
require.Nil(t, err)
|
||||
bapp.InitChain(abci.RequestInitChain{vals, stateBytes})
|
||||
bapp.Commit()
|
||||
|
||||
// A checkTx context (true)
|
||||
ctxCheck := bapp.BaseApp.NewContext(true, abci.Header{})
|
||||
res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1)
|
||||
assert.Equal(t, acc1, res1)
|
||||
|
||||
packet := ibc.IBCPacket{
|
||||
SrcAddr: addr1,
|
||||
DestAddr: addr1,
|
||||
Coins: coins,
|
||||
SrcChain: chainid,
|
||||
DestChain: chainid,
|
||||
}
|
||||
|
||||
transferMsg := ibc.IBCTransferMsg{
|
||||
IBCPacket: packet,
|
||||
}
|
||||
|
||||
receiveMsg := ibc.IBCReceiveMsg{
|
||||
IBCPacket: packet,
|
||||
Relayer: addr1,
|
||||
Sequence: 0,
|
||||
}
|
||||
|
||||
SignCheckDeliver(t, bapp, transferMsg, 0, true)
|
||||
CheckBalance(t, bapp, "")
|
||||
SignCheckDeliver(t, bapp, transferMsg, 1, false)
|
||||
SignCheckDeliver(t, bapp, receiveMsg, 2, true)
|
||||
CheckBalance(t, bapp, "10foocoin")
|
||||
SignCheckDeliver(t, bapp, receiveMsg, 3, false)
|
||||
}
|
||||
|
||||
func SignCheckDeliver(t *testing.T, bapp *BasecoinApp, msg sdk.Msg, seq int64, expPass bool) {
|
||||
|
||||
// Sign the tx
|
||||
|
|
|
@ -23,7 +23,13 @@ func NewHandler(ibcm IBCMapper, ck bank.CoinKeeper) sdk.Handler {
|
|||
}
|
||||
|
||||
func handleIBCTransferMsg(ctx sdk.Context, ibcm IBCMapper, ck bank.CoinKeeper, msg IBCTransferMsg) sdk.Result {
|
||||
ibcm.PushPacket(ctx, msg.IBCPacket)
|
||||
packet := msg.IBCPacket
|
||||
_, err := ck.SubtractCoins(ctx, packet.SrcAddr, packet.Coins)
|
||||
if err != nil {
|
||||
return err.Result()
|
||||
}
|
||||
ibcm.PushPacket(ctx, packet)
|
||||
|
||||
return sdk.Result{}
|
||||
}
|
||||
|
||||
|
@ -35,6 +41,11 @@ func handleIBCReceiveMsg(ctx sdk.Context, ibcm IBCMapper, ck bank.CoinKeeper, ms
|
|||
}
|
||||
ibcm.SetIngressSequence(ctx, packet.SrcChain, seq+1)
|
||||
|
||||
_, err := ck.AddCoins(ctx, packet.DestAddr, packet.Coins)
|
||||
if err != nil {
|
||||
return err.Result()
|
||||
}
|
||||
|
||||
// handle packet
|
||||
// packet.Handle(ctx)...
|
||||
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
package ibc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/go-crypto"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/store"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
func defaultContext(key sdk.StoreKey) sdk.Context {
|
||||
db := dbm.NewMemDB()
|
||||
cms := store.NewCommitMultiStore(db)
|
||||
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
|
||||
cms.LoadLatestVersion()
|
||||
ctx := sdk.NewContext(cms, abci.Header{}, false, nil)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func newAddress() crypto.Address {
|
||||
return crypto.GenPrivKeyEd25519().PubKey().Address()
|
||||
}
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
key := sdk.NewKVStoreKey("ibc")
|
||||
ctx := defaultContext(key)
|
||||
ibcm := NewIBCMapper(key)
|
||||
|
||||
h := NewHandler(ibcm)
|
||||
|
||||
src := newAddress()
|
||||
dest := newAddress()
|
||||
chainid := "ibcchain"
|
||||
coin := sdk.Coin{Denom: "neutron", Amount: 10000}
|
||||
|
||||
packet := IBCPacket{
|
||||
SrcAddr: src,
|
||||
DestAddr: dest,
|
||||
Coins: sdk.Coins{coin},
|
||||
SrcChain: chainid,
|
||||
DestChain: chainid,
|
||||
}
|
||||
|
||||
store := ctx.KVStore(key)
|
||||
|
||||
var msg sdk.Msg
|
||||
var res sdk.Result
|
||||
var egl int64
|
||||
var igs int64
|
||||
|
||||
egl = ibcm.getEgressLength(store, chainid)
|
||||
assert.Equal(t, egl, int64(0))
|
||||
|
||||
msg = IBCTransferMsg{
|
||||
IBCPacket: packet,
|
||||
}
|
||||
res = h(ctx, msg)
|
||||
assert.True(t, res.IsOK())
|
||||
|
||||
egl = ibcm.getEgressLength(store, chainid)
|
||||
assert.Equal(t, egl, int64(1))
|
||||
|
||||
igs = ibcm.GetIngressSequence(ctx, chainid)
|
||||
assert.Equal(t, igs, int64(0))
|
||||
|
||||
msg = IBCReceiveMsg{
|
||||
IBCPacket: packet,
|
||||
Relayer: src,
|
||||
Sequence: 0,
|
||||
}
|
||||
res = h(ctx, msg)
|
||||
assert.True(t, res.IsOK())
|
||||
|
||||
igs = ibcm.GetIngressSequence(ctx, chainid)
|
||||
assert.Equal(t, igs, int64(1))
|
||||
|
||||
res = h(ctx, msg)
|
||||
assert.False(t, res.IsOK())
|
||||
|
||||
igs = ibcm.GetIngressSequence(ctx, chainid)
|
||||
assert.Equal(t, igs, int64(1))
|
||||
}
|
|
@ -13,7 +13,7 @@ type IBCMapper struct {
|
|||
cdc *wire.Codec
|
||||
}
|
||||
|
||||
func NewIBCMapper(ibcKey sdk.StoreKey) IBCMapper {
|
||||
func NewMapper(ibcKey sdk.StoreKey) IBCMapper {
|
||||
cdc := wire.NewCodec()
|
||||
|
||||
return IBCMapper{
|
||||
|
|
Loading…
Reference in New Issue