Skip to content

Commit

Permalink
fix: simulations for cosmos modules (#8)
Browse files Browse the repository at this point in the history
* fix: seperate antehandler for simulation

* fix: simulation errors

(cherry picked from commit 6961967)

* fix: simulation workflow

* fix: update sims.yml, add GOPATH on makefile

---------

Co-authored-by: dongsam <dongsamb@gmail.com>
  • Loading branch information
poorphd and dongsam authored Jul 21, 2023
1 parent fe733b3 commit 31450c7
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 180 deletions.
234 changes: 115 additions & 119 deletions .github/workflows/sims.yml
Original file line number Diff line number Diff line change
@@ -1,121 +1,117 @@
#name: Sims
name: Sims
# Sims workflow runs multiple types of simulations (nondeterminism, import-export, after-import)
# This workflow will run on all Pull Requests, if a .go, .mod or .sum file have been changed
# Temporary disable until it is fixed
#on:
# pull_request:
# push:
# branches:
# - main
# - develop
#
#jobs:
# build:
# runs-on: ubuntu-latest
# if: "!contains(github.event.head_commit.message, 'skip-sims')"
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-go@v2.1.3
# with:
# go-version: 1.18
# - name: Display go version
# run: go version
# - run: make build
#
# install-runsim:
# runs-on: ubuntu-latest
# needs: build
# steps:
# - uses: actions/setup-go@v2.1.3
# with:
# go-version: 1.18
# - name: Display go version
# run: go version
# - name: Install runsim
# run: export GO111MODULE="on" && go install github.com/cosmos/tools/cmd/runsim@v1.0.0
# - uses: actions/cache@v2.1.6
# with:
# path: ~/go/bin
# key: ${{ runner.os }}-go-runsim-binary
#
# test-sim-nondeterminism:
# runs-on: ubuntu-latest
# needs: [build, install-runsim]
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-go@v2.1.3
# with:
# go-version: 1.18
# - name: Display go version
# run: go version
# - uses: technote-space/get-diff-action@v4
# with:
# PATTERNS: |
# **/**.go
# go.mod
# go.sum
# - uses: actions/cache@v2.1.6
# with:
# path: ~/go/bin
# key: ${{ runner.os }}-go-runsim-binary
# if: env.GIT_DIFF
# - name: test-sim-nondeterminism
# run: |
# make test-sim-nondeterminism
# if: env.GIT_DIFF
#
# test-sim-import-export:
# runs-on: ubuntu-latest
# needs: [build, install-runsim]
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-go@v2.1.3
# with:
# go-version: 1.18
# - name: Display go version
# run: go version
# - uses: technote-space/get-diff-action@v4
# with:
# SUFFIX_FILTER: |
# **/**.go
# go.mod
# go.sum
# SET_ENV_NAME_INSERTIONS: 1
# SET_ENV_NAME_LINES: 1
# - uses: actions/cache@v2.1.6
# with:
# path: ~/go/bin
# key: ${{ runner.os }}-go-runsim-binary
# if: env.GIT_DIFF
# - name: test-sim-import-export
# run: |
# make test-sim-import-export
# if: env.GIT_DIFF
#
# test-sim-after-import:
# runs-on: ubuntu-latest
# needs: [build, install-runsim]
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-go@v2.1.3
# with:
# go-version: 1.18
# - name: Display go version
# run: go version
# - uses: technote-space/get-diff-action@v4
# with:
# SUFFIX_FILTER: |
# **/**.go
# go.mod
# go.sum
# SET_ENV_NAME_INSERTIONS: 1
# SET_ENV_NAME_LINES: 1
# - uses: actions/cache@v2.1.6
# with:
# path: ~/go/bin
# key: ${{ runner.os }}-go-runsim-binary
# if: env.GIT_DIFF
# - name: test-sim-after-import
# run: |
# make test-sim-after-import
# if: env.GIT_DIFF
on:
pull_request:
push:
branches:
- main
- develop

jobs:
build:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, 'skip-sims')"
steps:
- uses: actions/checkout@v3.5.2
- uses: actions/setup-go@v4
with:
go-version: 1.18
- name: Install runsim
run: go install github.com/cosmos/tools/cmd/runsim@v1.0.0
- name: Display go version
run: go version
- run: make build

