Skip to content

Commit

Permalink
Remove check for wasm limit size in state sync (#1471)
Browse files Browse the repository at this point in the history
* Remove check for wasm limit size in state sync

* Fix comments

* Store original value in variable
  • Loading branch information
pinosu authored Jul 5, 2023
1 parent c158b77 commit 1763477
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion x/wasm/client/cli/gov_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func parseVerificationFlags(gzippedWasm []byte, flags *flag.FlagSet) (string, st
// wasm is gzipped in parseStoreCodeArgs
// checksum generation will be decoupled here
// reference https://github.com/CosmWasm/wasmvm/issues/359
raw, err := ioutils.Uncompress(gzippedWasm, uint64(types.MaxWasmSize))
raw, err := ioutils.Uncompress(gzippedWasm, int64(types.MaxWasmSize))
if err != nil {
return "", "", nil, fmt.Errorf("invalid zip: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions x/wasm/ioutils/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

// Uncompress expects a valid gzip source to unpack or fails. See IsGzip
func Uncompress(gzipSrc []byte, limit uint64) ([]byte, error) {
if uint64(len(gzipSrc)) > limit {
func Uncompress(gzipSrc []byte, limit int64) ([]byte, error) {
if int64(len(gzipSrc)) > limit {
return nil, types.ErrLimit.Wrapf("max %d bytes", limit)
}
zr, err := gzip.NewReader(bytes.NewReader(gzipSrc))
Expand All @@ -21,7 +21,7 @@ func Uncompress(gzipSrc []byte, limit uint64) ([]byte, error) {
}
zr.Multistream(false)
defer zr.Close()
bz, err := io.ReadAll(LimitReader(zr, int64(limit)))
bz, err := io.ReadAll(LimitReader(zr, limit))
if types.ErrLimit.Is(err) {
return nil, errorsmod.Wrapf(err, "max %d bytes", limit)
}
Expand Down
11 changes: 9 additions & 2 deletions x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func TestGenesisExportImport(t *testing.T) {
return false
})

originalMaxWasmSize := types.MaxWasmSize
types.MaxWasmSize = 1

// re-import
var importState types.GenesisState
err = dstKeeper.cdc.UnmarshalJSON(exportedGenesis, &importState)
Expand All @@ -133,6 +136,12 @@ func TestGenesisExportImport(t *testing.T) {
srcIT := srcCtx.KVStore(wasmKeeper.storeKey).Iterator(nil, nil)
dstIT := dstCtx.KVStore(dstKeeper.storeKey).Iterator(nil, nil)

t.Cleanup(func() {
types.MaxWasmSize = originalMaxWasmSize
srcIT.Close()
dstIT.Close()
})

for i := 0; srcIT.Valid(); i++ {
require.True(t, dstIT.Valid(), "[%s] destination DB has less elements than source. Missing: %x", wasmKeeper.storeKey.Name(), srcIT.Key())
require.Equal(t, srcIT.Key(), dstIT.Key(), i)
Expand All @@ -143,8 +152,6 @@ func TestGenesisExportImport(t *testing.T) {
if !assert.False(t, dstIT.Valid()) {
t.Fatalf("dest Iterator still has key :%X", dstIT.Key())
}
srcIT.Close()
dstIT.Close()
}

func TestGenesisInit(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (k Keeper) create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte,

if ioutils.IsGzip(wasmCode) {
ctx.GasMeter().ConsumeGas(k.gasRegister.UncompressCosts(len(wasmCode)), "Uncompress gzip bytecode")
wasmCode, err = ioutils.Uncompress(wasmCode, uint64(types.MaxWasmSize))
wasmCode, err = ioutils.Uncompress(wasmCode, int64(types.MaxWasmSize))
if err != nil {
return 0, checksum, types.ErrCreateFailed.Wrap(errorsmod.Wrap(err, "uncompress wasm archive").Error())
}
Expand Down Expand Up @@ -212,7 +212,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 {
if ioutils.IsGzip(wasmCode) {
var err error
wasmCode, err = ioutils.Uncompress(wasmCode, uint64(types.MaxWasmSize))
wasmCode, err = ioutils.Uncompress(wasmCode, math.MaxInt64)
if err != nil {
return types.ErrCreateFailed.Wrap(errorsmod.Wrap(err, "uncompress wasm archive").Error())
}
Expand Down
3 changes: 2 additions & 1 deletion x/wasm/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/hex"
"io"
"math"

errorsmod "cosmossdk.io/errors"
"github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -99,7 +100,7 @@ func restoreV1(_ sdk.Context, k *Keeper, compressedCode []byte) error {
if !ioutils.IsGzip(compressedCode) {
return types.ErrInvalid.Wrap("not a gzip")
}
wasmCode, err := ioutils.Uncompress(compressedCode, uint64(types.MaxWasmSize))
wasmCode, err := ioutils.Uncompress(compressedCode, math.MaxInt64)
if err != nil {
return errorsmod.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down
6 changes: 6 additions & 0 deletions x/wasm/keeper/snapshotter_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func TestSnapshotter(t *testing.T) {
require.NoError(t, err)
assert.NotNil(t, snapshot)

originalMaxWasmSize := types.MaxWasmSize
types.MaxWasmSize = 1
t.Cleanup(func() {
types.MaxWasmSize = originalMaxWasmSize
})

// when snapshot imported into dest app instance
destWasmApp := app.SetupWithEmptyStore(t)
require.NoError(t, destWasmApp.SnapshotManager().Restore(*snapshot))
Expand Down

0 comments on commit 1763477

Please sign in to comment.