fix(sims): TestAppSimulationAfterImport and legacy proposal handling (#21800)

This commit is contained in:
Alexander Peters
2024-09-18 15:03:03 +00:00
committed by GitHub
parent 356df96770
commit cc8366c3ee
11 changed files with 201 additions and 60 deletions
+9 -7
View File
@@ -92,9 +92,9 @@ func updateValidators(
if update.Power == 0 {
if _, ok := current[str]; !ok {
tb.Fatalf("tried to delete a nonexistent validator: %s", str)
tb.Logf("tried to delete a nonexistent validator: %s", str)
continue
}
event("end_block", "validator_updates", "kicked")
delete(current, str)
} else if _, ok := current[str]; ok {
@@ -187,15 +187,17 @@ func RandomRequestFinalizeBlock(
params.evidenceFraction = 0.9
}
totalBlocksProcessed := len(pastTimes)
startHeight := blockHeight - int64(totalBlocksProcessed) + 1
for r.Float64() < params.EvidenceFraction() {
vals := voteInfos
height := blockHeight
misbehaviorTime := time
if r.Float64() < params.PastEvidenceFraction() && height > 1 {
height = int64(r.Intn(int(height)-1)) + 1 // CometBFT starts at height 1
// array indices offset by one
misbehaviorTime = pastTimes[height-1]
vals = pastVoteInfos[height-1]
if r.Float64() < params.PastEvidenceFraction() && totalBlocksProcessed > 1 {
n := int64(r.Intn(totalBlocksProcessed))
misbehaviorTime = pastTimes[n]
vals = pastVoteInfos[n]
height = startHeight + n
}
validator := vals[r.Intn(len(vals))].Validator
+10 -9
View File
@@ -48,6 +48,7 @@ func initChain(
ChainId: chainID,
ConsensusParams: consensusParams,
Time: genesisTimestamp,
InitialHeight: int64(config.InitialBlockHeight),
}
res, err := app.InitChain(&req)
if err != nil {
@@ -60,7 +61,7 @@ func initChain(
// SimulateFromSeed tests an application by running the provided
// operations, testing the provided invariants, but using the provided config.Seed.
func SimulateFromSeed( // exists for backwards compatibility only
func SimulateFromSeed(
tb testing.TB,
logger corelog.Logger,
w io.Writer,
@@ -72,7 +73,7 @@ func SimulateFromSeed( // exists for backwards compatibility only
config simtypes.Config,
cdc codec.JSONCodec,
addressCodec address.Codec,
) (exportedParams Params, err error) {
) (exportedParams Params, accs []simtypes.Account, err error) {
tb.Helper()
mode, _, _ := getTestingMode(tb)
return SimulateFromSeedX(tb, logger, w, app, appStateFn, randAccFn, ops, blockedAddrs, config, cdc, NewLogWriter(mode))
@@ -92,7 +93,7 @@ func SimulateFromSeedX(
config simtypes.Config,
cdc codec.JSONCodec,
logWriter LogWriter,
) (exportedParams Params, err error) {
) (exportedParams Params, accs []simtypes.Account, err error) {
tb.Helper()
defer func() {
if err != nil {
@@ -110,7 +111,7 @@ func SimulateFromSeedX(
logger.Debug("Randomized simulation setup", "params", mustMarshalJSONIndent(params))
timeDiff := maxTimePerBlock - minTimePerBlock
accs := randAccFn(r, params.NumKeys())
accs = randAccFn(r, params.NumKeys())
eventStats := NewEventStats()
// Second variable to keep pending validator set (delayed one block since
@@ -119,7 +120,7 @@ func SimulateFromSeedX(
// 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 params, errors.New("at least two genesis accounts are required")
return params, accs, errors.New("at least two genesis accounts are required")
}
config.ChainID = chainID
@@ -187,7 +188,7 @@ func SimulateFromSeedX(
}
if _, err := app.FinalizeBlock(finalizeBlockReq); err != nil {
return params, fmt.Errorf("block finalization failed at height %d: %w", blockHeight, err)
return params, accs, fmt.Errorf("block finalization failed at height %d: %+w", blockHeight, err)
}
for blockHeight < int64(config.NumBlocks+config.InitialBlockHeight) {
@@ -199,7 +200,7 @@ func SimulateFromSeedX(
res, err := app.FinalizeBlock(finalizeBlockReq)
if err != nil {
return params, fmt.Errorf("block finalization failed at height %d: %w", blockHeight, err)
return params, accs, fmt.Errorf("block finalization failed at height %d: %w", blockHeight, err)
}
ctx := app.NewContextLegacy(false, cmtproto.Header{
@@ -246,7 +247,7 @@ func SimulateFromSeedX(
if config.Commit {
app.SimWriteState()
if _, err := app.Commit(); err != nil {
return params, fmt.Errorf("commit failed at height %d: %w", blockHeight, err)
return params, accs, fmt.Errorf("commit failed at height %d: %w", blockHeight, err)
}
}
@@ -278,7 +279,7 @@ func SimulateFromSeedX(
} else {
eventStats.Print(w)
}
return exportedParams, err
return exportedParams, accs, err
}
type blockSimFn func(