Skip to content

Commit

Permalink
fix(simulation): add an SigverifyTx flag in the simulation test wheth…
Browse files Browse the repository at this point in the history
…er to sigverify check for transaction
  • Loading branch information
chenqunl committed Oct 9, 2023
1 parent 0b0b283 commit 8996f6b
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ test-sim-custom-genesis-fast:
@echo "Running custom genesis simulation..."
@echo "By default, ${HOME}/.simapp/config/genesis.json will be used."
@cd ${CURRENT_DIR}/simapp && go test -mod=readonly -run TestFullAppSimulation -Genesis=${HOME}/.simapp/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=99 -Period=5 -SigverifyTx=false -v -timeout 24h

test-sim-import-export: runsim
@echo "Running application import/export simulation. This may take several minutes..."
Expand All @@ -285,7 +285,7 @@ test-sim-after-import: runsim
test-sim-custom-genesis-multi-seed: runsim
@echo "Running multi-seed custom genesis simulation..."
@echo "By default, ${HOME}/.simapp/config/genesis.json will be used."
@cd ${CURRENT_DIR}/simapp && $(BINDIR)/runsim -Genesis=${HOME}/.simapp/config/genesis.json -SimAppPkg=. -ExitOnFail 400 5 TestFullAppSimulation
@cd ${CURRENT_DIR}/simapp && $(BINDIR)/runsim -Genesis=${HOME}/.simapp/config/genesis.json -SigverifyTx=false -SimAppPkg=. -ExitOnFail 400 5 TestFullAppSimulation

test-sim-multi-seed-long: runsim
@echo "Running long multi-seed application simulation. This may take awhile!"
Expand Down
27 changes: 11 additions & 16 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ type (
)

const (
execModeCheck execMode = iota // Check a transaction
execModeReCheck // Recheck a (pending) transaction after a commit
execModeSimulate // Simulate a transaction
execModePrepareProposal // Prepare a block proposal
execModeProcessProposal // Process a block proposal
execModeVoteExtension // Extend or verify a pre-commit vote
execModeFinalize // Finalize a block proposal
execModeSimulationFinalize // Finalize a block proposal when run simulation, for example TestFullAppSimulation
execModeCheck execMode = iota // Check a transaction
execModeReCheck // Recheck a (pending) transaction after a commit
execModeSimulate // Simulate a transaction
execModePrepareProposal // Prepare a block proposal
execModeProcessProposal // Process a block proposal
execModeVoteExtension // Extend or verify a pre-commit vote
execModeFinalize // Finalize a block proposal
)

