Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate MaxWasmSize constraints into a single var #826

Merged
merged 5 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased](https://github.com/CosmWasm/wasmd/tree/HEAD)

**Merged pull requests:**

- Consolidate MaxWasmSize constraints into a single var [\#826](https://github.com/CosmWasm/wasmd/pull/826)

[Full Changelog](https://github.com/CosmWasm/wasmd/compare/v0.26.0...HEAD)

**Implemented Enhancements**
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ used by stateless `ValidateBasic()`. Thus, we made them public `var` and these c
file of your custom chain.

* `wasmtypes.MaxLabelSize = 64` to set the maximum label size on instantiation (default 128)
* `wasmtypes.MaxWasmSize=777000` to set the max size of compiled wasm to be accepted (default 819200)

## Genesis Configuration
We strongly suggest **to limit the max block gas in the genesis** and not use the default value (`-1` for infinite).
Expand Down
1 change: 0 additions & 1 deletion docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ Params defines the set of wasm parameters.
| ----- | ---- | ----- | ----------- |
| `code_upload_access` | [AccessConfig](#cosmwasm.wasm.v1.AccessConfig) | | |
| `instantiate_default_permission` | [AccessType](#cosmwasm.wasm.v1.AccessType) | | |
| `max_wasm_code_size` | [uint64](#uint64) | | |



Expand Down
2 changes: 0 additions & 2 deletions proto/cosmwasm/wasm/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ message Params {
];
AccessType instantiate_default_permission = 2
[ (gogoproto.moretags) = "yaml:\"instantiate_default_permission\"" ];
uint64 max_wasm_code_size = 3
[ (gogoproto.moretags) = "yaml:\"max_wasm_code_size\"" ];
}

// CodeInfo is data for the uploaded contract WASM code
Expand Down
1 change: 0 additions & 1 deletion x/wasm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
TStoreKey = types.TStoreKey
QuerierRoute = types.QuerierRoute
RouterKey = types.RouterKey
MaxWasmSize = types.MaxWasmSize
WasmModuleEventType = types.WasmModuleEventType
AttributeKeyContractAddr = types.AttributeKeyContractAddr
ProposalTypeStoreCode = types.ProposalTypeStoreCode
Expand Down
3 changes: 1 addition & 2 deletions x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) {
"code_upload_access": {
"permission": "Everybody"
},
"instantiate_default_permission": "Everybody",
"max_wasm_code_size": 500000
"instantiate_default_permission": "Everybody"
},
"codes": [
{
Expand Down
10 changes: 2 additions & 8 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,6 @@ func (k Keeper) getInstantiateAccessConfig(ctx sdk.Context) types.AccessType {
return a
}

func (k Keeper) GetMaxWasmCodeSize(ctx sdk.Context) uint64 {
var a uint64
k.paramSpace.Get(ctx, types.ParamStoreKeyMaxWasmCodeSize, &a)
return a
}

// GetParams returns the total set of wasm parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
var params types.Params
Expand All @@ -164,7 +158,7 @@ func (k Keeper) create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte,
if !authZ.CanCreateCode(k.getUploadAccessConfig(ctx), creator) {
return 0, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not create code")
}
wasmCode, err = uncompress(wasmCode, k.GetMaxWasmCodeSize(ctx))
wasmCode, err = uncompress(wasmCode, uint64(types.MaxWasmSize))
if err != nil {
return 0, sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down Expand Up @@ -206,7 +200,7 @@ func (k Keeper) storeCodeInfo(ctx sdk.Context, codeID uint64, codeInfo types.Cod
}

func (k Keeper) importCode(ctx sdk.Context, codeID uint64, codeInfo types.CodeInfo, wasmCode []byte) error {
wasmCode, err := uncompress(wasmCode, k.GetMaxWasmCodeSize(ctx))
wasmCode, err := uncompress(wasmCode, uint64(types.MaxWasmSize))
if err != nil {
return sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down
1 change: 0 additions & 1 deletion x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func TestCreateStoresInstantiatePermission(t *testing.T) {
keepers.WasmKeeper.SetParams(ctx, types.Params{
CodeUploadAccess: types.AllowEverybody,
InstantiateDefaultPermission: spec.srcPermission,
MaxWasmCodeSize: types.DefaultMaxWasmCodeSize,
})
fundAccounts(t, ctx, accKeeper, bankKeeper, myAddr, deposit)

Expand Down
4 changes: 0 additions & 4 deletions x/wasm/keeper/proposal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestStoreCodeProposal(t *testing.T) {
wasmKeeper.SetParams(ctx, types.Params{
CodeUploadAccess: types.AllowNobody,
InstantiateDefaultPermission: types.AccessTypeNobody,
MaxWasmCodeSize: types.DefaultMaxWasmCodeSize,
})
wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)
Expand Down Expand Up @@ -67,7 +66,6 @@ func TestInstantiateProposal(t *testing.T) {
wasmKeeper.SetParams(ctx, types.Params{
CodeUploadAccess: types.AllowNobody,
InstantiateDefaultPermission: types.AccessTypeNobody,
MaxWasmCodeSize: types.DefaultMaxWasmCodeSize,
})

wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
Expand Down Expand Up @@ -131,7 +129,6 @@ func TestMigrateProposal(t *testing.T) {
wasmKeeper.SetParams(ctx, types.Params{
CodeUploadAccess: types.AllowNobody,
InstantiateDefaultPermission: types.AccessTypeNobody,
MaxWasmCodeSize: types.DefaultMaxWasmCodeSize,
})

wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
Expand Down Expand Up @@ -383,7 +380,6 @@ func TestAdminProposals(t *testing.T) {
wasmKeeper.SetParams(ctx, types.Params{
CodeUploadAccess: types.AllowNobody,
InstantiateDefaultPermission: types.AccessTypeNobody,
MaxWasmCodeSize: types.DefaultMaxWasmCodeSize,
})

codeInfoFixture := types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode))
Expand Down
6 changes: 0 additions & 6 deletions x/wasm/simulation/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ func ParamChanges(r *rand.Rand, cdc codec.Codec) []simtypes.ParamChange {
return fmt.Sprintf("%q", params.CodeUploadAccess.Permission.String())
},
),
simulation.NewSimParamChange(types.ModuleName, string(types.ParamStoreKeyMaxWasmCodeSize),
func(r *rand.Rand) string {
return fmt.Sprintf(`"%d"`, params.MaxWasmCodeSize)
},
),
}
}

Expand All @@ -43,6 +38,5 @@ func RandomParams(r *rand.Rand) types.Params {
return types.Params{
CodeUploadAccess: accessConfig,
InstantiateDefaultPermission: accessConfig.Permission,
MaxWasmCodeSize: uint64(simtypes.RandIntBetween(r, 1, 600) * 1024),
}
}
22 changes: 0 additions & 22 deletions x/wasm/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ import (
"gopkg.in/yaml.v2"
)

const (
// DefaultMaxWasmCodeSize limit max bytes read to prevent gzip bombs
DefaultMaxWasmCodeSize = 600 * 1024 * 2
)

var ParamStoreKeyUploadAccess = []byte("uploadAccess")
var ParamStoreKeyInstantiateAccess = []byte("instantiateAccess")
var ParamStoreKeyMaxWasmCodeSize = []byte("maxWasmCodeSize")

var AllAccessTypes = []AccessType{
AccessTypeNobody,
Expand Down Expand Up @@ -96,7 +90,6 @@ func DefaultParams() Params {
return Params{
CodeUploadAccess: AllowEverybody,
InstantiateDefaultPermission: AccessTypeEverybody,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
}
}

Expand All @@ -113,7 +106,6 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyUploadAccess, &p.CodeUploadAccess, validateAccessConfig),
paramtypes.NewParamSetPair(ParamStoreKeyInstantiateAccess, &p.InstantiateDefaultPermission, validateAccessType),
paramtypes.NewParamSetPair(ParamStoreKeyMaxWasmCodeSize, &p.MaxWasmCodeSize, validateMaxWasmCodeSize),
}
}

Expand All @@ -125,9 +117,6 @@ func (p Params) ValidateBasic() error {
if err := validateAccessConfig(p.CodeUploadAccess); err != nil {
return errors.Wrap(err, "upload access")
}
if err := validateMaxWasmCodeSize(p.MaxWasmCodeSize); err != nil {
return errors.Wrap(err, "max wasm code size")
}
return nil
}

Expand Down Expand Up @@ -155,17 +144,6 @@ func validateAccessType(i interface{}) error {
return sdkerrors.Wrapf(ErrInvalid, "unknown type: %q", a)
}

func validateMaxWasmCodeSize(i interface{}) error {
a, ok := i.(uint64)
if !ok {
return sdkerrors.Wrapf(ErrInvalid, "type: %T", i)
}
if a == 0 {
return sdkerrors.Wrap(ErrInvalid, "must be greater 0")
}
return nil
}

func (a AccessConfig) ValidateBasic() error {
switch a.Permission {
case AccessTypeUnspecified:
Expand Down
20 changes: 1 addition & 19 deletions x/wasm/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,81 +28,64 @@ func TestValidateParams(t *testing.T) {
src: Params{
CodeUploadAccess: AllowNobody,
InstantiateDefaultPermission: AccessTypeNobody,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
},
"all good with everybody": {
src: Params{
CodeUploadAccess: AllowEverybody,
InstantiateDefaultPermission: AccessTypeEverybody,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
},
"all good with only address": {
src: Params{
CodeUploadAccess: AccessTypeOnlyAddress.With(anyAddress),
InstantiateDefaultPermission: AccessTypeOnlyAddress,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
},
"reject empty type in instantiate permission": {
src: Params{
CodeUploadAccess: AllowNobody,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
expErr: true,
},
"reject unknown type in instantiate": {
src: Params{
CodeUploadAccess: AllowNobody,
InstantiateDefaultPermission: 1111,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
expErr: true,
},
"reject invalid address in only address": {
src: Params{
CodeUploadAccess: AccessConfig{Permission: AccessTypeOnlyAddress, Address: invalidAddress},
InstantiateDefaultPermission: AccessTypeOnlyAddress,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
expErr: true,
},
"reject CodeUploadAccess Everybody with obsolete address": {
src: Params{
CodeUploadAccess: AccessConfig{Permission: AccessTypeEverybody, Address: anyAddress.String()},
InstantiateDefaultPermission: AccessTypeOnlyAddress,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
expErr: true,
},
"reject CodeUploadAccess Nobody with obsolete address": {
src: Params{
CodeUploadAccess: AccessConfig{Permission: AccessTypeNobody, Address: anyAddress.String()},
InstantiateDefaultPermission: AccessTypeOnlyAddress,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
expErr: true,
},
"reject empty CodeUploadAccess": {
src: Params{
InstantiateDefaultPermission: AccessTypeOnlyAddress,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
expErr: true,
},
"reject undefined permission in CodeUploadAccess": {
src: Params{
CodeUploadAccess: AccessConfig{Permission: AccessTypeUnspecified},
InstantiateDefaultPermission: AccessTypeOnlyAddress,
MaxWasmCodeSize: DefaultMaxWasmCodeSize,
},
expErr: true,
},
"reject empty max wasm code size": {
src: Params{
CodeUploadAccess: AllowNobody,
InstantiateDefaultPermission: AccessTypeNobody,
},
expErr: true,
},
Expand Down Expand Up @@ -167,8 +150,7 @@ func TestParamsUnmarshalJson(t *testing.T) {

"defaults": {
src: `{"code_upload_access": {"permission": "Everybody"},
"instantiate_default_permission": "Everybody",
"max_wasm_code_size": 1228800}`,
"instantiate_default_permission": "Everybody"}`,
exp: DefaultParams(),
},
}
Expand Down
Loading