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

imp: represent unlimited approvals with MaxUint256 value #3454

Merged
merged 31 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
060cdf6
imp: represent unlimited approvals with a nil value
Vvaradinov Apr 13, 2023
201ea0f
CHANGELOG
Vvaradinov Apr 13, 2023
10a8df5
Update CHANGELOG.md
crodriguezvega Apr 14, 2023
98d9d3c
fix: updated unlimited spending limit to be represented with the MaxI…
Vvaradinov Apr 25, 2023
01fa2d5
Merge branch 'Vvaradinov/ics20-unlimited-authz' of https://github.com…
Vvaradinov Apr 25, 2023
87daf70
Merge branch 'main' into Vvaradinov/ics20-unlimited-authz
Vvaradinov Apr 25, 2023
bdb9fc6
Update modules/apps/transfer/types/transfer_authorization.go
Vvaradinov Apr 26, 2023
05719c1
Update CHANGELOG.md
Vvaradinov Apr 26, 2023
7dc4640
fix: lint
Vvaradinov Apr 26, 2023
b33dba6
Merge branch 'main' into Vvaradinov/ics20-unlimited-authz
fedekunze Apr 30, 2023
633c254
Update modules/apps/transfer/types/transfer_authorization.go
fedekunze Apr 30, 2023
f80ada0
fix: update failing test and add test case suggested in review
Vvaradinov May 4, 2023
ff05f63
fix: moved isAllowedAddress check before coin loop
Vvaradinov May 4, 2023
005a537
fix: use maxUint256 case so it aligns with what's being passed from t…
Vvaradinov May 8, 2023
21cd1f6
refactor transfer authz to remove coins iteration in favour of explic…
damiannolan May 9, 2023
b3abb10
make format
damiannolan May 9, 2023
edc031b
Update modules/apps/transfer/types/transfer_authorization.go
Vvaradinov May 9, 2023
a55efb7
Merge branch 'main' into Vvaradinov/ics20-unlimited-authz
damiannolan May 9, 2023
05cfa35
fix: add the Authorization to Updated.
Vvaradinov May 9, 2023
e8b2fed
Merge branch 'Vvaradinov/ics20-unlimited-authz' of https://github.com…
Vvaradinov May 9, 2023
7914b62
moving allowlist check to before spend limit logic
damiannolan May 9, 2023
1d4260d
Apply suggestions from code review
fedekunze May 10, 2023
c85fbb3
fix: add comment to spend limit check
Vvaradinov May 10, 2023
715a082
Merge branch 'main' into Vvaradinov/ics20-unlimited-authz
Vvaradinov May 11, 2023
30293f6
review feedback
crodriguezvega May 12, 2023
b7f81c6
Merge pull request #1 from cosmos/Vvaradinov/ics20-unlimited-authz
Vvaradinov May 15, 2023
464bccd
Merge branch 'main' into Vvaradinov/ics20-unlimited-authz
Vvaradinov May 15, 2023
4eb96d3
Update modules/apps/transfer/types/transfer_authorization.go
Vvaradinov May 15, 2023
cc9cf3f
Update docs/apps/transfer/authorizations.md
damiannolan May 15, 2023
3552971
Merge branch 'main' into Vvaradinov/ics20-unlimited-authz
crodriguezvega May 15, 2023
aab8b18
fix: changed to new sentinel value name
Vvaradinov May 15, 2023
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
### State Machine Breaking

### Improvements

