Skip to content

Commit 7f3d719

Browse files
committed
feat: Register all contracts in IBCv2 port router
1 parent 5a9b50c commit 7f3d719

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

app/app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,10 @@ 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)
878882
}
879883

880884
return app

x/wasm/keeper/genesis_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
abci "github.com/cometbft/cometbft/abci/types"
1414
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
1515
dbm "github.com/cosmos/cosmos-db"
16+
ibcapi "github.com/cosmos/ibc-go/v10/modules/core/api"
1617
fuzz "github.com/google/gofuzz"
1718
"github.com/stretchr/testify/assert"
1819
"github.com/stretchr/testify/require"
@@ -698,7 +699,7 @@ func setupKeeper(t *testing.T) (*Keeper, sdk.Context) {
698699
types.VMConfig{},
699700
AvailableCapabilities,
700701
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
701-
nil,
702+
ibcapi.NewRouter(),
702703
)
703704
return &srcKeeper, ctx
704705
}

x/wasm/keeper/keeper.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func (k Keeper) instantiate(
382382
ibc2Port := PortIDForContractV2(contractAddress)
383383
contractInfo.IBC2PortID = ibc2Port
384384

385-
// TODO: Remove AddRoute in https://github.com/CosmWasm/wasmd/issues/2144
385+
// TODO: Remove the registration of each contract in https://github.com/CosmWasm/wasmd/issues/2278
386386
if !k.ibcRouterV2.HasRoute(ibc2Port) {
387387
k.ibcRouterV2.AddRoute(ibc2Port, NewIBC2Handler(k))
388388
}
@@ -1091,6 +1091,13 @@ func (k Keeper) importContractState(ctx context.Context, contractAddress sdk.Acc
10911091
}
10921092
prefixStore.Set(model.Key, model.Value)
10931093
}
1094+
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+
10941101
return nil
10951102
}
10961103

@@ -1132,6 +1139,24 @@ func (k Keeper) IterateCodeInfos(ctx context.Context, cb func(uint64, types.Code
11321139
}
11331140
}
11341141

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+
11351160
func (k Keeper) GetByteCode(ctx context.Context, codeID uint64) ([]byte, error) {
11361161
store := k.storeService.OpenKVStore(ctx)
11371162
var codeInfo types.CodeInfo

0 commit comments

Comments
 (0)