Skip to content

Commit

Permalink
Put GzipIt and Uncompress logic in a common folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Apr 27, 2022
1 parent eb2c36d commit 71ff81e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions x/wasm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

wasmUtils "github.com/CosmWasm/wasmd/x/wasm/client/utils"
"github.com/CosmWasm/wasmd/x/wasm/ioutils"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

Expand Down Expand Up @@ -85,13 +85,13 @@ func parseStoreCodeArgs(file string, sender sdk.AccAddress, flags *flag.FlagSet)
}

// gzip the wasm file
if wasmUtils.IsWasm(wasm) {
wasm, err = wasmUtils.GzipIt(wasm)
if ioutils.IsWasm(wasm) {
wasm, err = ioutils.GzipIt(wasm)

if err != nil {
return types.MsgStoreCode{}, err
}
} else if !wasmUtils.IsGzip(wasm) {
} else if !ioutils.IsGzip(wasm) {
return types.MsgStoreCode{}, fmt.Errorf("invalid input file. Use wasm binary or gzip")
}

Expand Down
8 changes: 4 additions & 4 deletions x/wasm/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"

wasmUtils "github.com/CosmWasm/wasmd/x/wasm/client/utils"
"github.com/CosmWasm/wasmd/x/wasm/ioutils"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

Expand Down Expand Up @@ -55,13 +55,13 @@ func storeCodeHandlerFn(cliCtx client.Context) http.HandlerFunc {
wasm := req.WasmBytes

// gzip the wasm file
if wasmUtils.IsWasm(wasm) {
wasm, err = wasmUtils.GzipIt(wasm)
if ioutils.IsWasm(wasm) {
wasm, err = ioutils.GzipIt(wasm)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
} else if !wasmUtils.IsGzip(wasm) {
} else if !ioutils.IsGzip(wasm) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid input file, use wasm binary or zip")
return
}
Expand Down
11 changes: 3 additions & 8 deletions x/wasm/keeper/ioutil.go → x/wasm/ioutils/ioutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package ioutils

import (
"bytes"
Expand All @@ -9,13 +9,8 @@ import (
"github.com/CosmWasm/wasmd/x/wasm/types"
)

// magic bytes to identify gzip.
// See https://www.ietf.org/rfc/rfc1952.txt
// and https://github.com/golang/go/blob/master/src/net/http/sniff.go#L186
var gzipIdent = []byte("\x1F\x8B\x08")

// uncompress returns gzip uncompressed content or given src when not gzip.
func uncompress(src []byte, limit uint64) ([]byte, error) {
// Uncompress returns gzip uncompressed content if input was gzip, or original src otherwise
func Uncompress(src []byte, limit uint64) ([]byte, error) {
switch n := uint64(len(src)); {
case n < 3:
return src, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package ioutils

import (
"bytes"
Expand All @@ -16,10 +16,10 @@ import (
)

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

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

const maxSize = 400_000
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestUncompress(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
r, err := uncompress(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
6 changes: 5 additions & 1 deletion x/wasm/client/utils/utils.go → x/wasm/ioutils/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package utils
package ioutils

import (
"bytes"
"compress/gzip"
)

var (
// magic bytes to identify gzip.
// See https://www.ietf.org/rfc/rfc1952.txt
// and https://github.com/golang/go/blob/master/src/net/http/sniff.go#L186
gzipIdent = []byte("\x1F\x8B\x08")

wasmIdent = []byte("\x00\x61\x73\x6D")
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package ioutils

import (
"io/ioutil"
Expand All @@ -8,7 +8,7 @@ import (
)

func GetTestData() ([]byte, []byte, []byte, error) {
wasmCode, err := ioutil.ReadFile("../../keeper/testdata/hackatom.wasm")
wasmCode, err := ioutil.ReadFile("../keeper/testdata/hackatom.wasm")

if err != nil {
return nil, nil, nil, err
Expand Down
5 changes: 3 additions & 2 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/CosmWasm/wasmd/x/wasm/ioutils"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

Expand Down Expand Up @@ -164,7 +165,7 @@ func (k Keeper) create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte,
if !authZ.CanCreateCode(k.getUploadAccessConfig(ctx), creator) {
return 0, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not create code")
}
wasmCode, err = uncompress(wasmCode, k.GetMaxWasmCodeSize(ctx))
wasmCode, err = ioutils.Uncompress(wasmCode, k.GetMaxWasmCodeSize(ctx))
if err != nil {
return 0, sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down Expand Up @@ -206,7 +207,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 {
wasmCode, err := uncompress(wasmCode, k.GetMaxWasmCodeSize(ctx))
wasmCode, err := ioutils.Uncompress(wasmCode, k.GetMaxWasmCodeSize(ctx))
if err != nil {
return sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions x/wasm/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/CosmWasm/wasmd/x/wasm/client/utils"
"github.com/CosmWasm/wasmd/x/wasm/ioutils"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

Expand Down Expand Up @@ -84,7 +84,7 @@ func (ws *WasmSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) e
return true
}

compressedWasm, err := utils.GzipIt(wasmBytes)
compressedWasm, err := ioutils.GzipIt(wasmBytes)
if err != nil {
rerr = err
return true
Expand Down Expand Up @@ -113,7 +113,7 @@ func restoreV1(ctx sdk.Context, k Keeper, payload []byte) error {
// TODO: more structure here?
wasmCode := payload

wasmCode, err := uncompress(wasmCode, k.GetMaxWasmCodeSize(ctx))
wasmCode, err := ioutils.Uncompress(wasmCode, k.GetMaxWasmCodeSize(ctx))
if err != nil {
return sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down

0 comments on commit 71ff81e

Please sign in to comment.