Skip to content

Commit

Permalink
feat: Allow futureOps to queue more Ops (#10469)
Browse files Browse the repository at this point in the history
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## 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

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### 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)
  • Loading branch information
fkneeland-figure authored Nov 11, 2021
1 parent 2b02f06 commit 84860a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
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
}

0 comments on commit 84860a8

Please sign in to comment.