Skip to content

Commit 9528caa

Browse files
committed
Add setup cost for IBC operations
1 parent efffb9d commit 9528caa

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

x/wasm/keeper/relay.go

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

33
import (
4+
"encoding/json"
45
"time"
56

67
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
@@ -29,11 +30,21 @@ func (k Keeper) OnOpenChannel(
2930
msg wasmvmtypes.IBCChannelOpenMsg,
3031
) (string, error) {
3132
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "ibc-open-channel")
32-
_, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
33+
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
3334
if err != nil {
3435
return "", err
3536
}
3637

38+
sdkCtx := sdk.UnwrapSDKContext(ctx)
39+
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))
46+
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-open-channel")
47+
3748
env := types.NewEnv(ctx, contractAddr)
3849
querier := k.newQueryHandler(ctx, contractAddr)
3950

@@ -78,6 +89,16 @@ func (k Keeper) OnConnectChannel(
7889
return err
7990
}
8091

92+
sdkCtx := sdk.UnwrapSDKContext(ctx)
93+
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))
100+
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-connect-channel")
101+
81102
env := types.NewEnv(ctx, contractAddr)
82103
querier := k.newQueryHandler(ctx, contractAddr)
83104

@@ -116,6 +137,16 @@ func (k Keeper) OnCloseChannel(
116137
return err
117138
}
118139

140+
sdkCtx := sdk.UnwrapSDKContext(ctx)
141+
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))
148+
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-close-channel")
149+
119150
params := types.NewEnv(ctx, contractAddr)
120151
querier := k.newQueryHandler(ctx, contractAddr)
121152

@@ -153,6 +184,16 @@ func (k Keeper) OnRecvPacket(
153184
return nil, err
154185
}
155186

187+
sdkCtx := sdk.UnwrapSDKContext(ctx)
188+
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))
195+
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-recv-packet")
196+
156197
env := types.NewEnv(ctx, contractAddr)
157198
querier := k.newQueryHandler(ctx, contractAddr)
158199

@@ -226,6 +267,16 @@ func (k Keeper) OnAckPacket(
226267
return err
227268
}
228269

270+
sdkCtx := sdk.UnwrapSDKContext(ctx)
271+
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))
278+
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-ack-packet")
279+
229280
env := types.NewEnv(ctx, contractAddr)
230281
querier := k.newQueryHandler(ctx, contractAddr)
231282

@@ -261,6 +312,16 @@ func (k Keeper) OnTimeoutPacket(
261312
return err
262313
}
263314

315+
sdkCtx := sdk.UnwrapSDKContext(ctx)
316+
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))
323+
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-timeout-packet")
324+
264325
env := types.NewEnv(ctx, contractAddr)
265326
querier := k.newQueryHandler(ctx, contractAddr)
266327

@@ -295,6 +356,16 @@ func (k Keeper) IBCSourceCallback(
295356
return err
296357
}
297358

359+
sdkCtx := sdk.UnwrapSDKContext(ctx)
360+
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))
367+
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-source-chain-callback")
368+
298369
env := types.NewEnv(ctx, contractAddr)
299370
querier := k.newQueryHandler(ctx, contractAddr)
300371

@@ -329,6 +400,16 @@ func (k Keeper) IBCDestinationCallback(
329400
return err
330401
}
331402

403+
sdkCtx := sdk.UnwrapSDKContext(ctx)
404+
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))
411+
sdkCtx.GasMeter().ConsumeGas(setupCost, "Loading CosmWasm module: ibc-destination-chain-callback")
412+
332413
env := types.NewEnv(ctx, contractAddr)
333414
querier := k.newQueryHandler(ctx, contractAddr)
334415

0 commit comments

Comments
 (0)