Skip to content

Commit

Permalink
Merge pull request #7 from AllInBetsCom/cron-fix
Browse files Browse the repository at this point in the history
cron module rework
  • Loading branch information
rockstarRhino authored Jun 12, 2024
2 parents aa85548 + 5a77676 commit 4b8d0e8
Show file tree
Hide file tree
Showing 38 changed files with 2,360 additions and 1,057 deletions.
4 changes: 2 additions & 2 deletions app/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func Setup(t *testing.T, isCheckTx bool) *App {
DefaultSecurityAddress := []string{"cosmos1xkxed7rdzvmyvgdshpe445ddqwn47fru24fnlp"}
params := cronTypes.Params{SecurityAddress: DefaultSecurityAddress}
cronGenesis := cronTypes.GenesisState{
Params: params,
WhitelistedContracts: []cronTypes.WhitelistedContract{},
Params: params,
CronJobs: []cronTypes.CronJob{},
}
genesisState[cronTypes.ModuleName] = app.AppCodec().MustMarshalJSON(&cronGenesis)

Expand Down
33 changes: 19 additions & 14 deletions proto/wasmrollapp/cron/v1beta1/cron.proto
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
syntax = "proto3";
package wasmrollapp.cron.v1beta1;

import "gogoproto/gogo.proto";

option go_package = "github.com/dymensionxyz/rollapp-wasm/x/cron/types";

message WhitelistedContract {
// game_id is the unique identifier for the game
uint64 game_id = 1;
// security_address is the address of the security contract
string security_address = 2;
// contract_admin is the address of the contract admin
string contract_admin = 3;
// game_name is the name of the game
string game_name = 4;
// contract_address is the address of the contract
string contract_address = 5;
// game_type is the type of the game, single player or multiplayer or both
uint64 game_type = 6;
}
message CronJob {
// id is the unique identifier for the cron job
uint64 id = 1;
// name is the name of the cron job
string name = 2;
// description is the description of the cron job
string description = 3;
// Msgs that will be executed every period amount of time
repeated MsgContractCron msg_contract_cron = 4 [(gogoproto.nullable) = false];
}

message MsgContractCron {
// Contract is the address of the smart contract
string contract_address = 1;
// Msg is json encoded message to be passed to the contract
string json_msg = 2;
}
4 changes: 2 additions & 2 deletions proto/wasmrollapp/cron/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ message GenesisState {
(gogoproto.moretags) = "yaml:\"params\"",
(gogoproto.nullable) = false
];
repeated WhitelistedContract whitelisted_contracts = 2 [
(gogoproto.moretags) = "yaml:\"whitelisted_contracts\"",
repeated CronJob cron_jobs = 2 [
(gogoproto.moretags) = "yaml:\"cron_jobs\"",
(gogoproto.nullable) = false
];
}
41 changes: 29 additions & 12 deletions proto/wasmrollapp/cron/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ service Query {
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/wasmrollapp/cron/v1beta1/params";
}
// WhitelistedContracts queries the whitelisted contracts of the module.
rpc QueryWhitelistedContracts(QueryWhitelistedContractsRequest) returns (QueryWhitelistedContractsResponse) {
option (google.api.http).get = "/wasmrollapp/cron/v1beta1/whitelisted_contracts";
// Cron queries a cron by id.
rpc Cron(QueryCronRequest) returns (QueryCronResponse) {
option (google.api.http).get = "/wasmrollapp/cron/v1beta1/crons/{id}";
}
// Crons queries all the crons.
rpc Crons(QueryCronsRequest) returns (QueryCronsResponse) {
option (google.api.http).get = "/wasmrollapp/cron/v1beta1/crons";
}
}

Expand All @@ -30,16 +34,29 @@ message QueryParamsResponse {
Params params = 1 [(gogoproto.nullable) = false];
}

// QueryWhitelistedContractsRequest is request type for the Query/WhitelistedContracts RPC method.
message QueryWhitelistedContractsRequest{
cosmos.base.query.v1beta1.PageRequest pagination = 1
[(gogoproto.moretags) = "yaml:\"pagination\""];
// QueryCronRequest is request type for the Query/Cron RPC method.
message QueryCronRequest {
// id defines the id of the cron to query.
uint64 id = 1;
}

// QueryCronResponse is response type for the Query/Cron RPC method.
message QueryCronResponse {
// cron defines the cron of the requested id.
CronJob cron_job = 1 [(gogoproto.nullable) = false];
}

// QueryCronsRequest is request type for the Query/Crons RPC method.
message QueryCronsRequest {
// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

// QueryWhitelistedContractsResponse is response type for the Query/WhitelistedContracts RPC method.
message QueryWhitelistedContractsResponse {
repeated WhitelistedContract whilisted_contracts = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2
[(gogoproto.moretags) = "yaml:\"pagination\""];
// QueryCronsResponse is response type for the Query/Crons RPC method.
message QueryCronsResponse {
// crons defines all the crons.
repeated CronJob cron_jobs = 1 [(gogoproto.nullable) = false];
// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

59 changes: 41 additions & 18 deletions proto/wasmrollapp/cron/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,53 @@ option go_package = "github.com/dymensionxyz/rollapp-wasm/x/cron/types";

// Msg defines the Msg service.
service Msg {
rpc RegisterContract(MsgRegisterContract) returns(MsgRegisterContractResponse);
rpc DeregisterContract(MsgDeregisterContract) returns(MsgDeregisterContractResponse);
rpc RegisterCron(MsgRegisterCron) returns(MsgRegisterCronResponse);
rpc UpdateCronJob(MsgUpdateCronJob) returns(MsgUpdateCronJobResponse);
rpc DeleteCronJob(MsgDeleteCronJob) returns(MsgDeleteCronJobResponse);
}

// MsgRegisterContract defines the Msg/RegisterContract request type.
message MsgRegisterContract {
// security_address is the address of the security module
// MsgRegisterCron defines the Msg/RegisterCron request type.
message MsgRegisterCron {
// security_address is the address authorised to register the cron job
string security_address = 1;
// game_name is the name of the game
string game_name = 2;
// name is the name of the cron job
string name = 2;
// description is the description of the cron job
string description = 3;
// contract_address is the address of the contract
string contract_address = 3;
// game_type is the type of the game 1 -> single, 2 -> multi, 3 -> both
uint64 game_type = 4;
}
string contract_address = 4;
// Msg is json encoded message to be passed to the contract
string json_msg = 5;
}

// MsgRegisterContractResponse defines the Msg/RegisterContract response type.
message MsgRegisterContractResponse {}
// MsgRegisterCronResponse defines the Msg/RegisterCron response type.
message MsgRegisterCronResponse {}

// MsgUpdateCronJob defines the Msg/UpdateCronJob request type.
message MsgUpdateCronJob {
// security_address is the address authorised to update the cron job
string security_address = 1;
// id is the unique identifier for the cron job
uint64 id = 2;
// contract_address is the address of the contract
string contract_address = 3;
// Msg is json encoded message to be passed to the contract
string json_msg = 4;
}

// MsgUpdateCronJobResponse defines the Msg/UpdateCron response type.
message MsgUpdateCronJobResponse {}

// MsgDeregisterContract defines the Msg/DeregisterContract request type.
message MsgDeregisterContract {
// MsgDeleteCronJob defines the Msg/DeleteCronJob request type.
message MsgDeleteCronJob {
// security_address is the address authorised to delete the cron job
string security_address = 1;
uint64 game_id = 2;
// id is the unique identifier for the cron job
uint64 id = 2;
// contract_address is the address of the contract
string contract_address = 3;

}

// MsgDeregisterContractResponse defines the Msg/DeregisterContract response type.
message MsgDeregisterContractResponse {}
// MsgDeleteCronJobResponse defines the Msg/DeleteCronJob response type.
message MsgDeleteCronJobResponse {}
20 changes: 9 additions & 11 deletions x/cron/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ package cron
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/rollapp-wasm/x/cron/keeper"
cronTypes "github.com/dymensionxyz/rollapp-wasm/x/cron/types"
)

func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
whitelistedContracts := k.GetWhitelistedContracts(ctx)

for _, data := range whitelistedContracts {
if data.GameType == 1 {
k.SinglePlayer(ctx, data.ContractAddress, cronTypes.ResolveSinglePlayer, data.GameName)
} else if data.GameType == 2 {
k.MultiPlayer(ctx, data.ContractAddress, cronTypes.SetupMultiPlayer, cronTypes.ResolveMultiPlayer, data.GameName)
} else {
k.SinglePlayer(ctx, data.ContractAddress, cronTypes.ResolveSinglePlayer, data.GameName)
k.MultiPlayer(ctx, data.ContractAddress, cronTypes.SetupMultiPlayer, cronTypes.ResolveMultiPlayer, data.GameName)
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)
}
}
}
}
81 changes: 60 additions & 21 deletions x/cron/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package cli

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"


"github.com/dymensionxyz/rollapp-wasm/x/cron/types"
"github.com/spf13/cobra"
"strconv"
)

// GetQueryCmd returns the cli query commands for this module
Expand All @@ -23,41 +21,82 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryParams(),
QueryWhitelistedContracts())
// this line is used by starport scaffolding # 1
cmd.AddCommand(CmdQueryCron(),
CmdQueryCrons(),
CmdQueryParams())

