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

chore: refactor NewTransferAuthorization constructor to use varargs #2991

Merged
merged 6 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 1 addition & 10 deletions modules/apps/transfer/types/transfer_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ const gasCostPerIteration = uint64(10)
var _ authz.Authorization = &TransferAuthorization{}

// NewTransferAuthorization creates a new TransferAuthorization object.
func NewTransferAuthorization(sourcePorts, sourceChannels []string, spendLimits []sdk.Coins, allowedAddrs [][]string) *TransferAuthorization {
allocations := []Allocation{}
for index := range sourcePorts {
allocations = append(allocations, Allocation{
SourcePort: sourcePorts[index],
SourceChannel: sourceChannels[index],
SpendLimit: spendLimits[index],
AllowList: allowedAddrs[index],
})
}
func NewTransferAuthorization(allocations ...Allocation) *TransferAuthorization {
return &TransferAuthorization{
Allocations: allocations,
}
Expand Down
58 changes: 47 additions & 11 deletions modules/apps/transfer/types/transfer_authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ var (
func TestTransferAuthorization(t *testing.T) {
app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
authorization := NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}})
allocation := Allocation{
SourcePort: sourcePort,
SourceChannel: sourceChannel,
SpendLimit: coins1000,
AllowList: []string{toAddr.String()},
}
authorization := NewTransferAuthorization(allocation)

t.Log("verify authorization returns valid method name")
require.Equal(t, authorization.MsgTypeURL(), "/ibc.applications.transfer.v1.MsgTransfer")
Expand All @@ -41,7 +47,7 @@ func TestTransferAuthorization(t *testing.T) {
require.Nil(t, resp.Updated)

t.Log("verify updated authorization returns remaining spent limit")
authorization = NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}})
authorization = NewTransferAuthorization(allocation)
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, "")
Expand All @@ -50,7 +56,14 @@ func TestTransferAuthorization(t *testing.T) {
require.NoError(t, err)
require.False(t, resp.Delete)
require.NotNil(t, resp.Updated)
sendAuth := NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins500}, [][]string{{toAddr.String()}})

allocation = Allocation{
SourcePort: sourcePort,
SourceChannel: sourceChannel,
SpendLimit: coins500,
AllowList: []string{toAddr.String()},
}
sendAuth := NewTransferAuthorization(allocation)
require.Equal(t, sendAuth.String(), resp.Updated.String())

t.Log("expect updated authorization nil after spending remaining amount")
Expand All @@ -60,26 +73,49 @@ func TestTransferAuthorization(t *testing.T) {
require.Nil(t, resp.Updated)

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()}})
allocation = Allocation{
SourcePort: sourcePort,
SourceChannel: sourceChannel,
SpendLimit: coins1000,
AllowList: []string{toAddr.String()},
}
authorization = NewTransferAuthorization(allocation)
transfer = NewMsgTransfer(sourcePort2, sourceChannel2, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "")
_, err = authorization.Accept(ctx, transfer)
require.Error(t, err)

t.Log("expect removing only 1 allocation if spend limit is finalized for the port")
authorization = NewTransferAuthorization(
[]string{sourcePort, sourcePort2},
[]string{sourceChannel, sourceChannel2},
[]sdk.Coins{coins1000, coins1000},
[][]string{{toAddr.String()}, {toAddr.String()}})

allocations := []Allocation{
{
SourcePort: sourcePort,
SourceChannel: sourceChannel,
SpendLimit: coins1000,
AllowList: []string{toAddr.String()},
},
{
SourcePort: sourcePort2,
SourceChannel: sourceChannel2,
SpendLimit: coins1000,
AllowList: []string{toAddr.String()},
},
}
authorization = NewTransferAuthorization(allocations...)
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)
require.Equal(t, resp.Updated, NewTransferAuthorization([]string{sourcePort2}, []string{sourceChannel2}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}}))
require.Equal(t, resp.Updated, NewTransferAuthorization(allocations[1]))
require.False(t, resp.Delete)

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