Skip to content

Commit

Permalink
fix(x/ecocredit): don't allow self transfer in MsgSend (#1674)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleem1314 authored Dec 9, 2022
1 parent 605edb1 commit 11cb236
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1480](https://github.com/regen-network/regen-ledger/pull/1480) Fix amino codec registration for all messages
- [#1583](https://github.com/regen-network/regen-ledger/pull/1583) Return gRPC response code in base query response
- [#1588](https://github.com/regen-network/regen-ledger/pull/1588) Return gRPC response code in basket and marketplace query response
- [#1674](https://github.com/regen-network/regen-ledger/pull/1674) Update `MsgSend` to return an error if sender and recipient are same

#### Removed

Expand Down
5 changes: 5 additions & 0 deletions x/ecocredit/base/keeper/msg_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ import (

// Send sends credits to a recipient.
// Send also retires credits if the amount to retire is specified in the request.
// NOTE: This method will return an error if both sender and recipient are same.
func (k Keeper) Send(ctx context.Context, req *types.MsgSend) (*types.MsgSendResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
sender, _ := sdk.AccAddressFromBech32(req.Sender)
recipient, _ := sdk.AccAddressFromBech32(req.Recipient)

if sender.Equals(recipient) {
return nil, sdkerrors.ErrInvalidRequest.Wrap("sender and recipient cannot be the same")
}

for _, credit := range req.Credits {
err := k.sendEcocredits(sdkCtx, credit, recipient, sender)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions x/ecocredit/base/types/v1/features/msg_send.feature
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,23 @@ Feature: MsgSend
}
}
"""

Scenario: an error is returned if sender and recipient are the same
Given the message
"""
{
"sender":"regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"recipient":"regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"credits": [
{
"batch_denom": "C01-001-20200101-20210101-001",
"tradable_amount": "100",
"retired_amount": "100",
"retirement_jurisdiction": "US-WA",
"retirement_reason": "offsetting electricity consumption"
}
]
}
"""
When the message is validated
Then expect the error "sender and recipient cannot be the same: invalid request"
4 changes: 4 additions & 0 deletions x/ecocredit/base/types/v1/msg_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (m *MsgSend) ValidateBasic() error {
return sdkerrors.ErrInvalidAddress.Wrapf("recipient: %s", err)
}

if m.Sender == m.Recipient {
return sdkerrors.ErrInvalidRequest.Wrap("sender and recipient cannot be the same")
}

if len(m.Credits) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("credits cannot be empty")
}
Expand Down

0 comments on commit 11cb236

Please sign in to comment.