Skip to content

Commit b72d182

Browse files
committed
Ensure some contraints and limits on pin/unpin code ids
1 parent 09b5008 commit b72d182

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

x/wasm/types/tx.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,25 @@ func (msg MsgPinCodes) GetSignBytes() []byte {
485485
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
486486
}
487487

488+
const maxCodeIDTotal = 50
489+
488490
func (msg MsgPinCodes) ValidateBasic() error {
489491
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
490492
return errorsmod.Wrap(err, "authority")
491493
}
492-
if len(msg.CodeIDs) == 0 {
494+
return validateCodeIDs(msg.CodeIDs)
495+
}
496+
497+
// ensure not empty, not duplicates and not exceeding max number
498+
func validateCodeIDs(codeIDs []uint64) error {
499+
switch n := len(codeIDs); {
500+
case n == 0:
493501
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty code ids")
502+
case n > maxCodeIDTotal:
503+
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "total number of code ids is greater than %d", maxCodeIDTotal)
504+
}
505+
if hasDuplicates(codeIDs) {
506+
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "duplicate code ids")
494507
}
495508
return nil
496509
}
@@ -519,10 +532,7 @@ func (msg MsgUnpinCodes) ValidateBasic() error {
519532
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
520533
return errorsmod.Wrap(err, "authority")
521534
}
522-
if len(msg.CodeIDs) == 0 {
523-
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty code ids")
524-
}
525-
return nil
535+
return validateCodeIDs(msg.CodeIDs)
526536
}
527537

528538
func (msg MsgSudoContract) Route() string {
@@ -684,6 +694,7 @@ func (msg MsgRemoveCodeUploadParamsAddresses) ValidateBasic() error {
684694
func checkDuplicatedAddresses(addresses []string) error {
685695
index := map[string]struct{}{}
686696
for _, addr := range addresses {
697+
addr = strings.ToUpper(addr)
687698
if _, err := sdk.AccAddressFromBech32(addr); err != nil {
688699
return errorsmod.Wrap(err, "addresses")
689700
}
@@ -739,3 +750,15 @@ func (msg MsgStoreAndMigrateContract) ValidateBasic() error {
739750
}
740751
return nil
741752
}
753+
754+
// returns true when slice contains any duplicates
755+
func hasDuplicates[T comparable](s []T) bool {
756+
index := make(map[T]struct{}, len(s))
757+
for _, v := range s {
758+
if _, exists := index[v]; exists {
759+
return true
760+
}
761+
index[v] = struct{}{}
762+
}
763+
return false
764+
}

x/wasm/types/tx_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,6 @@ func TestMsgRemoveCodeUploadParamsAddressesValidation(t *testing.T) {
885885
func TestMsgPinCodesValidation(t *testing.T) {
886886
// proper address size
887887
goodAddress := sdk.AccAddress(make([]byte, 20)).String()
888-
889888
specs := map[string]struct {
890889
src MsgPinCodes
891890
expErr bool
@@ -915,6 +914,20 @@ func TestMsgPinCodesValidation(t *testing.T) {
915914
},
916915
expErr: true,
917916
},
917+
"exceeds max code ids": {
918+
src: MsgPinCodes{
919+
Authority: goodAddress,
920+
CodeIDs: genCodeIDs(51),
921+
},
922+
expErr: true,
923+
},
924+
"duplicate code ids": {
925+
src: MsgPinCodes{
926+
Authority: goodAddress,
927+
CodeIDs: []uint64{1, 1},
928+
},
929+
expErr: true,
930+
},
918931
}
919932
for msg, spec := range specs {
920933
t.Run(msg, func(t *testing.T) {
@@ -961,6 +974,20 @@ func TestMsgUnpinCodesValidation(t *testing.T) {
961974
},
962975
expErr: true,
963976
},
977+
"exceeds max code ids": {
978+
src: MsgUnpinCodes{
979+
Authority: goodAddress,
980+
CodeIDs: genCodeIDs(51),
981+
},
982+
expErr: true,
983+
},
984+
"duplicate code ids": {
985+
src: MsgUnpinCodes{
986+
Authority: goodAddress,
987+
CodeIDs: []uint64{1, 1},
988+
},
989+
expErr: true,
990+
},
964991
}
965992
for msg, spec := range specs {
966993
t.Run(msg, func(t *testing.T) {
@@ -974,6 +1001,14 @@ func TestMsgUnpinCodesValidation(t *testing.T) {
9741001
}
9751002
}
9761003

1004+
func genCodeIDs(max int) []uint64 {
1005+
r := make([]uint64, max)
1006+
for i := 0; i < max; i++ {
1007+
r[i] = uint64(i)
1008+
}
1009+
return r
1010+
}
1011+
9771012
func TestMsgSudoContractValidation(t *testing.T) {
9781013
badAddress := "abcd"
9791014
// proper address size

0 commit comments

Comments
 (0)