2018-03-18 15:38:18 -07:00
|
|
|
package ibc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-07-25 13:43:37 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
2018-07-02 13:34:06 -07:00
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-03-18 15:38:18 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
|
|
|
)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// AccountMapper(/Keeper) and IBCMapper should use different StoreKey later
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
func defaultContext(key sdk.StoreKey) sdk.Context {
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
cms := store.NewCommitMultiStore(db)
|
|
|
|
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
|
|
|
|
cms.LoadLatestVersion()
|
2018-06-21 18:03:05 -07:00
|
|
|
ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger())
|
2018-03-18 15:38:18 -07:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
func newAddress() sdk.AccAddress {
|
2018-07-25 13:43:37 -07:00
|
|
|
return sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
|
2018-03-18 15:38:18 -07:00
|
|
|
}
|
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
func getCoins(ck bank.Keeper, ctx sdk.Context, addr sdk.AccAddress) (sdk.Coins, sdk.Error) {
|
2018-04-07 00:02:00 -07:00
|
|
|
zero := sdk.Coins(nil)
|
2018-04-26 07:14:51 -07:00
|
|
|
coins, _, err := ck.AddCoins(ctx, addr, zero)
|
|
|
|
return coins, err
|
2018-03-18 15:38:18 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
func makeCodec() *codec.Codec {
|
|
|
|
var cdc = codec.New()
|
2018-04-06 17:25:08 -07:00
|
|
|
|
|
|
|
// Register Msgs
|
|
|
|
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
|
2018-04-18 21:49:24 -07:00
|
|
|
cdc.RegisterConcrete(bank.MsgSend{}, "test/ibc/Send", nil)
|
|
|
|
cdc.RegisterConcrete(bank.MsgIssue{}, "test/ibc/Issue", nil)
|
2018-04-06 17:25:08 -07:00
|
|
|
cdc.RegisterConcrete(IBCTransferMsg{}, "test/ibc/IBCTransferMsg", nil)
|
|
|
|
cdc.RegisterConcrete(IBCReceiveMsg{}, "test/ibc/IBCReceiveMsg", nil)
|
|
|
|
|
|
|
|
// Register AppAccount
|
2018-05-23 22:09:01 -07:00
|
|
|
cdc.RegisterInterface((*auth.Account)(nil), nil)
|
2018-04-06 17:25:08 -07:00
|
|
|
cdc.RegisterConcrete(&auth.BaseAccount{}, "test/ibc/Account", nil)
|
2018-09-13 11:17:32 -07:00
|
|
|
codec.RegisterCrypto(cdc)
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-07-05 09:04:21 -07:00
|
|
|
cdc.Seal()
|
|
|
|
|
2018-03-18 15:38:18 -07:00
|
|
|
return cdc
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIBC(t *testing.T) {
|
2018-03-19 16:22:49 -07:00
|
|
|
cdc := makeCodec()
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
key := sdk.NewKVStoreKey("ibc")
|
|
|
|
ctx := defaultContext(key)
|
|
|
|
|
2018-06-25 19:05:47 -07:00
|
|
|
am := auth.NewAccountMapper(cdc, key, auth.ProtoBaseAccount)
|
2018-09-10 11:25:34 -07:00
|
|
|
ck := bank.NewBaseKeeper(am)
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
src := newAddress()
|
|
|
|
dest := newAddress()
|
|
|
|
chainid := "ibcchain"
|
2018-04-07 00:02:00 -07:00
|
|
|
zero := sdk.Coins(nil)
|
2018-07-30 17:09:50 -07:00
|
|
|
mycoins := sdk.Coins{sdk.NewInt64Coin("mycoin", 10)}
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-04-26 07:14:51 -07:00
|
|
|
coins, _, err := ck.AddCoins(ctx, src, mycoins)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, mycoins, coins)
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
ibcm := NewMapper(cdc, key, DefaultCodespace)
|
2018-03-18 15:38:18 -07:00
|
|
|
h := NewHandler(ibcm, ck)
|
|
|
|
packet := IBCPacket{
|
|
|
|
SrcAddr: src,
|
|
|
|
DestAddr: dest,
|
|
|
|
Coins: mycoins,
|
|
|
|
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)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, egl, int64(0))
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
msg = IBCTransferMsg{
|
|
|
|
IBCPacket: packet,
|
|
|
|
}
|
|
|
|
res = h(ctx, msg)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, res.IsOK())
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
coins, err = getCoins(ck, ctx, src)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, zero, coins)
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
egl = ibcm.getEgressLength(store, chainid)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, egl, int64(1))
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
igs = ibcm.GetIngressSequence(ctx, chainid)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, igs, int64(0))
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
msg = IBCReceiveMsg{
|
|
|
|
IBCPacket: packet,
|
|
|
|
Relayer: src,
|
|
|
|
Sequence: 0,
|
|
|
|
}
|
|
|
|
res = h(ctx, msg)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, res.IsOK())
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
coins, err = getCoins(ck, ctx, dest)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, mycoins, coins)
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
igs = ibcm.GetIngressSequence(ctx, chainid)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, igs, int64(1))
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
res = h(ctx, msg)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, res.IsOK())
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
igs = ibcm.GetIngressSequence(ctx, chainid)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, igs, int64(1))
|
2018-03-18 15:38:18 -07:00
|
|
|
}
|