From 84860a8d15b54e4609d3a02b3a02002f94bcb988 Mon Sep 17 00:00:00 2001 From: fkneeland-figure <86616427+fkneeland-figure@users.noreply.github.com> Date: Thu, 11 Nov 2021 02:32:06 -0700 Subject: [PATCH] feat: Allow futureOps to queue more Ops (#10469) ## Description I modified where we handle queued operations to get all of the future operations from each queued operation that is executed. We then add any new futureOps to the queued ops list. This enables simulations to run chains of Operations rather than only being able to go one layer deep. This is helpful in the case of building simulations around smart contracts that require multiple operations occurring in order. Closes: #10468 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- CHANGELOG.md | 1 + x/simulation/simulate.go | 40 +++++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73235a201b9a..6565b5cdd74d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/x/simulation/simulate.go b/x/simulation/simulate.go index e1ff8f1826b6..9b8c433be10a 100644 --- a/x/simulation/simulate.go +++ b/x/simulation/simulate.go @@ -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 @@ -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 { @@ -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 { @@ -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 }