Skip to content

Commit

Permalink
fix(ibc): move the genesis bridge transfer blocker from ante to ics4w…
Browse files Browse the repository at this point in the history
…rapper (#1561)
  • Loading branch information
danwt authored Nov 27, 2024
1 parent f3252be commit 6bd4748
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 78 deletions.
3 changes: 0 additions & 3 deletions app/ante/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/dymensionxyz/dymension/v3/x/delayedack"
lightclientante "github.com/dymensionxyz/dymension/v3/x/lightclient/ante"
"github.com/dymensionxyz/dymension/v3/x/rollapp/genesisbridge"
)

func newEthAnteHandler(options HandlerOptions) sdk.AnteHandler {
Expand Down Expand Up @@ -83,7 +82,6 @@ func newLegacyCosmosAnteHandlerEip712(options HandlerOptions) sdk.AnteHandler {
delayedack.NewIBCProofHeightDecorator(),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
ethante.NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper),
genesisbridge.NewTransferEnabledDecorator(options.RollappKeeper, options.IBCKeeper.ChannelKeeper),
)
}

Expand Down Expand Up @@ -125,6 +123,5 @@ func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler {
lightclientante.NewIBCMessagesDecorator(*options.LightClientKeeper, options.IBCKeeper.ClientKeeper, options.IBCKeeper.ChannelKeeper, options.RollappKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
ethante.NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper),
genesisbridge.NewTransferEnabledDecorator(options.RollappKeeper, options.IBCKeeper.ChannelKeeper),
)
}
6 changes: 5 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,11 @@ func (a *AppKeepers) InitKeepers(
appCodec,
a.keys[ibctransfertypes.StoreKey],
a.GetSubspace(ibctransfertypes.ModuleName),
denommetadatamodule.NewICS4Wrapper(a.IBCKeeper.ChannelKeeper, a.RollappKeeper, a.BankKeeper),
genesisbridge.NewICS4Wrapper(
denommetadatamodule.NewICS4Wrapper(a.IBCKeeper.ChannelKeeper, a.RollappKeeper, a.BankKeeper),
a.RollappKeeper,
a.IBCKeeper.ChannelKeeper,
),
a.IBCKeeper.ChannelKeeper,
&a.IBCKeeper.PortKeeper,
a.AccountKeeper,
Expand Down
5 changes: 3 additions & 2 deletions ibctesting/transfers_enabled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *transfersEnabledSuite) TestHubToRollappDisabled() {

shouldFail := true

for range 2 {
for i := range 2 {

apptesting.FundAccount(s.hubApp(), s.hubCtx(), s.hubChain().SenderAccount.GetAddress(), sdk.Coins{msg.Token})

Expand All @@ -76,7 +76,8 @@ func (s *transfersEnabledSuite) TestHubToRollappDisabled() {
[]sdk.Msg{msg},
hubChainID(),
[]uint64{s.hubChain().SenderAccount.GetAccountNumber()},
[]uint64{s.hubChain().SenderAccount.GetSequence()},
// TODO: not sure why, but the simapp doesn't seem to properly update the sequence after a failed tx
[]uint64{s.hubChain().SenderAccount.GetSequence() + uint64(i)},
true,
!shouldFail,
s.hubChain().SenderPrivKey,
Expand Down
72 changes: 0 additions & 72 deletions x/rollapp/genesisbridge/ante_decorator.go

This file was deleted.

79 changes: 79 additions & 0 deletions x/rollapp/genesisbridge/ics4_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package genesisbridge

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
)

type ChannelKeeper interface {
GetChannelClientState(ctx sdk.Context, portID, channelID string) (string, exported.ClientState, error) // implemented by ibc channel keeper
}

type ICS4Wrapper struct {
porttypes.ICS4Wrapper
rollappK RollappKeeperMinimal
getChannelClientState ChannelKeeper
}

func NewICS4Wrapper(
next porttypes.ICS4Wrapper,
rollappKeeper RollappKeeperMinimal,
getChannelClientState ChannelKeeper,
) *ICS4Wrapper {
return &ICS4Wrapper{
ICS4Wrapper: next,
rollappK: rollappKeeper,
getChannelClientState: getChannelClientState,
}
}

func (w *ICS4Wrapper) SendPacket(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
sourcePort string,
sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (sequence uint64, err error) {
if err := w.transferAllowed(ctx, sourcePort, sourceChannel); err != nil {
return 0, errorsmod.Wrap(err, "transfer allowed")
}
return w.ICS4Wrapper.SendPacket(
ctx,
chanCap,
sourcePort,
sourceChannel,
timeoutHeight,
timeoutTimestamp,
data,
)
}

func (w *ICS4Wrapper) transferAllowed(ctx sdk.Context, sourcePort string, sourceChannel string) error {
ra, err := w.rollappK.GetRollappByPortChan(ctx, sourcePort, sourceChannel)
if err != nil {
if errorsmod.IsOf(err, types.ErrRollappNotFound) {
// Two cases
// 1. Non rollapp
// Transfers are enabled
// 2. It is for rollapp but the light client of this transfer is not yet canonical or will never
// be marked canonical: for a correct rollapp the transfer channel is only created after it's
// marked canonical, so this transfer corresponds to a not-relevant channel.
// Note: IBC prevents sending to a channel which is not OPEN, which prevents making the transfer
// for a channel before it is marked canonical in the onOpenAck hook.
return nil
}
return errorsmod.Wrap(err, "rollapp by port chan")
}
if !ra.GenesisState.TransfersEnabled {
return gerrc.ErrFailedPrecondition.Wrap("transfers disabled for rollapp")
}
return nil
}

0 comments on commit 6bd4748

Please sign in to comment.