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

Problem: sim tests are fail #218

Draft
wants to merge 9 commits into
base: v2.0.0-cronos
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion module/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,21 @@ proto-update-deps:
###############################################################################

SIMAPP = github.com/peggyjv/gravity-bridge/module/v2/app
BINDIR ?= ~/go/bin
runsim: $(BINDIR)/runsim
$(BINDIR)/runsim:
@echo "Installing runsim..."
@(cd /tmp && go install github.com/cosmos/tools/cmd/[email protected])

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=100 -BlockSize=200 -Commit=true -Period=0 -v -timeout 24h

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

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
10 changes: 4 additions & 6 deletions module/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (app *Gravity) ExportAppStateAndValidators(

// prepare for fresh start at zero height
// NOTE zero height genesis is a temporary feature which will be deprecated
// in favour of export at a block height
//
// in favour of export at a block height
func (app *Gravity) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) {
applyWhiteList := false

Expand All @@ -69,10 +70,7 @@ func (app *Gravity) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []st

// 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 {
log.Fatal(err)
}
_, _ = app.distrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
return false
})

Expand Down Expand Up @@ -160,7 +158,7 @@ func (app *Gravity) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []st
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 Down
49 changes: 31 additions & 18 deletions module/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/rand"
"os"
"runtime/debug"
"strings"
"testing"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -71,24 +73,24 @@ func TestFullAppSimulation(t *testing.T) {
app.BaseApp,
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts,
SimulationOperations(*app, app.AppCodec(), config),
simapp.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
app.AppCodec(),
)

// export state and simParams before the simulation error is checked
err = CheckExportSimulation(*app, config, simParams)
err = simapp.CheckExportSimulation(app, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)

if config.Commit {
PrintStats(db)
simapp.PrintStats(db)
}
}

func TestAppImportExport(t *testing.T) {
config, db, dir, logger, skip, err := SetupSimulation("leveldb-app-sim", "Simulation")
config, db, dir, logger, skip, err := simapp.SetupSimulation("leveldb-app-sim", "Simulation")
if skip {
t.Skip("skipping application import/export simulation")
}
Expand All @@ -109,19 +111,19 @@ func TestAppImportExport(t *testing.T) {
app.BaseApp,
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts,
SimulationOperations(*app, app.AppCodec(), config),
simapp.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
app.AppCodec(),
)

// export state and simParams before the simulation error is checked
err = CheckExportSimulation(*app, config, simParams)
err = simapp.CheckExportSimulation(app, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)

if config.Commit {
PrintStats(db)
simapp.PrintStats(db)
}

fmt.Printf("exporting genesis...\n")
Expand All @@ -139,16 +141,28 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := NewGravityApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, FlagPeriodValue, MakeEncodingConfig(), EmptyAppOptions{}, fauxMerkleModeOpt)
newApp := NewGravityApp(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, DefaultNodeHome, FlagPeriodValue, MakeEncodingConfig(), EmptyAppOptions{}, fauxMerkleModeOpt)
require.Equal(t, appName, newApp.Name())

var genesisState GenesisState
err = json.Unmarshal(appState.AppState, &genesisState)
require.NoError(t, err)

defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("%v", r)
if !strings.Contains(err, "validator set is empty after InitGenesis") {
panic(r)
}
logger.Info("Skipping simulation as all validators have been unbonded")
logger.Info("err", err, "stacktrace", string(debug.Stack()))
}
}()

ctxA := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
ctxB := newApp.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
newApp.mm.InitGenesis(ctxB, app.AppCodec(), genesisState)
newApp.StoreConsensusParams(ctxB, appState.ConsensusParams)

fmt.Printf("comparing stores...\n")

Expand Down Expand Up @@ -180,7 +194,7 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")