* (tests) [\#3138](https://github.com/cosmos/ibc-go/pull/3138) Support benchmarks and fuzz tests through `testing.TB`.
* (ics20) [\#3454](https://github.com/cosmos/ibc-go/pull/3454) Represent authz unlimited spending limit as `math.MaxInt64` value.
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

### Features

Expand Down
52 changes: 31 additions & 21 deletions modules/apps/transfer/types/transfer_authorization.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package types

import (
"math"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"

ibcerrors "github.com/cosmos/ibc-go/v7/internal/errors"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
Expand All @@ -31,28 +32,37 @@ func (a TransferAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.Accep
return authz.AcceptResponse{}, errorsmod.Wrap(ibcerrors.ErrInvalidType, "type mismatch")
}

for index, allocation := range a.Allocations {
if !(allocation.SourceChannel == msgTransfer.SourceChannel && allocation.SourcePort == msgTransfer.SourcePort) {
continue
}
var (
limitLeft = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(math.MaxInt64)))
isNegative bool
)

limitLeft, isNegative := allocation.SpendLimit.SafeSub(msgTransfer.Token)
if isNegative {
return authz.AcceptResponse{}, errorsmod.Wrapf(ibcerrors.ErrInsufficientFunds, "requested amount is more than spend limit")
}

if !isAllowedAddress(ctx, msgTransfer.Receiver, allocation.AllowList) {
return authz.AcceptResponse{}, errorsmod.Wrap(ibcerrors.ErrInvalidAddress, "not allowed address for transfer")
}

if limitLeft.IsZero() {
a.Allocations = append(a.Allocations[:index], a.Allocations[index+1:]...)
if len(a.Allocations) == 0 {
return authz.AcceptResponse{Accept: true, Delete: true}, nil
for index, allocation := range a.Allocations {
if allocation.SourceChannel == msgTransfer.SourceChannel && allocation.SourcePort == msgTransfer.SourcePort {
for _, coin := range allocation.SpendLimit {
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
if coin.Amount.Int64() == math.MaxInt64 {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
continue
}

limitLeft, isNegative = allocation.SpendLimit.SafeSub(msgTransfer.Token)
if isNegative {
return authz.AcceptResponse{}, errorsmod.Wrapf(ibcerrors.ErrInsufficientFunds, "requested amount is more than spend limit")
}

if !isAllowedAddress(ctx, msgTransfer.Receiver, allocation.AllowList) {
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
return authz.AcceptResponse{}, errorsmod.Wrap(ibcerrors.ErrInvalidAddress, "not allowed address for transfer")
}

if limitLeft.IsZero() {
a.Allocations = append(a.Allocations[:index], a.Allocations[index+1:]...)
if len(a.Allocations) == 0 {
return authz.AcceptResponse{Accept: true, Delete: true}, nil
}
return authz.AcceptResponse{Accept: true, Delete: false, Updated: &TransferAuthorization{
Allocations: a.Allocations,
}}, nil
}
}
return authz.AcceptResponse{Accept: true, Delete: false, Updated: &TransferAuthorization{
Allocations: a.Allocations,
}}, nil
}
a.Allocations[index] = Allocation{
SourcePort: allocation.SourcePort,
Expand Down
29 changes: 28 additions & 1 deletion modules/apps/transfer/types/transfer_authorization_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types_test

import (
"math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: import formatting

"github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/cosmos/ibc-go/v7/testing/mock"
Expand Down Expand Up @@ -49,6 +50,25 @@ func (suite *TypesTestSuite) TestTransferAuthorizationAccept() {
suite.Require().True(isEqual)
},
},
{
"success: with unlimited spending limit MaxInt64",
func() {
transferAuthz.Allocations[0].SpendLimit = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(math.MaxInt64)))
msgTransfer.Token = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(math.MaxInt64))
},
func(res authz.AcceptResponse, err error) {
suite.Require().NoError(err)

suite.Require().True(res.Accept)
suite.Require().False(res.Delete)

updatedAuthz, ok := res.Updated.(*types.TransferAuthorization)
suite.Require().True(ok)

isEqual := updatedAuthz.Allocations[0].SpendLimit.IsEqual(sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(math.MaxInt64))))
suite.Require().True(isEqual)
},
},
{
"success: with empty allow list",
func() {
Expand Down Expand Up @@ -190,6 +210,13 @@ func (suite *TypesTestSuite) TestTransferAuthorizationValidateBasic() {
},
true,
},
{
"success: with max Int allocation spend limit",
func() {
transferAuthz.Allocations[0].SpendLimit = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(math.MaxInt64)))
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
},
true,
},
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
{
"empty allocations",
func() {
Expand Down