Skip to content

Commit

Permalink
feat: campaign crud authorization check
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpioborn committed Sep 14, 2023
1 parent 9513218 commit 70b06df
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 103 deletions.
4 changes: 4 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ func NewAppKeeper(
appKeepers.keys[rewardmoduletypes.StoreKey],
appKeepers.keys[rewardmoduletypes.MemStoreKey],
appKeepers.GetSubspace(rewardmoduletypes.ModuleName),
appKeepers.OVMKeeper,
rewardmodulekeeper.SdkExpectedKeepers{
AuthzKeeper: appKeepers.AuthzKeeper,
},
)

//// SGE modules \\\\
Expand Down
11 changes: 6 additions & 5 deletions proto/sge/reward/campaign.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ message Campaign {
(gogoproto.jsontag) = "uid",
json_name = "uid"
];
string funder_address = 3;
// start_ts is the start timestamp of a campaign.
uint64 start_ts = 3 [
uint64 start_ts = 4 [
(gogoproto.customname) = "StartTS",
(gogoproto.jsontag) = "start_ts",
json_name = "start_ts"
];
// end_ts is the end timestamp of a campaign.
uint64 end_ts = 4 [
uint64 end_ts = 5 [
(gogoproto.customname) = "EndTS",
(gogoproto.jsontag) = "end_ts",
json_name = "end_ts"
];
// reward_type is the type of defined reward.
RewardType reward_type = 5;
RewardType reward_type = 6;
// reward_defs is the list of definitions of the campaign rewards.
repeated Definition reward_defs = 6 [ (gogoproto.nullable) = false ];
repeated Definition reward_defs = 7 [ (gogoproto.nullable) = false ];
// pool is the tracker of pool funds of the campaign.
Pool pool = 7 [ (gogoproto.nullable) = false ];
Pool pool = 8 [ (gogoproto.nullable) = false ];
}

// Pool is the type for the campaign funding pool.
Expand Down
2 changes: 1 addition & 1 deletion utils/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ValidateMsgAuthorization(
granteeAddr := sdk.MustAccAddressFromBech32(creator)
granterAddr, err := sdk.AccAddressFromBech32(depositor)
if err != nil {
return cosmerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid depositor address (%s)", err)
return cosmerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid granter address (%s)", err)
}
authorization, expiration := authzKeeper.GetAuthorization(
ctx,
Expand Down
9 changes: 9 additions & 0 deletions x/reward/keeper/campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package keeper_test
import (
"strconv"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/google/uuid"
"github.com/sge-network/sge/testutil/nullify"
"github.com/sge-network/sge/testutil/sample"
"github.com/stretchr/testify/require"

"github.com/sge-network/sge/x/reward/keeper"
Expand All @@ -20,6 +22,13 @@ func createNCampaign(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Camp
items := make([]types.Campaign, n)
for i := range items {
items[i].UID = uuid.NewString()
items[i].Creator = sample.AccAddress()
items[i].FunderAddress = sample.AccAddress()
items[i].StartTS = uint64(time.Now().Unix())
items[i].EndTS = uint64(time.Now().Add(5 * time.Minute).Unix())
items[i].RewardType = types.RewardType_REWARD_TYPE_REFERRAL
items[i].RewardDefs = []types.Definition{}
items[i].Pool = types.Pool{Spent: sdk.ZeroInt(), Total: sdk.NewInt(100)}

keeper.SetCampaign(ctx, items[i])
}
Expand Down
27 changes: 19 additions & 8 deletions x/reward/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,40 @@ import (

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
authzKeeper types.AuthzKeeper
ovmKeeper types.OVMKeeper
}
)

// SdkExpectedKeepers contains expected keepers parameter needed by NewKeeper
type SdkExpectedKeepers struct {
AuthzKeeper types.AuthzKeeper
}

func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
ovmKeeper types.OVMKeeper,
expectedKeepers SdkExpectedKeepers,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
ovmKeeper: ovmKeeper,
authzKeeper: expectedKeepers.AuthzKeeper,
}
}

Expand Down
61 changes: 38 additions & 23 deletions x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
cosmerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/sge-network/sge/utils"
"github.com/sge-network/sge/x/reward/types"
)

Expand All @@ -21,41 +22,55 @@ func (k msgServer) CreateCampaign(goCtx context.Context, msg *types.MsgCreateCam
return nil, cosmerrors.Wrap(sdkerrors.ErrInvalidRequest, "index already set")
}

campaign := types.Campaign{
Creator: msg.Creator,
UID: msg.Uid,
var payload types.CreateCampaignPayload
if err := k.ovmKeeper.VerifyTicketUnmarshal(goCtx, msg.Ticket, &payload); err != nil {
return nil, cosmerrors.Wrapf(types.ErrInTicketVerification, "%s", err)
}

k.SetCampaign(
ctx,
campaign,
if msg.Creator != payload.FunderAddress {
if err := utils.ValidateMsgAuthorization(k.authzKeeper, ctx, msg.Creator, payload.FunderAddress, msg,
types.ErrAuthorizationNotFound, types.ErrAuthorizationNotAccepted); err != nil {
return nil, err
}
}

campaign := types.NewCampaign(
msg.Creator, payload.FunderAddress, msg.Uid,
payload.StartTs, payload.EndTs,
payload.Type,
payload.RewardDefs,
types.NewPool(payload.PoolAmount),
)
k.SetCampaign(ctx, campaign)

return &types.MsgCreateCampaignResponse{}, nil
}

func (k msgServer) UpdateCampaign(goCtx context.Context, msg *types.MsgUpdateCampaign) (*types.MsgUpdateCampaignResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

var payload types.UpdateCampaignPayload
if err := k.ovmKeeper.VerifyTicketUnmarshal(goCtx, msg.Ticket, &payload); err != nil {
return nil, cosmerrors.Wrapf(types.ErrInTicketVerification, "%s", err)
}

// Check if the value exists
valFound, isFound := k.GetCampaign(
ctx,
msg.Uid,
)
valFound, isFound := k.GetCampaign(ctx, msg.Uid)
if !isFound {
return nil, cosmerrors.Wrap(sdkerrors.ErrKeyNotFound, "index not set")
}

// Checks if the the msg creator is the same as the current owner
if msg.Creator != valFound.Creator {
return nil, cosmerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
if msg.Creator != valFound.FunderAddress {
if err := utils.ValidateMsgAuthorization(k.authzKeeper, ctx, msg.Creator, valFound.FunderAddress, msg,
types.ErrAuthorizationNotFound, types.ErrAuthorizationNotAccepted); err != nil {
return nil, err
}
}

campaign := types.Campaign{
Creator: msg.Creator,
UID: msg.Uid,
}
valFound.EndTS = payload.EndTs

k.SetCampaign(ctx, campaign)
k.SetCampaign(ctx, valFound)

return &types.MsgUpdateCampaignResponse{}, nil
}
Expand All @@ -73,14 +88,14 @@ func (k msgServer) DeleteCampaign(goCtx context.Context, msg *types.MsgDeleteCam
}

// Checks if the the msg creator is the same as the current owner
if msg.Creator != valFound.Creator {
return nil, cosmerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner")
if msg.Creator != valFound.FunderAddress {
if err := utils.ValidateMsgAuthorization(k.authzKeeper, ctx, msg.Creator, valFound.FunderAddress, msg,
types.ErrAuthorizationNotFound, types.ErrAuthorizationNotAccepted); err != nil {
return nil, err
}
}

k.RemoveCampaign(
ctx,
msg.Uid,
)
k.RemoveCampaign(ctx, msg.Uid)

return &types.MsgDeleteCampaignResponse{}, nil
}
Loading

0 comments on commit 70b06df

Please sign in to comment.