From c1769948e9c00aabe3904ee115714df939033b1b 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 (cherry picked from commit 17634778313992a5a9f6f6741cc728b9fb6c78dc) # Conflicts: # x/wasm/keeper/genesis_test.go --- x/wasm/client/cli/gov_tx.go | 2 +- x/wasm/ioutils/ioutil.go | 6 ++--- x/wasm/keeper/genesis_test.go | 25 +++++++++++++++++++ x/wasm/keeper/keeper.go | 4 +-- x/wasm/keeper/snapshotter.go | 3 ++- x/wasm/keeper/snapshotter_integration_test.go | 6 +++++ 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/x/wasm/client/cli/gov_tx.go b/x/wasm/client/cli/gov_tx.go index f5a0a64a4d..e7a6725bca 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 0b3f6dc29f..c3ae1d8ba2 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, sdkerrors.Wrapf(err, "max %d bytes", limit) } diff --git a/x/wasm/keeper/genesis_test.go b/x/wasm/keeper/genesis_test.go index b778d75561..73e9c6abab 100644 --- a/x/wasm/keeper/genesis_test.go +++ b/x/wasm/keeper/genesis_test.go @@ -114,6 +114,9 @@ func TestGenesisExportImport(t *testing.T) { return false }) + originalMaxWasmSize := types.MaxWasmSize + types.MaxWasmSize = 1 + // re-import var importState wasmTypes.GenesisState err = dstKeeper.cdc.UnmarshalJSON(exportedGenesis, &importState) @@ -125,6 +128,7 @@ func TestGenesisExportImport(t *testing.T) { srcIT := srcCtx.KVStore(srcStoreKeys[j]).Iterator(nil, nil) dstIT := dstCtx.KVStore(dstStoreKeys[j]).Iterator(nil, nil) +<<<<<<< HEAD for i := 0; srcIT.Valid(); i++ { require.True(t, dstIT.Valid(), "[%s] destination DB has less elements than source. Missing: %x", srcStoreKeys[j].Name(), srcIT.Key()) require.Equal(t, srcIT.Key(), dstIT.Key(), i) @@ -138,6 +142,27 @@ func TestGenesisExportImport(t *testing.T) { srcIT.Close() dstIT.Close() } +======= + 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) + require.Equal(t, srcIT.Value(), dstIT.Value(), "[%s] element (%d): %X", wasmKeeper.storeKey.Name(), i, srcIT.Key()) + dstIT.Next() + srcIT.Next() + } + if !assert.False(t, dstIT.Valid()) { + t.Fatalf("dest Iterator still has key :%X", dstIT.Key()) + } +>>>>>>> 17634778 (Remove check for wasm limit size in state sync (#1471)) } func TestGenesisInit(t *testing.T) { diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index f1e87366e2..6c7b3c375a 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -147,7 +147,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(sdkerrors.Wrap(err, "uncompress wasm archive").Error()) } @@ -189,7 +189,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(sdkerrors.Wrap(err, "uncompress wasm archive").Error()) } diff --git a/x/wasm/keeper/snapshotter.go b/x/wasm/keeper/snapshotter.go index ea8a87ffd3..0b08f1559a 100644 --- a/x/wasm/keeper/snapshotter.go +++ b/x/wasm/keeper/snapshotter.go @@ -3,6 +3,7 @@ package keeper import ( "encoding/hex" "io" + "math" snapshot "github.com/cosmos/cosmos-sdk/snapshots/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -102,7 +103,7 @@ func restoreV1(ctx 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 sdkerrors.Wrap(types.ErrCreateFailed, err.Error()) } diff --git a/x/wasm/keeper/snapshotter_integration_test.go b/x/wasm/keeper/snapshotter_integration_test.go index a2c32e57c2..bb161256ed 100644 --- a/x/wasm/keeper/snapshotter_integration_test.go +++ b/x/wasm/keeper/snapshotter_integration_test.go @@ -66,6 +66,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))