Skip to content

Commit

Permalink
fix: context and finalizeBlockReq block time (sims) (cosmos#16286)
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica authored May 25, 2023
1 parent fac0395 commit b6088ab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
6 changes: 3 additions & 3 deletions x/simulation/mock_cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ func RandomRequestFinalizeBlock(
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
time = pastTimes[height-1]
misbehaviorTime = pastTimes[height-1]
vals = pastVoteInfos[height-1]
}

Expand All @@ -207,7 +207,7 @@ func RandomRequestFinalizeBlock(
Type: abci.MisbehaviorType_DUPLICATE_VOTE,
Validator: validator,
Height: height,
Time: time,
Time: misbehaviorTime,
TotalVotingPower: totalVotingPower,
},
)
Expand Down
29 changes: 14 additions & 15 deletions x/simulation/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ 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, genesisTimestamp, accs, chainID := initChain(r, params, accs, app, appStateFn, config, cdc)
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")
}
Expand All @@ -85,7 +85,7 @@ func SimulateFromSeed(

fmt.Printf(
"Starting the simulation from time %v (unixtime %v)\n",
genesisTimestamp.UTC().Format(time.UnixDate), genesisTimestamp.Unix(),
blockTime.UTC().Format(time.UnixDate), blockTime.Unix(),
)

// remove module account address if they exist in accs
Expand All @@ -105,10 +105,9 @@ func SimulateFromSeed(
pastVoteInfos [][]abci.VoteInfo
timeOperationQueue []simulation.FutureOperation

blockHeight int64 = 1
blockTime = genesisTimestamp
proposerAddress = validators.randomProposer(r)
opCount = 0
blockHeight = int64(config.InitialBlockHeight)
proposerAddress = validators.randomProposer(r)
opCount = 0
)

// Setup code to catch SIGTERM's
Expand All @@ -129,8 +128,8 @@ func SimulateFromSeed(
pastTimes,
pastVoteInfos,
eventStats.Tally,
1,
genesisTimestamp,
blockHeight,
blockTime,
validators.randomProposer(r),
)

Expand Down Expand Up @@ -169,12 +168,12 @@ func SimulateFromSeed(
exportedParams = params
}

for height := config.InitialBlockHeight; height < config.NumBlocks+config.InitialBlockHeight && !stopEarly; height++ {
for blockHeight < int64(config.NumBlocks+config.InitialBlockHeight) && !stopEarly {
pastTimes = append(pastTimes, blockTime)
pastVoteInfos = append(pastVoteInfos, finalizeBlockReq.DecidedLastCommit.Votes)

// Run the BeginBlock handler
logWriter.AddEntry(BeginBlockEntry(int64(height)))
logWriter.AddEntry(BeginBlockEntry(blockHeight))

res, err := app.FinalizeBlock(finalizeBlockReq)
if err != nil {
Expand All @@ -183,7 +182,7 @@ func SimulateFromSeed(

ctx := app.NewContext(false, cmtproto.Header{
Height: blockHeight,
Time: genesisTimestamp,
Time: blockTime,
ProposerAddress: proposerAddress,
ChainID: config.ChainID,
})
Expand All @@ -206,7 +205,7 @@ func SimulateFromSeed(
// run standard operations
operations := blockSimulator(r, app, ctx, accs, cmtproto.Header{
Height: blockHeight,
Time: genesisTimestamp,
Time: blockTime,
ProposerAddress: proposerAddress,
ChainID: config.ChainID,
})
Expand All @@ -218,7 +217,7 @@ func SimulateFromSeed(
blockTime = blockTime.Add(time.Duration(int64(r.Intn(int(timeDiff)))) * time.Second)
proposerAddress = validators.randomProposer(r)

logWriter.AddEntry(EndBlockEntry(int64(height)))
logWriter.AddEntry(EndBlockEntry(blockHeight))

if config.Commit {
app.Commit()
Expand All @@ -232,15 +231,15 @@ func SimulateFromSeed(

// Generate a random RequestBeginBlock with the current validator set
// for the next block
finalizeBlockReq = RandomRequestFinalizeBlock(r, params, validators, pastTimes, pastVoteInfos, eventStats.Tally, blockHeight, genesisTimestamp, proposerAddress)
finalizeBlockReq = RandomRequestFinalizeBlock(r, params, validators, pastTimes, pastVoteInfos, eventStats.Tally, blockHeight, blockTime, proposerAddress)

// Update the validator set, which will be reflected in the application
// on the next block
validators = nextValidators
nextValidators = updateValidators(tb, r, params, validators, res.ValidatorUpdates, eventStats.Tally)

// update the exported params
if config.ExportParamsPath != "" && config.ExportParamsHeight == height {
if config.ExportParamsPath != "" && int64(config.ExportParamsHeight) == blockHeight {
exportedParams = params
}
}
Expand Down

0 comments on commit b6088ab

Please sign in to comment.