Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ibc hooks & add unit tests #35

Merged
merged 12 commits into from
Sep 8, 2021
5 changes: 4 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ func New(
app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper,
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper,
)
transferModule := transfer.NewAppModule(app.TransferKeeper)

// Create evidence Keeper for to register the IBC light client misbehaviour evidence route
evidenceKeeper := evidencekeeper.NewKeeper(
Expand Down Expand Up @@ -405,6 +404,10 @@ func New(
)
cronosModule := cronosmodule.NewAppModule(appCodec, app.CronosKeeper)

// Set IBC hooks
app.TransferKeeper = *app.TransferKeeper.SetHooks(app.CronosKeeper)
transferModule := transfer.NewAppModule(app.TransferKeeper)

app.GovKeeper = govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter,
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alp

// TODO: fix keyring upstream
replace github.com/99designs/keyring => github.com/crypto-org-chain/keyring v1.1.6-fixes

// TODO: remove when middleware will be implemented
replace github.com/cosmos/ibc-go => github.com/crypto-org-chain/ibc-go v1.0.1-hooks
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ github.com/cosmos/iavl v0.15.0-rc5/go.mod h1:WqoPL9yPTQ85QBMT45OOUzPxG/U/JcJoN7u
github.com/cosmos/iavl v0.15.3/go.mod h1:OLjQiAQ4fGD2KDZooyJG9yz+p2ao2IAYSbke8mVvSA4=
github.com/cosmos/iavl v0.16.0 h1:ICIOB8xysirTX27GmVAaoeSpeozzgSu9d49w36xkVJA=
github.com/cosmos/iavl v0.16.0/go.mod h1:2A8O/Jz9YwtjqXMO0CjnnbTYEEaovE8jWcwrakH3PoE=
github.com/cosmos/ibc-go v1.0.0/go.mod h1:2wHKQUa+BLJMEyN635KrHfmTTwSNHBtXcqdY8JWGuXA=
github.com/cosmos/ibc-go v1.0.1 h1:3g2e4lghZea6Yrvj5PBViZrHdcEbG6iq7eulq3vsmxk=
github.com/cosmos/ibc-go v1.0.1/go.mod h1:pfLnoW9yUdjSMw3rD0baIsqLBauVAlGFQ1zQ3HGK6J0=
github.com/cosmos/ledger-cosmos-go v0.11.1 h1:9JIYsGnXP613pb2vPjFeMMjBI5lEDsEaF6oYorTy6J4=
github.com/cosmos/ledger-cosmos-go v0.11.1/go.mod h1:J8//BsAGTo3OC/vDLjMRFLW6q0WAaXvHnVc7ZmE8iUY=
github.com/cosmos/ledger-go v0.9.2 h1:Nnao/dLwaVTk1Q5U9THldpUMMXU94BOTWPddSmVB6pI=
Expand All @@ -220,6 +217,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/crypto-org-chain/ibc-go v1.0.1-hooks h1:XNW6yqIAjDBbRHt7KlfCmB/mCcwjfghOmUP4SFN3mVY=
github.com/crypto-org-chain/ibc-go v1.0.1-hooks/go.mod h1:pfLnoW9yUdjSMw3rD0baIsqLBauVAlGFQ1zQ3HGK6J0=
github.com/crypto-org-chain/keyring v1.1.6-fixes h1:AUFSu56NY6XobY6XfRoDx6v3loiOrHK5MNUm32GEjwA=
github.com/crypto-org-chain/keyring v1.1.6-fixes/go.mod h1:0mkLWIoZuQ7uBoospo5Q9zIpqq6rYCPJDSUdeCJvPM8=
github.com/danieljoos/wincred v1.0.2 h1:zf4bhty2iLuwgjgpraD2E9UbvO+fe54XXGJbOwe23fU=
Expand Down
3 changes: 2 additions & 1 deletion gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,10 @@

["github.com/cosmos/ibc-go"]
sumVersion = "v1.0.1"
vendorPath = "github.com/crypto-org-chain/ibc-go"
["github.com/cosmos/ibc-go".fetch]
type = "git"
url = "https://github.com/cosmos/ibc-go"
url = "https://github.com/crypto-org-chain/ibc-go"
rev = "446797b77958996bf1f1d15205cb75a4dbe1f3fa"
sha256 = "1vyjk79c0hjigpjamc2xibgjlmrljrd1wpx2lj52j5biff46z5hk"

Expand Down
2 changes: 1 addition & 1 deletion x/cronos/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func CmdSendToCryptoOrg() *cobra.Command {
return err
}

msg := types.NewMsgMsgTransferTokens(clientCtx.GetFromAddress().String(), argsTo, coins)
msg := types.NewMsgTransferTokens(clientCtx.GetFromAddress().String(), argsTo, coins)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand Down
64 changes: 64 additions & 0 deletions x/cronos/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cronos_test

import (
"github.com/crypto-org-chain/cronos/x/cronos"
"github.com/crypto-org-chain/cronos/x/cronos/types"
)

func (suite *CronosTestSuite) TestInitGenesis() {
testCases := []struct {
name string
malleate func()
genState *types.GenesisState
expPanic bool
}{
{
"default",
func() {},
types.DefaultGenesis(),
false,
},
{
"Wrong ibcCroDenom length",
func() {},
&types.GenesisState{
Params: types.Params{
IbcCroDenom: "ibc/6B5A664BF0AF4F71B2F0BAA33141E2F1321242FBD5D19762F541EC971ACB086534",
}},
true,
},
{
"Wrong ibcCroDenom prefix",
func() {},
&types.GenesisState{
Params: types.Params{
IbcCroDenom: "aaa/6B5A664BF0AF4F71B2F0BAA33141E2F1321242FBD5D19762F541EC971ACB0865",
}},
true,
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
tc.malleate()
if tc.expPanic {
suite.Require().Panics(
func() {
cronos.InitGenesis(suite.ctx, suite.app.CronosKeeper, *tc.genState)
},
)
} else {
suite.Require().NotPanics(
func() {
cronos.InitGenesis(suite.ctx, suite.app.CronosKeeper, *tc.genState)
},
)
}
})
}
}

