Skip to content

Commit

Permalink
Ensure some contraints and limits on pin/unpin code ids
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Sep 13, 2023
1 parent 09b5008 commit 596488a
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 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)

Check warning on line 503 in x/wasm/types/tx.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/tx.go#L502-L503

Added lines #L502 - L503 were not covered by tests
}
if hasDuplicates(codeIDs) {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "duplicate code ids")

Check warning on line 506 in x/wasm/types/tx.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/tx.go#L506

Added line #L506 was not covered by tests
}
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)
if _, err := sdk.AccAddressFromBech32(addr); err != nil {
return errorsmod.Wrap(err, "addresses")
}
Expand Down Expand Up @@ -739,3 +750,14 @@ 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

Check warning on line 759 in x/wasm/types/tx.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/tx.go#L759

Added line #L759 was not covered by tests
}
}
return false
}

0 comments on commit 596488a

Please sign in to comment.