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

Ensure some contraints and limits on pin/unpin code ids #1624

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 28 additions & 5 deletions x/wasm/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,25 @@ func (msg MsgPinCodes) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

const maxCodeIDTotal = 50

func (msg MsgPinCodes) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return errorsmod.Wrap(err, "authority")
}
if len(msg.CodeIDs) == 0 {
return validateCodeIDs(msg.CodeIDs)
}

// ensure not empty, not duplicates and not exceeding max number
func validateCodeIDs(codeIDs []uint64) error {
switch n := len(codeIDs); {
case n == 0:
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty code ids")
case n > maxCodeIDTotal:
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "total number of code ids is greater than %d", maxCodeIDTotal)
}
if hasDuplicates(codeIDs) {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "duplicate code ids")
}
return nil
}
Expand Down Expand Up @@ -519,10 +532,7 @@ func (msg MsgUnpinCodes) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return errorsmod.Wrap(err, "authority")
}
if len(msg.CodeIDs) == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty code ids")
}
return nil
return validateCodeIDs(msg.CodeIDs)
}

func (msg MsgSudoContract) Route() string {
Expand Down Expand Up @@ -684,6 +694,7 @@ func (msg MsgRemoveCodeUploadParamsAddresses) ValidateBasic() error {
func checkDuplicatedAddresses(addresses []string) error {
index := map[string]struct{}{}
for _, addr := range addresses {
addr = strings.ToUpper(addr)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

if _, err := sdk.AccAddressFromBech32(addr); err != nil {
return errorsmod.Wrap(err, "addresses")
}
Expand Down Expand Up @@ -739,3 +750,15 @@ func (msg MsgStoreAndMigrateContract) ValidateBasic() error {
}
return nil
}

// returns true when slice contains any duplicates
func hasDuplicates[T comparable](s []T) bool {
index := make(map[T]struct{}, len(s))
for _, v := range s {
if _, exists := index[v]; exists {
return true
}
index[v] = struct{}{}
}
return false
}
37 changes: 36 additions & 1 deletion x/wasm/types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,6 @@ func TestMsgRemoveCodeUploadParamsAddressesValidation(t *testing.T) {
func TestMsgPinCodesValidation(t *testing.T) {
// proper address size
goodAddress := sdk.AccAddress(make([]byte, 20)).String()

specs := map[string]struct {
src MsgPinCodes
expErr bool
Expand Down Expand Up @@ -915,6 +914,20 @@ func TestMsgPinCodesValidation(t *testing.T) {
},
expErr: true,
},
"exceeds max code ids": {
src: MsgPinCodes{
Authority: goodAddress,
CodeIDs: genCodeIDs(51),
},
expErr: true,
},
"duplicate code ids": {
src: MsgPinCodes{
Authority: goodAddress,
CodeIDs: []uint64{1, 1},
},
expErr: true,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
Expand Down Expand Up @@ -961,6 +974,20 @@ func TestMsgUnpinCodesValidation(t *testing.T) {
},
expErr: true,
},
"exceeds max code ids": {
src: MsgUnpinCodes{
Authority: goodAddress,
CodeIDs: genCodeIDs(51),
},
expErr: true,
},
"duplicate code ids": {
src: MsgUnpinCodes{
Authority: goodAddress,
CodeIDs: []uint64{1, 1},
},
expErr: true,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
Expand All @@ -974,6 +1001,14 @@ func TestMsgUnpinCodesValidation(t *testing.T) {
}
}

func genCodeIDs(max int) []uint64 {
r := make([]uint64, max)
for i := 0; i < max; i++ {
r[i] = uint64(i)
}
return r
}

func TestMsgSudoContractValidation(t *testing.T) {
badAddress := "abcd"
// proper address size
Expand Down