From e8469d5b4cdd65e49a0ed25e5f8ac6bf188013d6 Mon Sep 17 00:00:00 2001 From: fkneeland Date: Fri, 29 Oct 2021 14:51:01 -0600 Subject: [PATCH 1/8] Added withBlockTime when creating a ctx for simulations --- baseapp/baseapp.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index ec9fb28ad449..bd3642a5dec8 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "time" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" @@ -358,7 +359,7 @@ func (app *BaseApp) setCheckState(header tmproto.Header) { ms := app.cms.CacheMultiStore() app.checkState = &state{ ms: ms, - ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices), + ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices).WithBlockTime(time.Now()), } } From 7a7d17f7179d4e1f8adcb3ecd524282d4642f986 Mon Sep 17 00:00:00 2001 From: fkneeland Date: Fri, 29 Oct 2021 15:03:26 -0600 Subject: [PATCH 2/8] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 787ddee263c3..a70b69eeea0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -147,6 +147,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 not setting BlockTime ### State Machine Breaking From 5678355c1c83a567c72b43cf7b86a183f7f0ac43 Mon Sep 17 00:00:00 2001 From: fkneeland Date: Fri, 29 Oct 2021 15:13:01 -0600 Subject: [PATCH 3/8] moved setting of time to init function only --- baseapp/baseapp.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index bd3642a5dec8..f3c36dee82e1 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -290,7 +290,7 @@ func (app *BaseApp) init() error { } // needed for the export command which inits from store but never calls initchain - app.setCheckState(tmproto.Header{}) + app.setCheckState(tmproto.Header{Time: time.Now()}) app.Seal() // make sure the snapshot interval is a multiple of the pruning KeepEvery interval @@ -359,7 +359,7 @@ func (app *BaseApp) setCheckState(header tmproto.Header) { ms := app.cms.CacheMultiStore() app.checkState = &state{ ms: ms, - ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices).WithBlockTime(time.Now()), + ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices), } } From 9cbb1bbe06a0e81a928237594e033397b49c9b4e Mon Sep 17 00:00:00 2001 From: fkneeland Date: Tue, 2 Nov 2021 14:04:32 -0600 Subject: [PATCH 4/8] move time now to only in simulation --- baseapp/baseapp.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index f3c36dee82e1..8b495980c395 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -290,7 +290,7 @@ func (app *BaseApp) init() error { } // needed for the export command which inits from store but never calls initchain - app.setCheckState(tmproto.Header{Time: time.Now()}) + app.setCheckState(tmproto.Header{}) app.Seal() // make sure the snapshot interval is a multiple of the pruning KeepEvery interval @@ -482,6 +482,10 @@ func (app *BaseApp) getState(mode runTxMode) *state { return app.deliverState } + if app.checkState.ctx.BlockTime().UnixNano() < 1 { + app.checkState.ctx = app.checkState.ctx.WithBlockTime(time.Now()) + } + return app.checkState } From 84cf7b2c4f86a45795360aecb636fc07a1e45fc1 Mon Sep 17 00:00:00 2001 From: fkneeland Date: Tue, 2 Nov 2021 16:42:11 -0600 Subject: [PATCH 5/8] Updated how we randomly generate time for simulations --- baseapp/baseapp.go | 6 ------ types/simulation/rand_util.go | 8 ++++++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 8b495980c395..ae4748220442 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -4,8 +4,6 @@ import ( "context" "errors" "fmt" - "time" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -482,10 +480,6 @@ func (app *BaseApp) getState(mode runTxMode) *state { return app.deliverState } - if app.checkState.ctx.BlockTime().UnixNano() < 1 { - app.checkState.ctx = app.checkState.ctx.WithBlockTime(time.Now()) - } - return app.checkState } diff --git a/types/simulation/rand_util.go b/types/simulation/rand_util.go index 84cd4492c8a7..3be124201767 100644 --- a/types/simulation/rand_util.go +++ b/types/simulation/rand_util.go @@ -86,8 +86,12 @@ 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) + // json.Marshal breaks for timestamps with year greater than 9999 + // UnixNano breaks with year greater than 2262 + now := time.Now().UnixMilli() / 1000 + + // Calculate a random time in seconds between epoch and 200 years from now + unixTime := r.Int63n(now + 60*60*24*365*200) return time.Unix(unixTime, 0) } From c9ab160e821cc6fddefa498a268fd4cbcc1e095a Mon Sep 17 00:00:00 2001 From: fkneeland Date: Tue, 2 Nov 2021 16:43:52 -0600 Subject: [PATCH 6/8] updated --- CHANGELOG.md | 2 +- baseapp/baseapp.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a70b69eeea0d..5a8d6748be55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -147,7 +147,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 not setting BlockTime +* [\#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/baseapp/baseapp.go b/baseapp/baseapp.go index ae4748220442..ec9fb28ad449 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" From 1f6a7836681cf3291a99aa737c35a635824b24de Mon Sep 17 00:00:00 2001 From: fkneeland Date: Mon, 8 Nov 2021 09:52:41 -0700 Subject: [PATCH 7/8] fix tests --- types/simulation/rand_util.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/simulation/rand_util.go b/types/simulation/rand_util.go index 3be124201767..476afc0fffe4 100644 --- a/types/simulation/rand_util.go +++ b/types/simulation/rand_util.go @@ -88,10 +88,10 @@ func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec { func RandTimestamp(r *rand.Rand) time.Time { // json.Marshal breaks for timestamps with year greater than 9999 // UnixNano breaks with year greater than 2262 - now := time.Now().UnixMilli() / 1000 + start := time.Date(2021, time.Month(1), 1, 1, 1, 1, 1, time.UTC).UnixMilli() / 1000 - // Calculate a random time in seconds between epoch and 200 years from now - unixTime := r.Int63n(now + 60*60*24*365*200) + // Calculate a random time in seconds between epoch and 200 years from Jan 1, 2021 + unixTime := r.Int63n(start + 60*60*24*365*200) return time.Unix(unixTime, 0) } From 82e81d202621b7903f2966c963afeecc023d4dbe Mon Sep 17 00:00:00 2001 From: fkneeland Date: Mon, 8 Nov 2021 10:13:50 -0700 Subject: [PATCH 8/8] fixing tests more --- types/simulation/rand_util.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/types/simulation/rand_util.go b/types/simulation/rand_util.go index 476afc0fffe4..10926f0a132d 100644 --- a/types/simulation/rand_util.go +++ b/types/simulation/rand_util.go @@ -88,11 +88,14 @@ func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec { func RandTimestamp(r *rand.Rand) time.Time { // json.Marshal breaks for timestamps with year greater than 9999 // UnixNano breaks with year greater than 2262 - start := time.Date(2021, time.Month(1), 1, 1, 1, 1, 1, time.UTC).UnixMilli() / 1000 + start := time.Date(2062, time.Month(1), 1, 1, 1, 1, 1, time.UTC).UnixMilli() - // Calculate a random time in seconds between epoch and 200 years from Jan 1, 2021 - unixTime := r.Int63n(start + 60*60*24*365*200) - return time.Unix(unixTime, 0) + // 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.