Skip to content

Commit 5a9b50c

Browse files
feat: Remove HRP from port ID to comply with the IBC port ID format (#2266)
* feat: Remove HRP from port ID to comply with the IBC port ID format * Update x/wasm/keeper/ibc_test.go Co-authored-by: Christoph Otter <chris@confio.gmbh> --------- Co-authored-by: Christoph Otter <chris@confio.gmbh>
1 parent 276870d commit 5a9b50c

File tree

6 files changed

+64
-15
lines changed

6 files changed

+64
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- Remove IBC fee handling, as it is no longer supported by the IBC protocol [\#2254](https://github.com/CosmWasm/wasmd/pull/2254)
1515
- Add support for IBCv2 Send entry point [\#2252](https://github.com/CosmWasm/wasmd/pull/2252)
1616
- Introduce IBCv2 endpoint for receiving message acknowledgements [\#2261](https://github.com/CosmWasm/wasmd/pull/2261)
17+
- IBCv2 WriteAcknowledgement handler for async ACK [\#2173](https://github.com/CosmWasm/wasmd/pull/2173)
18+
- Remove HRP from port ID to comply with the IBC port ID format [\#2266](https://github.com/CosmWasm/wasmd/pull/2266)
1719

1820
## [v0.55.0](https://github.com/CosmWasm/wasmd/tree/v0.55.0) (2025-03-11)
1921

x/wasm/keeper/ibc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ func ContractFromPortID(portID string) (sdk.AccAddress, error) {
2828
const portIDPrefixV2 = "wasm2"
2929

3030
func PortIDForContractV2(addr sdk.AccAddress) string {
31-
return portIDPrefixV2 + addr.String()
31+
blockchainPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
32+
return portIDPrefixV2 + strings.TrimPrefix(addr.String(), blockchainPrefix)
3233
}
3334

3435
func ContractFromPortID2(portID string) (sdk.AccAddress, error) {
3536
if !strings.HasPrefix(portID, portIDPrefixV2) {
3637
return nil, errorsmod.Wrapf(types.ErrInvalid, "without prefix")
3738
}
38-
return sdk.AccAddressFromBech32(portID[len(portIDPrefixV2):])
39+
blockchainPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
40+
return sdk.AccAddressFromBech32(blockchainPrefix + portID[len(portIDPrefixV2):])
3941
}

x/wasm/keeper/ibc_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keeper
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -50,3 +51,47 @@ func TestContractFromPortID(t *testing.T) {
5051
})
5152
}
5253
}
54+
55+
func TestContractFromPortID2(t *testing.T) {
56+
contractAddr := BuildContractAddressClassic(1, 100)
57+
trimmed := strings.TrimPrefix(contractAddr.String(), sdk.GetConfig().GetBech32AccountAddrPrefix())
58+
specs := map[string]struct {
59+
srcPort string
60+
expAddr sdk.AccAddress
61+
expErr bool
62+
}{
63+
"all good": {
64+
srcPort: fmt.Sprintf("wasm2%s", trimmed),
65+
expAddr: contractAddr,
66+
},
67+
"hrp present in bech32": {
68+
srcPort: fmt.Sprintf("wasm2%s", contractAddr.String()),
69+
expErr: true,
70+
},
71+
"without prefix": {
72+
srcPort: trimmed,
73+
expErr: true,
74+
},
75+
"invalid prefix": {
76+
srcPort: fmt.Sprintf("wasmx%s", contractAddr.String()),
77+
expErr: true,
78+
},
79+
"invalid account": {
80+
srcPort: "wasm2foobar",
81+
expErr: true,
82+
},
83+
}
84+
for name, spec := range specs {
85+
t.Run(name, func(t *testing.T) {
86+
gotAddr, gotErr := ContractFromPortID2(spec.srcPort)
87+
if spec.expErr {
88+
require.Error(t, gotErr)
89+
return
90+
}
91+
require.NoError(t, gotErr)
92+
assert.Equal(t, spec.expAddr, gotAddr)
93+
gotPort := PortIDForContractV2(gotAddr)
94+
assert.Equal(t, spec.srcPort, gotPort)
95+
})
96+
}
97+
}

x/wasm/keeper/keeper_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func TestInstantiate(t *testing.T) {
423423

424424
gasAfter := ctx.GasMeter().GasConsumed()
425425
if types.EnableGasVerification {
426-
require.Equal(t, uint64(0x1c527), gasAfter-gasBefore)
426+
require.Equal(t, uint64(0x1c473), gasAfter-gasBefore)
427427
}
428428

429429
// ensure it is stored properly
@@ -891,7 +891,7 @@ func TestInstantiateWithContractFactoryChildQueriesParent(t *testing.T) {
891891

892892
// when
893893
contractAddress, data, err := keepers.ContractKeeper.Instantiate(ctx, example.CodeID, example.CreatorAddr, nil, nil, "test", nil)
894-
ibc2PortID := "wasm2" + contractAddress.String()
894+
ibc2PortID := PortIDForContractV2(contractAddress)
895895

896896
// then
897897
require.NoError(t, err)
@@ -961,7 +961,7 @@ func TestExecute(t *testing.T) {
961961
// make sure gas is properly deducted from ctx
962962
gasAfter := ctx.GasMeter().GasConsumed()
963963
if types.EnableGasVerification {
964-
require.Equal(t, uint64(0x1adc3), gasAfter-gasBefore)
964+
require.Equal(t, uint64(0x1adb1), gasAfter-gasBefore)
965965
}
966966
// ensure bob now exists and got both payments released
967967
bobAcct = accKeeper.GetAccount(ctx, bob)

x/wasm/keeper/recurse_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func initRecurseContract(t *testing.T) (contract sdk.AccAddress, ctx sdk.Context
5656

5757
func TestGasCostOnQuery(t *testing.T) {
5858
const (
59-
GasNoWork uint64 = 64_209
60-
GasWork50 uint64 = 64_456
59+
GasNoWork uint64 = 64_191
60+
GasWork50 uint64 = 64_438
6161
// should be discounted exactly by the difference between normal instance cost and discounted instance cost
6262
GasNoWorkDiscounted uint64 = GasNoWork - (types.DefaultInstanceCost - types.DefaultInstanceCostDiscount)
6363
GasWork50Discounted uint64 = GasWork50 - (types.DefaultInstanceCost - types.DefaultInstanceCostDiscount)
@@ -214,7 +214,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
214214

215215
const (
216216
// Note: about 100 SDK gas (10k CosmWasm gas) for each round of sha256
217-
GasWork2k uint64 = 77_038 // = SetupContractCost + x // we have 6x gas used in cpu than in the instance
217+
GasWork2k uint64 = 77_020 // = SetupContractCost + x // we have 6x gas used in cpu than in the instance
218218

219219
// should be discounted exactly by the difference between normal instance cost and discounted instance cost
220220
GasWork2kDiscounted uint64 = GasWork2k - (types.DefaultInstanceCost - types.DefaultInstanceCostDiscount)

x/wasm/keeper/relay_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestOnOpenChannel(t *testing.T) {
8383
}
8484
require.NoError(t, err)
8585
// verify gas consumed
86-
const storageCosts = storetypes.Gas(3119)
86+
const storageCosts = storetypes.Gas(3101)
8787
assert.Equal(t, spec.expGas, ctx.GasMeter().GasConsumed()-before-storageCosts)
8888
})
8989
}
@@ -189,7 +189,7 @@ func TestOnConnectChannel(t *testing.T) {
189189
}
190190
require.NoError(t, err)
191191
// verify gas consumed
192-
const storageCosts = storetypes.Gas(3119)
192+
const storageCosts = storetypes.Gas(3101)
193193
assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts)
194194
// verify msgs dispatched
195195
require.Len(t, *capturedMsgs, len(spec.contractResp.Messages))
@@ -299,7 +299,7 @@ func TestOnCloseChannel(t *testing.T) {
299299
}
300300
require.NoError(t, err)
301301
// verify gas consumed
302-
const storageCosts = storetypes.Gas(3119)
302+
const storageCosts = storetypes.Gas(3101)
303303
assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts)
304304
// verify msgs dispatched
305305
require.Len(t, *capturedMsgs, len(spec.contractResp.Messages))
@@ -318,7 +318,7 @@ func TestOnRecvPacket(t *testing.T) {
318318
parentCtx, keepers := CreateTestInput(t, false, AvailableCapabilities, WithMessageHandler(messenger))
319319
example := SeedNewContractInstance(t, parentCtx, keepers, &m)
320320
const myContractGas = 40
321-
const storageCosts = storetypes.Gas(3119)
321+
const storageCosts = storetypes.Gas(3101)
322322

323323
specs := map[string]struct {
324324
contractAddr sdk.AccAddress
@@ -495,7 +495,7 @@ func TestOnRecvPacket(t *testing.T) {
495495
}
496496

497497
// verify gas consumed
498-
const storageCosts = storetypes.Gas(3119)
498+
const storageCosts = storetypes.Gas(3101)
499499
assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts)
500500

501501
// verify msgs dispatched on success/ err response
@@ -606,7 +606,7 @@ func TestOnAckPacket(t *testing.T) {
606606
}
607607
require.NoError(t, err)
608608
// verify gas consumed
609-
const storageCosts = storetypes.Gas(3119)
609+
const storageCosts = storetypes.Gas(3101)
610610
assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts)
611611
// verify msgs dispatched
612612
require.Len(t, *capturedMsgs, len(spec.contractResp.Messages))
@@ -726,7 +726,7 @@ func TestOnTimeoutPacket(t *testing.T) {
726726
}
727727
require.NoError(t, err)
728728
// verify gas consumed
729-
const storageCosts = storetypes.Gas(3119)
729+
const storageCosts = storetypes.Gas(3101)
730730
assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts)
731731
// verify msgs dispatched
732732
require.Len(t, *capturedMsgs, len(spec.contractResp.Messages))

0 commit comments

Comments
 (0)