From 5c1279c4ae7d077eee6520bc255cd27102541c9e Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:20:51 +0300 Subject: [PATCH 1/5] fix: error messages --- x/reward/keeper/msg_server_campaign.go | 2 +- x/reward/keeper/msg_server_campaign_test.go | 23 +++++++++++++++++++++ x/reward/keeper/msg_server_reward.go | 4 ++++ x/reward/types/ticket.go | 10 +++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/x/reward/keeper/msg_server_campaign.go b/x/reward/keeper/msg_server_campaign.go index 6447d7f1..e82e9a8e 100644 --- a/x/reward/keeper/msg_server_campaign.go +++ b/x/reward/keeper/msg_server_campaign.go @@ -19,7 +19,7 @@ func (k msgServer) CreateCampaign(goCtx context.Context, msg *types.MsgCreateCam // Check if the value already exists _, isFound := k.GetCampaign(ctx, msg.Uid) if isFound { - return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "index already set") + return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "campaign with the provided uid is already set") } var payload types.CreateCampaignPayload diff --git a/x/reward/keeper/msg_server_campaign_test.go b/x/reward/keeper/msg_server_campaign_test.go index 9c35b534..4b06dc10 100644 --- a/x/reward/keeper/msg_server_campaign_test.go +++ b/x/reward/keeper/msg_server_campaign_test.go @@ -64,6 +64,29 @@ func TestCampaignMsgServerCreate(t *testing.T) { } } +func TestCampaignMsgServerUnAuthorizedCreate(t *testing.T) { + k, ctx := setupKeeper(t) + srv := keeper.NewMsgServerImpl(*k) + ctx = ctx.WithBlockTime(time.Now()) + wctx := sdk.WrapSDKContext(ctx) + creator := simapp.TestParamUsers["user1"].Address.String() + ticketClaim := jwt.MapClaims{ + "exp": time.Now().Add(time.Minute * 5).Unix(), + "iat": time.Now().Unix(), + "promoter": sample.AccAddress(), + } + ticket, err := simapp.CreateJwtTicket(ticketClaim) + require.Nil(t, err) + + expected := &types.MsgCreateCampaign{ + Creator: creator, + Uid: uuid.NewString(), + Ticket: ticket, + } + _, err = srv.CreateCampaign(wctx, expected) + require.ErrorIs(t, types.ErrAuthorizationNotFound, err) +} + func TestCampaignMsgServerUpdate(t *testing.T) { expectedUID := uuid.NewString() diff --git a/x/reward/keeper/msg_server_reward.go b/x/reward/keeper/msg_server_reward.go index 5d238cb5..3be35b67 100644 --- a/x/reward/keeper/msg_server_reward.go +++ b/x/reward/keeper/msg_server_reward.go @@ -13,6 +13,10 @@ import ( func (k msgServer) GrantReward(goCtx context.Context, msg *types.MsgGrantReward) (*types.MsgGrantRewardResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) + if _, isFound := k.GetReward(ctx, msg.Uid); isFound { + return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "reward grant with the provided uid exists") + } + campaign, isFound := k.GetCampaign(ctx, msg.CampaignUid) if !isFound { return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "campaign with the uid not found") diff --git a/x/reward/types/ticket.go b/x/reward/types/ticket.go index f9c2c0b7..a038f6f8 100644 --- a/x/reward/types/ticket.go +++ b/x/reward/types/ticket.go @@ -44,6 +44,16 @@ func (payload *CreateCampaignPayload) Validate(blockTime uint64) error { return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "sub account should have unlock period") } + totalRewardAmount := payload.RewardAmount.MainAccountAmount. + Add(payload.RewardAmount.SubaccountAmount) + if payload.TotalFunds.LT(totalRewardAmount) { + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "defined reward amount is more than total funds %s", totalRewardAmount) + } + + if payload.ClaimsPerCategory == 0 { + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "claim per category should be a positive number") + } + return nil } From 01472bd304120a09beaaf8ab45487c368d8864f3 Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:32:11 +0300 Subject: [PATCH 2/5] fix: tests of campaign for claims --- x/reward/keeper/msg_server_campaign_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/x/reward/keeper/msg_server_campaign_test.go b/x/reward/keeper/msg_server_campaign_test.go index 4b06dc10..0574e5cd 100644 --- a/x/reward/keeper/msg_server_campaign_test.go +++ b/x/reward/keeper/msg_server_campaign_test.go @@ -42,9 +42,10 @@ func TestCampaignMsgServerCreate(t *testing.T) { SubaccountAmount: sdkmath.NewInt(100), UnlockPeriod: uint64(ctx.BlockTime().Add(10 * time.Minute).Unix()), }, - "total_funds": sdkmath.NewInt(1000000), - "is_active": true, - "meta": "sample campaign", + "total_funds": sdkmath.NewInt(1000000), + "is_active": true, + "meta": "sample campaign", + "claims_per_category": 1, } ticket, err := simapp.CreateJwtTicket(ticketClaim) require.Nil(t, err) @@ -138,9 +139,10 @@ func TestCampaignMsgServerUpdate(t *testing.T) { SubaccountAmount: sdkmath.NewInt(100), UnlockPeriod: uint64(ctx.BlockTime().Add(10 * time.Minute).Unix()), }, - "total_funds": sdkmath.NewInt(1000000), - "is_active": true, - "meta": "sample campaign", + "total_funds": sdkmath.NewInt(1000000), + "is_active": true, + "meta": "sample campaign", + "claims_per_category": 1, } ticket, err := simapp.CreateJwtTicket(ticketClaim) require.Nil(t, err) From 886a1fd436d668081e5097dafa947accec93b56a Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Mon, 13 Nov 2023 13:06:28 +0300 Subject: [PATCH 3/5] fix: reward amount check for campaign ticket --- x/reward/types/ticket.go | 10 ++++++++-- x/reward/types/ticket_test.go | 7 ++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/x/reward/types/ticket.go b/x/reward/types/ticket.go index a038f6f8..1b810eec 100644 --- a/x/reward/types/ticket.go +++ b/x/reward/types/ticket.go @@ -44,8 +44,14 @@ func (payload *CreateCampaignPayload) Validate(blockTime uint64) error { return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "sub account should have unlock period") } - totalRewardAmount := payload.RewardAmount.MainAccountAmount. - Add(payload.RewardAmount.SubaccountAmount) + totalRewardAmount := sdkmath.ZeroInt() + if !payload.RewardAmount.MainAccountAmount.IsNil() { + totalRewardAmount = totalRewardAmount.Add(payload.RewardAmount.MainAccountAmount) + } + if !payload.RewardAmount.SubaccountAmount.IsNil() { + totalRewardAmount = totalRewardAmount.Add(payload.RewardAmount.SubaccountAmount) + } + if payload.TotalFunds.LT(totalRewardAmount) { return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "defined reward amount is more than total funds %s", totalRewardAmount) } diff --git a/x/reward/types/ticket_test.go b/x/reward/types/ticket_test.go index c7ace0dc..512f04da 100644 --- a/x/reward/types/ticket_test.go +++ b/x/reward/types/ticket_test.go @@ -100,9 +100,10 @@ func TestCreateCampaignPayloadValidation(t *testing.T) { SubaccountAmount: sdkmath.NewInt(1000), UnlockPeriod: uint64(time.Now().Add(10 * time.Minute).Unix()), }, - TotalFunds: poolBalance, - IsActive: true, - Meta: "sample campaign", + TotalFunds: poolBalance, + ClaimsPerCategory: 1, + IsActive: true, + Meta: "sample campaign", }, }, } From 8772808139c1a813b9edb4488c231c83d55648bc Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Mon, 13 Nov 2023 14:19:55 +0300 Subject: [PATCH 4/5] fix: total fund of reward message --- x/reward/types/ticket.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/reward/types/ticket.go b/x/reward/types/ticket.go index 1b810eec..7ac719e5 100644 --- a/x/reward/types/ticket.go +++ b/x/reward/types/ticket.go @@ -53,7 +53,7 @@ func (payload *CreateCampaignPayload) Validate(blockTime uint64) error { } if payload.TotalFunds.LT(totalRewardAmount) { - return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "defined reward amount is more than total funds %s", totalRewardAmount) + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "defined reward amount %s is more than total funds %s", totalRewardAmount, payload.TotalFunds) } if payload.ClaimsPerCategory == 0 { From f66a1b6ae6bdbc446852450c5dae4701b5b29b77 Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Mon, 13 Nov 2023 14:27:16 +0300 Subject: [PATCH 5/5] fix: promoter check while withdrawal of campaign pool --- x/reward/keeper/msg_server_campaign.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/reward/keeper/msg_server_campaign.go b/x/reward/keeper/msg_server_campaign.go index e82e9a8e..04de0e2f 100644 --- a/x/reward/keeper/msg_server_campaign.go +++ b/x/reward/keeper/msg_server_campaign.go @@ -130,6 +130,10 @@ func (k msgServer) WithdrawFunds(goCtx context.Context, msg *types.MsgWithdrawFu return nil, sdkerrors.Wrap(sdkerrtypes.ErrKeyNotFound, "campaign not found") } + if payload.Promoter != valFound.Promoter { + return nil, sdkerrors.Wrap(sdkerrtypes.ErrKeyNotFound, "promoter should be the same as stored campaign promoter") + } + // Checks if the msg creator is the same as the current owner if msg.Creator != valFound.Promoter { if err := utils.ValidateMsgAuthorization(k.authzKeeper, ctx, msg.Creator, valFound.Promoter, msg,