Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simulation testing #469

Merged
merged 24 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c006e51
Added simulation
gjermundgaraba Feb 10, 2023
ad16825
lint
gjermundgaraba Feb 10, 2023
0def1df
Test fix
gjermundgaraba Feb 10, 2023
ad8391e
Linters are fun
gjermundgaraba Feb 10, 2023
65dec89
Added ci for simulation tests
gjermundgaraba Feb 13, 2023
da3a924
Added missing content in github action file
gjermundgaraba Feb 13, 2023
69d15d4
Merge branch 'main' into 295-simulation-testing
gjermundgaraba Feb 13, 2023
3069d48
dont stop on failure, upload logs as artifacts
gjermundgaraba Feb 13, 2023
642ae8e
day well spent
gjermundgaraba Feb 13, 2023
e7277b0
Remove artifact handling
gjermundgaraba Feb 13, 2023
a95425a
Remove try flag from earthly
gjermundgaraba Feb 13, 2023
dd9ed9a
Fix earthfile
gjermundgaraba Feb 13, 2023
32e108a
go back to old runsim
gjermundgaraba Feb 13, 2023
1e90b62
Speed up simulation testing a lot
gjermundgaraba Feb 14, 2023
372c6a4
lint
gjermundgaraba Feb 14, 2023
720425c
Fix export for sims
gjermundgaraba Feb 14, 2023
56d8a7e
Merge branch 'main' into 295-simulation-testing
gjermundgaraba Feb 14, 2023
a973ef3
Merge branch 'main' into 295-simulation-testing
gjermundgaraba Feb 17, 2023
eeede8d
Split up regular tests to make it a bit faster + add non-determinism …
gjermundgaraba Feb 17, 2023
b059c95
Update CODING_GUIDELINES with simulation related things
gjermundgaraba Feb 17, 2023
bb6872a
add modules for import/export sim testing
gjermundgaraba Feb 17, 2023
17babc8
Fix GH action chain test
gjermundgaraba Feb 17, 2023
b24d2e1
linting yo
gjermundgaraba Feb 17, 2023
367a677
Make it more probable to have collections with either all active or a…
gjermundgaraba Feb 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/chain_simulation_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ name: "Simulation tests on EmpowerChain"

on:
workflow_dispatch:
schedule:
- cron: "0 */2 * * *"