test-sim-nondeterminism:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v3.5.2
- uses: actions/setup-go@v4
with:
go-version: 1.18
- name: Display go version
run: go version
- uses: technote-space/get-diff-action@v4
with:
PATTERNS: |
**/**.go
go.mod
go.sum
- uses: actions/cache@v3.3.1
with:
path: ~/go/bin
key: ${{ runner.os }}-go-runsim-binary
if: env.GIT_DIFF
- name: test-sim-nondeterminism
run: |
make test-sim-nondeterminism
if: env.GIT_DIFF

install-runsim:
runs-on: ubuntu-latest
needs: build
steps:
- name: Install runsim
run: go install github.com/cosmos/tools/cmd/runsim@v1.0.0
- uses: actions/cache@v3.3.1
with:
path: ~/go/bin
key: ${{ runner.os }}-go-runsim-binary

test-sim-import-export:
runs-on: ubuntu-latest
needs: [build, install-runsim]
steps:
- uses: actions/checkout@v3.5.2
- uses: actions/setup-go@v4
with:
go-version: 1.18
- name: Display go version
run: go version
- uses: technote-space/get-diff-action@v4
with:
SUFFIX_FILTER: |
**/**.go
go.mod
go.sum
SET_ENV_NAME_INSERTIONS: 1
SET_ENV_NAME_LINES: 1
- uses: actions/cache@v3.3.1
with:
path: ~/go/bin
key: ${{ runner.os }}-go-runsim-binary
if: env.GIT_DIFF
- name: test-sim-import-export
run: |
make test-sim-import-export
if: env.GIT_DIFF

