From 3258537793f1e3ca82825e14f4d99c56dba55058 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 9 Dec 2022 10:24:24 +0530 Subject: [PATCH 1/2] fix: don't allow self transfer in msg-send --- x/ecocredit/base/keeper/msg_send.go | 5 +++++ .../base/types/v1/features/msg_send.feature | 20 +++++++++++++++++++ x/ecocredit/base/types/v1/msg_send.go | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/x/ecocredit/base/keeper/msg_send.go b/x/ecocredit/base/keeper/msg_send.go index a1690c1654..2a9468aaee 100644 --- a/x/ecocredit/base/keeper/msg_send.go +++ b/x/ecocredit/base/keeper/msg_send.go @@ -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 { diff --git a/x/ecocredit/base/types/v1/features/msg_send.feature b/x/ecocredit/base/types/v1/features/msg_send.feature index 4a2aa6df7d..63bf82608b 100644 --- a/x/ecocredit/base/types/v1/features/msg_send.feature +++ b/x/ecocredit/base/types/v1/features/msg_send.feature @@ -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" diff --git a/x/ecocredit/base/types/v1/msg_send.go b/x/ecocredit/base/types/v1/msg_send.go index af40501128..e7c9bb01da 100644 --- a/x/ecocredit/base/types/v1/msg_send.go +++ b/x/ecocredit/base/types/v1/msg_send.go @@ -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") } From 2cb15136764263e89543aac6dfa65aa1741fa681 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 9 Dec 2022 15:18:55 +0530 Subject: [PATCH 2/2] chore: add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e601dc6426..362ac12364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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