func (suite *CronosTestSuite) TestExportGenesis() {
genesisState := cronos.ExportGenesis(suite.ctx, suite.app.CronosKeeper)
suite.Require().Equal(genesisState.Params.IbcCroDenom, types.DefaultParams().IbcCroDenom)
}
135 changes: 135 additions & 0 deletions x/cronos/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package cronos_test

import (
"errors"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/crypto-org-chain/cronos/app"
"github.com/crypto-org-chain/cronos/x/cronos"
"github.com/crypto-org-chain/cronos/x/cronos/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
"strings"
"testing"
"time"
)

type CronosTestSuite struct {
suite.Suite

ctx sdk.Context
handler sdk.Handler
app *app.App
address sdk.AccAddress
}

func TestCronosTestSuite(t *testing.T) {
suite.Run(t, new(CronosTestSuite))
}

func (suite *CronosTestSuite) SetupTest() {
checkTx := false
suite.app = app.Setup(false)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1, ChainID: app.TestAppChainID, Time: time.Now().UTC()})
suite.handler = cronos.NewHandler(suite.app.CronosKeeper)

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

func (suite *CronosTestSuite) TestInvalidMsg() {
res, err := suite.handler(sdk.NewContext(nil, tmproto.Header{}, false, nil), testdata.NewTestMsg())
suite.Require().Error(err)
suite.Nil(res)

_, _, log := sdkerrors.ABCIInfo(err, false)
suite.Require().True(strings.Contains(log, "unrecognized cronos message type"))
}

func (suite *CronosTestSuite) TestMsgConvertVouchers() {
testCases := []struct {
name string
msg *types.MsgConvertVouchers
malleate func()
expectedError error
}{
{
"Wrong address",
types.NewMsgConvertVouchers("test", sdk.NewCoins(sdk.NewCoin("aphoton", sdk.NewInt(1)))),
func() {},
errors.New("decoding bech32 failed: invalid bech32 string length 4"),
},
{
"Empty address",
types.NewMsgConvertVouchers("", sdk.NewCoins(sdk.NewCoin("aphoton", sdk.NewInt(1)))),
func() {},
errors.New("empty address string is not allowed"),
},
{
"Correct address with non supported coin denom",
types.NewMsgConvertVouchers(suite.address.String(), sdk.NewCoins(sdk.NewCoin("fake", sdk.NewInt(1)))),
func() {},
errors.New("coin fake is not supported"),
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
handler := cronos.NewHandler(suite.app.CronosKeeper)
_, err := handler(suite.ctx, tc.msg)
if tc.expectedError != nil {
suite.Require().EqualError(err, tc.expectedError.Error())
} else {
suite.Require().NoError(err)
}
})
}
}

func (suite *CronosTestSuite) TestMsgTransferTokens() {
testCases := []struct {
name string
msg *types.MsgTransferTokens
malleate func()
expectedError error
}{
{
"Wrong from address",
types.NewMsgTransferTokens("test", "to", sdk.NewCoins(sdk.NewCoin("aphoton", sdk.NewInt(1)))),
func() {},
errors.New("decoding bech32 failed: invalid bech32 string length 4"),
},
{
"Empty from address",
types.NewMsgTransferTokens("", "to", sdk.NewCoins(sdk.NewCoin("aphoton", sdk.NewInt(1)))),
func() {},
errors.New("empty address string is not allowed"),
},
{
"Empty to address",
types.NewMsgTransferTokens(suite.address.String(), "", sdk.NewCoins(sdk.NewCoin("aphoton", sdk.NewInt(1)))),
func() {},
errors.New("to address cannot be empty"),
},
{
"Correct address with non supported coin denom",
types.NewMsgTransferTokens(suite.address.String(), "to", sdk.NewCoins(sdk.NewCoin("fake", sdk.NewInt(1)))),
func() {},
errors.New("coin fake is not supported"),
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
handler := cronos.NewHandler(suite.app.CronosKeeper)
_, err := handler(suite.ctx, tc.msg)
if tc.expectedError != nil {
suite.Require().EqualError(err, tc.expectedError.Error())
} else {
suite.Require().NoError(err)
}
})
}
}
3 changes: 2 additions & 1 deletion x/cronos/keeper/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func (k Keeper) DeployModuleCRC20(ctx sdk.Context, denom string) (common.Address
if err != nil {
return common.Address{}, err
}
data := append(types.ModuleCRC20Contract.Bin, ctor...)
data := types.ModuleCRC20Contract.Bin
data = append(data, ctor...)

msg, res, err := k.CallEVM(ctx, nil, data, big.NewInt(0))
if err != nil {
Expand Down
Loading