Skip to content

Commit

Permalink
chore: move msg validation to msg server
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 committed Feb 1, 2024
1 parent bc0ec3f commit 6518f88
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
10 changes: 10 additions & 0 deletions x/wasm-storage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,20 @@ func unzipWasm(wasm []byte) ([]byte, error) {
}

func (m msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
// validate authority
if _, err := sdk.AccAddressFromBech32(req.Authority); err != nil {
return nil, fmt.Errorf("invalid authority address: %s", err)
}

if m.GetAuthority() != req.Authority {
return nil, fmt.Errorf("invalid authority; expected %s, got %s", m.GetAuthority(), req.Authority)
}

// validate params
if err := req.Params.ValidateBasic(); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := m.SetParams(ctx, req.Params); err != nil {
return nil, err
Expand Down
41 changes: 34 additions & 7 deletions x/wasm-storage/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,31 +266,58 @@ func (s *KeeperTestSuite) TestMarshalJSON() {
}
}

func (s *KeeperTestSuite) TestSetParams() {
func (s *KeeperTestSuite) TestUpdateParams() {
authority := s.wasmStorageKeeper.GetAuthority()
cases := []struct {
name string
preRun func()
input types.Params
input types.MsgUpdateParams
expErr bool
expErrMsg string
}{
{
name: "happy path",
input: types.Params{
MaxWasmSize: 1000000, // 1 MB
input: types.MsgUpdateParams{
Authority: s.authority,
Params: types.Params{
MaxWasmSize: 1000000, // 1 MB
},
},
preRun: func() {},
expErr: false,
expErrMsg: "",
},
// negative cases would be caught in ValidateBasic before reaching keeper
{
name: "invalid authority",
input: types.MsgUpdateParams{
Authority: "cosmos16wfryel63g7axeamw68630wglalcnk3l0zuadc",
Params: types.Params{
MaxWasmSize: 1, // 1 MB
},
},
preRun: func() {},
expErr: true,
expErrMsg: "invalid authority; expected " + authority + ", got cosmos16wfryel63g7axeamw68630wglalcnk3l0zuadc",
},
{
name: "invalid max wasm size",
input: types.MsgUpdateParams{
Authority: authority,
Params: types.Params{
MaxWasmSize: 0, // 0 MB
},
},
preRun: func() {},
expErr: true,
expErrMsg: "invalid max Wasm size: 0",
},
}

for _, tc := range cases {
s.Run(tc.name, func() {
s.SetupTest()
tc.preRun()
err := s.wasmStorageKeeper.SetParams(s.ctx, tc.input)
_, err := s.msgSrvr.UpdateParams(s.ctx, &tc.input)
if tc.expErr {
s.Require().Error(err)
s.Require().Equal(tc.expErrMsg, err.Error())
Expand All @@ -299,7 +326,7 @@ func (s *KeeperTestSuite) TestSetParams() {

// Check that the Params were correctly set
params := s.wasmStorageKeeper.GetParams(s.ctx)
s.Require().Equal(tc.input, params)
s.Require().Equal(tc.input.Params, params)
}
})
}
Expand Down
12 changes: 0 additions & 12 deletions x/wasm-storage/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,3 @@ func (msg MsgInstantiateAndRegisterProxyContract) ValidateBasic() error {
}
return nil
}

func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return fmt.Errorf("invalid authority address: %s", err)
}

if err := m.Params.ValidateBasic(); err != nil {
return err
}

return nil
}

0 comments on commit 6518f88

Please sign in to comment.