Skip to content

Commit

Permalink
E2E Test for spend limit and invalid addresses (#3000)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored Jan 18, 2023
1 parent b4bd0a7 commit ee9db57
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
138 changes: 138 additions & 0 deletions e2e/tests/transfer/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
test "github.com/strangelove-ventures/ibctest/v6/testutil"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -177,6 +178,143 @@ func (suite *AuthzTransferTestSuite) TestAuthz_MsgTransfer_Succeeds() {
})
}

func (suite *AuthzTransferTestSuite) TestAuthz_InvalidTransferAuthorizations() {
t := suite.T()
ctx := context.TODO()

relayer, channelA := suite.SetupChainsRelayerAndChannel(ctx, transferChannelOptions())
chainA, chainB := suite.GetChains()

chainADenom := chainA.Config().Denom

granterWallet := suite.CreateUserOnChainA(ctx, testvalues.StartingTokenAmount)
granterAddress := granterWallet.Bech32Address(chainA.Config().Bech32Prefix)

granteeWallet := suite.CreateUserOnChainA(ctx, testvalues.StartingTokenAmount)
granteeAddress := granteeWallet.Bech32Address(chainA.Config().Bech32Prefix)

receiverWallet := suite.CreateUserOnChainB(ctx, testvalues.StartingTokenAmount)
receiverWalletAddress := receiverWallet.Bech32Address(chainB.Config().Bech32Prefix)

t.Run("start relayer", func(t *testing.T) {
suite.StartRelayer(relayer)
})

const spendLimit = 1000

t.Run("broadcast MsgGrant", func(t *testing.T) {
transferAuth := transfertypes.TransferAuthorization{
Allocations: []transfertypes.Allocation{
{
SourcePort: channelA.PortID,
SourceChannel: channelA.ChannelID,
SpendLimit: sdk.NewCoins(sdk.NewCoin(chainADenom, sdk.NewInt(spendLimit))),
AllowList: []string{receiverWalletAddress},
},
},
}

authAny, err := codectypes.NewAnyWithValue(&transferAuth)
suite.Require().NoError(err)

msgGrant := &authz.MsgGrant{
Granter: granterAddress,
Grantee: granteeAddress,
Grant: authz.Grant{
Authorization: authAny,
// no expiration
Expiration: nil,
},
}

resp, err := suite.BroadcastMessages(context.TODO(), chainA, granterWallet, msgGrant)
suite.AssertValidTxResponse(resp)
suite.Require().NoError(err)
})

t.Run("exceed spend limit", func(t *testing.T) {
const invalidSpendAmount = spendLimit + 1

t.Run("broadcast MsgExec for ibc MsgTransfer", func(t *testing.T) {
transferMsg := transfertypes.MsgTransfer{
SourcePort: channelA.PortID,
SourceChannel: channelA.ChannelID,
Token: sdk.Coin{Denom: chainADenom, Amount: sdk.NewInt(invalidSpendAmount)},
Sender: granterAddress,
Receiver: receiverWalletAddress,
TimeoutHeight: suite.GetTimeoutHeight(ctx, chainB),
}

transferAny, err := codectypes.NewAnyWithValue(&transferMsg)
suite.Require().NoError(err)

msgExec := &authz.MsgExec{
Grantee: granteeAddress,
Msgs: []*codectypes.Any{transferAny},
}

resp, err := suite.BroadcastMessages(context.TODO(), chainA, granteeWallet, msgExec)
suite.Require().NotEqual(0, resp.Code)
suite.Require().Contains(resp.RawLog, sdkerrors.ErrInsufficientFunds.Error())
suite.Require().NoError(err)
})

t.Run("verify granter wallet amount", func(t *testing.T) {
actualBalance, err := suite.GetChainANativeBalance(ctx, granterWallet)
suite.Require().NoError(err)
suite.Require().Equal(testvalues.StartingTokenAmount, actualBalance)
})

t.Run("verify receiver wallet amount", func(t *testing.T) {
chainBIBCToken := testsuite.GetIBCToken(chainADenom, channelA.Counterparty.PortID, channelA.Counterparty.ChannelID)
actualBalance, err := chainB.GetBalance(ctx, receiverWalletAddress, chainBIBCToken.IBCDenom())
suite.Require().NoError(err)
suite.Require().Equal(int64(0), actualBalance)
})

t.Run("granter grant spend limit unchanged", func(t *testing.T) {
grantAuths, err := suite.QueryGranterGrants(ctx, chainA, granterAddress)

suite.Require().NoError(err)
suite.Require().Len(grantAuths, 1)
grantAuthorization := grantAuths[0]

transferAuth := suite.extractTransferAuthorizationFromGrantAuthorization(grantAuthorization)
expectedSpendLimit := sdk.NewCoins(sdk.NewCoin(chainADenom, sdk.NewInt(spendLimit)))
suite.Require().Equal(expectedSpendLimit, transferAuth.Allocations[0].SpendLimit)
})
})

t.Run("send funds to invalid address", func(t *testing.T) {
invalidWallet := suite.CreateUserOnChainB(ctx, testvalues.StartingTokenAmount)
invalidWalletAddress := invalidWallet.Bech32Address(chainB.Config().Bech32Prefix)

t.Run("broadcast MsgExec for ibc MsgTransfer", func(t *testing.T) {
transferMsg := transfertypes.MsgTransfer{
SourcePort: channelA.PortID,
SourceChannel: channelA.ChannelID,
Token: sdk.Coin{Denom: chainADenom, Amount: sdk.NewInt(spendLimit)},
Sender: granterAddress,
Receiver: invalidWalletAddress,
TimeoutHeight: suite.GetTimeoutHeight(ctx, chainB),
}

transferAny, err := codectypes.NewAnyWithValue(&transferMsg)
suite.Require().NoError(err)

msgExec := &authz.MsgExec{
Grantee: granteeAddress,
Msgs: []*codectypes.Any{transferAny},
}

resp, err := suite.BroadcastMessages(context.TODO(), chainA, granteeWallet, msgExec)
suite.Require().NotEqual(0, resp.Code)
suite.Require().Contains(resp.RawLog, sdkerrors.ErrInvalidAddress.Error())
suite.Require().NoError(err)
})
})
}

// extractTransferAuthorizationFromGrantAuthorization extracts a TransferAuthorization from the given
// GrantAuthorization.
func (suite *AuthzTransferTestSuite) extractTransferAuthorizationFromGrantAuthorization(grantAuth *authz.GrantAuthorization) *transfertypes.TransferAuthorization {
Expand Down
2 changes: 1 addition & 1 deletion e2e/testvalues/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ func TendermintClientID(id int) string {
}

func SolomachineClientID(id int) string {
return fmt.Sprint("06-solomachine-%d", id)
return fmt.Sprintf("06-solomachine-%d", id)
}

0 comments on commit ee9db57

Please sign in to comment.