Skip to content

Commit

Permalink
add max length and update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanchristo committed Nov 22, 2022
1 parent fd2dbed commit a317ef5
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1467](https://github.com/regen-network/regen-ledger/pull/1467) Add `ClassFee` state validation checks
- [#1467](https://github.com/regen-network/regen-ledger/pull/1467) Add `BasketFee` state validation checks
- [#1484](https://github.com/regen-network/regen-ledger/pull/1484) Add `Msg/UpdateCurator`
- [#1625](https://github.com/regen-network/regen-ledger/pull/1625) Add `reason`/`retirement_reason`

#### Changed

Expand Down
2 changes: 1 addition & 1 deletion proto/regen/ecocredit/marketplace/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ message MsgBuyDirect {
// retiring credits. The reason will be included in EventRetire and is not
// stored in state.
//
// Since Revision 2
// Since Revision 1
string retirement_reason = 7;
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/base/keeper/features/msg_retire.feature
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ Feature: Msg/Retire
"jurisdiction": "US-WA",
"reason": "offsetting electricity consumption"
}
"""
"""
18 changes: 18 additions & 0 deletions x/ecocredit/base/types/v1/features/msg_retire.feature
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ Feature: MsgRetire
When the message is validated
Then expect the error "jurisdiction: expected format <country-code>[-<region-code>[ <postal-code>]]: parse error: invalid request"

Scenario: an error is returned if reason exceeds 512 characters
Given the message
"""
{
"owner": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"credits": [
{
"batch_denom": "C01-001-20200101-20210101-001",
"amount": "100"
}
],
"jurisdiction": "US-WA"
}
"""
And reason with length "513"
When the message is validated
Then expect the error "reason: max length 512: limit exceeded"

Scenario: a valid amino message
Given the message
"""
Expand Down
29 changes: 24 additions & 5 deletions x/ecocredit/base/types/v1/features/msg_send.feature
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Feature: MsgSend
}
"""
When the message is validated
Then expect the error "tradable amount or retired amount required: invalid request"
Then expect the error "credits[0]: tradable amount or retired amount required: invalid request"

Scenario: an error is returned if credits tradable amount is a negative decimal
Given the message
Expand All @@ -173,7 +173,7 @@ Feature: MsgSend
}
"""
When the message is validated
Then expect the error "expected a non-negative decimal, got -100: invalid decimal string"
Then expect the error "credits[0]: expected a non-negative decimal, got -100: invalid decimal string"

Scenario: an error is returned if credits retired amount is a negative decimal
Given the message
Expand All @@ -190,7 +190,7 @@ Feature: MsgSend
}
"""
When the message is validated
Then expect the error "expected a non-negative decimal, got -100: invalid decimal string"
Then expect the error "credits[0]: expected a non-negative decimal, got -100: invalid decimal string"

Scenario: an error is returned if credits retired amount is positive and retirement jurisdiction is empty
Given the message
Expand All @@ -207,7 +207,7 @@ Feature: MsgSend
}
"""
When the message is validated
Then expect the error "retirement jurisdiction: empty string is not allowed: parse error: invalid request"
Then expect the error "credits[0]: retirement jurisdiction: empty string is not allowed: parse error: invalid request"

Scenario: an error is returned if credits retired amount is positive and retirement jurisdiction is not formatted
Given the message
Expand All @@ -225,7 +225,26 @@ Feature: MsgSend
}
"""
When the message is validated
Then expect the error "retirement jurisdiction: expected format <country-code>[-<region-code>[ <postal-code>]]: parse error: invalid request"
Then expect the error "credits[0]: retirement jurisdiction: expected format <country-code>[-<region-code>[ <postal-code>]]: parse error: invalid request"

Scenario: an error is returned if credits retired amount is positive and retirement reason exceeds 512 characters
Given the message
"""
{
"sender": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"recipient": "regen1tnh2q55v8wyygtt9srz5safamzdengsnlm0yy4",
"credits": [
{
"batch_denom": "C01-001-20200101-20210101-001",
"retired_amount": "100",
"retirement_jurisdiction": "US-WA"
}
]
}
"""
And retirement reason with length "513"
When the message is validated
Then expect the error "credits[0]: retirement reason: max length 512: limit exceeded"

Scenario: a valid amino message
Given the message
Expand Down
17 changes: 15 additions & 2 deletions x/ecocredit/base/types/v1/features/types_batch_issuance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Feature: BatchIssuance
When the batch issuance is validated
Then expect the error "retired amount: expected a non-negative decimal, got -100: invalid decimal string"

Scenario: an error is returned if issuance retired amount is positive and jurisdiction is empty
Scenario: an error is returned if issuance retired amount is positive and retirement jurisdiction is empty
Given the batch issuance
"""
{
Expand All @@ -97,7 +97,7 @@ Feature: BatchIssuance
When the batch issuance is validated
Then expect the error "retirement jurisdiction: empty string is not allowed: parse error: invalid request"

Scenario: an error is returned if issuance retired amount is positive and jurisdiction is not formatted
Scenario: an error is returned if issuance retired amount is positive and retirement jurisdiction is not formatted
Given the batch issuance
"""
{
Expand All @@ -108,3 +108,16 @@ Feature: BatchIssuance
"""
When the batch issuance is validated
Then expect the error "retirement jurisdiction: expected format <country-code>[-<region-code>[ <postal-code>]]: parse error: invalid request"

Scenario: an error is returned if issuance retired amount is positive and retirement reason exceeds 512 characters
Given the batch issuance
"""
{
"recipient": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"retired_amount": "100",
"retirement_jurisdiction": "US-WA"
}
"""
And retirement reason with length "513"
When the batch issuance is validated
Then expect the error "retirement reason: max length 512: limit exceeded"
6 changes: 6 additions & 0 deletions x/ecocredit/base/types/v1/msg_retire.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v1
import (
"cosmossdk.io/errors"

"github.com/regen-network/regen-ledger/x/ecocredit"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
Expand Down Expand Up @@ -43,6 +45,10 @@ func (m *MsgRetire) ValidateBasic() error {
return sdkerrors.ErrInvalidRequest.Wrapf("jurisdiction: %s", err)
}

if len(m.Reason) > base.MaxNoteLength {
return ecocredit.ErrMaxLimit.Wrapf("reason: max length %d", base.MaxNoteLength)
}

return nil
}

Expand Down
9 changes: 9 additions & 0 deletions x/ecocredit/base/types/v1/msg_retire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v1
import (
"bytes"
"encoding/json"
"strconv"
"strings"
"testing"

"github.com/gogo/protobuf/jsonpb"
Expand Down Expand Up @@ -31,6 +33,13 @@ func (s *msgRetire) TheMessage(a gocuke.DocString) {
require.NoError(s.t, err)
}

func (s *msgRetire) ReasonWithLength(a string) {
length, err := strconv.ParseInt(a, 10, 64)
require.NoError(s.t, err)

s.msg.Reason = strings.Repeat("x", int(length))
}

func (s *msgRetire) TheMessageIsValidated() {
s.err = s.msg.ValidateBasic()
}
Expand Down
16 changes: 11 additions & 5 deletions x/ecocredit/base/types/v1/msg_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package v1
import (
"fmt"

"cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"

"github.com/regen-network/regen-ledger/types/math"
"github.com/regen-network/regen-ledger/x/ecocredit"
"github.com/regen-network/regen-ledger/x/ecocredit/base"
)

Expand Down Expand Up @@ -46,21 +48,25 @@ func (m *MsgSend) ValidateBasic() error {
}

if credits.TradableAmount == "" && credits.RetiredAmount == "" {
return sdkerrors.ErrInvalidRequest.Wrap("tradable amount or retired amount required")
return sdkerrors.ErrInvalidRequest.Wrapf("%s: tradable amount or retired amount required", creditIndex)
}

if _, err := math.NewNonNegativeDecFromString(credits.TradableAmount); err != nil {
return err
return errors.Wrapf(err, "%s", creditIndex)
}

retiredAmount, err := math.NewNonNegativeDecFromString(credits.RetiredAmount)
if err != nil {
return err
return errors.Wrapf(err, "%s", creditIndex)
}

if !retiredAmount.IsZero() {
if err = base.ValidateJurisdiction(credits.RetirementJurisdiction); err != nil {
return sdkerrors.ErrInvalidRequest.Wrapf("retirement jurisdiction: %s", err)
return sdkerrors.ErrInvalidRequest.Wrapf("%s: retirement jurisdiction: %s", creditIndex, err)
}

if len(credits.RetirementReason) > base.MaxNoteLength {
return ecocredit.ErrMaxLimit.Wrapf("%s: retirement reason: max length %d", creditIndex, base.MaxNoteLength)
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions x/ecocredit/base/types/v1/msg_send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v1
import (
"bytes"
"encoding/json"
"strconv"
"strings"
"testing"

"github.com/gogo/protobuf/jsonpb"
Expand Down Expand Up @@ -31,6 +33,13 @@ func (s *msgSend) TheMessage(a gocuke.DocString) {
require.NoError(s.t, err)
}

func (s *msgSend) RetirementReasonWithLength(a string) {
length, err := strconv.ParseInt(a, 10, 64)
require.NoError(s.t, err)

s.msg.Credits[0].RetirementReason = strings.Repeat("x", int(length))
}

func (s *msgSend) TheMessageIsValidated() {
s.err = s.msg.ValidateBasic()
}
Expand Down
6 changes: 6 additions & 0 deletions x/ecocredit/base/types/v1/types_batch_issuance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v1
import (
"cosmossdk.io/errors"

"github.com/regen-network/regen-ledger/x/ecocredit"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand Down Expand Up @@ -36,6 +38,10 @@ func (i *BatchIssuance) Validate() error {
if err = base.ValidateJurisdiction(i.RetirementJurisdiction); err != nil {
return sdkerrors.ErrInvalidRequest.Wrapf("retirement jurisdiction: %s", err)
}

if len(i.RetirementReason) > base.MaxNoteLength {
return ecocredit.ErrMaxLimit.Wrapf("retirement reason: max length %d", base.MaxNoteLength)
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions x/ecocredit/base/types/v1/types_batch_issuance_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1

import (
"strconv"
"strings"
"testing"

"github.com/gogo/protobuf/jsonpb"
Expand Down Expand Up @@ -28,6 +30,13 @@ func (s *batchIssuance) TheBatchIssuance(a gocuke.DocString) {
require.NoError(s.t, err)
}

func (s *batchIssuance) RetirementReasonWithLength(a string) {
length, err := strconv.ParseInt(a, 10, 64)
require.NoError(s.t, err)

s.issuance.RetirementReason = strings.Repeat("x", int(length))
}

func (s *batchIssuance) TheBatchIssuanceIsValidated() {
s.err = s.issuance.Validate()
}
Expand Down
15 changes: 15 additions & 0 deletions x/ecocredit/basket/types/v1/features/msg_take.feature
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ Feature: MsgTake
When the message is validated
Then expect no error

Scenario: an error is returned if retirement reason exceeds 512 characters
Given the message
"""
{
"owner": "regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw",
"basket_denom": "eco.uC.NCT",
"amount": "100",
"retirement_jurisdiction": "US-WA",
"retire_on_take": true
}
"""
And retirement reason with length "513"
When the message is validated
Then expect the error "retirement reason: max length 512: limit exceeded"

Scenario: a valid amino message
Given the message
"""
Expand Down
5 changes: 5 additions & 0 deletions x/ecocredit/basket/types/v1/msg_take.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/regen-network/regen-ledger/x/ecocredit"

"github.com/regen-network/regen-ledger/x/ecocredit/basket"

Expand Down Expand Up @@ -64,6 +65,10 @@ func (m MsgTake) ValidateBasic() error {
return sdkerrors.ErrInvalidRequest.Wrapf("retirement jurisdiction: %s", err)
}
}

if len(m.RetirementReason) > base.MaxNoteLength {
return ecocredit.ErrMaxLimit.Wrapf("retirement reason: max length %d", base.MaxNoteLength)
}
}

return nil
Expand Down
9 changes: 9 additions & 0 deletions x/ecocredit/basket/types/v1/msg_take_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v1
import (
"bytes"
"encoding/json"
"strconv"
"strings"
"testing"

"github.com/gogo/protobuf/jsonpb"
Expand Down Expand Up @@ -31,6 +33,13 @@ func (s *msgTakeSuite) TheMessage(a gocuke.DocString) {
require.NoError(s.t, err)
}

func (s *msgTakeSuite) RetirementReasonWithLength(a string) {
length, err := strconv.ParseInt(a, 10, 64)
require.NoError(s.t, err)

s.msg.RetirementReason = strings.Repeat("x", int(length))
}

func (s *msgTakeSuite) TheMessageIsValidated() {
s.err = s.msg.ValidateBasic()
}
Expand Down
22 changes: 22 additions & 0 deletions x/ecocredit/marketplace/types/v1/features/msg_buy_direct.feature
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,28 @@ Feature: MsgBuyDirect
When the message is validated
Then expect the error "orders[0]: retirement jurisdiction: expected format <country-code>[-<region-code>[ <postal-code>]]: parse error: invalid request"

Scenario: an error is returned if disable auto-retire is true and retirement reason exceeds 512 characters
Given the message
"""
{
"buyer": "regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw",
"orders": [
{
"sell_order_id": 1,
"quantity": "100",
"bid_price": {
"denom": "regen",
"amount": "100"
},
"retirement_jurisdiction": "US-WA"
}
]
}
"""
And retirement reason with length "513"
When the message is validated
Then expect the error "orders[0]: retirement reason: max length 512: limit exceeded"

Scenario: a valid amino message
Given the message
"""
Expand Down
Loading

0 comments on commit a317ef5

Please sign in to comment.