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

test: simulation import and export #3033

Merged
merged 72 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
c3299ef
debug sim test
kingpinXD Sep 30, 2024
7d95021
start modifuing for v50
kingpinXD Oct 1, 2024
fcce58e
add sim test v1
kingpinXD Oct 1, 2024
b774b1e
add sim export
kingpinXD Oct 1, 2024
ac8fddf
move simulation tests to a separate directory
kingpinXD Oct 1, 2024
b55f439
remove unused functions
kingpinXD Oct 1, 2024
913aa19
add comments
kingpinXD Oct 2, 2024
10d8a38
enable gov module
kingpinXD Oct 2, 2024
49d430c
enable gov module
kingpinXD Oct 2, 2024
73fbcf5
fix lint error
kingpinXD Oct 2, 2024
cad3f4f
add todos for BasicManager
kingpinXD Oct 3, 2024
c07d519
add todos for BasicManager
kingpinXD Oct 3, 2024
4f3ebbe
add cleanup for TestFullAppSimulation
kingpinXD Oct 3, 2024
56b4698
register legacy router
kingpinXD Oct 3, 2024
c1db4cf
added a doc for simulation testing
kingpinXD Oct 3, 2024
d98add5
undo db close for multi threaded test
kingpinXD Oct 3, 2024
bff4121
add description for tests
kingpinXD Oct 14, 2024
74b0ad7
remove go module from simulation tests
kingpinXD Oct 15, 2024
f8c095d
make basicsmanager private
kingpinXD Oct 18, 2024
b49be6b
Update Makefile
kingpinXD Oct 22, 2024
cf4cb45
Update changelog.md
kingpinXD Oct 22, 2024
d5ae57c
format simulation.md
kingpinXD Oct 22, 2024
1f744f8
rebase develop
kingpinXD Oct 22, 2024
e1cc91f
add test for import export
kingpinXD Oct 22, 2024
d674455
add test for import export
kingpinXD Oct 22, 2024
651a61a
test sim after import
kingpinXD Oct 22, 2024
5e3bce7
add bench test
kingpinXD Oct 22, 2024
3b44c10
remove bench
kingpinXD Oct 23, 2024
6eea359
add changelog
kingpinXD Oct 23, 2024
9dfc005
remove cosmos utils
kingpinXD Oct 23, 2024
a40f4d9
remove print lines
kingpinXD Oct 23, 2024
fdbacce
add comments
kingpinXD Oct 23, 2024
a868450
ci: Add simulation tests workflow
Oct 23, 2024
924d063
Merge branch 'ci/add-simulation-test' into sim-import-export
kingpinXD Oct 23, 2024
bea00ab
merge ci workflow
kingpinXD Oct 23, 2024
21b91d4
Update .github/workflows/simulation-test.yaml
kingpinXD Oct 23, 2024
59bc886
Update simulation-test.yaml
julianrubino Oct 23, 2024
33bb1b9
add branch sim-import-export to trigger ci run
kingpinXD Oct 23, 2024
9791e99
format files
kingpinXD Oct 23, 2024
643490d
uncomment simulation tests from CI
kingpinXD Oct 23, 2024
064e05e
fix defer functions
kingpinXD Oct 23, 2024
57b5d4e
Rename sim tests file, add result aggregation and merge jobs
Oct 24, 2024
6fcad07
Merge branch 'develop' into sim-import-export
kingpinXD Oct 24, 2024
4b0dda4
ignore simulation tests from codecov
kingpinXD Oct 24, 2024
e8d127a
Merge remote-tracking branch 'origin/sim-import-export' into sim-impo…
kingpinXD Oct 24, 2024
d18978b
Add runs on to matrix conditional job
Oct 24, 2024
195bc8d
add default make-targets
Oct 24, 2024
7458f94
bump go
Oct 24, 2024
399569d
Update .github/workflows/sim.yaml workflow name
julianrubino Oct 25, 2024
cf60f5a
Delete main branch from sim test workflow
julianrubino Oct 25, 2024
5c682e9
improve formatting for simulation.md
kingpinXD Oct 25, 2024
06c5d75
move simulation tests to root directory
kingpinXD Oct 29, 2024
d27702d
remove panic for Import Export Test
kingpinXD Oct 29, 2024
cb4be57
Merge branch 'develop' into sim-import-export
kingpinXD Oct 29, 2024
5132383
Trigger Sim Tests on Labeled PRs or when any file from x/**/* is changed
Oct 29, 2024
902e191
Merge branch 'sim-import-export' of github.com:zeta-chain/node into s…
Oct 29, 2024
f2341e8
update sim.yaml
kingpinXD Oct 29, 2024
29f04e8
Update sim.yaml jobs needs
julianrubino Oct 29, 2024
2fdb90b
update sim.yaml
kingpinXD Oct 29, 2024
19c27fa
Merge remote-tracking branch 'origin/sim-import-export' into sim-impo…
kingpinXD Oct 29, 2024
eaa150d
update codecov.yml
kingpinXD Oct 29, 2024
0392598
Merge branch 'develop' into sim-import-export
kingpinXD Oct 29, 2024
476a623
fixed some comments
kingpinXD Oct 30, 2024
086f973
Merge remote-tracking branch 'origin/sim-import-export' into sim-impo…
kingpinXD Oct 30, 2024
ded60cb
remove directory sim
kingpinXD Oct 30, 2024
6cc76e8
remove directory sim
kingpinXD Oct 30, 2024
0e76223
Merge branch 'develop' into sim-import-export
kingpinXD Oct 30, 2024
d8b713a
format directory
kingpinXD Oct 30, 2024
6e3b843
Merge remote-tracking branch 'origin/sim-import-export' into sim-impo…
kingpinXD Oct 30, 2024
60a0aa4
remove utils file
kingpinXD Oct 31, 2024
b9abe91
add to codecov
kingpinXD Nov 1, 2024
897eb3e
Merge branch 'develop' into sim-import-export
kingpinXD Nov 1, 2024
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
111 changes: 111 additions & 0 deletions .github/workflows/sim.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: sim

