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

(cherry picked from commit 1763477)

# Conflicts:
#	x/wasm/keeper/genesis_test.go
  • Loading branch information
pinosu authored and mergify[bot] committed Jul 21, 2023
1 parent 5c09ba9 commit c176994
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 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, sdkerrors.Wrapf(err, "max %d bytes", limit)
}
Expand Down
25 changes: 25 additions & 0 deletions x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
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 @@ -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())
}
Expand Down Expand Up @@ -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())
}
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"

snapshot "github.com/cosmos/cosmos-sdk/snapshots/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -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())
}
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 @@ -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))
Expand Down

0 comments on commit c176994

Please sign in to comment.