Skip to content

Commit a2e48b7

Browse files
committed
Use ExpectedJSONSize function
1 parent 547fd66 commit a2e48b7

File tree

1 file changed

+8
-49
lines changed

1 file changed

+8
-49
lines changed

x/wasm/keeper/relay.go

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

33
import (
4-
"encoding/json"
54
"time"
65

76
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
@@ -37,12 +36,7 @@ func (k Keeper) OnOpenChannel(
3736

3837
sdkCtx := sdk.UnwrapSDKContext(ctx)
3938
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
40-
msgBz, err := json.Marshal(msg) // this is not great
41-
if err != nil {
42-
// this should never happen, as we just built this message
43-
return "", errorsmod.Wrap(types.ErrInvalidMsg, err.Error())
44-
}
45-
setupCost := k.gasRegister.SetupContractCost(discount, len(msgBz))
39+
setupCost := k.gasRegister.SetupContractCost(discount, msg.ExpectedJSONSize())
4640
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-open-channel")
4741

4842
env := types.NewEnv(ctx, contractAddr)
@@ -91,12 +85,7 @@ func (k Keeper) OnConnectChannel(
9185

9286
sdkCtx := sdk.UnwrapSDKContext(ctx)
9387
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
94-
msgBz, err := json.Marshal(msg) // this is not great
95-
if err != nil {
96-
// this should never happen, as we just built this message
97-
return errorsmod.Wrap(types.ErrInvalidMsg, err.Error())
98-
}
99-
setupCost := k.gasRegister.SetupContractCost(discount, len(msgBz))
88+
setupCost := k.gasRegister.SetupContractCost(discount, msg.ExpectedJSONSize())
10089
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-connect-channel")
10190

10291
env := types.NewEnv(ctx, contractAddr)
@@ -139,12 +128,7 @@ func (k Keeper) OnCloseChannel(
139128

140129
sdkCtx := sdk.UnwrapSDKContext(ctx)
141130
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
142-
msgBz, err := json.Marshal(msg) // this is not great
143-
if err != nil {
144-
// this should never happen, as we just built this message
145-
return errorsmod.Wrap(types.ErrInvalidMsg, err.Error())
146-
}
147-
setupCost := k.gasRegister.SetupContractCost(discount, len(msgBz))
131+
setupCost := k.gasRegister.SetupContractCost(discount, msg.ExpectedJSONSize())
148132
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-close-channel")
149133

150134
params := types.NewEnv(ctx, contractAddr)
@@ -186,12 +170,7 @@ func (k Keeper) OnRecvPacket(
186170

187171
sdkCtx := sdk.UnwrapSDKContext(ctx)
188172
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
189-
msgBz, err := json.Marshal(msg) // this is not great
190-
if err != nil {
191-
// this should never happen, as we just built this message
192-
return nil, errorsmod.Wrap(types.ErrInvalidMsg, err.Error())
193-
}
194-
setupCost := k.gasRegister.SetupContractCost(discount, len(msgBz))
173+
setupCost := k.gasRegister.SetupContractCost(discount, msg.ExpectedJSONSize())
195174
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-recv-packet")
196175

197176
env := types.NewEnv(ctx, contractAddr)
@@ -269,12 +248,7 @@ func (k Keeper) OnAckPacket(
269248

270249
sdkCtx := sdk.UnwrapSDKContext(ctx)
271250
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
272-
msgBz, err := json.Marshal(msg) // this is not great
273-
if err != nil {
274-
// this should never happen, as we just built this message
275-
return errorsmod.Wrap(types.ErrInvalidMsg, err.Error())
276-
}
277-
setupCost := k.gasRegister.SetupContractCost(discount, len(msgBz))
251+
setupCost := k.gasRegister.SetupContractCost(discount, msg.ExpectedJSONSize())
278252
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-ack-packet")
279253

280254
env := types.NewEnv(ctx, contractAddr)
@@ -314,12 +288,7 @@ func (k Keeper) OnTimeoutPacket(
314288

315289
sdkCtx := sdk.UnwrapSDKContext(ctx)
316290
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
317-
msgBz, err := json.Marshal(msg) // this is not great
318-
if err != nil {
319-
// this should never happen, as we just built this message
320-
return errorsmod.Wrap(types.ErrInvalidMsg, err.Error())
321-
}
322-
setupCost := k.gasRegister.SetupContractCost(discount, len(msgBz))
291+
setupCost := k.gasRegister.SetupContractCost(discount, msg.ExpectedJSONSize())
323292
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-timeout-packet")
324293

325294
env := types.NewEnv(ctx, contractAddr)
@@ -358,12 +327,7 @@ func (k Keeper) IBCSourceCallback(
358327

359328
sdkCtx := sdk.UnwrapSDKContext(ctx)
360329
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
361-
msgBz, err := json.Marshal(msg) // this is not great
362-
if err != nil {
363-
// this should never happen, as we just built this message
364-
return errorsmod.Wrap(types.ErrInvalidMsg, err.Error())
365-
}
366-
setupCost := k.gasRegister.SetupContractCost(discount, len(msgBz))
330+
setupCost := k.gasRegister.SetupContractCost(discount, msg.ExpectedJSONSize())
367331
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-source-chain-callback")
368332

369333
env := types.NewEnv(ctx, contractAddr)
@@ -402,12 +366,7 @@ func (k Keeper) IBCDestinationCallback(
402366

403367
sdkCtx := sdk.UnwrapSDKContext(ctx)
404368
sdkCtx, discount := k.checkDiscountEligibility(sdkCtx, codeInfo.CodeHash, k.IsPinnedCode(ctx, contractInfo.CodeID))
405-
msgBz, err := json.Marshal(msg) // this is not great
406-
if err != nil {
407-
// this should never happen, as we just built this message
408-
return errorsmod.Wrap(types.ErrInvalidMsg, err.Error())
409-
}
410-
setupCost := k.gasRegister.SetupContractCost(discount, len(msgBz))
369+
setupCost := k.gasRegister.SetupContractCost(discount, msg.ExpectedJSONSize())
411370
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-destination-chain-callback")
412371

413372
env := types.NewEnv(ctx, contractAddr)

0 commit comments

Comments
 (0)