From fb22d9f92e9314a81f27f9574c5317e1610f5734 Mon Sep 17 00:00:00 2001 From: Trevor Miller Date: Tue, 23 Jan 2024 21:27:19 -0500 Subject: [PATCH] reserve trackerId = approvalId scoped to approval --- ...age_update_user_approved_transfers_test.go | 70 ++++++++++++++++++- x/badges/types/validate_basic.go | 29 +++++++- 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/x/badges/types/message_update_user_approved_transfers_test.go b/x/badges/types/message_update_user_approved_transfers_test.go index 05d73752..67061832 100644 --- a/x/badges/types/message_update_user_approved_transfers_test.go +++ b/x/badges/types/message_update_user_approved_transfers_test.go @@ -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) diff --git a/x/badges/types/validate_basic.go b/x/badges/types/validate_basic.go index 2f807f7d..7f839916 100644 --- a/x/badges/types/validate_basic.go +++ b/x/badges/types/validate_basic.go @@ -172,6 +172,11 @@ func ValidateAddressList(addressList *AddressList) error { } } + //check duplicate addresses + if duplicateInStringArray(addressList.Addresses) { + return ErrDuplicateAddresses + } + return nil } @@ -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") } @@ -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 !") } @@ -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 !") }