Skip to content

Commit 47cf38f

Browse files
committed
chore: Use prefix-based routing for IBCv2 contracts
1 parent 1d69301 commit 47cf38f

File tree

5 files changed

+13
-29
lines changed

5 files changed

+13
-29
lines changed

app/app.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,6 @@ func NewWasmApp(
582582
panic(fmt.Sprintf("error while reading wasm config: %s", err))
583583
}
584584

585-
ibcRouterV2 := ibcapi.NewRouter()
586-
587585
// The last arguments can contain custom message handlers, and custom query handlers,
588586
// if we want to allow any custom callbacks
589587
app.WasmKeeper = wasmkeeper.NewKeeper(
@@ -603,7 +601,6 @@ func NewWasmApp(
603601
wasmtypes.VMConfig{},
604602
wasmkeeper.BuiltInCapabilities(),
605603
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
606-
ibcRouterV2,
607604
wasmOpts...,
608605
)
609606

@@ -645,8 +642,11 @@ func NewWasmApp(
645642
AddRoute(icahosttypes.SubModuleName, icaHostStack)
646643
app.IBCKeeper.SetRouter(ibcRouter)
647644

645+
ibcRouterV2 := ibcapi.NewRouter()
648646
ibcRouterV2 = ibcRouterV2.
649-
AddRoute(ibctransfertypes.PortID, transferv2.NewIBCModule(app.TransferKeeper))
647+
AddRoute(ibctransfertypes.PortID, transferv2.NewIBCModule(app.TransferKeeper)).
648+
AddRoute(wasmkeeper.PortIDPrefixV2, wasmkeeper.NewIBC2Handler(app.WasmKeeper))
649+
650650
app.IBCKeeper.SetRouterV2(ibcRouterV2)
651651

652652
clientKeeper := app.IBCKeeper.ClientKeeper

x/wasm/keeper/ibc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ func ContractFromPortID(portID string) (sdk.AccAddress, error) {
2525
}
2626

2727
// The port prefix refers to "CosmWasm over IBC v2" and ensures packets are routed to the right entry points
28-
const portIDPrefixV2 = "wasm2"
28+
const PortIDPrefixV2 = "wasm2"
2929

3030
func PortIDForContractV2(addr sdk.AccAddress) string {
31-
return portIDPrefixV2 + addr.String()
31+
return PortIDPrefixV2 + addr.String()
3232
}
3333

3434
func ContractFromPortID2(portID string) (sdk.AccAddress, error) {
35-
if !strings.HasPrefix(portID, portIDPrefixV2) {
35+
if !strings.HasPrefix(portID, PortIDPrefixV2) {
3636
return nil, errorsmod.Wrapf(types.ErrInvalid, "without prefix")
3737
}
38-
return sdk.AccAddressFromBech32(portID[len(portIDPrefixV2):])
38+
return sdk.AccAddressFromBech32(portID[len(PortIDPrefixV2):])
3939
}

x/wasm/keeper/keeper.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
wasmvm "github.com/CosmWasm/wasmvm/v3"
1616
wasmvmtypes "github.com/CosmWasm/wasmvm/v3/types"
1717
channeltypes "github.com/cosmos/ibc-go/v10/modules/core/04-channel/types"
18-
ibcapi "github.com/cosmos/ibc-go/v10/modules/core/api"
1918

2019
"cosmossdk.io/collections"
2120
corestoretypes "cosmossdk.io/core/store"
@@ -111,12 +110,6 @@ type Keeper struct {
111110

112111
// wasmLimits contains the limits sent to wasmvm on init
113112
wasmLimits wasmvmtypes.WasmLimits
114-
115-
ibcRouterV2 *ibcapi.Router
116-
}
117-
118-
func (k Keeper) GetIBCRouterV2() *ibcapi.Router {
119-
return k.ibcRouterV2
120113
}
121114

122115
func (k Keeper) getUploadAccessConfig(ctx context.Context) types.AccessConfig {
@@ -379,13 +372,7 @@ func (k Keeper) instantiate(
379372
contractInfo.IBCPortID = ibcPort
380373
}
381374

382-
ibc2Port := PortIDForContractV2(contractAddress)
383-
contractInfo.IBC2PortID = ibc2Port
384-
385-
// TODO: Remove AddRoute in https://github.com/CosmWasm/wasmd/issues/2144
386-
if !k.ibcRouterV2.HasRoute(ibc2Port) {
387-
k.ibcRouterV2.AddRoute(ibc2Port, NewIBC2Handler(k))
388-
}
375+
contractInfo.IBC2PortID = PortIDForContractV2(contractAddress)
389376

390377
// store contract before dispatch so that contract could be called back
391378
historyEntry := contractInfo.InitialHistory(initMsg)
@@ -554,6 +541,8 @@ func (k Keeper) migrate(
554541
}
555542
k.mustStoreContractInfo(ctx, contractAddress, contractInfo)
556543

544+
contractInfo.IBC2PortID = PortIDForContractV2(contractAddress)
545+
557546
sdkCtx.EventManager().EmitEvent(sdk.NewEvent(
558547
types.EventTypeMigrate,
559548
sdk.NewAttribute(types.AttributeKeyCodeID, strconv.FormatUint(newCodeID, 10)),

x/wasm/keeper/keeper_cgo.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
wasmvm "github.com/CosmWasm/wasmvm/v3"
99
wasmvmtypes "github.com/CosmWasm/wasmvm/v3/types"
10-
ibcapi "github.com/cosmos/ibc-go/v10/modules/core/api"
1110

1211
"cosmossdk.io/collections"
1312
corestoretypes "cosmossdk.io/core/store"
@@ -36,7 +35,6 @@ func NewKeeper(
3635
vmConfig types.VMConfig,
3736
availableCapabilities []string,
3837
authority string,
39-
ibcRouterV2 *ibcapi.Router,
4038
opts ...Option,
4139
) Keeper {
4240
sb := collections.NewSchemaBuilder(storeService)
@@ -56,9 +54,8 @@ func NewKeeper(
5654
propagateGovAuthorization: map[types.AuthorizationPolicyAction]struct{}{
5755
types.AuthZActionInstantiate: {},
5856
},
59-
authority: authority,
60-
wasmLimits: vmConfig.WasmLimits,
61-
ibcRouterV2: ibcRouterV2,
57+
authority: authority,
58+
wasmLimits: vmConfig.WasmLimits,
6259
}
6360
keeper.messenger = NewDefaultMessageHandler(keeper, router, ics4Wrapper, bankKeeper, cdc, portSource)
6461
keeper.wasmVMQueryHandler = DefaultQueryPlugins(bankKeeper, stakingKeeper, distrKeeper, channelKeeper, keeper)

x/wasm/keeper/test_common.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/cosmos/ibc-go/v10/modules/apps/transfer"
1818
ibctransfertypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types"
1919
ibc "github.com/cosmos/ibc-go/v10/modules/core"
20-
ibcapi "github.com/cosmos/ibc-go/v10/modules/core/api"
2120
ibcexported "github.com/cosmos/ibc-go/v10/modules/core/exported"
2221
ibckeeper "github.com/cosmos/ibc-go/v10/modules/core/keeper"
2322
"github.com/stretchr/testify/require"
@@ -388,7 +387,6 @@ func createTestInput(
388387
vmConfig,
389388
availableCapabilities,
390389
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
391-
ibcapi.NewRouter(),
392390
opts...,
393391
)
394392
require.NoError(t, keeper.SetParams(ctx, types.DefaultParams()))

0 commit comments

Comments
 (0)