fmt.Printf("compared %d key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)
require.Equal(t, len(failedKVAs), 0, GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
require.Equal(t, len(failedKVAs), 0, simapp.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
}
}

Expand All @@ -206,19 +220,19 @@ func TestAppSimulationAfterImport(t *testing.T) {
app.BaseApp,
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts,
SimulationOperations(*app, app.AppCodec(), config),
simapp.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
app.AppCodec(),
)

// export state and simParams before the simulation error is checked
err = CheckExportSimulation(*app, config, simParams)
err = simapp.CheckExportSimulation(app, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)

if config.Commit {
PrintStats(db)
simapp.PrintStats(db)
}

if stopEarly {
Expand All @@ -233,28 +247,27 @@ func TestAppSimulationAfterImport(t *testing.T) {

fmt.Printf("importing genesis...\n")

_, newDB, newDir, _, _, err := SetupSimulation("leveldb-app-sim-2", "Simulation-2")
_, newDB, newDir, _, _, err := simapp.SetupSimulation("leveldb-app-sim-2", "Simulation-2")
require.NoError(t, err, "simulation setup failed")

defer func() {
newDB.Close()
require.NoError(t, newDB.Close())
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := NewGravityApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, FlagPeriodValue, MakeEncodingConfig(), EmptyAppOptions{}, fauxMerkleModeOpt)
newApp := NewGravityApp(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, DefaultNodeHome, FlagPeriodValue, MakeEncodingConfig(), EmptyAppOptions{}, fauxMerkleModeOpt)
require.Equal(t, appName, newApp.Name())

newApp.InitChain(abci.RequestInitChain{
AppStateBytes: appState.AppState,
})

_, _, err = simulation.SimulateFromSeed(
t,
os.Stdout,
newApp.BaseApp,
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts,
SimulationOperations(*newApp, newApp.AppCodec(), config),
simapp.SimulationOperations(newApp, newApp.AppCodec(), config),
newApp.ModuleAccountAddrs(),
config,
app.AppCodec(),
Expand Down Expand Up @@ -311,7 +324,7 @@ func TestAppStateDeterminism(t *testing.T) {
require.NoError(t, err)

if config.Commit {
PrintStats(db)
simapp.PrintStats(db)
}

appHash := app.LastCommitID().Hash
Expand Down
70 changes: 67 additions & 3 deletions module/app/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
tmjson "github.com/tendermint/tendermint/libs/json"
Expand All @@ -18,6 +19,8 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// TODO: audit this code when we hook up simulations
Expand All @@ -29,10 +32,10 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
return func(r *rand.Rand, accs []simtypes.Account, config simtypes.Config,
) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) {

if FlagGenesisTimeValue == 0 {
if simapp.FlagGenesisTimeValue == 0 {
genesisTimestamp = simtypes.RandTimestamp(r)
} else {
genesisTimestamp = time.Unix(FlagGenesisTimeValue, 0)
genesisTimestamp = time.Unix(simapp.FlagGenesisTimeValue, 0)
}

chainID = config.ChainID
Expand All @@ -44,7 +47,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
// override the default chain-id from simapp to set it later to the config
genesisDoc, accounts := StateFromGenesisFileFn(r, cdc, config.GenesisFile)

if FlagGenesisTimeValue == 0 {
if simapp.FlagGenesisTimeValue == 0 {
// use genesis timestamp if no custom timestamp is provided (i.e no random timestamp)
genesisTimestamp = genesisDoc.GenesisTime
}
Expand All @@ -71,6 +74,67 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams)
}

rawState := make(map[string]json.RawMessage)
err := json.Unmarshal(appState, &rawState)
if err != nil {
panic(err)
}

stakingStateBz, ok := rawState[stakingtypes.ModuleName]
if !ok {
panic("staking genesis state is missing")
}

stakingState := new(stakingtypes.GenesisState)
err = cdc.UnmarshalJSON(stakingStateBz, stakingState)
if err != nil {
panic(err)
}
// compute not bonded balance
notBondedTokens := sdk.ZeroInt()
for _, val := range stakingState.Validators {
if val.Status != stakingtypes.Unbonded {
continue
}
notBondedTokens = notBondedTokens.Add(val.GetTokens())
}
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens)
// edit bank state to make it have the not bonded pool tokens
bankStateBz, ok := rawState[banktypes.ModuleName]
// TODO(fdymylja/jonathan): should we panic in this case
if !ok {
panic("bank genesis state is missing")
}
bankState := new(banktypes.GenesisState)
err = cdc.UnmarshalJSON(bankStateBz, bankState)
if err != nil {
panic(err)
}

stakingAddr := authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName).String()
var found bool
for _, balance := range bankState.Balances {
if balance.Address == stakingAddr {
found = true
break
}
}
if !found {
bankState.Balances = append(bankState.Balances, banktypes.Balance{
Address: stakingAddr,
Coins: sdk.NewCoins(notBondedCoins),
})
}

// change appState back
rawState[stakingtypes.ModuleName] = cdc.MustMarshalJSON(stakingState)
rawState[banktypes.ModuleName] = cdc.MustMarshalJSON(bankState)

// replace appstate
appState, err = json.Marshal(rawState)
if err != nil {
panic(err)
}
return appState, simAccs, chainID, genesisTimestamp
}
}
Expand Down
Loading