Skip to content

Commit 13a27a3

Browse files
committed
chore: Use prefix-based routing for IBCv2 contracts
1 parent 7f3d719 commit 13a27a3

File tree

5 files changed

+13
-57
lines changed

5 files changed

+13
-57
lines changed

app/app.go

Lines changed: 4 additions & 8 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(
@@ -604,7 +602,6 @@ func NewWasmApp(
604602
wasmtypes.VMConfig{},
605603
wasmkeeper.BuiltInCapabilities(),
606604
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
607-
ibcRouterV2,
608605
wasmOpts...,
609606
)
610607

@@ -646,8 +643,11 @@ func NewWasmApp(
646643
AddRoute(icahosttypes.SubModuleName, icaHostStack)
647644
app.IBCKeeper.SetRouter(ibcRouter)
648645

646+
ibcRouterV2 := ibcapi.NewRouter()
649647
ibcRouterV2 = ibcRouterV2.
650-
AddRoute(ibctransfertypes.PortID, transferv2.NewIBCModule(app.TransferKeeper))
648+
AddRoute(ibctransfertypes.PortID, transferv2.NewIBCModule(app.TransferKeeper)).
649+
AddRoute(wasmkeeper.PortIDPrefixV2, wasmkeeper.NewIBC2Handler(app.WasmKeeper))
650+
651651
app.IBCKeeper.SetRouterV2(ibcRouterV2)
652652

653653
clientKeeper := app.IBCKeeper.ClientKeeper
@@ -875,10 +875,6 @@ func NewWasmApp(
875875
if err := app.WasmKeeper.InitializePinnedCodes(ctx); err != nil {
876876
panic(fmt.Sprintf("failed initialize pinned codes %s", err))
877877
}
878-
879-
// TODO: Remove the registration of each contract in https://github.com/CosmWasm/wasmd/issues/2278
880-
// and add IBCv2 port ID prefix before calling `SetRouterV2` during WasmKeeper creation.
881-
app.WasmKeeper.RegisterContractsInIbc2Router(ctx)
882878
}
883879

884880
return app

x/wasm/keeper/ibc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ 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 {
3131
blockchainPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
32-
return portIDPrefixV2 + strings.TrimPrefix(addr.String(), blockchainPrefix)
32+
return PortIDPrefixV2 + strings.TrimPrefix(addr.String(), blockchainPrefix)
3333
}
3434

3535
func ContractFromPortID2(portID string) (sdk.AccAddress, error) {
36-
if !strings.HasPrefix(portID, portIDPrefixV2) {
36+
if !strings.HasPrefix(portID, PortIDPrefixV2) {
3737
return nil, errorsmod.Wrapf(types.ErrInvalid, "without prefix")
3838
}
3939
blockchainPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
40-
return sdk.AccAddressFromBech32(blockchainPrefix + portID[len(portIDPrefixV2):])
40+
return sdk.AccAddressFromBech32(blockchainPrefix + portID[len(PortIDPrefixV2):])
4141
}

x/wasm/keeper/keeper.go

Lines changed: 3 additions & 38 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 the registration of each contract in https://github.com/CosmWasm/wasmd/issues/2278
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)),
@@ -1092,12 +1081,6 @@ func (k Keeper) importContractState(ctx context.Context, contractAddress sdk.Acc
10921081
prefixStore.Set(model.Key, model.Value)
10931082
}
10941083

1095-
// TODO: Remove the registration of each contract in https://github.com/CosmWasm/wasmd/issues/2278
1096-
contractPortIBC2ID := PortIDForContractV2(contractAddress)
1097-
if !k.ibcRouterV2.HasRoute(contractPortIBC2ID) {
1098-
k.ibcRouterV2.AddRoute(contractPortIBC2ID, NewIBC2Handler(k))
1099-
}
1100-
11011084
return nil
11021085
}
11031086

@@ -1139,24 +1122,6 @@ func (k Keeper) IterateCodeInfos(ctx context.Context, cb func(uint64, types.Code
11391122
}
11401123
}
11411124

1142-
// TODO: Remove the registration of each contract in https://github.com/CosmWasm/wasmd/issues/2278 (remove this method)
1143-
func (k Keeper) IterateContractAddresses(ctx context.Context, cb func(sdk.AccAddress)) {
1144-
prefixStore := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.ContractKeyPrefix)
1145-
iter := prefixStore.Iterator(nil, nil)
1146-
defer iter.Close()
1147-
1148-
for ; iter.Valid(); iter.Next() {
1149-
cb(iter.Key())
1150-
}
1151-
}
1152-
1153-
// TODO: Remove the registration of each contract in https://github.com/CosmWasm/wasmd/issues/2278 (remove this method)
1154-
func (k Keeper) RegisterContractsInIbc2Router(ctx context.Context) {
1155-
k.IterateContractAddresses(ctx, func(contractAddr sdk.AccAddress) {
1156-
k.ibcRouterV2.AddRoute(PortIDForContractV2(contractAddr), NewIBC2Handler(k))
1157-
})
1158-
}
1159-
11601125
func (k Keeper) GetByteCode(ctx context.Context, codeID uint64) ([]byte, error) {
11611126
store := k.storeService.OpenKVStore(ctx)
11621127
var codeInfo types.CodeInfo

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"
@@ -37,7 +36,6 @@ func NewKeeper(
3736
vmConfig types.VMConfig,
3837
availableCapabilities []string,
3938
authority string,
40-
ibcRouterV2 *ibcapi.Router,
4139
opts ...Option,
4240
) Keeper {
4341
sb := collections.NewSchemaBuilder(storeService)
@@ -57,9 +55,8 @@ func NewKeeper(
5755
propagateGovAuthorization: map[types.AuthorizationPolicyAction]struct{}{
5856
types.AuthZActionInstantiate: {},
5957
},
60-
authority: authority,
61-
wasmLimits: vmConfig.WasmLimits,
62-
ibcRouterV2: ibcRouterV2,
58+
authority: authority,
59+
wasmLimits: vmConfig.WasmLimits,
6360
}
6461
keeper.messenger = NewDefaultMessageHandler(keeper, router, ics4Wrapper, channelKeeperV2, bankKeeper, cdc, portSource)
6562
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"
@@ -389,7 +388,6 @@ func createTestInput(
389388
vmConfig,
390389
availableCapabilities,
391390
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
392-
ibcapi.NewRouter(),
393391
opts...,
394392
)
395393
require.NoError(t, keeper.SetParams(ctx, types.DefaultParams()))

0 commit comments

Comments
 (0)