on:
push:
branches:
- develop
pull_request:
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
types: [opened, synchronize, labeled]
schedule:
- cron: "0 6 * * *"
workflow_dispatch:
inputs:
make-targets:
description: 'Comma separated list of make targets to run (e.g., test-sim-nondeterminism, test-sim-fullappsimulation)'
required: true
default: 'test-sim-nondeterminism'

concurrency:
group: simulation-${{ github.head_ref || github.sha }}
cancel-in-progress: true

jobs:
changed-files:
runs-on: ubuntu-latest
outputs:
modified_files: ${{ steps.changes.outputs.modified_files }}
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Get changed files in x directory
id: changes
run: |
echo "::set-output name=modified_files::$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^x/' | xargs)"

matrix-conditionals:
needs: changed-files
if: |
contains(github.event.pull_request.labels.*.name, 'SIM_TESTS') || needs.changed-files.outputs.modified_files
runs-on: ubuntu-22.04
outputs:
SIM_TEST_NOND: ${{ steps.matrix-conditionals.outputs.SIM_TEST_NOND }}
SIM_TEST_FULL: ${{ steps.matrix-conditionals.outputs.SIM_TEST_FULL }}
SIM_TEST_IMPORT_EXPORT: ${{ steps.matrix-conditionals.outputs.SIM_TEST_IMPORT_EXPORT }}
SIM_TEST_AFTER_IMPORT: ${{ steps.matrix-conditionals.outputs.SIM_TEST_AFTER_IMPORT }}
steps:
- id: matrix-conditionals
uses: actions/github-script@v7
with:
script: |
const makeTargetsInput = context.payload.inputs ? context.payload.inputs['make-targets'] : null;
const defaultTargets = ['test-sim-nondeterminism', 'test-sim-fullappsimulation', 'test-sim-import-export', 'test-sim-after-import'];

const makeTargets = makeTargetsInput ? makeTargetsInput.split(',') : defaultTargets;

core.setOutput('SIM_TEST_NOND', makeTargets.includes('test-sim-nondeterminism'));
core.setOutput('SIM_TEST_FULL', makeTargets.includes('test-sim-fullappsimulation'));
core.setOutput('SIM_TEST_IMPORT_EXPORT', makeTargets.includes('test-sim-import-export'));
core.setOutput('SIM_TEST_AFTER_IMPORT', makeTargets.includes('test-sim-after-import'));

