Skip to content

Commit

Permalink
Merge pull request #4 from AllInBetsCom/cron-fix
Browse files Browse the repository at this point in the history
module fixes + testcases post review
  • Loading branch information
rockstarRhino authored May 24, 2024
2 parents 20bdb67 + f57c84f commit 6357e42
Show file tree
Hide file tree
Showing 39 changed files with 575 additions and 419 deletions.
5 changes: 2 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ import (
"github.com/dymensionxyz/rollapp-wasm/x/cron"
cronkeeper "github.com/dymensionxyz/rollapp-wasm/x/cron/keeper"
crontypes "github.com/dymensionxyz/rollapp-wasm/x/cron/types"

// TODO: revisit
// "github.com/dymensionxyz/rollapp-wasm/x/gasless"
// gaslessclient "github.com/dymensionxyz/rollapp-wasm/x/gasless/client"
Expand Down Expand Up @@ -601,7 +600,7 @@ func NewRollapp(
upgrade.NewAppModule(app.UpgradeKeeper),
hubgenesis.NewAppModule(appCodec, app.HubGenesisKeeper, app.AccountKeeper),
denommetadata.NewAppModule(app.DenomMetadataKeeper, app.BankKeeper),
cron.NewAppModule(appCodec, app.CronKeeper, app.AccountKeeper, app.BankKeeper, app.WasmKeeper),
cron.NewAppModule(appCodec, app.CronKeeper, app.WasmKeeper),
// TODO: revisit
// gasless.NewAppModule(appCodec, app.GaslessKeeper, app.AccountKeeper, app.BankKeeper),
}
Expand Down Expand Up @@ -785,7 +784,7 @@ func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.Wa
SignModeHandler: txConfig.SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
IBCKeeper: app.IBCKeeper,
IBCKeeper: app.IBCKeeper,
// TODO: revisit
// GaslessKeeper: app.GaslessKeeper,
// BankKeeper: app.BankKeeper,
Expand Down
117 changes: 117 additions & 0 deletions app/test_suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package app

import (
"encoding/json"
"testing"
"time"

"github.com/CosmWasm/wasmd/x/wasm"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
sequencerstypes "github.com/dymensionxyz/dymension-rdk/x/sequencers/types"
cronTypes "github.com/dymensionxyz/rollapp-wasm/x/cron/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
)

// DefaultConsensusParams defines the default Tendermint consensus params used in
// App testing.
var DefaultConsensusParams = &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxBytes: 200000,
MaxGas: -1,
},
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
MaxBytes: 10000,
},
Validator: &tmproto.ValidatorParams{
PubKeyTypes: []string{
tmtypes.ABCIPubKeyTypeEd25519,
},
},
}

var (
ProposerPK = simapp.CreateTestPubKeys(1)[0]
ProposerConsAddr = sdk.ConsAddress(ProposerPK.Address())

OperatorPK = secp256k1.GenPrivKey().PubKey()
)

var TestChainID = "rollappwasm_1234-1"

func setup(withGenesis bool, invCheckPeriod uint) (*App, map[string]json.RawMessage) {
db := dbm.NewMemDB()

encCdc := MakeEncodingConfig()
var emptyWasmOpts []wasm.Option
testApp := NewRollapp(
log.NewNopLogger(), db, nil, true, map[int64]bool{}, "/tmp", invCheckPeriod, encCdc, GetEnabledProposals(), EmptyAppOptions{}, emptyWasmOpts,
)
if withGenesis {
return testApp, NewDefaultGenesisState(encCdc.Codec)
}
return testApp, map[string]json.RawMessage{}
}

// Setup initializes a new Rollapp. A Nop logger is set in Rollapp.
func Setup(t *testing.T, isCheckTx bool) *App {
t.Helper()

pk, err := cryptocodec.ToTmProtoPublicKey(ProposerPK)
require.NoError(t, err)

app, genesisState := setup(true, 5)

// setup for sequencer
seqGenesis := sequencerstypes.GenesisState{
Params: sequencerstypes.DefaultParams(),
GenesisOperatorAddress: sdk.ValAddress(OperatorPK.Address()).String(),
}
genesisState[sequencerstypes.ModuleName] = app.AppCodec().MustMarshalJSON(&seqGenesis)

// setup for cron
DefaultSecurityAddress := []string{"cosmos1xkxed7rdzvmyvgdshpe445ddqwn47fru24fnlp"}
params := cronTypes.Params{SecurityAddress: DefaultSecurityAddress}
cronGenesis := cronTypes.GenesisState{
Params: params,
WhitelistedContracts: []cronTypes.WhitelistedContract{},
}
genesisState[cronTypes.ModuleName] = app.AppCodec().MustMarshalJSON(&cronGenesis)

// for now bank genesis won't be set here, funding accounts should be called with fund utils.FundModuleAccount

stateBytes, err := json.MarshalIndent(genesisState, "", " ")
require.NoError(t, err)
// init chain will set the validator set and initialize the genesis accounts
app.InitChain(
abci.RequestInitChain{
Time: time.Time{},
ChainId: TestChainID,
ConsensusParams: DefaultConsensusParams,
Validators: []abci.ValidatorUpdate{
{PubKey: pk, Power: 1},
},
AppStateBytes: stateBytes,
InitialHeight: 0,
},
)

return app
}

// EmptyAppOptions is a stub implementing AppOptions
type EmptyAppOptions struct{}