return cmd
}

func QueryWhitelistedContracts() *cobra.Command {
// CmdQueryCron implements the cron query command.
func CmdQueryCron() *cobra.Command {
cmd := &cobra.Command{
Use: "whitelisted-contracts",
Short: "Query all whitelisted contracts",
Use: "cron [id]",
Short: "Query a cron by id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientQueryContext(cmd)
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pagination, err := client.ReadPageRequest(cmd.Flags())
queryClient := types.NewQueryClient(clientCtx)
cronID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("cron-id '%s' not a valid uint", args[0])
}
params := &types.QueryCronRequest{
Id: cronID,
}
res, err := queryClient.Cron(cmd.Context(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

queryClient := types.NewQueryClient(ctx)

res, err := queryClient.QueryWhitelistedContracts(cmd.Context(), &types.QueryWhitelistedContractsRequest{
Pagination: pagination,
})
// CmdQueryCrons implements the crons query command.
func CmdQueryCrons() *cobra.Command {
cmd := &cobra.Command{
Use: "crons",
Short: "Query all crons",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Crons(cmd.Context(), &types.QueryCronsRequest{})
if err != nil {
return err
}
return ctx.PrintProto(res)
return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "whitelisted-contracts")
return cmd
}

// CmdQueryParams implements the params query command.
func CmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "shows the parameters of the module",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
34 changes: 0 additions & 34 deletions x/cron/client/cli/query_params.go

This file was deleted.

Loading

0 comments on commit 4b8d0e8

Please sign in to comment.