simulation-tests:
needs:
- matrix-conditionals
if: |
contains(github.event.pull_request.labels.*.name, 'SIM_TESTS') || needs.changed-files.outputs.modified_files
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
include:
- make-target: "test-sim-nondeterminism"
condition: ${{ needs.matrix-conditionals.outputs.SIM_TEST_NOND == 'true' }}
- make-target: "test-sim-fullappsimulation"
condition: ${{ needs.matrix-conditionals.outputs.SIM_TEST_FULL == 'true' }}
- make-target: "test-sim-import-export"
condition: ${{ needs.matrix-conditionals.outputs.SIM_TEST_IMPORT_EXPORT == 'true' }}
- make-target: "test-sim-after-import"
condition: ${{ needs.matrix-conditionals.outputs.SIM_TEST_AFTER_IMPORT == 'true' }}
name: ${{ matrix.make-target }}
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Install dependencies
run: make runsim

- name: Run ${{ matrix.make-target }}
if: ${{ matrix.condition }}
run: |
make ${{ matrix.make-target }}

sim-ok:
needs:
- simulation-tests
if: |
contains(github.event.pull_request.labels.*.name, 'SIM_TESTS') || needs.changed-files.outputs.modified_files
runs-on: ubuntu-22.04
steps:
- name: Aggregate Results
run: |
result="${{ needs.simulation-tests.result }}"
if [[ $result == "success" || $result == "skipped" ]]; then
exit 0
else
exit 1
fi
26 changes: 21 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ start-upgrade-import-mainnet-test: zetanode-upgrade
###############################################################################

BINDIR ?= $(GOPATH)/bin
SIMAPP = ./tests/simulation
SIMAPP = ./simulation


# Run sim is a cosmos tool which helps us to run multiple simulations in parallel.
Expand All @@ -381,16 +381,22 @@ $(BINDIR)/runsim:
# Period: Invariant check period
# Timeout: Timeout for the simulation test
define run-sim-test
@echo "Running $(1)..."
@echo "Running $(1)"
@go test -mod=readonly $(SIMAPP) -run $(2) -Enabled=true \
-NumBlocks=$(3) -BlockSize=$(4) -Commit=true -Period=0 -v -timeout $(5)
endef

test-sim-nondeterminism:
$(call run-sim-test,"non-determinism test",TestAppStateDeterminism,100,200,2h)
$(call run-sim-test,"non-determinism test",TestAppStateDeterminism,100,200,30m)

test-sim-fullappsimulation:
$(call run-sim-test,"TestFullAppSimulation",TestFullAppSimulation,100,200,2h)
$(call run-sim-test,"TestFullAppSimulation",TestFullAppSimulation,100,200,30m)

test-sim-import-export:
$(call run-sim-test,"test-import-export",TestAppImportExport,100,200,30m)

test-sim-after-import:
$(call run-sim-test,"test-sim-after-import",TestAppSimulationAfterImport,100,200,30m)

test-sim-multi-seed-long: runsim
@echo "Running long multi-seed application simulation."
Expand All @@ -400,13 +406,23 @@ test-sim-multi-seed-short: runsim
@echo "Running short multi-seed application simulation."
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 10 TestFullAppSimulation

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

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

.PHONY: \
test-sim-nondeterminism \
test-sim-fullappsimulation \
test-sim-multi-seed-long \
test-sim-multi-seed-short
test-sim-multi-seed-short \
test-sim-import-export \
test-sim-after-import \
test-sim-import-export-long \
test-sim-after-import-long


###############################################################################
Expand Down
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,10 @@ func (app *App) BasicManager() module.BasicManager {
return app.mb
}

func (app *App) ModuleManager() *module.Manager {
return app.mm
}

