Skip to content

Instantly share code, notes, and snippets.

@bigs
Created April 26, 2022 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigs/1340f1c69ba2464527b69d02e7b6e1c4 to your computer and use it in GitHub Desktop.
Save bigs/1340f1c69ba2464527b69d02e7b6e1c4 to your computer and use it in GitHub Desktop.
A quick attempt at an `AnteHandler` based approach to enforcing code size limits based on a param
diff --git a/x/wasm/alias.go b/x/wasm/alias.go
index 610ce97..3e32935 100644
--- a/x/wasm/alias.go
+++ b/x/wasm/alias.go
@@ -17,7 +17,6 @@ const (
TStoreKey = types.TStoreKey
QuerierRoute = types.QuerierRoute
RouterKey = types.RouterKey
- MaxWasmSize = types.MaxWasmSize
MaxLabelSize = types.MaxLabelSize
WasmModuleEventType = types.WasmModuleEventType
AttributeKeyContractAddr = types.AttributeKeyContractAddr
diff --git a/x/wasm/keeper/ante.go b/x/wasm/keeper/ante.go
index 1ffd34b..b15cf87 100644
--- a/x/wasm/keeper/ante.go
+++ b/x/wasm/keeper/ante.go
@@ -4,6 +4,8 @@ import (
"encoding/binary"
sdk "github.com/cosmos/cosmos-sdk/types"
+ "github.com/cosmos/cosmos-sdk/types/errors"
+ paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
@@ -94,3 +96,34 @@ func (d LimitSimulationGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
}
return next(ctx, tx, simulate)
}
+
+// LimitMaxWasmCodeSizeDecorator ante decorator to limit the maximum size of
+type LimitMaxWasmCodeSizeDecorator struct {
+ paramSpace paramtypes.Subspace
+}
+
+func (l LimitMaxWasmCodeSizeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
+ if !simulate {
+ return next(ctx, tx, simulate)
+ }
+
+ var maxWasmCodeSize uint64
+ l.paramSpace.Get(ctx, types.ParamStoreKeyMaxWasmCodeSize, &maxWasmCodeSize)
+
+ for _, msg := range tx.GetMsgs() {
+ switch msg := msg.(type) {
+ case *types.MsgStoreCode:
+ if uint64(len(msg.WASMByteCode)) > maxWasmCodeSize {
+ return ctx, errors.Wrapf(types.ErrLimit, "contract code cannot be longer than %d bytes", maxWasmCodeSize)
+ }
+ default:
+ continue
+ }
+ }
+
+ return next(ctx, tx, simulate)
+}
+
+func NewLimitMaxWasmCodeSizeDecorator(paramSpace paramtypes.Subspace) *LimitMaxWasmCodeSizeDecorator {
+ return &LimitMaxWasmCodeSizeDecorator{paramSpace: paramSpace}
+}
diff --git a/x/wasm/types/validation.go b/x/wasm/types/validation.go
index 374782c..380326a 100644
--- a/x/wasm/types/validation.go
+++ b/x/wasm/types/validation.go
@@ -5,8 +5,6 @@ import (
)
const (
- MaxWasmSize = 500 * 1024
-
// MaxLabelSize is the longest label that can be used when Instantiating a contract
MaxLabelSize = 128
)
@@ -15,9 +13,6 @@ func validateWasmCode(s []byte) error {
if len(s) == 0 {
return sdkerrors.Wrap(ErrEmpty, "is required")
}
- if len(s) > MaxWasmSize {
- return sdkerrors.Wrapf(ErrLimit, "cannot be longer than %d bytes", MaxWasmSize)
- }
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment