Put GzipIt and Uncompress logic in a common folder
This commit is contained in:
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package keeper
|
||||
package ioutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -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
|
||||
@@ -1,4 +1,4 @@
|
||||
package keeper
|
||||
package ioutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package ioutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -6,7 +6,11 @@ import (
|
||||
)
|
||||
|
||||
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")
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package ioutils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
@@ -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
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user