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

Make isAllowedAddress private #2985

Merged
merged 3 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 14 additions & 11 deletions modules/apps/transfer/types/transfer_authz.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/authz"

host "github.com/cosmos/ibc-go/v6/modules/core/24-host"

"golang.org/x/exp/slices"
Expand Down Expand Up @@ -36,16 +37,6 @@ func (a TransferAuthorization) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgTransfer{})
}

func IsAllowedAddress(ctx sdk.Context, receiver string, allowedAddrs []string) bool {
for _, addr := range allowedAddrs {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "transfer authorization")
if addr == receiver {
return true
}
}
return false
}

// Accept implements Authorization.Accept.
func (a TransferAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
msgTransfer, ok := msg.(*MsgTransfer)
Expand All @@ -60,7 +51,7 @@ func (a TransferAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.Accep
return authz.AcceptResponse{}, sdkerrors.ErrInsufficientFunds.Wrapf("requested amount is more than spend limit")
}

if !IsAllowedAddress(ctx, msgTransfer.Receiver, allocation.AllowedAddresses) {
if !isAllowedAddress(ctx, msgTransfer.Receiver, allocation.AllowedAddresses) {
return authz.AcceptResponse{}, sdkerrors.ErrInvalidAddress.Wrapf("not allowed address for transfer")
}

Expand Down Expand Up @@ -114,3 +105,15 @@ func (a TransferAuthorization) ValidateBasic() error {
}
return nil
}

// isAllowedAddress returns a boolean indicating if the receiver address is valid for transfer.
// gasCostPerIteration gas is consumed for each iteration.
func isAllowedAddress(ctx sdk.Context, receiver string, allowedAddrs []string) bool {
for _, addr := range allowedAddrs {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "transfer authorization")
if addr == receiver {
return true
}
}
return false
}
Comment on lines +109 to +117
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this to the bottom of the file as it was in between multiple public methods.

10 changes: 5 additions & 5 deletions modules/apps/transfer/types/transfer_authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestTransferAuthorization(t *testing.T) {
t.Log("verify authorization returns valid method name")
require.Equal(t, authorization.MsgTypeURL(), "/ibc.applications.transfer.v1.MsgTransfer")
require.NoError(t, authorization.ValidateBasic())
transfer := NewMsgTransfer(sourcePort, sourceChannel, coin1000, fromAddr.String(), toAddr.String(), timeoutHeight, 0)
transfer := NewMsgTransfer(sourcePort, sourceChannel, coin1000, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "")
require.NoError(t, authorization.ValidateBasic())

t.Log("verify updated authorization returns nil")
Expand All @@ -44,7 +44,7 @@ func TestTransferAuthorization(t *testing.T) {
authorization = NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}})
require.Equal(t, authorization.MsgTypeURL(), "/ibc.applications.transfer.v1.MsgTransfer")
require.NoError(t, authorization.ValidateBasic())
transfer = NewMsgTransfer(sourcePort, sourceChannel, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0)
transfer = NewMsgTransfer(sourcePort, sourceChannel, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "")
require.NoError(t, authorization.ValidateBasic())
resp, err = authorization.Accept(ctx, transfer)
require.NoError(t, err)
Expand All @@ -61,7 +61,7 @@ func TestTransferAuthorization(t *testing.T) {

t.Log("expect error when spend limit for specific port and channel is not set")
authorization = NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}})
transfer = NewMsgTransfer(sourcePort2, sourceChannel2, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0)
transfer = NewMsgTransfer(sourcePort2, sourceChannel2, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "")
_, err = authorization.Accept(ctx, transfer)
require.Error(t, err)

Expand All @@ -71,7 +71,7 @@ func TestTransferAuthorization(t *testing.T) {
[]string{sourceChannel, sourceChannel2},
[]sdk.Coins{coins1000, coins1000},
[][]string{{toAddr.String()}, {toAddr.String()}})
transfer = NewMsgTransfer(sourcePort, sourceChannel, coin1000, fromAddr.String(), toAddr.String(), timeoutHeight, 0)
transfer = NewMsgTransfer(sourcePort, sourceChannel, coin1000, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "")
resp, err = authorization.Accept(ctx, transfer)
require.NoError(t, err)
require.NotNil(t, resp.Updated)
Expand All @@ -80,7 +80,7 @@ func TestTransferAuthorization(t *testing.T) {

t.Log("expect error when transferring to not allowed address")
authorization = NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{fromAddr.String()}})
transfer = NewMsgTransfer(sourcePort, sourceChannel, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0)
transfer = NewMsgTransfer(sourcePort, sourceChannel, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "")
_, err = authorization.Accept(ctx, transfer)
require.Error(t, err)
}