diff --git a/x/reward/keeper/msg_server_campaign_test.go b/x/reward/keeper/msg_server_campaign_test.go index a38b72b8..79271b12 100644 --- a/x/reward/keeper/msg_server_campaign_test.go +++ b/x/reward/keeper/msg_server_campaign_test.go @@ -11,7 +11,7 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors" + sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors" "github.com/sge-network/sge/testutil/sample" "github.com/sge-network/sge/testutil/simapp" @@ -90,7 +90,7 @@ func TestCampaignMsgServerUpdate(t *testing.T) { request: &types.MsgUpdateCampaign{ Uid: uuid.NewString(), }, - err: sdkerrortypes.ErrKeyNotFound, + err: sdkerrtypes.ErrKeyNotFound, }, } { t.Run(tc.desc, func(t *testing.T) { @@ -182,7 +182,7 @@ func TestCampaignMsgServerDelete(t *testing.T) { request: &types.MsgDeleteCampaign{ Uid: uuid.NewString(), }, - err: sdkerrortypes.ErrKeyNotFound, + err: sdkerrtypes.ErrKeyNotFound, }, } { t.Run(tc.desc, func(t *testing.T) { diff --git a/x/reward/keeper/msg_server_reward.go b/x/reward/keeper/msg_server_reward.go index 504c3216..1a2cf5c8 100644 --- a/x/reward/keeper/msg_server_reward.go +++ b/x/reward/keeper/msg_server_reward.go @@ -5,7 +5,7 @@ import ( sdkerrors "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors" + sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors" "github.com/sge-network/sge/x/reward/types" ) @@ -16,7 +16,7 @@ func (k msgServer) ApplyReward(goCtx context.Context, msg *types.MsgApplyReward) // Check if the value already exists campaign, isFound := k.GetCampaign(ctx, msg.CampaignUid) if isFound { - return nil, sdkerrors.Wrap(sdkerrortypes.ErrInvalidRequest, "campaign with the same uid is already set") + return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "campaign with the same uid is already set") } if err := campaign.CheckExpiration(uint64(ctx.BlockTime().Unix())); err == nil { @@ -25,7 +25,7 @@ func (k msgServer) ApplyReward(goCtx context.Context, msg *types.MsgApplyReward) rewardFactory, err := campaign.GetRewardsFactory() if err != nil { - return nil, sdkerrors.Wrap(sdkerrortypes.ErrInvalidRequest, "failed to retrieve reward factory") + return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "failed to retrieve reward factory") } distribution, err := rewardFactory.CalculateDistributions(goCtx, ctx, @@ -35,7 +35,7 @@ func (k msgServer) ApplyReward(goCtx context.Context, msg *types.MsgApplyReward) }, campaign.RewardDefs, msg.Ticket) if err != nil { - return nil, sdkerrors.Wrap(sdkerrortypes.ErrInvalidRequest, "failed to get destribution from the ticket") + return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "failed to get destribution from the ticket") } if err := campaign.CheckPoolBalance(distribution); err != nil { diff --git a/x/reward/types/messages_campaign.go b/x/reward/types/messages_campaign.go index b51db289..8d859007 100644 --- a/x/reward/types/messages_campaign.go +++ b/x/reward/types/messages_campaign.go @@ -3,7 +3,8 @@ package types import ( sdkerrors "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors" + sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/sge-network/sge/utils" ) const ( @@ -50,8 +51,17 @@ func (msg *MsgCreateCampaign) GetSignBytes() []byte { func (msg *MsgCreateCampaign) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(msg.Creator) if err != nil { - return sdkerrors.Wrapf(sdkerrortypes.ErrInvalidAddress, "invalid creator address (%s)", err) + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err) } + + if !utils.IsValidUID(msg.Uid) { + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid uid") + } + + if msg.Ticket == "" { + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid ticket") + } + return nil } @@ -93,8 +103,17 @@ func (msg *MsgUpdateCampaign) GetSignBytes() []byte { func (msg *MsgUpdateCampaign) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(msg.Creator) if err != nil { - return sdkerrors.Wrapf(sdkerrortypes.ErrInvalidAddress, "invalid creator address (%s)", err) + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err) + } + + if !utils.IsValidUID(msg.Uid) { + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid uid") } + + if msg.Ticket == "" { + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid ticket") + } + return nil } @@ -134,7 +153,12 @@ func (msg *MsgDeleteCampaign) GetSignBytes() []byte { func (msg *MsgDeleteCampaign) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(msg.Creator) if err != nil { - return sdkerrors.Wrapf(sdkerrortypes.ErrInvalidAddress, "invalid creator address (%s)", err) + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err) } + + if !utils.IsValidUID(msg.Uid) { + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid uid") + } + return nil } diff --git a/x/reward/types/messages_campaign_test.go b/x/reward/types/messages_campaign_test.go index f2a2a82b..9d3e4ab5 100644 --- a/x/reward/types/messages_campaign_test.go +++ b/x/reward/types/messages_campaign_test.go @@ -3,6 +3,7 @@ package types_test import ( "testing" + "github.com/google/uuid" "github.com/stretchr/testify/require" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -21,12 +22,35 @@ func TestMsgCreateCampaign_ValidateBasic(t *testing.T) { name: "invalid address", msg: types.MsgCreateCampaign{ Creator: "invalid_address", + Uid: uuid.NewString(), + Ticket: "ticket", }, err: sdkerrors.ErrInvalidAddress, - }, { + }, + { + name: "invalid uid", + msg: types.MsgCreateCampaign{ + Creator: sample.AccAddress(), + Uid: "invalid uid", + Ticket: "ticket", + }, + err: sdkerrors.ErrInvalidRequest, + }, + { + name: "invalid ticket", + msg: types.MsgCreateCampaign{ + Creator: sample.AccAddress(), + Uid: uuid.NewString(), + Ticket: "", + }, + err: sdkerrors.ErrInvalidRequest, + }, + { name: "valid address", msg: types.MsgCreateCampaign{ Creator: sample.AccAddress(), + Uid: uuid.NewString(), + Ticket: "ticket", }, }, } @@ -52,12 +76,35 @@ func TestMsgUpdateCampaign_ValidateBasic(t *testing.T) { name: "invalid address", msg: types.MsgUpdateCampaign{ Creator: "invalid_address", + Uid: uuid.NewString(), + Ticket: "ticket", }, err: sdkerrors.ErrInvalidAddress, - }, { + }, + { + name: "invalid uid", + msg: types.MsgUpdateCampaign{ + Creator: sample.AccAddress(), + Uid: "invalid uid", + Ticket: "ticket", + }, + err: sdkerrors.ErrInvalidRequest, + }, + { + name: "invalid ticket", + msg: types.MsgUpdateCampaign{ + Creator: sample.AccAddress(), + Uid: uuid.NewString(), + Ticket: "", + }, + err: sdkerrors.ErrInvalidRequest, + }, + { name: "valid address", msg: types.MsgUpdateCampaign{ Creator: sample.AccAddress(), + Uid: uuid.NewString(), + Ticket: "ticket", }, }, } @@ -83,12 +130,23 @@ func TestMsgDeleteCampaign_ValidateBasic(t *testing.T) { name: "invalid address", msg: types.MsgDeleteCampaign{ Creator: "invalid_address", + Uid: uuid.NewString(), }, err: sdkerrors.ErrInvalidAddress, - }, { + }, + { + name: "invalid uid", + msg: types.MsgDeleteCampaign{ + Creator: sample.AccAddress(), + Uid: "invalid uid", + }, + err: sdkerrors.ErrInvalidRequest, + }, + { name: "valid address", msg: types.MsgDeleteCampaign{ Creator: sample.AccAddress(), + Uid: uuid.NewString(), }, }, } diff --git a/x/reward/types/messages_reward.go b/x/reward/types/messages_reward.go index 69e51c14..3ad89276 100644 --- a/x/reward/types/messages_reward.go +++ b/x/reward/types/messages_reward.go @@ -3,7 +3,6 @@ package types import ( sdkerrors "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors" sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors" "github.com/sge-network/sge/utils" @@ -51,7 +50,7 @@ func (msg *MsgApplyReward) GetSignBytes() []byte { func (msg *MsgApplyReward) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(msg.Creator) if err != nil { - return sdkerrors.Wrapf(sdkerrortypes.ErrInvalidAddress, "invalid creator address (%s)", err) + return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err) } if !utils.IsValidUID(msg.CampaignUid) {