Skip to content

Commit

Permalink
Merge pull request #292 from scorpioborn/fix/validation-bugfix
Browse files Browse the repository at this point in the history
Fix / Validation messages for campaign and reward
  • Loading branch information
3eyedraga authored Nov 13, 2023
2 parents 547d1a7 + f66a1b6 commit abbd5a5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
6 changes: 5 additions & 1 deletion x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 31 additions & 6 deletions x/reward/keeper/msg_server_campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -64,6 +65,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()

Expand Down Expand Up @@ -115,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)
Expand Down
4 changes: 4 additions & 0 deletions x/reward/keeper/msg_server_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 16 additions & 0 deletions x/reward/types/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ func (payload *CreateCampaignPayload) Validate(blockTime uint64) error {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "sub account should have unlock period")
}

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 %s is more than total funds %s", totalRewardAmount, payload.TotalFunds)
}

if payload.ClaimsPerCategory == 0 {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "claim per category should be a positive number")
}

return nil
}

Expand Down
7 changes: 4 additions & 3 deletions x/reward/types/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
Expand Down

0 comments on commit abbd5a5

Please sign in to comment.