jobs:
sim-tests:
name: Run EmpowerChain simulation tests
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
test:
[
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/chain_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,28 @@ jobs:
flags: chain
fail_ci_if_error: true

smoke-test:
name: Smoke tests EmpowerChain
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- uses: earthly/actions-setup@v1
with:
version: v0.6.30

- run: earthly --ci +smoketest
working-directory: ./chain

non-determinism-test:
name: Simulation non-determinism tests EmpowerChain
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- uses: earthly/actions-setup@v1
with:
version: v0.6.30

- run: earthly --ci +test-sim-nondeterminism
working-directory: ./chain
16 changes: 8 additions & 8 deletions chain/CODING_GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ If you are not sure what a good way of doing something in particular, there are
3. Implement/Update `Validate()` on any non-rpc messages (i.e. data structures to be persisted in the keeper and in genesis)
4. If you created any new rpc messages:
- Implement the `sdk.Msg` interface in `msgs.go` (`_ sdk.Msg = &MsgName{}`)
- Update `WeightedOperations` in simulation/operations.go with the new operation
- Don't forget to create tests for `ValidateBasic`!
5. Implement `MsgServer`/`Querier` and `Keeper` methods
- Don't forget events
- Don't forget integration tests
- If you create public Keeper methods, they need to be tested as well
6. Update genesis if necessary (in genesis and keeper/genesis)
7. Update the client/cli if necessary
8. Create e2e tests in `scripts/test/`
- Remember to `$ make install` after every change you make so the binary under test is the correct one.
- Hot tip: stop the test at some point before the serve is killed with `exit 0` and run just the script (e.g. `$ ./scripts/test/smoke_plastic_credit.sh`).
- This way the server is still running, and you can manually run your CLI commands in the terminal.
- To get access to the environment variables such as `$CHAIN_ID` and `$CHAIN_DIR` you can use the env script like this: `$ source ./scripts/serve_env.sh`.
9. Format and lint your code before committing: `$ make format` and `$ make lint`
6. If you created a new type to be stored (i.e. you added a new store key in keys.go)
- Update `NewDecodeStore` in simulation/decoder.go
7. Update genesis if necessary (in genesis and keeper/genesis)
- Update `RandomizedGenState` in simulation/genesis.go if necessary
8. Update the client/cli if necessary
9. Create e2e tests in `tests/e2e`
10. Format and lint your code before committing: `$ make format` and `$ make lint`

## Native modules vs smart contracts
Short version: core stable functionality/protocol implemented as native Cosmos SDK modules, extra functionality/nice-to-have stuff that can change often as smart contracts.
Expand Down
20 changes: 20 additions & 0 deletions chain/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ test-with-coverage:
RUN make test-with-coverage
SAVE ARTIFACT coverage.out AS LOCAL coverage.out

test-sim-import-export:
FROM +build
RUN make test-sim-import-export

test-sim-after-import:
FROM +build
RUN make test-sim-after-import

test-sim-multi-seed-short:
FROM +build
RUN make test-sim-multi-seed-short

test-sim-multi-seed-long:
FROM +build
RUN make test-sim-multi-seed-long

test-sim-nondeterminism:
FROM +build
RUN make test-sim-nondeterminism

smoketest:
FROM +build
RUN make smoketest
Expand Down
46 changes: 46 additions & 0 deletions chain/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,52 @@ smoketest: install
smoketest-ibc: install
./scripts/test/smoke_ibc.sh

###############################################################################
### Simulations ###
###############################################################################

SIMAPP = github.com/EmpowerPlastic/empowerchain/app/simulation
SIM_TEST_DIR = ./app/simulation
TOOLS_DESTDIR ?= $(GOPATH)/bin
RUNSIM = $(TOOLS_DESTDIR)/runsim
default_seeds := 1,2,4,7,32,123,124,582,1893,2989,3012,4728,37827,981928,87821, \
891823782,989182,89182391,11,22,44,77,99,2020,3232,123123,124124,582582,18931893, \
29892989,30123012,47284728,7601778,8090485,977367484,491163361,424254581,673398983

JOBS ?= 4
SEED ?= 1
SEEDS ?= $(subst $(whitespace),$(empty),$(default_seeds))
PERIOD ?= 5
NUM_BLOCKS ?= 100
BLOCK_SIZE ?= 200

# Install the runsim binary
runsim: $(RUNSIM)
$(RUNSIM):
@echo "Installing runsim..."
@go install github.com/cosmos/tools/cmd/[email protected]

test-sim-import-export: runsim
@echo "Running application import/export simulation. This may take several minutes..."
@runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestAppImportExport

test-sim-after-import: runsim
@echo "Running application simulation-after-import. This may take several minutes..."
@runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestAppSimulationAfterImport

test-sim-multi-seed-short: runsim
@echo "Running short multi-seed application simulation. This may take awhile!"
@runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 10 TestFullAppSimulation

test-sim-multi-seed-long: runsim
@echo "Running long multi-seed application simulation. This may take awhile!"
@runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 500 50 TestFullAppSimulation

test-sim-nondeterminism:
@echo "Running non-determinism test..."
go test $(SIM_TEST_DIR) -mod=readonly -run TestAppStateDeterminism -Enabled=true \
-NumBlocks=100 -BlockSize=200 -Commit=true -Period=0 -v -timeout 24h

###############################################################################
### Proto ###
###############################################################################
Expand Down
30 changes: 15 additions & 15 deletions chain/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ type EmpowerApp struct {
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedIBCFeeKeeper capabilitykeeper.ScopedKeeper

moduleManager *module.Manager
ModuleManager *module.Manager
configurator module.Configurator

simulationManager *module.SimulationManager
Expand Down Expand Up @@ -514,7 +514,7 @@ func New(

// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
app.moduleManager = module.NewManager(
app.ModuleManager = module.NewManager(
genutil.NewAppModule(
app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx,
encodingConfig.TxConfig,
Expand All @@ -538,7 +538,7 @@ func New(
consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper),
// Custom modules
proofofexistencemodule.NewAppModule(app.ProofofexistenceKeeper),
plasticcreditmodule.NewAppModule(appCodec, app.PlasticcreditKeeper),
plasticcreditmodule.NewAppModule(appCodec, app.PlasticcreditKeeper, app.AccountKeeper, app.BankKeeper),
accesscontrolmodule.NewAppModule(app.AccessControlKeeper),
// IBC modules
ibc.NewAppModule(app.IBCKeeper),
Expand All @@ -552,7 +552,7 @@ func New(
// CanWithdrawInvariant invariant.
// NOTE: staking module is required if HistoricalEntries param > 0
// NOTE: capability module's beginblocker must come before any modules using capabilities (e.g. IBC)
app.moduleManager.SetOrderBeginBlockers(
app.ModuleManager.SetOrderBeginBlockers(
upgradetypes.ModuleName,
capabilitytypes.ModuleName,
minttypes.ModuleName,
Expand Down Expand Up @@ -582,7 +582,7 @@ func New(
ibcfeetypes.ModuleName,
)

app.moduleManager.SetOrderEndBlockers(
app.ModuleManager.SetOrderEndBlockers(
crisistypes.ModuleName,
govtypes.ModuleName,
stakingtypes.ModuleName,
Expand Down Expand Up @@ -618,7 +618,7 @@ func New(
// NOTE: Capability module must occur first so that it can initialize any capabilities
// so that other modules that want to create or claim capabilities afterwards in InitChain
// can do so safely.
app.moduleManager.SetOrderInitGenesis(
app.ModuleManager.SetOrderInitGenesis(
capabilitytypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
Expand Down Expand Up @@ -648,16 +648,16 @@ func New(
ibcfeetypes.ModuleName,
)

app.moduleManager.RegisterInvariants(app.CrisisKeeper)
app.ModuleManager.RegisterInvariants(app.CrisisKeeper)
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.moduleManager.RegisterServices(app.configurator)
app.ModuleManager.RegisterServices(app.configurator)

// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
// Make sure it's called after `app.ModuleManager` and `app.configurator` are set.
app.setupUpgradeStoreLoaders()
app.setupUpgradeHandlers()

autocliv1.RegisterQueryServer(app.GRPCQueryRouter(), runtimeservices.NewAutoCLIQueryService(app.moduleManager.Modules))
autocliv1.RegisterQueryServer(app.GRPCQueryRouter(), runtimeservices.NewAutoCLIQueryService(app.ModuleManager.Modules))
reflectionSvc, err := runtimeservices.NewReflectionService()
if err != nil {
panic(err)
Expand All @@ -671,7 +671,7 @@ func New(
overrideModules := map[string]module.AppModuleSimulation{
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
}
app.simulationManager = module.NewSimulationManagerFromAppModules(app.moduleManager.Modules, overrideModules)
app.simulationManager = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, overrideModules)
app.simulationManager.RegisterStoreDecoders()

// initialize stores
Expand Down Expand Up @@ -738,12 +738,12 @@ func (app *EmpowerApp) Name() string { return app.BaseApp.Name() }

// BeginBlocker application updates every begin block
func (app *EmpowerApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
return app.moduleManager.BeginBlock(ctx, req)
return app.ModuleManager.BeginBlock(ctx, req)
}

// EndBlocker application updates every end block
func (app *EmpowerApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.moduleManager.EndBlock(ctx, req)
return app.ModuleManager.EndBlock(ctx, req)
}

// InitChainer application update at chain initialization
Expand All @@ -752,8 +752,8 @@ func (app *EmpowerApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) a
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.moduleManager.GetVersionMap())
return app.moduleManager.InitGenesis(ctx, app.appCodec, genesisState)
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap())
return app.ModuleManager.InitGenesis(ctx, app.appCodec, genesisState)
}

// LoadHeight loads a particular height
Expand Down Expand Up @@ -867,7 +867,7 @@ func (app *EmpowerApp) setupUpgradeHandlers() {
for _, u := range Upgrades {
app.UpgradeKeeper.SetUpgradeHandler(
u.UpgradeName,
u.CreateUpgradeHandler(app.moduleManager, app.configurator),
u.CreateUpgradeHandler(app.ModuleManager, app.configurator),
)
}
}
Expand Down
22 changes: 13 additions & 9 deletions chain/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (app *EmpowerApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllow
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState := app.moduleManager.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
genState := app.ModuleManager.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
Expand Down Expand Up @@ -74,20 +74,21 @@ func (app *EmpowerApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddr

// withdraw all validator commission
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, err := app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
if err != nil {
panic(err)
}
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
gjermundgaraba marked this conversation as resolved.
Show resolved Hide resolved
return false
})

// withdraw all delegator rewards
dels := app.StakingKeeper.GetAllDelegations(ctx)
for _, delegation := range dels {
_, err := app.DistrKeeper.WithdrawDelegationRewards(ctx, delegation.GetDelegatorAddr(), delegation.GetValidatorAddr())
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err)
}

delAddr := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

_, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
}

// clear validator slash events
Expand Down Expand Up @@ -163,7 +164,7 @@ func (app *EmpowerApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddr
counter := int16(0)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[1:])
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
Expand All @@ -178,10 +179,13 @@ func (app *EmpowerApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddr
counter++
}

iter.Close()
if err := iter.Close(); err != nil {
app.Logger().Error("error while closing the key-value store reverse prefix iterator: ", err)
return
}

if _, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx); err != nil {
panic(err)
log.Fatal(err)
}

/* Handle slashing state. */
Expand Down
Loading