Skip to content

Commit

Permalink
Fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed Jul 4, 2023
1 parent fab8f0e commit 207d5ef
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 67 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.UncompressWithLimit(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
20 changes: 3 additions & 17 deletions x/wasm/ioutils/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,8 @@ import (
)

// Uncompress expects a valid gzip source to unpack or fails. See IsGzip
func Uncompress(gzipSrc []byte) ([]byte, error) {
zr, err := gzip.NewReader(bytes.NewReader(gzipSrc))
if err != nil {
return nil, err
}
zr.Multistream(false)
defer zr.Close()

bz, err := io.ReadAll(zr)
return bz, err
}

// UncompressWithLimit fails if wasm size exceeds the limit.
// It expects a valid gzip source to unpack or fails. See IsGzip
func UncompressWithLimit(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 @@ -35,7 +21,7 @@ func UncompressWithLimit(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
47 changes: 1 addition & 46 deletions x/wasm/ioutils/ioutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,6 @@ func TestUncompress(t *testing.T) {
wasmGzipped, err := os.ReadFile("../keeper/testdata/hackatom.wasm.gzip")
require.NoError(t, err)

specs := map[string]struct {
src []byte
expError error
expResult []byte
}{
"handle wasm compressed": {
src: wasmGzipped,
expResult: wasmRaw,
},
"handle gzip identifier only": {
src: gzipIdent,
expError: io.ErrUnexpectedEOF,
},
"handle broken gzip": {
src: append(gzipIdent, byte(0x1)),
expError: io.ErrUnexpectedEOF,
},
"handle incomplete gzip": {
src: wasmGzipped[:len(wasmGzipped)-5],
expError: io.ErrUnexpectedEOF,
},
"handle limit gzip output": {
src: asGzip(bytes.Repeat([]byte{0x1}, 400_000)),
expResult: bytes.Repeat([]byte{0x1}, 400_000),
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
r, err := Uncompress(spec.src)
require.True(t, errors.Is(spec.expError, err), "exp %v got %+v", spec.expError, err)
if spec.expError != nil {
return
}
assert.Equal(t, spec.expResult, r)
})
}
}

func TestUncompressWithLimit(t *testing.T) {
wasmRaw, err := os.ReadFile("../keeper/testdata/hackatom.wasm")
require.NoError(t, err)

wasmGzipped, err := os.ReadFile("../keeper/testdata/hackatom.wasm.gzip")
require.NoError(t, err)

const maxSize = 400_000

specs := map[string]struct {
Expand Down Expand Up @@ -106,7 +61,7 @@ func TestUncompressWithLimit(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
r, err := UncompressWithLimit(spec.src, maxSize)
r, err := Uncompress(spec.src, maxSize)
require.True(t, errors.Is(spec.expError, err), "exp %v got %+v", spec.expError, err)
if spec.expError != nil {
return
Expand Down
2 changes: 2 additions & 0 deletions x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func TestGenesisExportImport(t *testing.T) {
}

func TestGenesisInit(t *testing.T) {
types.MaxWasmSize = 10 * 1024

wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)

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.UncompressWithLimit(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.UncompressWithLimit(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)
wasmCode, err := ioutils.Uncompress(compressedCode, math.MaxInt64)
if err != nil {
return errorsmod.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down
2 changes: 2 additions & 0 deletions x/wasm/keeper/snapshotter_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
)

func TestSnapshotter(t *testing.T) {
types.MaxWasmSize = 10 * 1024

specs := map[string]struct {
wasmFiles []string
}{
Expand Down

0 comments on commit 207d5ef

Please sign in to comment.