Skip to content

Commit

Permalink
test: campaign ticket validation
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpioborn committed Sep 26, 2023
1 parent 9363df8 commit 6754b06
Show file tree
Hide file tree
Showing 4 changed files with 169 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 @@ -32,7 +32,7 @@ func (k msgServer) CreateCampaign(goCtx context.Context, msg *types.MsgCreateCam
}
}

if err := payload.Validate(ctx); err != nil {
if err := payload.Validate(uint64(ctx.BlockTime().Unix())); err != nil {
return nil, err
}

Expand Down Expand Up @@ -82,6 +82,10 @@ func (k msgServer) UpdateCampaign(goCtx context.Context, msg *types.MsgUpdateCam
return nil, sdkerrors.Wrapf(types.ErrInTicketVerification, "%s", err)
}

if err := payload.Validate(uint64(ctx.BlockTime().Unix())); err != nil {
return nil, err
}

// Check if the value exists
valFound, isFound := k.GetCampaign(ctx, msg.Uid)
if !isFound {
Expand Down
3 changes: 3 additions & 0 deletions x/reward/keeper/msg_server_campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var _ = strconv.IntSize
func TestCampaignMsgServerCreate(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()
for i := 0; i < 5; i++ {
Expand Down Expand Up @@ -96,6 +97,7 @@ func TestCampaignMsgServerUpdate(t *testing.T) {
t.Run(tc.desc, func(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()
Expand Down Expand Up @@ -188,6 +190,7 @@ func TestCampaignMsgServerDelete(t *testing.T) {
t.Run(tc.desc, func(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()

Expand Down
14 changes: 5 additions & 9 deletions x/reward/types/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Validate validates campaign creation ticket payload.
func (payload *CreateCampaignPayload) Validate(ctx sdk.Context) error {
func (payload *CreateCampaignPayload) Validate(blockTime uint64) error {
_, err := sdk.AccAddressFromBech32(payload.FunderAddress)
if err != nil {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err)
Expand All @@ -18,28 +18,24 @@ func (payload *CreateCampaignPayload) Validate(ctx sdk.Context) error {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "start timestamp can not be after end time")
}

if payload.EndTs > uint64(ctx.BlockTime().Unix()) {
if payload.EndTs <= uint64(blockTime) {

Check failure on line 21 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

unnecessary conversion (unconvert)
return sdkerrors.Wrapf(ErrExpiredCampaign, "%d", payload.EndTs)
}

if payload.StartTs >= payload.EndTs {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "start timestamp can not be after end time")
}

if len(payload.RewardDefs) == 0 {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "at least one reward definition should be defined")
}

if !payload.PoolAmount.GT(sdkmath.ZeroInt()) {
if payload.PoolAmount.IsNil() || !payload.PoolAmount.GT(sdkmath.ZeroInt()) {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "pool amount should be positive")
}

return nil
}

// Validate validates campaign update ticket payload.
func (payload *UpdateCampaignPayload) Validate(ctx sdk.Context) error {
if payload.EndTs > uint64(ctx.BlockTime().Unix()) {
func (payload *UpdateCampaignPayload) Validate(blockTime uint64) error {
if payload.EndTs < uint64(blockTime) {

Check failure on line 38 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

unnecessary conversion (unconvert)
return sdkerrors.Wrapf(ErrExpiredCampaign, "%d", payload.EndTs)
}

Expand Down
156 changes: 156 additions & 0 deletions x/reward/types/ticket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package types_test

import (
"testing"
"time"

sdkmath "cosmossdk.io/math"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"

"github.com/sge-network/sge/testutil/sample"
"github.com/sge-network/sge/x/reward/types"
)

func TestCreateCampaignPayloadValidation(t *testing.T) {
funderAddr := sample.AccAddress()
poolBalance := sdkmath.NewInt(1000)
blockTime := uint64(time.Now().Unix())

tests := []struct {
name string
payload types.CreateCampaignPayload
err error
}{
{
name: "start after end",
payload: types.CreateCampaignPayload{
FunderAddress: funderAddr,
StartTs: blockTime + 10,
EndTs: blockTime + 5,
},
err: sdkerrtypes.ErrInvalidRequest,
},
{
name: "expired",
payload: types.CreateCampaignPayload{
FunderAddress: funderAddr,
StartTs: blockTime - 1,
EndTs: blockTime,
},
err: types.ErrExpiredCampaign,
},
{
name: "not enough defs",
payload: types.CreateCampaignPayload{
FunderAddress: funderAddr,
StartTs: blockTime + 1,
EndTs: blockTime + 2,
},
err: sdkerrtypes.ErrInvalidRequest,
},
{
name: "nil pool amount",
payload: types.CreateCampaignPayload{
FunderAddress: funderAddr,
StartTs: blockTime + 1,
EndTs: blockTime + 2,
RewardDefs: []types.Definition{
{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
DstAccType: types.ReceiverAccType_RECEIVER_ACC_TYPE_MAIN,
Amount: sdkmath.NewInt(1000),
UnlockTS: 0,
},
},
},
err: sdkerrtypes.ErrInvalidRequest,
},
{
name: "invalid pool amount",
payload: types.CreateCampaignPayload{
FunderAddress: funderAddr,
StartTs: blockTime + 1,
EndTs: blockTime + 2,
RewardDefs: []types.Definition{
{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
DstAccType: types.ReceiverAccType_RECEIVER_ACC_TYPE_MAIN,
Amount: sdkmath.NewInt(1000),
UnlockTS: 0,
},
},
PoolAmount: sdkmath.NewInt(0),
},
err: sdkerrtypes.ErrInvalidRequest,
},
{
name: "valid",
payload: types.CreateCampaignPayload{
FunderAddress: funderAddr,
StartTs: blockTime + 1,
EndTs: blockTime + 2,
Type: types.RewardType_REWARD_TYPE_SIGNUP,
RewardDefs: []types.Definition{
{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
DstAccType: types.ReceiverAccType_RECEIVER_ACC_TYPE_MAIN,
Amount: sdkmath.NewInt(1000),
UnlockTS: 0,
},
},
PoolAmount: poolBalance,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.payload.Validate(blockTime)
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
})
}
}

func TestUpdateCampaignPayloadValidation(t *testing.T) {
blockTime := uint64(time.Now().Unix())

tests := []struct {
name string
payload types.UpdateCampaignPayload
err error
}{
{
name: "expired",
payload: types.UpdateCampaignPayload{
EndTs: blockTime - 1,
},
err: types.ErrExpiredCampaign,
},
{
name: "valid now",
payload: types.UpdateCampaignPayload{
EndTs: blockTime,
},
},
{
name: "valid",
payload: types.UpdateCampaignPayload{
EndTs: blockTime + 1,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.payload.Validate(blockTime)
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
})
}
}

0 comments on commit 6754b06

Please sign in to comment.