Skip to content

Commit 88ae8da

Browse files
committed
Add context and hex port-id encoder
1 parent 2227803 commit 88ae8da

File tree

9 files changed

+51
-26
lines changed

9 files changed

+51
-26
lines changed

x/wasm/ibc.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (i IBCHandler) OnChanOpenInit(
5050
if err := ValidateChannelParams(channelID); err != nil {
5151
return "", err
5252
}
53-
contractAddr, err := i.keeper.ContractFromPortID(portID)
53+
contractAddr, err := i.keeper.ContractFromPortID(ctx, portID)
5454
if err != nil {
5555
return "", errorsmod.Wrapf(err, "contract port id")
5656
}
@@ -102,7 +102,7 @@ func (i IBCHandler) OnChanOpenTry(
102102
return "", err
103103
}
104104

105-
contractAddr, err := i.keeper.ContractFromPortID(portID)
105+
contractAddr, err := i.keeper.ContractFromPortID(ctx, portID)
106106
if err != nil {
107107
return "", errorsmod.Wrapf(err, "contract port id")
108108
}
@@ -150,7 +150,7 @@ func (i IBCHandler) OnChanOpenAck(
150150
counterpartyChannelID string,
151151
counterpartyVersion string,
152152
) error {
153-
contractAddr, err := i.keeper.ContractFromPortID(portID)
153+
contractAddr, err := i.keeper.ContractFromPortID(ctx, portID)
154154
if err != nil {
155155
return errorsmod.Wrapf(err, "contract port id")
156156
}
@@ -176,7 +176,7 @@ func (i IBCHandler) OnChanOpenAck(
176176

177177
// OnChanOpenConfirm implements the IBCModule interface
178178
func (i IBCHandler) OnChanOpenConfirm(ctx sdk.Context, portID, channelID string) error {
179-
contractAddr, err := i.keeper.ContractFromPortID(portID)
179+
contractAddr, err := i.keeper.ContractFromPortID(ctx, portID)
180180
if err != nil {
181181
return errorsmod.Wrapf(err, "contract port id")
182182
}
@@ -198,7 +198,7 @@ func (i IBCHandler) OnChanOpenConfirm(ctx sdk.Context, portID, channelID string)
198198

199199
// OnChanCloseInit implements the IBCModule interface
200200
func (i IBCHandler) OnChanCloseInit(ctx sdk.Context, portID, channelID string) error {
201-
contractAddr, err := i.keeper.ContractFromPortID(portID)
201+
contractAddr, err := i.keeper.ContractFromPortID(ctx, portID)
202202
if err != nil {
203203
return errorsmod.Wrapf(err, "contract port id")
204204
}
@@ -226,7 +226,7 @@ func (i IBCHandler) OnChanCloseInit(ctx sdk.Context, portID, channelID string) e
226226
// OnChanCloseConfirm implements the IBCModule interface
227227
func (i IBCHandler) OnChanCloseConfirm(ctx sdk.Context, portID, channelID string) error {
228228
// counterparty has closed the channel
229-
contractAddr, err := i.keeper.ContractFromPortID(portID)
229+
contractAddr, err := i.keeper.ContractFromPortID(ctx, portID)
230230
if err != nil {
231231
return errorsmod.Wrapf(err, "contract port id")
232232
}
@@ -267,7 +267,7 @@ func (i IBCHandler) OnRecvPacket(
267267
packet channeltypes.Packet,
268268
relayer sdk.AccAddress,
269269
) ibcexported.Acknowledgement {
270-
contractAddr, err := i.keeper.ContractFromPortID(packet.DestinationPort)
270+
contractAddr, err := i.keeper.ContractFromPortID(ctx, packet.DestinationPort)
271271
if err != nil {
272272
// this must not happen as ports were registered before
273273
panic(errorsmod.Wrapf(err, "contract port id"))
@@ -295,7 +295,7 @@ func (i IBCHandler) OnAcknowledgementPacket(
295295
acknowledgement []byte,
296296
relayer sdk.AccAddress,
297297
) error {
298-
contractAddr, err := i.keeper.ContractFromPortID(packet.SourcePort)
298+
contractAddr, err := i.keeper.ContractFromPortID(ctx, packet.SourcePort)
299299
if err != nil {
300300
return errorsmod.Wrapf(err, "contract port id")
301301
}
@@ -313,7 +313,7 @@ func (i IBCHandler) OnAcknowledgementPacket(
313313

314314
// OnTimeoutPacket implements the IBCModule interface
315315
func (i IBCHandler) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) error {
316-
contractAddr, err := i.keeper.ContractFromPortID(packet.SourcePort)
316+
contractAddr, err := i.keeper.ContractFromPortID(ctx, packet.SourcePort)
317317
if err != nil {
318318
return errorsmod.Wrapf(err, "contract port id")
319319
}

x/wasm/ibc_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package wasm
22

33
import (
4+
"context"
45
"testing"
56

67
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
@@ -202,14 +203,14 @@ var _ types.IBCContractKeeper = &IBCContractKeeperMock{}
202203
type IBCContractKeeperMock struct {
203204
types.IBCContractKeeper
204205
OnRecvPacketFn func(ctx sdk.Context, contractAddr sdk.AccAddress, msg wasmvmtypes.IBCPacketReceiveMsg) (ibcexported.Acknowledgement, error)
205-
ContractFromPortIDFn func(portID string) (sdk.AccAddress, error)
206+
ContractFromPortIDFn func(ctx context.Context, portID string) (sdk.AccAddress, error)
206207
}
207208

208-
func (m IBCContractKeeperMock) ContractFromPortID(portID string) (sdk.AccAddress, error) {
209+
func (m IBCContractKeeperMock) ContractFromPortID(ctx context.Context, portID string) (sdk.AccAddress, error) {
209210
if m.ContractFromPortIDFn == nil {
210211
panic("not expected to be called")
211212
}
212-
return m.ContractFromPortIDFn(portID)
213+
return m.ContractFromPortIDFn(ctx, portID)
213214
}
214215

215216
func (m IBCContractKeeperMock) OnRecvPacket(ctx sdk.Context, contractAddr sdk.AccAddress, msg wasmvmtypes.IBCPacketReceiveMsg) (ibcexported.Acknowledgement, error) {

x/wasm/keeper/handler_plugin_encoders.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func EncodeIBCMsg(
308308
switch {
309309
case msg.CloseChannel != nil:
310310
return []sdk.Msg{&channeltypes.MsgChannelCloseInit{
311-
PortId: ibcPortAllocator.PortIDForContract(sender),
311+
PortId: ibcPortAllocator.PortIDForContract(ctx, sender),
312312
ChannelId: msg.CloseChannel.ChannelID,
313313
Signer: sender.String(),
314314
}}, nil

x/wasm/keeper/handler_plugin_encoders_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
77
"github.com/cosmos/gogoproto/proto"
88
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
9-
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint:staticcheck
9+
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
1010
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"

x/wasm/keeper/ibc.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package keeper
22

33
import (
4+
"context"
5+
"encoding/hex"
46
"strings"
57

68
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
@@ -26,33 +28,54 @@ func (k Keeper) bindIbcPort(ctx sdk.Context, portID string) error {
2628
// Returns success if we already registered or just registered and error if we cannot
2729
// (lack of permissions or someone else has it)
2830
func (k Keeper) ensureIBCPort(ctx sdk.Context, contractAddr sdk.AccAddress) (string, error) {
29-
portID := k.ibcPortNameGenerator.PortIDForContract(contractAddr)
31+
portID := k.ibcPortNameGenerator.PortIDForContract(ctx, contractAddr)
3032
if _, ok := k.capabilityKeeper.GetCapability(ctx, host.PortPath(portID)); ok {
3133
return portID, nil
3234
}
3335
return portID, k.bindIbcPort(ctx, portID)
3436
}
3537

3638
type IBCPortNameGenerator interface {
37-
PortIDForContract(addr sdk.AccAddress) string
38-
ContractFromPortID(portID string) (sdk.AccAddress, error)
39+
// PortIDForContract converts an address into an ibc port-id.
40+
PortIDForContract(ctx context.Context, addr sdk.AccAddress) string
41+
// ContractFromPortID returns the contract address for given port-id. The method does not check if the contract exists
42+
ContractFromPortID(ctx context.Context, portID string) (sdk.AccAddress, error)
3943
}
4044

4145
const portIDPrefix = "wasm."
4246

47+
// DefaultIBCPortNameGenerator uses Bech32 address string in port-id
4348
type DefaultIBCPortNameGenerator struct{}
4449

45-
func (DefaultIBCPortNameGenerator) PortIDForContract(addr sdk.AccAddress) string {
50+
// PortIDForContract coverts contract into port-id in the format "wasm.<bech32-address>"
51+
func (DefaultIBCPortNameGenerator) PortIDForContract(ctx context.Context, addr sdk.AccAddress) string {
4652
return portIDPrefix + addr.String()
4753
}
4854

49-
func (DefaultIBCPortNameGenerator) ContractFromPortID(portID string) (sdk.AccAddress, error) {
55+
// ContractFromPortID reads the contract address from bech32 address in the port-id.
56+
func (DefaultIBCPortNameGenerator) ContractFromPortID(ctx context.Context, portID string) (sdk.AccAddress, error) {
5057
if !strings.HasPrefix(portID, portIDPrefix) {
5158
return nil, errorsmod.Wrapf(types.ErrInvalid, "without prefix")
5259
}
5360
return sdk.AccAddressFromBech32(portID[len(portIDPrefix):])
5461
}
5562

63+
// HexIBCPortNameGenerator uses Hex address string
64+
type HexIBCPortNameGenerator struct{}
65+
66+
// PortIDForContract coverts contract into port-id in the format "wasm.<hex-address>"
67+
func (HexIBCPortNameGenerator) PortIDForContract(ctx context.Context, addr sdk.AccAddress) string {
68+
return portIDPrefix + hex.EncodeToString(addr)
69+
}
70+
71+
// ContractFromPortID reads the contract address from hex address in the port-id.
72+
func (HexIBCPortNameGenerator) ContractFromPortID(ctx context.Context, portID string) (sdk.AccAddress, error) {
73+
if !strings.HasPrefix(portID, portIDPrefix) {
74+
return nil, errorsmod.Wrapf(types.ErrInvalid, "without prefix")
75+
}
76+
return sdk.AccAddressFromHexUnsafe(portID[len(portIDPrefix):])
77+
}
78+
5679
// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
5780
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
5881
return k.capabilityKeeper.AuthenticateCapability(ctx, cap, name)

x/wasm/keeper/ibc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestBindingPortForIBCContractOnInstantiate(t *testing.T) {
3636
require.NoError(t, err)
3737
require.NotEqual(t, example.Contract, addr)
3838

39-
portID2 := keepers.WasmKeeper.ibcPortNameGenerator.PortIDForContract(addr)
39+
portID2 := keepers.WasmKeeper.ibcPortNameGenerator.PortIDForContract(ctx, addr)
4040
owner, _, err = keepers.IBCKeeper.PortKeeper.LookupModuleByPort(ctx, portID2)
4141
require.NoError(t, err)
4242
require.Equal(t, "wasm", owner)
@@ -72,7 +72,7 @@ func TestContractFromPortID(t *testing.T) {
7272
}
7373
for name, spec := range specs {
7474
t.Run(name, func(t *testing.T) {
75-
gotAddr, gotErr := DefaultIBCPortNameGenerator{}.ContractFromPortID(spec.srcPort)
75+
gotAddr, gotErr := DefaultIBCPortNameGenerator{}.ContractFromPortID(nil, spec.srcPort)
7676
if spec.expErr {
7777
require.Error(t, gotErr)
7878
return

x/wasm/keeper/keeper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,8 +1195,9 @@ func (k Keeper) importContract(ctx context.Context, contractAddr sdk.AccAddress,
11951195
return k.importContractState(ctx, contractAddr, state)
11961196
}
11971197

1198-
func (k Keeper) ContractFromPortID(portID string) (sdk.AccAddress, error) {
1199-
return k.ibcPortNameGenerator.ContractFromPortID(portID)
1198+
// ContractFromPortID returns the contract address for given port-id. The method does not check if the contract exists
1199+
func (k Keeper) ContractFromPortID(ctx context.Context, portID string) (sdk.AccAddress, error) {
1200+
return k.ibcPortNameGenerator.ContractFromPortID(ctx, portID)
12001201
}
12011202

12021203
func (k Keeper) newQueryHandler(ctx sdk.Context, contractAddress sdk.AccAddress) QueryHandler {

x/wasm/relay_pingpong_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func TestPinPong(t *testing.T) {
6767
pongContract.contractAddr = pongContractAddr
6868

6969
var (
70-
sourcePortID = wasmkeeper.DefaultIBCPortNameGenerator{}.PortIDForContract(pingContractAddr)
71-
counterpartyPortID = wasmkeeper.DefaultIBCPortNameGenerator{}.PortIDForContract(pongContractAddr)
70+
sourcePortID = wasmkeeper.DefaultIBCPortNameGenerator{}.PortIDForContract(nil, pingContractAddr)
71+
counterpartyPortID = wasmkeeper.DefaultIBCPortNameGenerator{}.PortIDForContract(nil, pongContractAddr)
7272
)
7373

7474
path := wasmibctesting.NewPath(chainA, chainB)

x/wasm/types/exported_keepers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,5 @@ type IBCContractKeeper interface {
122122
AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool
123123

124124
// ContractFromPortID resolves contract address from the ibc port-di
125-
ContractFromPortID(portID string) (sdk.AccAddress, error)
125+
ContractFromPortID(ctx context.Context, portID string) (sdk.AccAddress, error)
126126
}

0 commit comments

Comments
 (0)