// Get implements AppOptions
func (ao EmptyAppOptions) Get(o string) interface{} {
return nil
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ require (
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/tendermint/tendermint v0.34.29
github.com/tendermint/tm-db v0.6.8-0.20220506192307-f628bb5dc95b
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe
google.golang.org/grpc v1.61.0
)
Expand Down Expand Up @@ -255,7 +257,6 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down Expand Up @@ -283,7 +284,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package aib.cron.v1beta1;
package wasmrollapp.cron.v1beta1;

import "gogoproto/gogo.proto";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
syntax = "proto3";
package aib.cron.v1beta1;
package wasmrollapp.cron.v1beta1;

import "gogoproto/gogo.proto";
import "aib/cron/v1beta1/params.proto";
import "aib/cron/v1beta1/cron.proto";
import "wasmrollapp/cron/v1beta1/params.proto";
import "wasmrollapp/cron/v1beta1/cron.proto";

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package aib.cron.v1beta1;
package wasmrollapp.cron.v1beta1;

import "gogoproto/gogo.proto";

Expand All @@ -12,9 +12,4 @@ message Params {
(gogoproto.jsontag) = "security_address,omitempty",
(gogoproto.moretags) = "yaml:\"security_address\""
];

uint64 contract_gas_limit = 2 [
(gogoproto.jsontag) = "contract_gas_limit,omitempty",
(gogoproto.moretags) = "yaml:\"contract_gas_limit\""
];
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
syntax = "proto3";
package aib.cron.v1beta1;
package wasmrollapp.cron.v1beta1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "aib/cron/v1beta1/params.proto";
import "aib/cron/v1beta1/cron.proto";
import "wasmrollapp/cron/v1beta1/params.proto";
import "wasmrollapp/cron/v1beta1/cron.proto";

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

// Query defines the gRPC querier service.
service Query {
// Parameters queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/aib/cron/v1beta1/params";
option (google.api.http).get = "/wasmrollapp/cron/v1beta1/params";
}
rpc QueryWhitelistedContracts(QueryWhitelistedContractsRequest) returns (QueryWhitelistedContractsResponse) {
option (google.api.http).get = "/aib/cron/v1beta1/whitelisted_contracts";
option (google.api.http).get = "/wasmrollapp/cron/v1beta1/whitelisted_contracts";
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
syntax = "proto3";
package aib.cron.v1beta1;
package wasmrollapp.cron.v1beta1;
import "gogoproto/gogo.proto";

import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "aib/cron/v1beta1/params.proto";
import "wasmrollapp/cron/v1beta1/params.proto";


option go_package = "github.com/dymensionxyz/rollapp-wasm/x/cron/types";
Expand Down
4 changes: 2 additions & 2 deletions x/cron/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

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

for _, data := range allContracts {
for _, data := range whitelistedContracts {
if data.GameType == 1 {
k.SinglePlayer(ctx, data.ContractAddress, cronTypes.ResolveSinglePlayer, data.GameName)
} else if data.GameType == 2 {
Expand Down
12 changes: 1 addition & 11 deletions x/cron/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@ package cli
import (
"fmt"
"strconv"
"time"

"github.com/spf13/cobra"

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

var (
DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds())
)

const (
flagPacketTimeoutTimestamp = "packet-timeout-timestamp"
listSeparator = ","
"github.com/dymensionxyz/rollapp-wasm/x/cron/types"
)

// GetTxCmd returns the transaction commands for this module
Expand Down
25 changes: 25 additions & 0 deletions x/cron/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cron_test

import (
"testing"

"github.com/dymensionxyz/rollapp-wasm/app"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/dymensionxyz/rollapp-wasm/x/cron"
"github.com/dymensionxyz/rollapp-wasm/x/cron/types"
"github.com/stretchr/testify/require"
)

func TestGenesis(t *testing.T) {
appd := app.Setup(t, false)
ctx := appd.BaseApp.NewContext(false, tmproto.Header{})

genesisState := types.GenesisState{
Params: types.DefaultParams(),
}

cron.InitGenesis(ctx, appd.CronKeeper, genesisState)
got := cron.ExportGenesis(ctx, appd.CronKeeper)
require.NotNil(t, got)
}
5 changes: 4 additions & 1 deletion x/cron/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
// InitGenesis initializes the capability module's state from a provided genesis
// state.
func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
if err := genState.Validate(); err != nil {
panic(err)
}
var (
GameID uint64
)
Expand All @@ -28,6 +31,6 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {

return &types.GenesisState{
Params: k.GetParams(ctx),
WhitelistedContracts: k.GetAllContract(ctx),
WhitelistedContracts: k.GetWhitelistedContracts(ctx),
}
}
9 changes: 8 additions & 1 deletion x/cron/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package keeper
import (
"context"

"github.com/dymensionxyz/rollapp-wasm/x/cron/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/dymensionxyz/rollapp-wasm/x/cron/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -58,3 +58,10 @@ func (q QueryServer) QueryWhitelistedContracts(c context.Context, req *types.Que
Pagination: pagination,
}, nil
}

func (q QueryServer) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := q.Keeper.GetParams(ctx)

return &types.QueryParamsResponse{Params: params}, nil
}
19 changes: 0 additions & 19 deletions x/cron/keeper/grpc_query_params.go

This file was deleted.

8 changes: 2 additions & 6 deletions x/cron/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/hex"
"golang.org/x/exp/slices"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand Down Expand Up @@ -68,12 +69,7 @@ func (k Keeper) SudoContractCall(ctx sdk.Context, contractAddress string, p []by

func (k Keeper) CheckSecurityAddress(ctx sdk.Context, from string) bool {
params := k.GetParams(ctx)
for _, addr := range params.SecurityAddress {
if addr == from {
return true
}
}
return false
return slices.Contains(params.SecurityAddress, from)
}

func (k Keeper) Store(ctx sdk.Context) sdk.KVStore {
Expand Down
Loading

0 comments on commit 6357e42

Please sign in to comment.