var _ servertypes.ABCI = (*BaseApp)(nil)
Expand Down Expand Up @@ -90,6 +89,7 @@ type BaseApp struct {
addrPeerFilter sdk.PeerFilter // filter peers by address and port
idPeerFilter sdk.PeerFilter // filter peers by node ID
fauxMerkleMode bool // if true, IAVL MountStores uses MountStoresDB for simulation speed.
sigverifyTx bool // whether to sigverify check for transaction

// manages snapshots, i.e. dumps of app state at certain intervals
snapshotManager *snapshots.Manager
Expand Down Expand Up @@ -199,6 +199,7 @@ func NewBaseApp(
msgServiceRouter: NewMsgServiceRouter(),
txDecoder: txDecoder,
fauxMerkleMode: false,
sigverifyTx: true,
queryGasLimit: math.MaxUint64,
}

Expand Down Expand Up @@ -611,7 +612,7 @@ func validateBasicTxMsgs(msgs []sdk.Msg) error {

func (app *BaseApp) getState(mode execMode) *state {
switch mode {
case execModeFinalize, execModeSimulationFinalize:
case execModeFinalize:
return app.finalizeBlockState

case execModePrepareProposal:
Expand Down Expand Up @@ -643,7 +644,7 @@ func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte) sdk.Context {
WithTxBytes(txBytes)
// WithVoteInfos(app.voteInfos) // TODO: identify if this is needed

ctx = ctx.WithIsSigverifyTx(mode != execModeSimulationFinalize)
ctx = ctx.WithIsSigverifyTx(app.sigverifyTx)

ctx = ctx.WithConsensusParams(app.GetConsensusParams(ctx))

Expand Down Expand Up @@ -800,12 +801,6 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte) (gInfo sdk.GasInfo, res
var gasWanted uint64

ctx := app.getContextForTx(mode, txBytes)
// after we set the sigverifyTx field in ctx in the function app.getContextForTx(mode, txBytes)
// we restore the mode value to facilitate logical judgment.
if mode == execModeSimulationFinalize {
mode = execModeFinalize
}

ms := ctx.MultiStore()

// only run the tx if there is block gas remaining
Expand Down
5 changes: 5 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ func (app *BaseApp) SetFauxMerkleMode() {
app.fauxMerkleMode = true
}

// SetNotSigverify during simulation testing, transaction signature verification needs to be ignored.
func (app *BaseApp) SetNotSigverifyTx() {
app.sigverifyTx = false
}

// SetCommitMultiStoreTracer sets the store tracer on the BaseApp's underlying
// CommitMultiStore.
func (app *BaseApp) SetCommitMultiStoreTracer(w io.Writer) {
Expand Down
8 changes: 4 additions & 4 deletions baseapp/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package baseapp

import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

errorsmod "cosmossdk.io/errors"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -35,7 +34,8 @@ func (app *BaseApp) SimDeliver(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo,
if err != nil {
return sdk.GasInfo{}, nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err)
}
gasInfo, result, _, err := app.runTx(execModeSimulationFinalize, bz)

gasInfo, result, _, err := app.runTx(execModeFinalize, bz)
return gasInfo, result, err
}

Expand All @@ -46,7 +46,7 @@ func (app *BaseApp) SimTxFinalizeBlock(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.
return sdk.GasInfo{}, nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err)
}

gasInfo, result, _, err := app.runTx(execModeSimulationFinalize, bz)
gasInfo, result, _, err := app.runTx(execModeFinalize, bz)
return gasInfo, result, err
}

Expand Down
12 changes: 12 additions & 0 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func TestFullAppSimulation(t *testing.T) {
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue

app := NewSimApp(logger, db, nil, true, appOptions, fauxMerkleModeOpt, baseapp.SetChainID(SimAppChainID))
if !simcli.FlagSigverifyTxValue {
app.SetNotSigverifyTx()
}
require.Equal(t, "SimApp", app.Name())

// run randomized simulation
Expand Down Expand Up @@ -121,6 +124,9 @@ func TestAppImportExport(t *testing.T) {
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue

app := NewSimApp(logger, db, nil, true, appOptions, fauxMerkleModeOpt, baseapp.SetChainID(SimAppChainID))
if !simcli.FlagSigverifyTxValue {
app.SetNotSigverifyTx()
}
require.Equal(t, "SimApp", app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -240,6 +246,9 @@ func TestAppSimulationAfterImport(t *testing.T) {
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue

app := NewSimApp(logger, db, nil, true, appOptions, fauxMerkleModeOpt, baseapp.SetChainID(SimAppChainID))
if !simcli.FlagSigverifyTxValue {
app.SetNotSigverifyTx()
}
require.Equal(t, "SimApp", app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -362,6 +371,9 @@ func TestAppStateDeterminism(t *testing.T) {

db := dbm.NewMemDB()
app := NewSimApp(logger, db, nil, true, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
if !simcli.FlagSigverifyTxValue {
app.SetNotSigverifyTx()
}

fmt.Printf(
"running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n",
Expand Down
20 changes: 13 additions & 7 deletions testutil/sims/state_helpers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package sims

import (
"bufio"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"io"
"math/rand"
"os"
"path/filepath"
"time"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -251,19 +253,23 @@ func AppStateRandomizedFn(
// AppStateFromGenesisFileFn util function to generate the genesis AppState
// from a genesis.json file.
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (genutiltypes.AppGenesis, []simtypes.Account, error) {
bytes, err := os.ReadFile(genesisFile)
file, err := os.Open(filepath.Clean(genesisFile))
if err != nil {
panic(err)
}

var genesis genutiltypes.AppGenesis
if err = json.Unmarshal(bytes, &genesis); err != nil {
return genesis, nil, err
genesis, err := genutiltypes.AppGenesisFromReader(bufio.NewReader(file))
if err != nil {
return *genesis, nil, err
}

if err := file.Close(); err != nil {
return *genesis, nil, err
}

var appState map[string]json.RawMessage
if err = json.Unmarshal(genesis.AppState, &appState); err != nil {
return genesis, nil, err
return *genesis, nil, err
}

var authGenesis authtypes.GenesisState
Expand All @@ -285,13 +291,13 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile str

a, ok := acc.GetCachedValue().(sdk.AccountI)
if !ok {
return genesis, nil, fmt.Errorf("expected account")
return *genesis, nil, fmt.Errorf("expected account")
}

// create simulator accounts
simAcc := simtypes.Account{PrivKey: privKey, PubKey: privKey.PubKey(), Address: a.GetAddress(), ConsKey: ed25519.GenPrivKeyFromSecret(privkeySeed)}
newAccs[i] = simAcc
}

return genesis, newAccs, nil
return *genesis, newAccs, nil
}
1 change: 0 additions & 1 deletion types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ func (c Context) WithIsReCheckTx(isRecheckTx bool) Context {
}

// WithIsSigverifyTx called with true will sigverify in auth module
// If the transaction is executed in execModeSimulationFinalize mode, the sigverify check will be skipped.
func (c Context) WithIsSigverifyTx(isSigverifyTx bool) Context {
c.sigverifyTx = isSigverifyTx
return c
Expand Down
2 changes: 2 additions & 0 deletions x/simulation/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
FlagVerboseValue bool
FlagPeriodValue uint
FlagGenesisTimeValue int64
FlagSigverifyTxValue bool
)

// GetSimulatorFlags gets the values of all the available simulation flags
Expand All @@ -56,6 +57,7 @@ func GetSimulatorFlags() {
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")
flag.BoolVar(&FlagSigverifyTxValue, "SigverifyTx", true, "whether to sigverify check for transaction ")
}

// NewConfigFromFlags creates a simulation from the retrieved values of the flags.
Expand Down
6 changes: 4 additions & 2 deletions x/simulation/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ func SimulateFromSeed(
// Second variable to keep pending validator set (delayed one block since
// TM 0.24) Initially this is the same as the initial validator set
validators, blockTime, accs, chainID := initChain(r, params, accs, app, appStateFn, config, cdc)
if len(accs) == 0 {
return true, params, fmt.Errorf("must have greater than zero genesis accounts")
// At least 2 accounts must be added here, otherwise when executing SimulateMsgSend
// two accounts will be selected to meet the conditions from != to and it will fall into an infinite loop.
if len(accs) <= 1 {
return true, params, fmt.Errorf("At least two genesis accounts are required")

Check failure on line 90 in x/simulation/simulate.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ST1005: error strings should not be capitalized (stylecheck)

Check failure on line 90 in x/simulation/simulate.go

View workflow job for this annotation

GitHub Actions / Analyze

ST1005: error strings should not be capitalized (stylecheck)
}

config.ChainID = chainID
Expand Down

0 comments on commit 8996f6b

Please sign in to comment.