Skip to content

Commit

Permalink
reserve trackerId = approvalId scoped to approval
Browse files Browse the repository at this point in the history
  • Loading branch information
trevormil committed Jan 24, 2024
1 parent f05becd commit fb22d9f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
70 changes: 69 additions & 1 deletion x/badges/types/message_update_user_approved_transfers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,80 @@ func TestMsgUpdateUserApprovals_ValidateBasic(t *testing.T) {
Creator: sample.AccAddress(),
},
},
{
name: "tracker ID = ID of another approval",
msg: types.MsgUpdateUserApprovals{
Creator: sample.AccAddress(),
OutgoingApprovals: []*types.UserOutgoingApproval{
{
ToListId: "All",
InitiatedByListId: "All",
ApprovalId: "approval_id",
AmountTrackerId: "other_approval_id",
ChallengeTrackerId: "challenge_id",
},
{
ToListId: "All",
InitiatedByListId: "All",
ApprovalId: "other_approval_id",
AmountTrackerId: "other_approval_id",
ChallengeTrackerId: "challenge_id",
},
},
},
err: types.ErrAmountTrackerIdIsNil,
},
{
name: "tracker ID = ID of another approval",
msg: types.MsgUpdateUserApprovals{
Creator: sample.AccAddress(),
OutgoingApprovals: []*types.UserOutgoingApproval{
{
ToListId: "All",
InitiatedByListId: "All",
ApprovalId: "approval_id",
AmountTrackerId: "other_approval_id",
ChallengeTrackerId: "challenge_id",
},
{
ToListId: "All",
InitiatedByListId: "All",
ApprovalId: "dfsdgffds_approval_id",
AmountTrackerId: "other_approval_id",
ChallengeTrackerId: "challenge_id",
},
},
},
},
{
name: "tracker ID = ID of another approval",
msg: types.MsgUpdateUserApprovals{
Creator: sample.AccAddress(),
OutgoingApprovals: []*types.UserOutgoingApproval{
{
ToListId: "All",
InitiatedByListId: "All",
ApprovalId: "approval_id",
AmountTrackerId: "sdafaf",
ChallengeTrackerId: "other_approval_id",
},
{
ToListId: "All",
InitiatedByListId: "All",
ApprovalId: "other_approval_id",
AmountTrackerId: "afdsa",
ChallengeTrackerId: "asfdadsf",
},
},
},
err: types.ErrAmountTrackerIdIsNil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
require.Error(t, err, tt.err)
return
}
require.NoError(t, err)
Expand Down
29 changes: 28 additions & 1 deletion x/badges/types/validate_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func ValidateAddressList(addressList *AddressList) error {
}
}

//check duplicate addresses
if duplicateInStringArray(addressList.Addresses) {
return ErrDuplicateAddresses
}

return nil
}

Expand Down Expand Up @@ -208,7 +213,7 @@ func ValidateCollectionApprovals(collectionApprovals []*CollectionApproval, canC
}
}

for _, collectionApproval := range collectionApprovals {
for i, collectionApproval := range collectionApprovals {
if collectionApproval == nil {
return sdkerrors.Wrapf(ErrInvalidRequest, "collection approved transfer is nil")
}
Expand Down Expand Up @@ -251,6 +256,17 @@ func ValidateCollectionApprovals(collectionApprovals []*CollectionApproval, canC
return sdkerrors.Wrapf(ErrInvalidRequest, "approval tracker id can not be All")
}

//assert the amount tracker ID is not the approval ID of another approval
for j, otherCollectionApproval := range collectionApprovals {
if i == j {
continue
}

if collectionApproval.AmountTrackerId == otherCollectionApproval.ApprovalId {
return sdkerrors.Wrapf(ErrInvalidRequest, "approval tracker id can not be the approval id of another approval")
}
}

if strings.Contains(collectionApproval.AmountTrackerId, ":") || strings.Contains(collectionApproval.AmountTrackerId, "!") {
return sdkerrors.Wrapf(ErrIdsContainsInvalidChars, "approval tracker id can not contain : or !")
}
Expand All @@ -263,6 +279,17 @@ func ValidateCollectionApprovals(collectionApprovals []*CollectionApproval, canC
return sdkerrors.Wrapf(ErrInvalidRequest, "challenge tracker id can not be All")
}

//assert the challenge tracker ID is not the approval ID of another approval
for j, otherCollectionApproval := range collectionApprovals {
if i == j {
continue
}

if collectionApproval.ChallengeTrackerId == otherCollectionApproval.ApprovalId {
return sdkerrors.Wrapf(ErrInvalidRequest, "challenge tracker id can not be the approval id of another approval")
}
}

if strings.Contains(collectionApproval.ChallengeTrackerId, ":") || strings.Contains(collectionApproval.ChallengeTrackerId, "!") {
return sdkerrors.Wrapf(ErrIdsContainsInvalidChars, "challenge tracker id can not contain : or !")
}
Expand Down

0 comments on commit fb22d9f

Please sign in to comment.