fix: Update ctx BlockTime for simulations (#10467)

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

When running simulation tests with wasmd I am running into this error: 14e58bfa81/x/wasm/types/types.go (L258)

This is because when we are running in simulation mode we randomly create a date before the year 9999.  However, if a date is after the year 2262 `UnixNano` will overflow and possibly return a negative number.  To fix this I updated how we set the random genesis start time to be between the epoch and 200 years from now which means this error won't show itself again for another 40+ years. 

Closes: #10466 

<!-- 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)
This commit is contained in:
fkneeland-figure 2021-11-09 10:16:02 -07:00 committed by GitHub
parent 23942663d9
commit b9c5a0578b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -152,6 +152,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#10239](https://github.com/cosmos/cosmos-sdk/pull/10239) Fixed x/bank/044 migrateDenomMetadata.
* (x/upgrade) [\#10189](https://github.com/cosmos/cosmos-sdk/issues/10189) Removed potential sources of non-determinism in upgrades
* [\#10258](https://github.com/cosmos/cosmos-sdk/issues/10258) Fixes issue related to segmentaiton fault on mac m1 arm64
* [\#10466](https://github.com/cosmos/cosmos-sdk/issues/10466) Fixes error with simulation tests when genesis start time is randomly created after the year 2262
### State Machine Breaking

View File

@ -86,9 +86,16 @@ func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec {
// RandTimestamp generates a random timestamp
func RandTimestamp(r *rand.Rand) time.Time {
// json.Marshal breaks for timestamps greater with year greater than 9999
unixTime := r.Int63n(253373529600)
return time.Unix(unixTime, 0)
// json.Marshal breaks for timestamps with year greater than 9999
// UnixNano breaks with year greater than 2262
start := time.Date(2062, time.Month(1), 1, 1, 1, 1, 1, time.UTC).UnixMilli()
// Calculate a random amount of time in seconds between 0 and 200 years
unixTime := r.Int63n(60*60*24*365*200) * 1000 // convert to milliseconds
// Get milliseconds for a time between Jan 1, 2062 and Jan 1, 2262
rtime := time.UnixMilli(start + unixTime).UnixMilli() / 1000
return time.Unix(rtime, 0)
}
// RandIntBetween returns a random int between two numbers inclusively.