Skip to content

Commit

Permalink
feat: add max-metadata check for credit class and credit batch (#540)
Browse files Browse the repository at this point in the history
* feat: add max-length check

* feat: add tests

* Update /x/ecocredit/msgs.go

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
aleem1314 and ryanchristo authored Sep 16, 2021
1 parent cbc060a commit 744489e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion x/ecocredit/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package ecocredit
import sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

var (
ErrParseFailure = sdkerrors.Register(ModuleName, 2, "parse error")
ErrParseFailure = sdkerrors.Register(ModuleName, 2, "parse error")
ErrMaxLimit = sdkerrors.Register(ModuleName, 3, "limit exceeded")
)
13 changes: 13 additions & 0 deletions x/ecocredit/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ var (
&MsgRetire{}, &MsgCancel{}
)

// MaxMetadataLength defines the max length of the metadata bytes field
// for the credit-class & credit-batch.
// TODO: This could be used as params once x/params is upgraded to use protobuf
const MaxMetadataLength = 256

// Route Implements LegacyMsg.
func (m MsgCreateClass) Route() string { return sdk.MsgTypeURL(&m) }

Expand All @@ -28,6 +33,10 @@ func (m MsgCreateClass) GetSignBytes() []byte {

func (m *MsgCreateClass) ValidateBasic() error {

if len(m.Metadata) > MaxMetadataLength {
return ErrMaxLimit.Wrap("credit class metadata")
}

if _, err := sdk.AccAddressFromBech32(m.Admin); err != nil {
return sdkerrors.Wrap(err, "admin")
}
Expand Down Expand Up @@ -67,6 +76,10 @@ func (m MsgCreateBatch) GetSignBytes() []byte {

func (m *MsgCreateBatch) ValidateBasic() error {

if len(m.Metadata) > MaxMetadataLength {
return ErrMaxLimit.Wrap("credit batch metadata")
}

if _, err := sdk.AccAddressFromBech32(m.Issuer); err != nil {
return sdkerrors.Wrap(err, "issuer")
}
Expand Down
27 changes: 27 additions & 0 deletions x/ecocredit/msgs_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package ecocredit

import (
"math/rand"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/testutil/testdata"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/stretchr/testify/require"
)

var (
s = rand.NewSource(1)
r = rand.New(s)
)

func TestMsgCreateClass(t *testing.T) {
_, _, addr1 := testdata.KeyTestPubAddr()
_, _, addr2 := testdata.KeyTestPubAddr()
Expand Down Expand Up @@ -66,6 +73,15 @@ func TestMsgCreateClass(t *testing.T) {
},
expErr: true,
},
"invalid metadata maxlength is exceeded": {
src: MsgCreateClass{
Admin: addr1.String(),
CreditTypeName: "carbon",
Issuers: []string{addr1.String(), addr2.String()},
Metadata: []byte(simtypes.RandStringOfLength(r, 288)),
},
expErr: true,
},
}

for msg, test := range tests {
Expand Down Expand Up @@ -330,6 +346,17 @@ func TestMsgCreateBatch(t *testing.T) {
},
expErr: true,
},
"invalid metadata maxlength is exceeded": {
src: MsgCreateBatch{
Issuer: addr1.String(),
ClassId: "C01",
StartDate: &startDate,
EndDate: &endDate,
ProjectLocation: "AB-CDE FG1 345",
Metadata: []byte(simtypes.RandStringOfLength(r, 288)),
},
expErr: true,
},
}

for msg, test := range tests {
Expand Down

0 comments on commit 744489e

Please sign in to comment.