Skip to content

Commit

Permalink
feat: add ValidateDenom for MsgSend, MsgRetire, and MsgCancel (
Browse files Browse the repository at this point in the history
…#554)

* feat: add ValidateDenom to ValidateBasic

* chore: address review changes

Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
Co-authored-by: Cory <cjlevinson@gmail.com>
  • Loading branch information
3 people authored Sep 29, 2021
1 parent f7c2cb8 commit 6d17f39
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion x/ecocredit/client/testsuite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (s *IntegrationTestSuite) TestQueryBatchInfo() {
name: "malformed batch denom",
args: []string{"abcde"},
expectErr: true,
expectedErrMsg: "denomination didn't match the format",
expectedErrMsg: "invalid denom",
},
{
name: "non-existent credit batch",
Expand Down
13 changes: 6 additions & 7 deletions x/ecocredit/client/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testsuite
import (
"encoding/base64"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand All @@ -17,7 +19,6 @@ import (
"github.com/regen-network/regen-ledger/x/ecocredit/client"
"github.com/stretchr/testify/suite"
tmcli "github.com/tendermint/tendermint/libs/cli"
"strings"
)

type IntegrationTestSuite struct {
Expand Down Expand Up @@ -794,9 +795,8 @@ func (s *IntegrationTestSuite) TestTxSend() {
},
s.commonTxFlags()...,
),
expectErr: true,
errInTxResponse: true,
expectedErrMsg: "abcde is not a valid credit batch denom",
expectErr: true,
expectedErrMsg: "invalid denom",
},
{
name: "invalid tradable amount",
Expand Down Expand Up @@ -946,9 +946,8 @@ func (s *IntegrationTestSuite) TestTxRetire() {
},
s.commonTxFlags()...,
),
expectErr: true,
errInTxResponse: true,
expectedErrMsg: "abcde is not a valid credit batch denom",
expectErr: true,
expectedErrMsg: "invalid denom",
},
{
name: "invalid amount",
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func ValidateClassID(classId string) error {
func ValidateDenom(denom string) error {
matches := reFullBatchDenom.FindStringSubmatch(denom)
if matches == nil {
return ErrParseFailure.Wrapf("denomination didn't match the format: expected A00-00000000-00000000-000, got %s", denom)
return ErrParseFailure.Wrap("invalid denom. Valid denom format is: A00-00000000-00000000-000")
}
return nil
}
13 changes: 7 additions & 6 deletions x/ecocredit/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (m *MsgSend) ValidateBasic() error {
}

for _, credit := range m.Credits {
if credit.BatchDenom == "" {
return sdkerrors.ErrInvalidRequest.Wrap("batch denom should not be empty")
if err := ValidateDenom(credit.BatchDenom); err != nil {
return err
}

if _, err := math.NewNonNegativeDecFromString(credit.TradableAmount); err != nil {
Expand Down Expand Up @@ -217,9 +217,10 @@ func (m *MsgRetire) ValidateBasic() error {
}

for _, credit := range m.Credits {
if credit.BatchDenom == "" {
return sdkerrors.ErrInvalidRequest.Wrap("batch denom should not be empty")
if err := ValidateDenom(credit.BatchDenom); err != nil {
return err
}

if _, err := math.NewPositiveDecFromString(credit.Amount); err != nil {
return err
}
Expand Down Expand Up @@ -261,8 +262,8 @@ func (m *MsgCancel) ValidateBasic() error {
}

for _, credit := range m.Credits {
if credit.BatchDenom == "" {
return sdkerrors.ErrInvalidRequest.Wrap("batch denom should not be empty")
if err := ValidateDenom(credit.BatchDenom); err != nil {
return err
}

if _, err := math.NewPositiveDecFromString(credit.Amount); err != nil {
Expand Down
28 changes: 14 additions & 14 deletions x/ecocredit/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func TestMsgSend(t *testing.T) {
Recipient: addr2.String(),
Credits: []*MsgSend_SendCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
TradableAmount: "10",
RetiredAmount: "10",
RetirementLocation: "ST-UVW XY Z12",
Expand Down Expand Up @@ -505,7 +505,7 @@ func TestMsgSend(t *testing.T) {
Recipient: addr2.String(),
Credits: []*MsgSend_SendCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
TradableAmount: "10",
RetiredAmount: "0",
},
Expand Down Expand Up @@ -567,7 +567,7 @@ func TestMsgRetire(t *testing.T) {
Holder: addr1.String(),
Credits: []*MsgRetire_RetireCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "10",
},
},
Expand All @@ -579,7 +579,7 @@ func TestMsgRetire(t *testing.T) {
src: MsgRetire{
Credits: []*MsgRetire_RetireCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "10",
},
},
Expand All @@ -592,7 +592,7 @@ func TestMsgRetire(t *testing.T) {
Holder: "wrongHolder",
Credits: []*MsgRetire_RetireCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "10",
},
},
Expand Down Expand Up @@ -624,7 +624,7 @@ func TestMsgRetire(t *testing.T) {
Holder: addr1.String(),
Credits: []*MsgRetire_RetireCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
},
},
Location: "AB-CDE FG1 345",
Expand All @@ -636,7 +636,7 @@ func TestMsgRetire(t *testing.T) {
Holder: addr1.String(),
Credits: []*MsgRetire_RetireCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "abc",
},
},
Expand All @@ -649,7 +649,7 @@ func TestMsgRetire(t *testing.T) {
Holder: addr1.String(),
Credits: []*MsgRetire_RetireCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "10",
},
},
Expand All @@ -661,7 +661,7 @@ func TestMsgRetire(t *testing.T) {
Holder: addr1.String(),
Credits: []*MsgRetire_RetireCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "10",
},
},
Expand Down Expand Up @@ -695,7 +695,7 @@ func TestMsgCancel(t *testing.T) {
Holder: addr1.String(),
Credits: []*MsgCancel_CancelCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "10",
},
},
Expand All @@ -706,7 +706,7 @@ func TestMsgCancel(t *testing.T) {
src: MsgCancel{
Credits: []*MsgCancel_CancelCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "10",
},
},
Expand All @@ -718,7 +718,7 @@ func TestMsgCancel(t *testing.T) {
Holder: "wrongHolder",
Credits: []*MsgCancel_CancelCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "10",
},
},
Expand Down Expand Up @@ -747,7 +747,7 @@ func TestMsgCancel(t *testing.T) {
Holder: addr1.String(),
Credits: []*MsgCancel_CancelCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
},
},
},
Expand All @@ -758,7 +758,7 @@ func TestMsgCancel(t *testing.T) {
Holder: addr1.String(),
Credits: []*MsgCancel_CancelCredits{
{
BatchDenom: "some_denom",
BatchDenom: "A00-00000000-00000000-000",
Amount: "abc",
},
},
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/server/testsuite/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *IntegrationTestSuite) TestQueryBatchInfo() {
"empty batch denom",
&ecocredit.QueryBatchInfoRequest{},
true,
"denomination didn't match the format",
"invalid denom",
},
{
"batch not found",
Expand Down

0 comments on commit 6d17f39

Please sign in to comment.