From 17634778313992a5a9f6f6741cc728b9fb6c78dc Mon Sep 17 00:00:00 2001 From: pinosu <95283998+pinosu@users.noreply.github.com> Date: Wed, 5 Jul 2023 11:33:35 +0200 Subject: [PATCH] Remove check for wasm limit size in state sync (#1471) * Remove check for wasm limit size in state sync * Fix comments * Store original value in variable --- x/wasm/client/cli/gov_tx.go | 2 +- x/wasm/ioutils/ioutil.go | 6 +++--- x/wasm/keeper/genesis_test.go | 11 +++++++++-- x/wasm/keeper/keeper.go | 4 ++-- x/wasm/keeper/snapshotter.go | 3 ++- x/wasm/keeper/snapshotter_integration_test.go | 6 ++++++ 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/x/wasm/client/cli/gov_tx.go b/x/wasm/client/cli/gov_tx.go index be6114b958..52d7963842 100644 --- a/x/wasm/client/cli/gov_tx.go +++ b/x/wasm/client/cli/gov_tx.go @@ -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) } diff --git a/x/wasm/ioutils/ioutil.go b/x/wasm/ioutils/ioutil.go index b671e5ad65..4478b03a37 100644 --- a/x/wasm/ioutils/ioutil.go +++ b/x/wasm/ioutils/ioutil.go @@ -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)) @@ -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) } diff --git a/x/wasm/keeper/genesis_test.go b/x/wasm/keeper/genesis_test.go index c82c699057..7ba2cdbec2 100644 --- a/x/wasm/keeper/genesis_test.go +++ b/x/wasm/keeper/genesis_test.go @@ -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) @@ -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) @@ -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) { diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index 491f094a3b..32c982ea51 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -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()) } @@ -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()) } diff --git a/x/wasm/keeper/snapshotter.go b/x/wasm/keeper/snapshotter.go index 4ac85b46fe..159cbe154c 100644 --- a/x/wasm/keeper/snapshotter.go +++ b/x/wasm/keeper/snapshotter.go @@ -3,6 +3,7 @@ package keeper import ( "encoding/hex" "io" + "math" errorsmod "cosmossdk.io/errors" "github.com/cometbft/cometbft/libs/log" @@ -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()) } diff --git a/x/wasm/keeper/snapshotter_integration_test.go b/x/wasm/keeper/snapshotter_integration_test.go index 2e4fe6c2a1..bb6c4d5809 100644 --- a/x/wasm/keeper/snapshotter_integration_test.go +++ b/x/wasm/keeper/snapshotter_integration_test.go @@ -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))