Skip to content

Commit

Permalink
add ibc_test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-nguy committed Sep 6, 2021
1 parent 7b0a8c4 commit 3cdf8d9
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 1 deletion.
217 changes: 216 additions & 1 deletion x/cronos/keeper/ibc_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,216 @@
package keeper
package keeper_test

import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crypto-org-chain/cronos/app"
cronosmodulekeeper "github.com/crypto-org-chain/cronos/x/cronos/keeper"
keepertest "github.com/crypto-org-chain/cronos/x/cronos/keeper/mock"
"github.com/crypto-org-chain/cronos/x/cronos/types"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
)

func (suite *KeeperTestSuite) TestConvertVouchersToEvmCoins() {

privKey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
address := sdk.AccAddress(privKey.PubKey().Address())

testCases := []struct {
name string
from string
coin sdk.Coins
malleate func()
expectedError error
preCheck func()
}{
{
"Wrong from address",
"test",
sdk.NewCoins(sdk.NewCoin(types.IbcCroDenomDefaultValue, sdk.NewInt(1))),
func() {},
errors.New("decoding bech32 failed: invalid bech32 string length 4"),
func() {},
},
{
"Empty address",
"",
sdk.NewCoins(sdk.NewCoin(types.IbcCroDenomDefaultValue, sdk.NewInt(1))),
func() {},
errors.New("empty address string is not allowed"),
func() {},
},
{
"Correct address with non supported coin denom",
address.String(),
sdk.NewCoins(sdk.NewCoin("fake", sdk.NewInt(1))),
func() {},
nil,
func() {},
},
{
"Correct address with not enough IBC CRO token",
address.String(),
sdk.NewCoins(sdk.NewCoin(types.IbcCroDenomDefaultValue, sdk.NewInt(123))),
func() {},
errors.New("0ibc/6B5A664BF0AF4F71B2F0BAA33141E2F1321242FBD5D19762F541EC971ACB0865 is smaller than 123ibc/6B5A664BF0AF4F71B2F0BAA33141E2F1321242FBD5D19762F541EC971ACB0865: insufficient funds"),
func() {},
},
{
"Correct address with enough IBC CRO token",
address.String(),
sdk.NewCoins(sdk.NewCoin(types.IbcCroDenomDefaultValue, sdk.NewInt(123))),
func() {
suite.MintCoins(address, sdk.NewCoins(sdk.NewCoin(types.IbcCroDenomDefaultValue, sdk.NewInt(123))))
// Verify balance IBC coin post operation
ibcCroCoin := suite.GetBalance(address, types.IbcCroDenomDefaultValue)
suite.Require().Equal(sdk.NewInt(123), ibcCroCoin.Amount)
// Verify balance EVM coin post operation
evmCoinDenom := suite.app.EvmKeeper.GetParams(suite.ctx).EvmDenom
evmCoin := suite.GetBalance(address, evmCoinDenom)
suite.Require().Equal(sdk.NewInt(0), evmCoin.Amount)
},
nil,
func() {
// Verify balance IBC coin pre operation
ibcCroCoin := suite.GetBalance(address, types.IbcCroDenomDefaultValue)
suite.Require().Equal(sdk.NewInt(0), ibcCroCoin.Amount)
// Verify balance EVM coin pre operation
evmCoin := suite.GetBalance(address, suite.evmParam.EvmDenom)
suite.Require().Equal(sdk.NewInt(1230000000000), evmCoin.Amount)
},
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset

tc.malleate()
err := suite.app.CronosKeeper.ConvertVouchersToEvmCoins(suite.ctx, tc.from, tc.coin)
if tc.expectedError != nil {
suite.Require().EqualError(err, tc.expectedError.Error())
} else {
suite.Require().NoError(err)
tc.preCheck()
}
})
}
}

