From 9e15df4853370eeb49ea28d620286096b923ba3a Mon Sep 17 00:00:00 2001 From: fkneeland-figure <86616427+fkneeland-figure@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:16:02 -0700 Subject: [PATCH] fix: Update ctx BlockTime for simulations (#10467) ## Description When running simulation tests with wasmd I am running into this error: https://github.com/CosmWasm/wasmd/blob/14e58bfa81ea2ade0ff00ebd067ac0bc75e37cf8/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 --- ### 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 + types/simulation/rand_util.go | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9936044415f3..c2bf1f1f1ed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/types/simulation/rand_util.go b/types/simulation/rand_util.go index 84cd4492c8a7..10926f0a132d 100644 --- a/types/simulation/rand_util.go +++ b/types/simulation/rand_util.go @@ -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.