@@ -25,9 +25,14 @@ import (
2525 "github.com/CosmWasm/wasmd/x/wasm/types"
2626)
2727
28- // anyMsgGasCost is the gas cost for unpacking an AnyMsg, in CosmWasm gas units (not SDK gas units).
29- // With the default gas multiplier, this amounts to 5 SDK gas.
30- const anyMsgGasCost = 700000
28+ var (
29+ // MaxAnyMsgValueSize is the maximum allowed size in bytes for an AnyMsg Value field.
30+ MaxAnyMsgValueSize = 512 * 1024 // 512 KB
31+ // AnyMsgBaseGasCost is the base gas cost for unpacking an AnyMsg, in SDK gas units.
32+ AnyMsgBaseGasCost uint64 = 5
33+ // AnyMsgPerKBGasCost is the per-kilobyte gas cost for the AnyMsg Value field, in SDK gas units.
34+ AnyMsgPerKBGasCost uint64 = 1
35+ )
3136
3237type (
3338 BankEncoder func (sender sdk.AccAddress , msg * wasmvmtypes.BankMsg ) ([]sdk.Msg , error )
@@ -220,13 +225,17 @@ func EncodeStakingMsg(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk
220225
221226func EncodeAnyMsg (unpacker codectypes.AnyUnpacker ) AnyEncoder {
222227 return func (ctx sdk.Context , sender sdk.AccAddress , msg * wasmvmtypes.AnyMsg ) ([]sdk.Msg , error ) {
228+ if len (msg .Value ) > MaxAnyMsgValueSize {
229+ return nil , errorsmod .Wrapf (types .ErrLimit , "AnyMsg value size %d exceeds limit %d" , len (msg .Value ), MaxAnyMsgValueSize )
230+ }
231+
232+ ctx .GasMeter ().ConsumeGas (AnyMsgBaseGasCost + AnyMsgPerKBGasCost * uint64 (len (msg .Value ))/ 1024 , "unpacking AnyMsg" )
233+
223234 codecAny := codectypes.Any {
224235 TypeUrl : msg .TypeURL ,
225236 Value : msg .Value ,
226237 }
227238 var sdkMsg sdk.Msg
228-
229- ctx .GasMeter ().ConsumeGas (anyMsgGasCost / types .DefaultGasMultiplier , "unpacking AnyMsg" )
230239 if err := unpacker .UnpackAny (& codecAny , & sdkMsg ); err != nil {
231240 return nil , errorsmod .Wrap (types .ErrInvalidMsg , fmt .Sprintf ("Cannot unpack proto message with type URL: %s" , msg .TypeURL ))
232241 }
0 commit comments