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-12-20 11:09:43 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2018-03-18 15:38:18 -07:00
|
|
|
)
|
|
|
|
|
2018-10-20 23:22:48 -07:00
|
|
|
// AccountKeeper(/Keeper) and IBCMapper should use different StoreKey later
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
type testInput struct {
|
|
|
|
cdc *codec.Codec
|
|
|
|
ctx sdk.Context
|
|
|
|
ak auth.AccountKeeper
|
|
|
|
bk bank.BaseKeeper
|
|
|
|
ibcKey *sdk.KVStoreKey
|
2018-03-18 15:38:18 -07:00
|
|
|
}
|
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
func setupTestInput() testInput {
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
cdc := makeCodec()
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
ibcKey := sdk.NewKVStoreKey("ibcCapKey")
|
|
|
|
authCapKey := sdk.NewKVStoreKey("authCapKey")
|
|
|
|
fckCapKey := sdk.NewKVStoreKey("fckCapKey")
|
|
|
|
keyParams := sdk.NewKVStoreKey("params")
|
|
|
|
tkeyParams := sdk.NewTransientStoreKey("transient_params")
|
|
|
|
|
|
|
|
ms := store.NewCommitMultiStore(db)
|
|
|
|
ms.MountStoreWithDB(ibcKey, sdk.StoreTypeIAVL, db)
|
|
|
|
ms.MountStoreWithDB(authCapKey, sdk.StoreTypeIAVL, db)
|
|
|
|
ms.MountStoreWithDB(fckCapKey, sdk.StoreTypeIAVL, db)
|
|
|
|
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
|
|
|
|
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
|
|
|
|
ms.LoadLatestVersion()
|
|
|
|
|
|
|
|
pk := params.NewKeeper(cdc, keyParams, tkeyParams)
|
|
|
|
ak := auth.NewAccountKeeper(
|
|
|
|
cdc, authCapKey, pk.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount,
|
|
|
|
)
|
2019-01-28 19:06:48 -08:00
|
|
|
bk := bank.NewBaseKeeper(ak, pk.Subspace(bank.DefaultParamspace), bank.DefaultCodespace)
|
2018-12-20 11:09:43 -08:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: "test-chain-id"}, false, log.NewNopLogger())
|
|
|
|
|
|
|
|
ak.SetParams(ctx, auth.DefaultParams())
|
|
|
|
|
|
|
|
return testInput{cdc: cdc, ctx: ctx, ak: ak, bk: bk, ibcKey: ibcKey}
|
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)
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
func newAddress() sdk.AccAddress {
|
|
|
|
return sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
|
|
|
|
}
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
func getCoins(ck bank.Keeper, ctx sdk.Context, addr sdk.AccAddress) (sdk.Coins, sdk.Error) {
|
|
|
|
zero := sdk.Coins(nil)
|
|
|
|
coins, _, err := ck.AddCoins(ctx, addr, zero)
|
|
|
|
return coins, err
|
|
|
|
}
|
2018-03-18 15:38:18 -07:00
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
func TestIBC(t *testing.T) {
|
|
|
|
input := setupTestInput()
|
|
|
|
ctx := input.ctx
|
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-12-20 11:09:43 -08:00
|
|
|
coins, _, err := input.bk.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-12-20 11:09:43 -08:00
|
|
|
ibcm := NewMapper(input.cdc, input.ibcKey, DefaultCodespace)
|
|
|
|
h := NewHandler(ibcm, input.bk)
|
2018-03-18 15:38:18 -07:00
|
|
|
packet := IBCPacket{
|
|
|
|
SrcAddr: src,
|
|
|
|
DestAddr: dest,
|
|
|
|
Coins: mycoins,
|
|
|
|
SrcChain: chainid,
|
|
|
|
DestChain: chainid,
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
store := ctx.KVStore(input.ibcKey)
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
var msg sdk.Msg
|
|
|
|
var res sdk.Result
|
2018-11-26 03:29:21 -08:00
|
|
|
var egl uint64
|
|
|
|
var igs uint64
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
egl = ibcm.getEgressLength(store, chainid)
|
2018-11-26 03:29:21 -08:00
|
|
|
require.Equal(t, egl, uint64(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
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
coins, err = getCoins(input.bk, 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-11-26 03:29:21 -08:00
|
|
|
require.Equal(t, egl, uint64(1))
|
2018-03-18 15:38:18 -07:00
|
|
|
|
|
|
|
igs = ibcm.GetIngressSequence(ctx, chainid)
|
2018-11-26 03:29:21 -08:00
|
|
|
require.Equal(t, igs, uint64(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
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
coins, err = getCoins(input.bk, 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-11-26 03:29:21 -08:00
|
|
|
require.Equal(t, igs, uint64(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-11-26 03:29:21 -08:00
|
|
|
require.Equal(t, igs, uint64(1))
|
2018-03-18 15:38:18 -07:00
|
|
|
}
|