Skip to content

Commit

Permalink
Merge pull request #8 from AllInBetsCom/cron-fix
Browse files Browse the repository at this point in the history
post review fixes
  • Loading branch information
rockstarRhino committed Jun 17, 2024
2 parents 4b8d0e8 + d32ded0 commit d415177
Show file tree
Hide file tree
Showing 25 changed files with 642 additions and 168 deletions.
1 change: 0 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ func NewRollapp(
app.CronKeeper = cronkeeper.NewKeeper(
appCodec,
app.keys[crontypes.StoreKey],
app.keys[crontypes.MemStoreKey],
app.GetSubspace(crontypes.ModuleName),
&app.WasmKeeper,
)
Expand Down
2 changes: 2 additions & 0 deletions proto/wasmrollapp/cron/v1beta1/cron.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ message CronJob {
string description = 3;
// Msgs that will be executed every period amount of time
repeated MsgContractCron msg_contract_cron = 4 [(gogoproto.nullable) = false];
// set cron enabled or not
bool enable_cron = 5;
}

message MsgContractCron {
Expand Down
2 changes: 0 additions & 2 deletions proto/wasmrollapp/cron/v1beta1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ message Params {
(gogoproto.jsontag) = "security_address,omitempty",
(gogoproto.moretags) = "yaml:\"security_address\""
];
// set module enabled or not
bool enable_cron = 2;
}
14 changes: 13 additions & 1 deletion proto/wasmrollapp/cron/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ service Msg {
rpc RegisterCron(MsgRegisterCron) returns(MsgRegisterCronResponse);
rpc UpdateCronJob(MsgUpdateCronJob) returns(MsgUpdateCronJobResponse);
rpc DeleteCronJob(MsgDeleteCronJob) returns(MsgDeleteCronJobResponse);
rpc ToggleCronJob(MsgToggleCronJob) returns(MsgToggleCronJobResponse);
}

// MsgRegisterCron defines the Msg/RegisterCron request type.
Expand Down Expand Up @@ -54,4 +55,15 @@ message MsgDeleteCronJob {
}

// MsgDeleteCronJobResponse defines the Msg/DeleteCronJob response type.
message MsgDeleteCronJobResponse {}
message MsgDeleteCronJobResponse {}

// MsgToggleCronJob defines the Msg/ToggleCronJob request type.
message MsgToggleCronJob {
// security_address is the address authorised to toggle the cron job
string security_address = 1;
// id is the unique identifier for the cron job
uint64 id = 2;
}

// MsgToggleCronJobResponse defines the Msg/ToggleCronJob response type.
message MsgToggleCronJobResponse {}
14 changes: 8 additions & 6 deletions x/cron/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
crons := k.GetCronJobs(ctx)
for _, cron := range crons {
for _, job := range cron.MsgContractCron {
err := k.SudoContractCall(ctx, job.ContractAddress, []byte(job.JsonMsg))
if err != nil {
ctx.Logger().Error("Cronjob failed for: ", cron.Name, " contract: ", job.ContractAddress)
} else {
ctx.Logger().Info("Cronjob success for: ", cron.Name, " contract: ", job.ContractAddress)
if cron.EnableCron {
for _, job := range cron.MsgContractCron {
err := k.SudoContractCall(ctx, job.ContractAddress, []byte(job.JsonMsg))
if err != nil {
ctx.Logger().Error("Cronjob failed for: ", cron.Name, " contract: ", job.ContractAddress)
} else {
ctx.Logger().Info("Cronjob success for: ", cron.Name, " contract: ", job.ContractAddress)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/cron/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string) *cobra.Command {
func GetQueryCmd() *cobra.Command {
// Group cron queries under a subcommand
cmd := &cobra.Command{
Use: types.ModuleName,
Expand Down
34 changes: 32 additions & 2 deletions x/cron/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cli

import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/dymensionxyz/rollapp-wasm/x/cron/types"
"github.com/spf13/cobra"
"strconv"
)

// GetTxCmd returns the transaction commands for this module
Expand All @@ -22,7 +23,8 @@ func GetTxCmd() *cobra.Command {

cmd.AddCommand(CmdRegisterCron(),
CmdUpdateCronJob(),
CmdDeleteCronJob())
CmdDeleteCronJob(),
CmdToggleCronJob())

return cmd
}
Expand Down Expand Up @@ -112,3 +114,31 @@ func CmdDeleteCronJob() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
return cmd
}

func CmdToggleCronJob() *cobra.Command {
cmd := &cobra.Command{
Use: "toggle-cron-job [id]",
Short: "Toggle cron job",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cronID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("cron-id '%s' not a valid uint", args[0])
}
msg := types.NewMsgToggleCronJob(
clientCtx.GetFromAddress().String(),
cronID,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
3 changes: 3 additions & 0 deletions x/cron/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgDeleteCronJob:
res, err := server.DeleteCronJob(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgToggleCronJob:
res, err := server.ToggleCronJob(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
2 changes: 1 addition & 1 deletion x/cron/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
)
for _, item := range genState.CronJobs {
k.SetCronJob(ctx, item)
// Set the cronID to the cronID in the whitelisted contract
// Set the last cron ID
cronID = item.Id
}
k.SetCronID(ctx, cronID)
Expand Down
5 changes: 1 addition & 4 deletions x/cron/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
conOps types.ContractOpsKeeper
}
)

func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
storeKey storetypes.StoreKey,
ps paramtypes.Subspace,
conOps types.ContractOpsKeeper,

Expand All @@ -36,7 +34,6 @@ func NewKeeper(
return Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
conOps: conOps,
}
Expand Down
35 changes: 22 additions & 13 deletions x/cron/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ var _ types.MsgServer = msgServer{}

func (k msgServer) RegisterCron(goCtx context.Context, msg *types.MsgRegisterCron) (*types.MsgRegisterCronResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// check if the cron is globally enabled
params := k.GetParams(ctx)
if !params.EnableCron {
return &types.MsgRegisterCronResponse{}, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Cron is disabled")
}
if err := msg.ValidateBasic(); err != nil {
ctx.Logger().Error(fmt.Sprintf("request invalid: %s", err))
return &types.MsgRegisterCronResponse{}, err
Expand Down Expand Up @@ -57,6 +52,7 @@ func (k msgServer) RegisterCron(goCtx context.Context, msg *types.MsgRegisterCro
Name: msg.Name,
Description: msg.Description,
MsgContractCron: []types.MsgContractCron{msgContractCron},
EnableCron: true,
}
k.SetCronJob(ctx, cron)
k.SetCronID(ctx, cronId+1)
Expand All @@ -65,10 +61,6 @@ func (k msgServer) RegisterCron(goCtx context.Context, msg *types.MsgRegisterCro

func (k msgServer) UpdateCronJob(goCtx context.Context, msg *types.MsgUpdateCronJob) (*types.MsgUpdateCronJobResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)
if !params.EnableCron {
return &types.MsgUpdateCronJobResponse{}, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Cron is disabled")
}
if err := msg.ValidateBasic(); err != nil {
ctx.Logger().Error(fmt.Sprintf("request invalid: %s", err))
return &types.MsgUpdateCronJobResponse{}, err
Expand Down Expand Up @@ -98,10 +90,6 @@ func (k msgServer) UpdateCronJob(goCtx context.Context, msg *types.MsgUpdateCron

func (k msgServer) DeleteCronJob(goCtx context.Context, msg *types.MsgDeleteCronJob) (*types.MsgDeleteCronJobResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)
if !params.EnableCron {
return &types.MsgDeleteCronJobResponse{}, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Cron is disabled")
}
if err := msg.ValidateBasic(); err != nil {
ctx.Logger().Error(fmt.Sprintf("request invalid: %s", err))
return &types.MsgDeleteCronJobResponse{}, err
Expand Down Expand Up @@ -131,3 +119,24 @@ func (k msgServer) DeleteCronJob(goCtx context.Context, msg *types.MsgDeleteCron
k.SetCronJob(ctx, cronJob)
return &types.MsgDeleteCronJobResponse{}, nil
}

func (k msgServer) ToggleCronJob(goCtx context.Context, msg *types.MsgToggleCronJob) (*types.MsgToggleCronJobResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := msg.ValidateBasic(); err != nil {
ctx.Logger().Error(fmt.Sprintf("request invalid: %s", err))
return &types.MsgToggleCronJobResponse{}, err
}
// check if sender is authorized
exists := k.CheckSecurityAddress(ctx, msg.SecurityAddress)
if !exists {
return &types.MsgToggleCronJobResponse{}, sdkerrors.ErrUnauthorized
}
// Get the cron job
cronJob, found := k.GetCronJob(ctx, msg.Id)
if !found {
return &types.MsgToggleCronJobResponse{}, errorsmod.Wrapf(sdkerrors.ErrNotFound, "cron job not found")
}
cronJob.EnableCron = !cronJob.EnableCron
k.SetCronJob(ctx, cronJob)
return &types.MsgToggleCronJobResponse{}, nil
}
2 changes: 1 addition & 1 deletion x/cron/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestGetParams(t *testing.T) {
appd := app.Setup(t, false)
ctx := appd.BaseApp.NewContext(false, tmproto.Header{})
defaultSecurityAddress := []string{"cosmos1xkxed7rdzvmyvgdshpe445ddqwn47fru24fnlp"}
params := cronTypes.Params{SecurityAddress: defaultSecurityAddress, EnableCron: true}
params := cronTypes.Params{SecurityAddress: defaultSecurityAddress}
appd.CronKeeper.SetParams(ctx, params)
require.EqualValues(t, params, appd.CronKeeper.GetParams(ctx))
}
4 changes: 2 additions & 2 deletions x/cron/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func (k Keeper) SetCronID(ctx sdk.Context, id uint64) {
var (
store = k.Store(ctx)
key = types.CronIDKey
key = types.LastCronIDKey
value = k.cdc.MustMarshal(
&protobuftypes.UInt64Value{
Value: id,
Expand All @@ -22,7 +22,7 @@ func (k Keeper) SetCronID(ctx sdk.Context, id uint64) {
func (k Keeper) GetCronID(ctx sdk.Context) uint64 {
var (
store = k.Store(ctx)
key = types.CronIDKey
key = types.LastCronIDKey
value = store.Get(key)
)
if value == nil {
Expand Down
2 changes: 1 addition & 1 deletion x/cron/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (a AppModuleBasic) GetTxCmd() *cobra.Command {

// GetQueryCmd returns the capability module's root query command.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd(types.StoreKey)
return cli.GetQueryCmd()
}

// ----------------------------------------------------------------------------
Expand Down
22 changes: 20 additions & 2 deletions x/cron/spec/01_concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ In the above tx -
- `contract address` - CosmWasm contract address.
- `json_msg` - sudo msg of the contract in json format

> Note : only the security address and contract admin are authorized can update the cron job
> Note : only the security address are authorized can update the cron job
### Update cron job

Expand All @@ -67,4 +67,22 @@ In the above tx -
- `id` - id of the cron job
- `contract address` - CosmWasm contract address.

> Note : only the security address and contract admin are authorized can delete the cron job
> Note : only the security address are authorized can delete the cron job
### Toggle cron job

```console
foo@bar:~$ wasmrollappd tx cron toggle-cron-job [id]
```

e.g

```console
foo@bar:~$ wasmrollappd tx cron toggle-cron-job 1 100000000awasm --from cooluser --chain-id test-1
```

In the above tx -

- `id` - id of the cron job

> Note : only the security address are authorized can toggle the cron job
5 changes: 2 additions & 3 deletions x/cron/spec/02_state.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ message CronJob {
string description = 3;
// Msgs that will be executed every period amount of time
repeated MsgContractCron msg_contract_cron = 4 [(gogoproto.nullable) = false];
// set cron enabled or not
bool enable_cron = 5;
}
```

Expand Down Expand Up @@ -61,9 +63,6 @@ message Params {
(gogoproto.jsontag) = "security_address,omitempty",
(gogoproto.moretags) = "yaml:\"security_address\""
];

// set module enabled or not
bool enable_cron = 2;
}
```

Expand Down
11 changes: 6 additions & 5 deletions x/cron/spec/03_clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ The CLI has been updated with new queries and transactions for the `x/cron` modu

### Transactions

| Command | Subcommand | Arguments | Description |
| :--------------------- | :---------------- | :------------------------------------------------- | :----------------------------------- |
| `wasmrollappd tx cron` | `register-cron` | [name] [description] [contract_address] [json_msg] | Register the cron job |
| `wasmrollappd tx cron` | `update-cron-job` | [id] [contract_address] [json_msg] | update the cron job |
| `wasmrollappd tx cron` | `delete-cron-job` | [id] [contract_address] | delete the cron job for the contract |
| Command | Subcommand | Arguments | Description |
| :--------------------- | :---------------- | :------------------------------------------------- | :---------------------------------------- |
| `wasmrollappd tx cron` | `register-cron` | [name] [description] [contract_address] [json_msg] | Register the cron job |
| `wasmrollappd tx cron` | `update-cron-job` | [id] [contract_address] [json_msg] | update the cron job |
| `wasmrollappd tx cron` | `delete-cron-job` | [id] [contract_address] | delete the cron job for the contract |
| `wasmrollappd tx cron` | `toggle-cron-job` | [id] | Toggle the cron job for the given cron ID |
2 changes: 2 additions & 0 deletions x/cron/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgRegisterCron{}, "wasmrollapp/cron/MsgRegisterCron", nil)
cdc.RegisterConcrete(&MsgUpdateCronJob{}, "wasmrollapp/cron/MsgUpdateCronJob", nil)
cdc.RegisterConcrete(&MsgDeleteCronJob{}, "wasmrollapp/cron/MsgDeleteCronJob", nil)
cdc.RegisterConcrete(&MsgToggleCronJob{}, "wasmrollapp/cron/MsgToggleCronJob", nil)
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
Expand All @@ -21,6 +22,7 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
&MsgRegisterCron{},
&MsgUpdateCronJob{},
&MsgDeleteCronJob{},
&MsgToggleCronJob{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}
Expand Down
Loading

0 comments on commit d415177

Please sign in to comment.