Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return error when Wasm uncompression fails #60

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/wasm-storage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func unzipWasm(wasm []byte) ([]byte, error) {
}
unzipped, err = ioutils.Uncompress(wasm, types.MaxWasmSize)
if err != nil {
return unzipped, nil
return nil, err
}
return unzipped, nil
}
117 changes: 93 additions & 24 deletions x/wasm-storage/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import (
)

func (s *KeeperTestSuite) TestStoreDataRequestWasm() {
wasm, err := os.ReadFile("test_utils/hello-world.wasm")
regWasm, err := os.ReadFile("test_utils/hello-world.wasm")
s.Require().NoError(err)
regWasmZipped, err := ioutils.GzipIt(regWasm)
s.Require().NoError(err)

oversizedWasm, err := os.ReadFile("test_utils/simple-dr.wasm")
hacheigriega marked this conversation as resolved.
Show resolved Hide resolved
s.Require().NoError(err)
oversizedWasmZipped, err := ioutils.GzipIt(oversizedWasm)
s.Require().NoError(err)
compWasm, err := ioutils.GzipIt(wasm)

cases := []struct {
name string
Expand All @@ -28,32 +34,66 @@ func (s *KeeperTestSuite) TestStoreDataRequestWasm() {
preRun: func() {},
input: types.MsgStoreDataRequestWasm{
Sender: s.authority,
Wasm: compWasm,
Wasm: regWasmZipped,
WasmType: types.WasmTypeDataRequest,
},
expErr: false,
expOutput: types.MsgStoreDataRequestWasmResponse{
Hash: hex.EncodeToString(crypto.Keccak256(wasm)),
Hash: hex.EncodeToString(crypto.Keccak256(regWasm)),
},
},
{
name: "Overlay wasm already exist",
name: "Data Request wasm already exist",
input: types.MsgStoreDataRequestWasm{
Sender: s.authority,
Wasm: compWasm,
Wasm: regWasmZipped,
WasmType: types.WasmTypeDataRequest,
},
preRun: func() {
input := types.MsgStoreDataRequestWasm{
Sender: s.authority,
Wasm: compWasm,
Wasm: regWasmZipped,
WasmType: types.WasmTypeDataRequest,
}
_, err := s.msgSrvr.StoreDataRequestWasm(s.ctx, &input)
s.Require().Nil(err)
},
expErr: true,
expErrMsg: "Data Request Wasm with given hash already exists",
expErrMsg: "data Request Wasm with given hash already exists",
},
// TO-DO: Add after migrating ValidateBasic logic
Copy link
Member

@mariocao mariocao Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the logic not already there?

if msg.WasmType != WasmTypeDataRequest && msg.WasmType != WasmTypeTally {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! But Cosmos is encouraging us to put that logic directly in msg_server starting with v0.50. See cosmos/cosmos-sdk#15648

// {
// name: "inconsistent Wasm type",
// input: types.MsgStoreDataRequestWasm{
// Sender: s.authority,
// Wasm: regWasmZipped,
// WasmType: types.WasmTypeRelayer,
// },
// preRun: func() {},
// expErr: true,
// expErrMsg: "not a Data Request Wasm",
// },
{
name: "unzipped Wasm",
input: types.MsgStoreDataRequestWasm{
Sender: s.authority,
Wasm: regWasm,
WasmType: types.WasmTypeDataRequest,
},
preRun: func() {},
expErr: true,
expErrMsg: "wasm is not gzip compressed",
},
{
name: "oversized Wasm",
input: types.MsgStoreDataRequestWasm{
Sender: s.authority,
Wasm: oversizedWasmZipped,
WasmType: types.WasmTypeDataRequest,
},
preRun: func() {},
expErr: true,
expErrMsg: "",
},
}
for _, tc := range cases {
Expand All @@ -62,7 +102,7 @@ func (s *KeeperTestSuite) TestStoreDataRequestWasm() {
tc.preRun()
res, err := s.msgSrvr.StoreDataRequestWasm(s.ctx, &tc.input)
if tc.expErr {
s.Require().Error(err, tc.expErrMsg)
s.Require().ErrorContains(err, tc.expErrMsg)
} else {
s.Require().Nil(err)
s.Require().Equal(tc.expOutput, *res)
Expand All @@ -72,9 +112,16 @@ func (s *KeeperTestSuite) TestStoreDataRequestWasm() {
}

func (s *KeeperTestSuite) TestStoreOverlayWasm() {
wasm, err := os.ReadFile("test_utils/hello-world.wasm")
regWasm, err := os.ReadFile("test_utils/hello-world.wasm")
s.Require().NoError(err)
regWasmZipped, err := ioutils.GzipIt(regWasm)
s.Require().NoError(err)
compWasm, err := ioutils.GzipIt(wasm)

oversizedWasm, err := os.ReadFile("test_utils/simple-dr.wasm")
s.Require().NoError(err)
oversizedWasmZipped, err := ioutils.GzipIt(oversizedWasm)
s.Require().NoError(err)

cases := []struct {
name string
preRun func()
Expand All @@ -87,45 +134,67 @@ func (s *KeeperTestSuite) TestStoreOverlayWasm() {
name: "happy path",
input: types.MsgStoreOverlayWasm{
Sender: s.authority,
Wasm: compWasm,
WasmType: types.WasmTypeDataRequest,
Wasm: regWasmZipped,
WasmType: types.WasmTypeRelayer,
},
preRun: func() {},
expErr: false,
expErrMsg: "",
expOutput: types.MsgStoreOverlayWasmResponse{
Hash: hex.EncodeToString(crypto.Keccak256(wasm)),
Hash: hex.EncodeToString(crypto.Keccak256(regWasm)),
},
},
{
name: "invalid authority",
input: types.MsgStoreOverlayWasm{
Sender: "this-is-not-valid",
Wasm: compWasm,
WasmType: types.WasmTypeDataRequest,
Sender: "cosmos16wfryel63g7axeamw68630wglalcnk3l0zuadc",
Wasm: regWasmZipped,
WasmType: types.WasmTypeRelayer,
},
preRun: func() {},
expErr: true,
expErrMsg: "invalid authority this-is-not-valid",
expErrMsg: "invalid authority",
},
{
name: "Overlay wasm already exist",
input: types.MsgStoreOverlayWasm{
Sender: s.authority,
Wasm: compWasm,
WasmType: types.WasmTypeDataRequest,
Wasm: regWasmZipped,
WasmType: types.WasmTypeRelayer,
},
preRun: func() {
input := types.MsgStoreOverlayWasm{
Sender: s.authority,
Wasm: compWasm,
WasmType: types.WasmTypeDataRequest,
Wasm: regWasmZipped,
WasmType: types.WasmTypeRelayer,
}
_, err := s.msgSrvr.StoreOverlayWasm(s.ctx, &input)
s.Require().Nil(err)
},
expErr: true,
expErrMsg: "Overlay Wasm with given hash already exists",
expErrMsg: "overlay Wasm with given hash already exists",
},
{
name: "unzipped Wasm",
input: types.MsgStoreOverlayWasm{
Sender: s.authority,
Wasm: regWasm,
WasmType: types.WasmTypeRelayer,
},
preRun: func() {},
expErr: true,
expErrMsg: "wasm is not gzip compressed",
},
{
name: "oversized Wasm",
input: types.MsgStoreOverlayWasm{
Sender: s.authority,
Wasm: oversizedWasmZipped,
WasmType: types.WasmTypeRelayer,
},
preRun: func() {},
expErr: true,
expErrMsg: "",
},
}
for _, tc := range cases {
Expand All @@ -134,7 +203,7 @@ func (s *KeeperTestSuite) TestStoreOverlayWasm() {
tc.preRun()
res, err := s.msgSrvr.StoreOverlayWasm(s.ctx, &tc.input)
if tc.expErr {
s.Require().Error(err, tc.expErrMsg)
s.Require().ErrorContains(err, tc.expErrMsg)
} else {
s.Require().Nil(err)
s.Require().Equal(tc.expOutput, *res)
Expand Down
Binary file added x/wasm-storage/keeper/test_utils/simple-dr.wasm
Binary file not shown.