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

feat: Allow futureOps to queue more Ops #10469

Merged
merged 10 commits into from
Nov 11, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#9699](https://github.com/cosmos/cosmos-sdk/pull/9699) Add `:`, `.`, `-`, and `_` as allowed characters in the default denom regular expression.
* (genesis) [\#9697](https://github.com/cosmos/cosmos-sdk/pull/9697) Ensure `InitGenesis` returns with non-empty validator set.
* [\#10341](https://github.com/cosmos/cosmos-sdk/pull/10341) Move from `io/ioutil` to `io` and `os` packages.
* [\#10468](https://github.com/cosmos/cosmos-sdk/pull/10468) Allow futureOps to queue additional operations in simulations

### Bug Fixes

Expand Down
40 changes: 25 additions & 15 deletions x/simulation/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,20 @@ func SimulateFromSeed(
ctx := app.NewContext(false, header)

// Run queued operations. Ignores blocksize if blocksize is too small
numQueuedOpsRan := runQueuedOperations(
numQueuedOpsRan, futureOps := runQueuedOperations(
operationQueue, int(header.Height), tb, r, app, ctx, accs, logWriter,
eventStats.Tally, config.Lean, config.ChainID,
)

numQueuedTimeOpsRan := runQueuedTimeOperations(
numQueuedTimeOpsRan, timeFutureOps := runQueuedTimeOperations(
timeOperationQueue, int(header.Height), header.Time,
tb, r, app, ctx, accs, logWriter, eventStats.Tally,
config.Lean, config.ChainID,
)

futureOps = append(futureOps, timeFutureOps...)
queueOperations(operationQueue, timeOperationQueue, futureOps)

// run standard operations
operations := blockSimulator(r, app, ctx, accs, header)
opCount += operations + numQueuedOpsRan + numQueuedTimeOpsRan
Expand Down Expand Up @@ -321,20 +324,23 @@ Comment: %s`,
func runQueuedOperations(queueOps map[int][]simulation.Operation,
height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp,
ctx sdk.Context, accounts []simulation.Account, logWriter LogWriter,
event func(route, op, evResult string), lean bool, chainID string) (numOpsRan int) {
event func(route, op, evResult string), lean bool, chainID string) (numOpsRan int, allFutureOps []simulation.FutureOperation) {

queuedOp, ok := queueOps[height]
if !ok {
return 0
return 0, nil
}

// Keep all future operations
allFutureOps = make([]simulation.FutureOperation, 0)

numOpsRan = len(queuedOp)
for i := 0; i < numOpsRan; i++ {
opMsg, futureOps, err := queuedOp[i](r, app, ctx, accounts, chainID)
if futureOps != nil && len(futureOps) > 0 {
allFutureOps = append(allFutureOps, futureOps...)
}

// For now, queued operations cannot queue more operations.
// If a need arises for us to support queued messages to queue more messages, this can
// be changed.
opMsg, _, err := queuedOp[i](r, app, ctx, accounts, chainID)
opMsg.LogEvent(event)

if !lean || opMsg.OK {
Expand All @@ -348,22 +354,22 @@ func runQueuedOperations(queueOps map[int][]simulation.Operation,
}
delete(queueOps, height)

return numOpsRan
return numOpsRan, allFutureOps
}

func runQueuedTimeOperations(queueOps []simulation.FutureOperation,
height int, currentTime time.Time, tb testing.TB, r *rand.Rand,
app *baseapp.BaseApp, ctx sdk.Context, accounts []simulation.Account,
logWriter LogWriter, event func(route, op, evResult string),
lean bool, chainID string) (numOpsRan int) {
lean bool, chainID string) (numOpsRan int, allFutureOps []simulation.FutureOperation) {

// Keep all future operations
allFutureOps = make([]simulation.FutureOperation, 0)

numOpsRan = 0
for len(queueOps) > 0 && currentTime.After(queueOps[0].BlockTime) {
opMsg, futureOps, err := queueOps[0].Op(r, app, ctx, accounts, chainID)

// For now, queued operations cannot queue more operations.
// If a need arises for us to support queued messages to queue more messages, this can
// be changed.
opMsg, _, err := queueOps[0].Op(r, app, ctx, accounts, chainID)
opMsg.LogEvent(event)

if !lean || opMsg.OK {
Expand All @@ -375,9 +381,13 @@ func runQueuedTimeOperations(queueOps []simulation.FutureOperation,
tb.FailNow()
}

if futureOps != nil && len(futureOps) > 0 {
allFutureOps = append(allFutureOps, futureOps...)
}

queueOps = queueOps[1:]
numOpsRan++
}

return numOpsRan
return numOpsRan, allFutureOps
}