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

Feature: replace ibc-hook with middleware #511

Merged
merged 6 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Improvements
- [cronos#418](https://github.com/crypto-org-chain/cronos/pull/418) Support logs in evm-hooks and return id for SendToEthereum events
- [cronos#489](https://github.com/crypto-org-chain/cronos/pull/489) Enable jemalloc memory allocator, and update rocksdb src to `v6.29.5`.
- [cronos#511](https://github.com/crypto-org-chain/cronos/pull/511) Replace ibc-hook with ibc middleware, use ibc-go upstream version.

*May 3, 2022*

Expand Down
8 changes: 5 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"

"github.com/crypto-org-chain/cronos/x/cronos/middleware"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -468,10 +470,10 @@ func New(
AddRoute(ibchost.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper)).
AddRoute(cronostypes.RouterKey, cronos.NewTokenMappingChangeProposalHandler(app.CronosKeeper))

// Set IBC hooks
app.TransferKeeper = *app.TransferKeeper.SetHooks(app.CronosKeeper)
// set the middleware
transferModule := transfer.NewAppModule(app.TransferKeeper)
transferIBCModule := transfer.NewIBCModule(app.TransferKeeper)
transferWithConversionIBCModule := middleware.NewIBCConversionModule(transferIBCModule, app.CronosKeeper)

app.GovKeeper = govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
Expand Down Expand Up @@ -512,7 +514,7 @@ func New(

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := porttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferWithConversionIBCModule)
// this line is used by starport scaffolding # ibc/app/router
app.IBCKeeper.SetRouter(ibcRouter)

Expand Down
10 changes: 10 additions & 0 deletions docs/architecture/adr-002.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Changelog
* 29-10-2021: Draft of the decision record
* 26-05-2022: Update decision record

## Context

Expand All @@ -14,6 +15,15 @@ The forked version will contain minimal code change compared to the original ver

The forked will be removed once ICS30 middleware is implemented in ibc-go and sufficient testing has been done so that we can ensure a safe upgrade.

** Update **

Cronos#429 (https://github.com/crypto-org-chain/cronos/pull/429) bumps ibc-go dependency to v3.0.0 which include ICS30 middleware support.

Cronos#511(https://github.com/crypto-org-chain/cronos/pull/511) replaces the "hook" with a custom ICS30 middleware.
The middleware implementation override `OnRecvPacket`, `OnAcknowledgmentPacket`, and `OnTimeoutPacket` which add additional logic
to the TransferApp to handle voucher conversion.


## Status

Accepted
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ replace (
github.com/99designs/keyring => github.com/crypto-org-chain/keyring v1.1.6-fixes
github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.44.4-0.20220518050709-bd4ca739c699

// TODO: remove when middleware will be implemented
github.com/cosmos/ibc-go/v3 => github.com/crypto-org-chain/ibc-go/v3 v3.0.0-hooks

github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

// TODO: remove when gravity update dependencies
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ github.com/cosmos/gorocksdb v1.2.0 h1:d0l3jJG8M4hBouIZq0mDUHZ+zjOx044J3nGRskwTb4
github.com/cosmos/gorocksdb v1.2.0/go.mod h1:aaKvKItm514hKfNJpUJXnnOWeBnk2GL4+Qw9NHizILw=
github.com/cosmos/iavl v0.17.3 h1:s2N819a2olOmiauVa0WAhoIJq9EhSXE9HDBAoR9k+8Y=
github.com/cosmos/iavl v0.17.3/go.mod h1:prJoErZFABYZGDHka1R6Oay4z9PrNeFFiMKHDAMOi4w=
github.com/cosmos/ibc-go/v3 v3.0.0 h1:XUNplHVS51Q2gMnTFsFsH9QJ7flsovMamnltKbEgPQ4=
github.com/cosmos/ibc-go/v3 v3.0.0/go.mod h1:Mb+1NXiPOLd+CPFlOC6BKeAUaxXlhuWenMmRiUiSmwY=
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 @@ -276,8 +278,6 @@ github.com/crypto-org-chain/cosmos-sdk v0.44.4-0.20220518050709-bd4ca739c699 h1:
github.com/crypto-org-chain/cosmos-sdk v0.44.4-0.20220518050709-bd4ca739c699/go.mod h1:YkIkmgbvtkoaWjW7NDSVzzdKZRwCiwqt5PbJzXyJ+qM=
github.com/crypto-org-chain/gravity-bridge/module/v2 v2.0.0-20220509015341-a016e2b04866 h1:biYuh4CJV/d8jDpWYvV/7MKFx2KF6J6zvYulasfV/7Y=
github.com/crypto-org-chain/gravity-bridge/module/v2 v2.0.0-20220509015341-a016e2b04866/go.mod h1:oZBIK8vfPH8IaS7vTnydkf1GkStt7YRumpn/PD2QHVY=
github.com/crypto-org-chain/ibc-go/v3 v3.0.0-hooks h1:A0/O8ylYOb+R+m22S0OlMEBFRdfpHD8trte/D4qeMpQ=
github.com/crypto-org-chain/ibc-go/v3 v3.0.0-hooks/go.mod h1:Mb+1NXiPOLd+CPFlOC6BKeAUaxXlhuWenMmRiUiSmwY=
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/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
Expand Down
9 changes: 4 additions & 5 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1054,13 +1054,12 @@
sha256 = "115964xzwlr8j0l8f5x0v31f5hnxhd0rh5cvgy560l0dd77i735k"

["github.com/cosmos/ibc-go/v3"]
sumVersion = "v3.0.0-hooks"
vendorPath = "github.com/crypto-org-chain/ibc-go/v3"
sumVersion = "v3.0.0"
["github.com/cosmos/ibc-go/v3".fetch]
type = "git"
url = "https://github.com/crypto-org-chain/ibc-go"
rev = "7076d27acc5933cfaaf176d25660f9582ca42345"
sha256 = "04y650niw0jm67y77v8fl7wcasjalfr69cf1fsxy34db6r6y5dq7"
url = "https://github.com/cosmos/ibc-go"
rev = "46e020640e66f9043c14c53a4d215a5b457d6703"
sha256 = "14aaifab36jidml8sihb3qsixi0k4vyii0896vg46xmr699y8zf1"

["github.com/cosmos/ledger-cosmos-go"]
sumVersion = "v0.11.1"
Expand Down
59 changes: 0 additions & 59 deletions x/cronos/keeper/ibc_hooks.go

This file was deleted.

89 changes: 0 additions & 89 deletions x/cronos/keeper/ibc_hooks_test.go

This file was deleted.

17 changes: 17 additions & 0 deletions x/cronos/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,20 @@ func (k Keeper) SetAutoContractForDenom(ctx sdk.Context, denom string, address c
store.Set(types.DenomToAutoContractKey(denom), address.Bytes())
store.Set(types.ContractToDenomKey(address.Bytes()), []byte(denom))
}

// OnRecvVouchers try to convert ibc voucher to evm coins, revert the state in case of failure
func (k Keeper) OnRecvVouchers(
ctx sdk.Context,
tokens sdk.Coins,
receiver string) {
cacheCtx, commit := ctx.CacheContext()
err := k.ConvertVouchersToEvmCoins(cacheCtx, receiver, tokens)
if err == nil {
commit()
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
} else {
k.Logger(ctx).Error(
fmt.Sprintf("Failed to convert vouchers to evm tokens for receiver %s, coins %s. Receive error %s",
receiver, tokens.String(), err))
}
}
82 changes: 82 additions & 0 deletions x/cronos/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper_test

import (
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"
"math/big"
"testing"
"time"
Expand Down Expand Up @@ -186,3 +189,82 @@ func (suite *KeeperTestSuite) MintCoinsToModule(module string, coins sdk.Coins)
func (suite *KeeperTestSuite) GetBalance(address sdk.AccAddress, denom string) sdk.Coin {
return suite.app.BankKeeper.GetBalance(suite.ctx, address, denom)
}

func (suite *KeeperTestSuite) TestOnRecvVouchers() {
privKey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
address := sdk.AccAddress(privKey.PubKey().Address())

testCases := []struct {
name string
coins sdk.Coins
malleate func()
postCheck func()
}{
{
"state reverted after error",
sdk.NewCoins(sdk.NewCoin(types.IbcCroDenomDefaultValue, sdk.NewInt(123)), sdk.NewCoin("bad", sdk.NewInt(10))),
func() {
suite.MintCoins(address, sdk.NewCoins(sdk.NewCoin(types.IbcCroDenomDefaultValue, sdk.NewInt(123))))
// 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
evmCoin := suite.GetBalance(address, suite.evmParam.EvmDenom)
suite.Require().Equal(sdk.NewInt(0), evmCoin.Amount)
},
func() {
// 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
evmCoin := suite.GetBalance(address, suite.evmParam.EvmDenom)
suite.Require().Equal(sdk.NewInt(0), evmCoin.Amount)
},
},
{
"state committed upon success",
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 pre operation
ibcCroCoin := suite.GetBalance(address, types.IbcCroDenomDefaultValue)
suite.Require().Equal(sdk.NewInt(123), ibcCroCoin.Amount)
// Verify balance EVM coin pre operation
evmCoin := suite.GetBalance(address, suite.evmParam.EvmDenom)
suite.Require().Equal(sdk.NewInt(0), evmCoin.Amount)
},
func() {
// 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)
},
},
}

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.BankKeeper,
keepertest.IbcKeeperMock{},
suite.app.GravityKeeper,
suite.app.EvmKeeper,
suite.app.AccountKeeper,
)
suite.app.CronosKeeper = cronosKeeper

tc.malleate()
suite.app.CronosKeeper.OnRecvVouchers(suite.ctx, tc.coins, address.String())
tc.postCheck()
})
}
}
Loading