Skip to content

Commit

Permalink
Enable larger wasm bytecode upload for gov proposals (CosmWasm#1095)
Browse files Browse the repository at this point in the history
* Enable larger wasm bytecode upload for gov proposals

* Set max proposal wasm code size to 3MB
  • Loading branch information
pinosu authored and Magicloud committed Jan 13, 2023
1 parent 41ca971 commit 440f7a0
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ 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)
* `wasmtypes.MaxProposalWasmSize=888000` to set the max size of gov proposal compiled wasm to be accepted (default 3145728)

## 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
2 changes: 1 addition & 1 deletion x/wasm/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c Code) ValidateBasic() error {
if err := c.CodeInfo.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "code info")
}
if err := validateWasmCode(c.CodeBytes); err != nil {
if err := validateWasmCode(c.CodeBytes, MaxProposalWasmSize); err != nil {
return sdkerrors.Wrap(err, "code bytes")
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestCodeValidateBasic(t *testing.T) {
},
"codeBytes greater limit": {
srcMutator: func(c *Code) {
c.CodeBytes = bytes.Repeat([]byte{0x1}, MaxWasmSize+1)
c.CodeBytes = bytes.Repeat([]byte{0x1}, MaxProposalWasmSize+1)
},
expError: true,
},
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (p StoreCodeProposal) ValidateBasic() error {
return sdkerrors.Wrap(err, "run as")
}

if err := validateWasmCode(p.WASMByteCode); err != nil {
if err := validateWasmCode(p.WASMByteCode, MaxProposalWasmSize); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "code bytes %s", err.Error())
}

Expand Down Expand Up @@ -303,7 +303,7 @@ func (p StoreAndInstantiateContractProposal) ValidateBasic() error {
return sdkerrors.Wrap(err, "run as")
}

if err := validateWasmCode(p.WASMByteCode); err != nil {
if err := validateWasmCode(p.WASMByteCode, MaxProposalWasmSize); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "code bytes %s", err.Error())
}

Expand Down
4 changes: 2 additions & 2 deletions x/wasm/types/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestValidateStoreCodeProposal(t *testing.T) {
},
"wasm code invalid": {
src: StoreCodeProposalFixture(func(p *StoreCodeProposal) {
p.WASMByteCode = bytes.Repeat([]byte{0x0}, MaxWasmSize+1)
p.WASMByteCode = bytes.Repeat([]byte{0x0}, MaxProposalWasmSize+1)
}),
expErr: true,
},
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestValidateStoreAndInstantiateContractProposal(t *testing.T) {
},
"wasm code invalid": {
src: StoreAndInstantiateContractProposalFixture(func(p *StoreAndInstantiateContractProposal) {
p.WASMByteCode = bytes.Repeat([]byte{0x0}, MaxWasmSize+1)
p.WASMByteCode = bytes.Repeat([]byte{0x0}, MaxProposalWasmSize+1)
}),
expErr: true,
},
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (msg MsgStoreCode) ValidateBasic() error {
return err
}

if err := validateWasmCode(msg.WASMByteCode); err != nil {
if err := validateWasmCode(msg.WASMByteCode, MaxWasmSize); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "code bytes %s", err.Error())
}

Expand Down
9 changes: 6 additions & 3 deletions x/wasm/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ var (

// MaxWasmSize is the largest a compiled contract code can be when storing code on chain
MaxWasmSize = 800 * 1024 // extension point for chains to customize via compile flag.

// MaxProposalWasmSize is the largest a gov proposal compiled contract code can be when storing code on chain
MaxProposalWasmSize = 3 * 1024 * 1024 // extension point for chains to customize via compile flag.
)

func validateWasmCode(s []byte) error {
func validateWasmCode(s []byte, maxSize int) 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)
if len(s) > maxSize {
return sdkerrors.Wrapf(ErrLimit, "cannot be longer than %d bytes", maxSize)
}
return nil
}
Expand Down

0 comments on commit 440f7a0

Please sign in to comment.