test-sim-after-import:
runs-on: ubuntu-latest
needs: [build, install-runsim]
steps:
- uses: actions/checkout@v3.5.2
- uses: actions/setup-go@v4
with:
go-version: 1.18
- name: Display go version
run: go version
- uses: technote-space/get-diff-action@v4
with:
SUFFIX_FILTER: |
**/**.go
go.mod
go.sum
SET_ENV_NAME_INSERTIONS: 1
SET_ENV_NAME_LINES: 1
- uses: actions/cache@v3.3.1
with:
path: ~/go/bin
key: ${{ runner.os }}-go-runsim-binary
if: env.GIT_DIFF
- name: test-sim-after-import
run: |
make test-sim-after-import
if: env.GIT_DIFF
51 changes: 42 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#!/usr/bin/make -f

###
# Find OS and Go environment
# GO contains the Go binary
# FS contains the OS file separator
###
ifeq ($(OS),Windows_NT)
GO := $(shell where go.exe 2> NUL)
FS := \\
else
GO := $(shell command -v go 2> /dev/null)
FS := /
endif

ifeq ($(GO),)
$(error could not find go. Is it in PATH? $(GO))
endif

PACKAGES_NOSIMULATION=$(shell go list ./... | grep -v '/simulation')
PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation')
DIFF_TAG=$(shell git rev-list --tags="v*" --max-count=1 --not $(shell git rev-list --tags="v*" "HEAD..origin"))
Expand All @@ -8,9 +25,10 @@ VERSION ?= $(shell echo $(shell git describe --tags $(or $(DIFF_TAG), $(DEFAULT_
TMVERSION := $(shell go list -m github.com/tendermint/tendermint | sed 's:.* ::')
COMMIT := $(shell git log -1 --format='%H')
LEDGER_ENABLED ?= true
GOPATH ?= $(shell $(GO) env GOPATH)
BINDIR ?= $(GOPATH)/bin
canto_BINARY = cantod
canto_DIR = canto
canto_DIR = cantod
BUILDDIR ?= $(CURDIR)/build
SIMAPP = ./app
HTTPS_GIT := https://github.com/canto/canto.git
Expand Down Expand Up @@ -197,7 +215,7 @@ RUNSIM = $(TOOLS_DESTDIR)/runsim
runsim: $(RUNSIM)
$(RUNSIM):
@echo "Installing runsim..."
@(cd /tmp && ${GO_MOD} go get github.com/cosmos/tools/cmd/runsim@master)
@(cd /tmp && go install github.com/cosmos/tools/cmd/runsim@v1.0.0)

statik: $(STATIK)
$(STATIK):
Expand Down Expand Up @@ -371,34 +389,49 @@ test-rpc-pending:
test-sim-nondeterminism:
@echo "Running non-determinism test..."
@go test -mod=readonly $(SIMAPP) -run TestAppStateDeterminism -Enabled=true \
-NumBlocks=100 -BlockSize=200 -Commit=true -Period=0 -v -timeout 24h
-NumBlocks=20 -BlockSize=100 -Commit=true -Period=1 -v -timeout 10m

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

test-sim-custom-genesis-fast:
@echo "Running custom genesis simulation..."
@echo "By default, ${HOME}/.$(canto_DIR)/config/genesis.json will be used."
@go test -mod=readonly $(SIMAPP) -run TestFullAppSimulation -Genesis=${HOME}/.$(canto_DIR)/config/genesis.json \
-Enabled=true -NumBlocks=100 -BlockSize=200 -Commit=true -Seed=99 -Period=5 -v -timeout 24h
-Enabled=true -NumBlocks=100 -BlockSize=200 -Commit=true -Seed=5 -Period=1 -v -timeout 1h

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

test-sim-import-export-long: runsim
@echo "Running application simulation-import-export-long. This may take several minutes..."
$(eval SEED := $(shell awk 'BEGIN{srand(); for (i=1; i<=50; i++) {n=int(10000*rand())+1; printf "%d%s", n, (i==50 ? "" : ",")}}'))
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -Seeds="$(SEED)" -ExitOnFail 500 5 TestAppImportExport

test-sim-after-import: runsim
@echo "Running application simulation-after-import. This may take several minutes..."
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestAppSimulationAfterImport
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -Seeds=1,10,100,1000 -ExitOnFail 50 5 TestAppSimulationAfterImport

test-sim-after-import-long: runsim
@echo "Running application simulation-after-import-long. This may take several minutes..."
$(eval SEED := $(shell awk 'BEGIN{srand(); for (i=1; i<=50; i++) {n=int(10000*rand())+1; printf "%d%s", n, (i==50 ? "" : ",")}}'))
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -Seeds="$(SEED)" -ExitOnFail 500 5 TestAppSimulationAfterImport

test-sim-custom-genesis-multi-seed: runsim
@echo "Running multi-seed custom genesis simulation..."
@echo "By default, ${HOME}/.$(canto_DIR)/config/genesis.json will be used."
@$(BINDIR)/runsim -Genesis=${HOME}/.$(canto_DIR)/config/genesis.json -SimAppPkg=$(SIMAPP) -ExitOnFail 400 5 TestFullAppSimulation
@$(BINDIR)/runsim -Genesis=${HOME}/.$(canto_DIR)/config/genesis.json -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestFullAppSimulation

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

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

test-sim-benchmark-invariants:
@echo "Running simulation invariant benchmarks..."
Expand Down
6 changes: 5 additions & 1 deletion app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func NewAnteHandler(options HandlerOptions) sdk.AnteHandler {
// handle as totally normal Cosmos SDK tx
switch tx.(type) {
case sdk.Tx:
anteHandler = newCosmosAnteHandler(options)
if options.Simulation {
anteHandler = newCosmosSimulationAnteHandler(options)
} else {
anteHandler = newCosmosAnteHandler(options)
}
default:
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
}
Expand Down
Loading

0 comments on commit 31450c7

Please sign in to comment.