func (app *App) BlockedAddrs() map[string]bool {
blockList := make(map[string]bool)

Expand Down
12 changes: 10 additions & 2 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package app

import (
"encoding/json"
"errors"
"log"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -75,7 +77,7 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str
// 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 {
if !errors.Is(err, distributiontypes.ErrNoValidatorCommission) && err != nil {
panic(err)
}
return false
Expand Down Expand Up @@ -161,7 +163,13 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str
counter := int16(0)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[1:])
key := iter.Key()
lumtis marked this conversation as resolved.
Show resolved Hide resolved
keyPrefixLength := 2
if len(key) <= keyPrefixLength {
app.Logger().Error("unexpected key in staking store", "key", key)
continue
}
addr := sdk.ValAddress(key[keyPrefixLength:])
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* [2894](https://github.com/zeta-chain/node/pull/2894) - increase gas limit for TSS vote tx
* [2932](https://github.com/zeta-chain/node/pull/2932) - add gateway upgrade as part of the upgrade test
* [2947](https://github.com/zeta-chain/node/pull/2947) - initialize simulation tests
* [3033](https://github.com/zeta-chain/node/pull/3033) - initialize simulation tests for import and export

### Fixes

Expand Down
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ ignore:
- "precompiles/**/*.json"
- "precompiles/**/*.sol"
- "precompiles/**/*.gen.go"
- "simulation/**/*"
60 changes: 52 additions & 8 deletions docs/development/SIMULATION_TESTING.md
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,34 +1,78 @@
# Zetachain simulation testing
## Overview
The blockchain simulation tests how the blockchain application would behave under real life circumstances by generating
and sending randomized messages.The goal of this is to detect and debug failures that could halt a live chain,by providing
logs and statistics about the operations run by the simulator as well as exporting the latest application state.

and sending randomized messages.The goal of this is to detect and debug failures that could halt a live chain by
providing logs and statistics about the operations run by the simulator as well as exporting the latest application
state.

## Simulation tests

### Nondeterminism test
Nondeterminism test runs a full application simulation , and produces multiple blocks as per the config
It checks the determinism of the application by comparing the apphash at the end of each run to other runs
The test certifies that , for the same set of operations ( irrespective of what the operations are ), we would reach the same final state if the initial state is the same
The test certifies that, for the same set of operations (regardless of what the operations are), we
would reach the same final state if the initial state is the same
Approximate run time is 2 minutes.
```bash
make test-sim-nondeterminism
```

### Full application simulation test
Full application runs a full app simulation test with the provided configuration.
At the end of the run it tries to export the genesis state to make sure the export works.
At the end of the run, it tries to export the genesis state to make sure the export works.
Approximate run time is 2 minutes.
```bash
make test-sim-full-app
```

### Import Export simulation test
The import export simulation test runs a full application simulation
and exports the application state at the end of the run.
This state is then imported into a new simulation.
At the end of the run, we compare the keys for the application state for both the simulations
to make sure they are the same.
Approximate run time is 2 minutes.
```bash
make test-sim-import-export
```

### Import and run simulation test
This simulation test exports the application state at the end of the run and imports it into a new simulation.
Approximate run time is 2 minutes.
```bash
make test-sim-after-import
```

### Multi seed long test
Multi seed long test runs a full application simulation with multiple seeds and multiple blocks.This runs the test for a longer duration compared to the multi seed short test
Multi seed long test runs a full application simulation with multiple seeds and multiple blocks.
It uses the `runsim` tool to run the same test in parallel threads.
Approximate run time is 30 minutes.
```bash
make test-sim-multi-seed-long
```

### Multi seed short test
lumtis marked this conversation as resolved.
Show resolved Hide resolved
Multi seed short test runs a full application simulation with multiple seeds and multiple blocks. This runs the test for a longer duration compared to the multi seed long test
Multi seed short test runs a full application simulation with multiple seeds and multiple blocks.
It uses the `runsim` tool to run the same test in parallel threads.
This test is a shorter version of the Multi seed long test.
Approximate run time is 10 minutes.
```bash
make test-sim-multi-seed-short
```
```

kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
### Import Export long test
This test runs the import export simulation test for a longer duration.
It uses the `runsim` tool to run the same test in parallel threads.
Approximate run time is 30 minutes.
```bash
make test-sim-import-export-long
```

### Import and run simulation test long
This test runs the import and run simulation test for a longer duration.
It uses the `runsim` tool to run the same test in parallel threads.
Approximate run time is 30 minutes.
```bash
make test-sim-after-import-long
```

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sim
package simulation

import (
"flag"
Expand Down
11 changes: 1 addition & 10 deletions tests/simulation/sim/sim_utils.go → simulation/simulation.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package sim
package simulation

import (
"fmt"

dbm "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -59,10 +57,3 @@ func NewSimApp(
}
return zetaApp, nil
}

// PrintStats prints the corresponding statistics from the app DB.
func PrintStats(db dbm.DB) {
fmt.Println("\nDB Stats")
fmt.Println(db.Stats()["leveldb.stats"])
fmt.Println("GoLevelDB cached block size", db.Stats()["leveldb.cachedblock"])
}
Loading
Loading