Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikluke committed May 20, 2024
1 parent 5cc09b9 commit cc94f15
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 264 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ install: go.sum
@echo "DONE"


OS := $(shell go env | grep GOOS | sed -E 's:.*="(.*)":\1:')
ARCH := $(shell go env | grep GOARCH | sed -E 's:.*="(.*)":\1:')
OS := $(shell go env | grep GOOS | sed -E "s:.*='(.*)':\1:")
ARCH := $(shell go env | grep GOARCH | sed -E "s:.*='(.*)':\1:")

.PHONY: build
build:
Expand Down Expand Up @@ -161,7 +161,7 @@ local-init:
$(V)rm -rf test

@echo initalizing test chain
$(V)$(SCORUMD) init --staking-bond-denom nsp local-network
$(V)$(SCORUMD) init --default-denom nsp local-network

@echo adding key 'test'
$(V)$(SCORUMD) keys add test --keyring-backend test --keyring-dir test
Expand Down
36 changes: 13 additions & 23 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/x/auth/posthandler"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"

Expand Down Expand Up @@ -153,8 +151,8 @@ var (
// and genesis verification.
ModuleBasics = module.NewBasicManager(
auth.AppModuleBasic{},
genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
authzmodule.AppModuleBasic{},
genutil.AppModuleBasic{},
bank.AppModuleBasic{},
capability.AppModuleBasic{},
staking.AppModuleBasic{},
Expand Down Expand Up @@ -311,7 +309,7 @@ func New(
authtypes.StoreKey, authz.ModuleName, banktypes.StoreKey, stakingtypes.StoreKey, crisistypes.StoreKey,
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey, govtypes.StoreKey,
paramstypes.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey, evidencetypes.StoreKey,
capabilitytypes.StoreKey, group.StoreKey, nft.StoreKey,
capabilitytypes.StoreKey, group.StoreKey, nft.StoreKey, consensusparamtypes.StoreKey,
// ibc
ibcexported.StoreKey, ibcfeetypes.StoreKey,
icahosttypes.StoreKey, icacontrollertypes.StoreKey,
Expand All @@ -338,6 +336,13 @@ func New(
memKeys: memKeys,
}

app.ParamsKeeper = initParamsKeeper(
appCodec,
legacyAmino,
app.keys[paramstypes.StoreKey],
app.tkeys[paramstypes.TStoreKey],
)

// set the BaseApp's parameter store
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(
appCodec, keys[consensusparamtypes.StoreKey],
Expand Down Expand Up @@ -500,7 +505,8 @@ func New(
)

app.ICAHostKeeper = icahostkeeper.NewKeeper(
appCodec, keys[icahosttypes.StoreKey],
appCodec,
keys[icahosttypes.StoreKey],
app.GetSubspace(icahosttypes.SubModuleName),
app.IBCFeeKeeper,
app.IBCKeeper.ChannelKeeper,
Expand Down Expand Up @@ -578,11 +584,8 @@ func New(
govConfig,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.GovKeeper = *govKeeper.SetHooks(
govtypes.NewMultiGovHooks(
// register the governance hooks
),
)
govKeeper.SetLegacyRouter(govRouter)
app.GovKeeper = *govKeeper

/**** IBC Routing ****/

Expand Down Expand Up @@ -830,8 +833,6 @@ func New(
app.ScopedICAHostKeeper = scopedICAHostKeeper
app.ScopedICAControllerKeeper = scopedICAControllerKeeper

app.setPostHandler()

// create the simulation manager and define the order of the modules for deterministic simulations
app.sm = module.NewSimulationManager(
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
Expand Down Expand Up @@ -860,17 +861,6 @@ func New(
return app
}

func (app *App) setPostHandler() {
postHandler, err := posthandler.NewPostHandler(
posthandler.HandlerOptions{},
)
if err != nil {
panic(err)
}

app.SetPostHandler(postHandler)
}

// Name returns the name of the App
func (app *App) Name() string { return app.BaseApp.Name() }

Expand Down
75 changes: 75 additions & 0 deletions app/sim/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package sim

import (
"flag"

"github.com/cosmos/cosmos-sdk/types/simulation"
)

// List of available flags for the simulator
var (
FlagGenesisFileValue string
FlagParamsFileValue string
FlagExportParamsPathValue string
FlagExportParamsHeightValue int
FlagExportStatePathValue string
FlagExportStatsPathValue string
FlagSeedValue int64
FlagInitialBlockHeightValue int
FlagNumBlocksValue int
FlagBlockSizeValue int
FlagLeanValue bool
FlagCommitValue bool
FlagOnOperationValue bool // TODO: Remove in favor of binary search for invariant violation
FlagAllInvariantsValue bool

FlagEnabledValue bool
FlagVerboseValue bool
FlagPeriodValue uint
FlagGenesisTimeValue int64
)

// GetSimulatorFlags gets the values of all the available simulation flags
func GetSimulatorFlags() {
// config fields
flag.StringVar(&FlagGenesisFileValue, "Genesis", "", "custom simulation genesis file; cannot be used with params file")
flag.StringVar(&FlagParamsFileValue, "Params", "", "custom simulation params file which overrides any random params; cannot be used with genesis")
flag.StringVar(&FlagExportParamsPathValue, "ExportParamsPath", "", "custom file path to save the exported params JSON")
flag.IntVar(&FlagExportParamsHeightValue, "ExportParamsHeight", 0, "height to which export the randomly generated params")
flag.StringVar(&FlagExportStatePathValue, "ExportStatePath", "", "custom file path to save the exported app state JSON")
flag.StringVar(&FlagExportStatsPathValue, "ExportStatsPath", "", "custom file path to save the exported simulation statistics JSON")
flag.Int64Var(&FlagSeedValue, "Seed", 42, "simulation random seed")
flag.IntVar(&FlagInitialBlockHeightValue, "InitialBlockHeight", 1, "initial block to start the simulation")
flag.IntVar(&FlagNumBlocksValue, "NumBlocks", 500, "number of new blocks to simulate from the initial block height")
flag.IntVar(&FlagBlockSizeValue, "BlockSize", 200, "operations per block")
flag.BoolVar(&FlagLeanValue, "Lean", false, "lean simulation log output")
flag.BoolVar(&FlagCommitValue, "Commit", false, "have the simulation commit")
flag.BoolVar(&FlagOnOperationValue, "SimulateEveryOperation", false, "run slow invariants every operation")
flag.BoolVar(&FlagAllInvariantsValue, "PrintAllInvariants", false, "print all invariants if a broken invariant is found")

// simulation flags
flag.BoolVar(&FlagEnabledValue, "Enabled", false, "enable the simulation")
flag.BoolVar(&FlagVerboseValue, "Verbose", false, "verbose log output")
flag.UintVar(&FlagPeriodValue, "Period", 0, "run slow invariants only once every period assertions")
flag.Int64Var(&FlagGenesisTimeValue, "GenesisTime", 0, "override genesis UNIX time instead of using a random UNIX time")
}

// NewConfigFromFlags creates a simulation from the retrieved values of the flags.
func NewConfigFromFlags() simulation.Config {
return simulation.Config{
GenesisFile: FlagGenesisFileValue,
ParamsFile: FlagParamsFileValue,
ExportParamsPath: FlagExportParamsPathValue,
ExportParamsHeight: FlagExportParamsHeightValue,
ExportStatePath: FlagExportStatePathValue,
ExportStatsPath: FlagExportStatsPathValue,
Seed: FlagSeedValue,
InitialBlockHeight: FlagInitialBlockHeightValue,
NumBlocks: FlagNumBlocksValue,
BlockSize: FlagBlockSizeValue,
Lean: FlagLeanValue,
Commit: FlagCommitValue,
OnOperation: FlagOnOperationValue,
AllInvariants: FlagAllInvariantsValue,
}
}
44 changes: 18 additions & 26 deletions app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ import (
"os"
"testing"

"github.com/scorum/cosmos-network/app/sim"

"github.com/cosmos/cosmos-sdk/testutil/sims"

dbm "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
simulationtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/scorum/cosmos-network/app"
"github.com/stretchr/testify/require"
)

const ChainID = "scorum"

func init() {
sims.GetSimulatorFlags()
sim.GetSimulatorFlags()
}

// interBlockCacheOpt returns a BaseApp option function that sets the persistent
Expand All @@ -33,15 +36,15 @@ func interBlockCacheOpt() func(*baseapp.BaseApp) {
}

func TestAppStateDeterminism(t *testing.T) {
config := sims.NewConfigFromFlags()
config := sim.NewConfigFromFlags()
config.InitialBlockHeight = 1
config.ExportParamsPath = ""
config.OnOperation = false
config.AllInvariants = true
config.NumBlocks = 250
config.BlockSize = 100
config.Commit = true
config.ChainID = sims.SimAppChainID
config.ChainID = ChainID

numSeeds := 5
numTimesToRunPerSeed := 3
Expand All @@ -52,24 +55,24 @@ func TestAppStateDeterminism(t *testing.T) {

for j := 0; j < numTimesToRunPerSeed; j++ {
var logger log.Logger
if sims.FlagVerboseValue {
if sim.FlagVerboseValue {
logger = log.TestingLogger()
} else {
logger = log.NewNopLogger()
}

db := dbm.NewMemDB()
app := app.New(
simapp := app.New(
logger,
db,
nil,
true,
map[int64]bool{},
sims.DefaultNodeHome,
sims.FlagPeriodValue,
testutil.TestEncodingConfig(),
app.DefaultNodeHome,
sim.FlagPeriodValue,
sims.AppOptionsMap{},
interBlockCacheOpt(),
baseapp.SetChainID(ChainID),
)

fmt.Printf(
Expand All @@ -80,21 +83,17 @@ func TestAppStateDeterminism(t *testing.T) {
_, _, err := simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
sims.AppStateFn(app.AppCodec(), app.SimulationManager()),
simapp.BaseApp,
sims.AppStateFn(simapp.AppCodec(), simapp.SimulationManager(), app.NewDefaultGenesisState(simapp.AppCodec())),
simulationtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
sims.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
sims.SimulationOperations(simapp, simapp.AppCodec(), config),
simapp.ModuleAccountAddrs(),
config,
app.AppCodec(),
simapp.AppCodec(),
)
require.NoError(t, err)

if config.Commit {
PrintStats(db)
}

appHash := app.LastCommitID().Hash
appHash := simapp.LastCommitID().Hash
appHashList[j] = appHash

if j != 0 {
Expand All @@ -106,10 +105,3 @@ func TestAppStateDeterminism(t *testing.T) {
}
}
}

// PrintStats prints the corresponding statistics from the app DB.
func PrintStats(db dbm.DB) {
fmt.Println("\nLevelDB Stats")
fmt.Println(db.Stats()["leveldb.stats"])
fmt.Println("LevelDB cached block size", db.Stats()["leveldb.cachedblock"])
}
7 changes: 0 additions & 7 deletions app/upgrade/v110/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgrade "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibctmmigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations"
)

const Name = "v1.0.1 Scorum Upgrade"
Expand All @@ -14,12 +13,6 @@ func Handler(
mm *module.Manager,
) func(ctx sdk.Context, _ upgrade.Plan, _ module.VersionMap) (module.VersionMap, error) {
return func(ctx sdk.Context, _ upgrade.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// prune expired tendermint consensus states to save storage space
_, err := ibctmmigrations.PruneExpiredConsensusStates(ctx, app.Codec, app.IBCKeeper.ClientKeeper)
if err != nil {
return nil, err
}

return mm.RunMigrations(ctx, cfg, fromVM)
}
}
17 changes: 12 additions & 5 deletions cmd/scorumd/cmd/collectgentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"

"github.com/scorum/cosmos-network/app"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/types"
Expand All @@ -18,7 +20,12 @@ import (
)

func CollectGenTxsCmd(genBalIterator banktypes.GenesisBalancesIterator, defaultNodeHome string) *cobra.Command {
cmd := genutilcli.CollectGenTxsCmd(genBalIterator, defaultNodeHome)
gentxModule, ok := app.ModuleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic)
if !ok {
panic(fmt.Errorf("expected %s module to be an instance of type %T", genutiltypes.ModuleName, genutil.AppModuleBasic{}))
}

cmd := genutilcli.CollectGenTxsCmd(genBalIterator, defaultNodeHome, gentxModule.GenTxValidator)

cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
Expand All @@ -31,10 +38,10 @@ func CollectGenTxsCmd(genBalIterator banktypes.GenesisBalancesIterator, defaultN
}

bankGenState := banktypes.GetGenesisStateFromAppState(cdc, appState)
bankGenState.Params.SendEnabled = append(
bankGenState.Params.SendEnabled,
&banktypes.SendEnabled{Denom: scorumtypes.SPDenom, Enabled: false},
&banktypes.SendEnabled{Denom: scorumtypes.GasDenom, Enabled: false},
bankGenState.SendEnabled = append(
bankGenState.SendEnabled,
banktypes.SendEnabled{Denom: scorumtypes.SPDenom, Enabled: false},
banktypes.SendEnabled{Denom: scorumtypes.GasDenom, Enabled: false},
)

bankGenStateBz, err := cdc.MarshalJSON(bankGenState)
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/cometbft/cometbft-db v0.12.1-0.20240508134034-e9d8487735c2
github.com/cosmos/cosmos-sdk v0.47.11
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.5.0
github.com/cosmos/ibc-go/v7 v7.4.0
github.com/gogo/protobuf v1.3.3
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
Expand All @@ -24,7 +24,6 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80
google.golang.org/grpc v1.62.1
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -171,6 +170,7 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.6.0 // indirect
Expand All @@ -191,3 +191,5 @@ require (
)

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

replace golang.org/x/exp => golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb // remove on bump to v0.50
Loading

0 comments on commit cc94f15

Please sign in to comment.