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

feat: add max-metadata check for credit class and credit batch #540

Merged
merged 5 commits into from
Sep 16, 2021
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
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