func (suite *KeeperTestSuite) TestIbcTransferCoins() {

privKey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
address := sdk.AccAddress(privKey.PubKey().Address())

testCases := []struct {
name string
from string
to string
coin sdk.Coins
malleate func()
expectedError error
preCheck func()
}{
{
"Wrong from address",
"test",
"to",
sdk.NewCoins(sdk.NewCoin(suite.evmParam.EvmDenom, sdk.NewInt(1))),
func() {},
errors.New("decoding bech32 failed: invalid bech32 string length 4"),
func() {},
},
{
"Empty address",
"",
"to",
sdk.NewCoins(sdk.NewCoin(suite.evmParam.EvmDenom, sdk.NewInt(1))),
func() {},
errors.New("empty address string is not allowed"),
func() {},
},
{
"Correct address with non supported coin denom",
address.String(),
"to",
sdk.NewCoins(sdk.NewCoin("fake", sdk.NewInt(1))),
func() {},
nil,
func() {},
},
{
"Correct address with too small amount EVM token",
address.String(),
"to",
sdk.NewCoins(sdk.NewCoin(suite.evmParam.EvmDenom, sdk.NewInt(123))),
func() {},
nil,
func() {},
},
{
"Correct address with not enough EVM token",
address.String(),
"to",
sdk.NewCoins(sdk.NewCoin(suite.evmParam.EvmDenom, sdk.NewInt(1230000000000))),
func() {},
errors.New("0aphoton is smaller than 1230000000000aphoton: insufficient funds"),
func() {},
},
{
"Correct address with enough EVM token",
address.String(),
"to",
sdk.NewCoins(sdk.NewCoin(suite.evmParam.EvmDenom, sdk.NewInt(1230000000000))),
func() {
// Mint Coin to user and module
suite.MintCoins(address, sdk.NewCoins(sdk.NewCoin(suite.evmParam.EvmDenom, sdk.NewInt(1230000000000))))
suite.MintCoinsToModule(types.ModuleName, sdk.NewCoins(sdk.NewCoin(types.IbcCroDenomDefaultValue, sdk.NewInt(123))))
// Verify balance IBC coin post operation
ibcCroCoin := suite.GetBalance(address, types.IbcCroDenomDefaultValue)
suite.Require().Equal(sdk.NewInt(0), ibcCroCoin.Amount)
// Verify balance EVM coin post operation
evmCoin := suite.GetBalance(address, suite.evmParam.EvmDenom)
suite.Require().Equal(sdk.NewInt(1230000000000), evmCoin.Amount)
},
nil,
func() {
// Verify balance IBC coin pre operation
ibcCroCoin := suite.GetBalance(address, types.IbcCroDenomDefaultValue)
suite.Require().Equal(sdk.NewInt(123), ibcCroCoin.Amount)
// Verify balance EVM coin pre operation
evmCoinDenom := suite.app.EvmKeeper.GetParams(suite.ctx).EvmDenom
evmCoin := suite.GetBalance(address, evmCoinDenom)
suite.Require().Equal(sdk.NewInt(0), evmCoin.Amount)
},
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset
// Create Cronos Keeper with mock transfer keeper
cronosKeeper := *cronosmodulekeeper.NewKeeper(
app.MakeEncodingConfig().Marshaler,
suite.app.GetKey(types.StoreKey),
suite.app.GetKey(types.MemStoreKey),
suite.app.GetSubspace(types.ModuleName),
suite.app.GetSubspace(evmtypes.ModuleName),
suite.app.BankKeeper,
keepertest.IbcKeeperMock{},
)
suite.app.CronosKeeper = cronosKeeper

tc.malleate()
err := suite.app.CronosKeeper.IbcTransferCoins(suite.ctx, tc.from, tc.to, tc.coin)
if tc.expectedError != nil {
suite.Require().EqualError(err, tc.expectedError.Error())
} else {
suite.Require().NoError(err)
tc.preCheck()
}
})
}
}
18 changes: 18 additions & 0 deletions x/cronos/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
evmtypes "github.com/tharsis/ethermint/x/evm/types"
"testing"
"time"

Expand Down Expand Up @@ -31,6 +32,9 @@ type KeeperTestSuite struct {

ctx sdk.Context
app *app.App

// EVM helpers
evmParam evmtypes.Params
}

func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {
Expand Down Expand Up @@ -87,6 +91,8 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
require.NoError(t, err)
suite.app.StakingKeeper.SetValidator(suite.ctx, validator)

suite.evmParam = suite.app.EvmKeeper.GetParams(suite.ctx)
}

func (suite *KeeperTestSuite) SetupTest() {
Expand All @@ -104,3 +110,15 @@ func (suite *KeeperTestSuite) MintCoins(address sdk.AccAddress, coins sdk.Coins)
}
return nil
}

func (suite *KeeperTestSuite) MintCoinsToModule(module string, coins sdk.Coins) error {
err := suite.app.BankKeeper.MintCoins(suite.ctx, module, coins)
if err != nil {
return err
}
return nil
}

func (suite *KeeperTestSuite) GetBalance(address sdk.AccAddress, denom string) sdk.Coin {
return suite.app.BankKeeper.GetBalance(suite.ctx, address, denom)
}
22 changes: 22 additions & 0 deletions x/cronos/keeper/mock/ibckeeper_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
)

type IbcKeeperMock struct {
}

func (i IbcKeeperMock) SendTransfer(ctx sdk.Context, sourcePort, sourceChannel string, token sdk.Coin, sender sdk.AccAddress, receiver string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64) error {
return nil
}

func (i IbcKeeperMock) GetDenomTrace(ctx sdk.Context, denomTraceHash tmbytes.HexBytes) (types.DenomTrace, bool) {
return types.DenomTrace{
Path: "transfer/channel-0",
BaseDenom: "basetcro",
}, true
}

0 comments on commit 3cdf8d9

Please sign in to comment.