diff --git a/command/regenesis/test_on_history.go b/command/regenesis/test_on_history.go index 9e454d1f49..1572116c58 100644 --- a/command/regenesis/test_on_history.go +++ b/command/regenesis/test_on_history.go @@ -106,7 +106,7 @@ func HistoryTestCmd() *cobra.Command { } tmpStorage := itrie.NewKV(tmpDB) - tt := time.Now() + tt := time.Now().UTC() err = itrie.CopyTrie(header.StateRoot.Bytes(), itrie.NewKV(trieDB), tmpStorage, []byte{}, false) if err != nil { diff --git a/consensus/dev/dev.go b/consensus/dev/dev.go index e72d5629c1..79a2053f62 100644 --- a/consensus/dev/dev.go +++ b/consensus/dev/dev.go @@ -158,7 +158,7 @@ func (d *Dev) writeNewBlock(parent *types.Header) error { ParentHash: parent.Hash, Number: num + 1, GasLimit: parent.GasLimit, // Inherit from parent for now, will need to adjust dynamically later. - Timestamp: uint64(time.Now().Unix()), + Timestamp: uint64(time.Now().UTC().Unix()), } // calculate gas limit based on parent header diff --git a/consensus/ibft/consensus_backend.go b/consensus/ibft/consensus_backend.go index 087fb024c1..2e875d35ef 100644 --- a/consensus/ibft/consensus_backend.go +++ b/consensus/ibft/consensus_backend.go @@ -202,7 +202,7 @@ func (i *backendIBFT) buildBlock(parent *types.Header) (*types.Block, error) { } // Set the header timestamp - potentialTimestamp := i.calcHeaderTimestamp(parent.Timestamp, time.Now()) + potentialTimestamp := i.calcHeaderTimestamp(parent.Timestamp, time.Now().UTC()) header.Timestamp = uint64(potentialTimestamp.Unix()) parentCommittedSeals, err := i.extractParentCommittedSeals(parent) diff --git a/consensus/ibft/consensus_backend_test.go b/consensus/ibft/consensus_backend_test.go index 97167bcca8..756644305d 100644 --- a/consensus/ibft/consensus_backend_test.go +++ b/consensus/ibft/consensus_backend_test.go @@ -13,7 +13,7 @@ func TestIBFTBackend_CalculateHeaderTimestamp(t *testing.T) { t.Parallel() // Reference time - now := time.Unix(time.Now().Unix(), 0) // Round down + now := time.Unix(time.Now().UTC().Unix(), 0) // Round down testTable := []struct { name string @@ -66,7 +66,7 @@ func TestIBFTBackend_RoundUpTime(t *testing.T) { t.Parallel() // Reference time - now := time.Now() + now := time.Now().UTC() calcExpected := func(time int64, multiple int64) int64 { if time%multiple == 0 { diff --git a/consensus/polybft/block_builder.go b/consensus/polybft/block_builder.go index d0a6f75997..05733308db 100644 --- a/consensus/polybft/block_builder.go +++ b/consensus/polybft/block_builder.go @@ -67,8 +67,8 @@ func (b *BlockBuilder) Reset() error { parentTime := time.Unix(int64(b.params.Parent.Timestamp), 0) headerTime := parentTime.Add(b.params.BlockTime) - if headerTime.Before(time.Now()) { - headerTime = time.Now() + if headerTime.Before(time.Now().UTC()) { + headerTime = time.Now().UTC() } b.header = &types.Header{ diff --git a/consensus/polybft/consensus_runtime_test.go b/consensus/polybft/consensus_runtime_test.go index cf06c3eb4b..71f81a1958 100644 --- a/consensus/polybft/consensus_runtime_test.go +++ b/consensus/polybft/consensus_runtime_test.go @@ -1126,7 +1126,7 @@ func createTestBitmaps(t *testing.T, validators AccountSet, numberOfBlocks uint6 bitmaps := make(map[uint64]bitmap.Bitmap, numberOfBlocks) - rand.Seed(time.Now().Unix()) + rand.Seed(time.Now().UTC().Unix()) for i := numberOfBlocks; i > 1; i-- { bitmap := bitmap.Bitmap{} diff --git a/consensus/polybft/fsm_test.go b/consensus/polybft/fsm_test.go index 7acb459475..4ebc793c02 100644 --- a/consensus/polybft/fsm_test.go +++ b/consensus/polybft/fsm_test.go @@ -774,7 +774,7 @@ func TestFSM_Validate_TimestampOlder(t *testing.T) { parent := &types.Header{ Number: parentBlockNumber, ExtraData: createTestExtra(validators.getPublicIdentities(), AccountSet{}, 4, 3, 3), - Timestamp: uint64(time.Now().Unix()), + Timestamp: uint64(time.Now().UTC().Unix()), } parent.ComputeHash() diff --git a/consensus/polybft/helpers_test.go b/consensus/polybft/helpers_test.go index 857ed75d9f..20dda925dc 100644 --- a/consensus/polybft/helpers_test.go +++ b/consensus/polybft/helpers_test.go @@ -136,7 +136,7 @@ func getEpochNumber(t *testing.T, blockNumber, epochSize uint64) uint64 { func newTestState(t *testing.T) *State { t.Helper() - dir := fmt.Sprintf("/tmp/consensus-temp_%v", time.Now().Format(time.RFC3339Nano)) + dir := fmt.Sprintf("/tmp/consensus-temp_%v", time.Now().UTC().Format(time.RFC3339Nano)) err := os.Mkdir(dir, 0775) if err != nil { diff --git a/consensus/polybft/polybft.go b/consensus/polybft/polybft.go index 67725dcfd4..af4ee05ead 100644 --- a/consensus/polybft/polybft.go +++ b/consensus/polybft/polybft.go @@ -344,7 +344,7 @@ func (p *Polybft) startConsensusProtocol() { sequenceCh, stopSequence = p.ibft.runSequence(latestHeader.Number + 1) } - now := time.Now() + now := time.Now().UTC() select { case <-syncerBlockCh: diff --git a/consensus/polybft/polybft_test.go b/consensus/polybft/polybft_test.go index 90ae145cf4..f42b9c33bc 100644 --- a/consensus/polybft/polybft_test.go +++ b/consensus/polybft/polybft_test.go @@ -127,7 +127,7 @@ func TestPolybft_VerifyHeader(t *testing.T) { parentHeader := &types.Header{ Number: polyBftConfig.EpochSize, - Timestamp: uint64(time.Now().UnixMilli()), + Timestamp: uint64(time.Now().UTC().UnixMilli()), } parentCommitment := updateHeaderExtra(parentHeader, parentDelta, nil, &CheckpointData{EpochNumber: 1}, accountSetParent) diff --git a/consensus/polybft/signer/signature_test.go b/consensus/polybft/signer/signature_test.go index be86837daf..3de35c682a 100644 --- a/consensus/polybft/signer/signature_test.go +++ b/consensus/polybft/signer/signature_test.go @@ -34,7 +34,7 @@ func Test_VerifySignature_NegativeCases(t *testing.T) { t.Parallel() // Get a random integer between 1 and 1000 - mRand.Seed(time.Now().UnixNano()) + mRand.Seed(time.Now().UTC().UnixNano()) messageSize := mRand.Intn(1000) + 1 validTestMsg := testGenRandomBytes(t, messageSize) diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index 73fda8c57b..c9b53d4d80 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -405,7 +405,7 @@ func TestPrivateKeyRead(t *testing.T) { } func TestPrivateKeyGeneration(t *testing.T) { - tempFile := "./privateKeyTesting-" + strconv.FormatInt(time.Now().Unix(), 10) + ".key" + tempFile := "./privateKeyTesting-" + strconv.FormatInt(time.Now().UTC().Unix(), 10) + ".key" t.Cleanup(func() { _ = os.Remove(tempFile) diff --git a/e2e-polybft/framework/test-cluster.go b/e2e-polybft/framework/test-cluster.go index 544812a071..916e7a7c53 100644 --- a/e2e-polybft/framework/test-cluster.go +++ b/e2e-polybft/framework/test-cluster.go @@ -55,7 +55,7 @@ const ( var startTime int64 func init() { - startTime = time.Now().UnixMilli() + startTime = time.Now().UTC().UnixMilli() } func resolveBinary() string { diff --git a/e2e/framework/ibft.go b/e2e/framework/ibft.go index 61fa1d421b..32d3b490a5 100644 --- a/e2e/framework/ibft.go +++ b/e2e/framework/ibft.go @@ -22,7 +22,7 @@ type IBFTServerConfigCallback func(index int, config *TestServerConfig) var startTime int64 func init() { - startTime = time.Now().UnixMilli() + startTime = time.Now().UTC().UnixMilli() } func NewIBFTServersManager( diff --git a/gorules/rules.go b/gorules/rules.go index 81b2bb52a7..5deaa0270a 100644 --- a/gorules/rules.go +++ b/gorules/rules.go @@ -5,15 +5,31 @@ import ( ) func OsFilePermissionRule(m dsl.Matcher) { - m.Match(`os.$name($file, $number, 0777)`).Report("os.$name called with file mode 0777") - m.Match(`os.$name($file, 0777)`).Report("os.$name called with file mode 0777") - m.Match(`os.$name(0777)`).Report("os.$name called with file mode 0777") - - m.Match(`os.$name($file, $number, os.ModePerm)`).Report("os.$name called with file mode os.ModePerm (0777)") - m.Match(`os.$name($file, os.ModePerm)`).Report("os.$name called with file mode os.ModePerm (0777)") - m.Match(`os.$name(os.ModePerm)`).Report("os.$name called with file mode os.ModePerm (0777)") + m.Match(`os.$name($file, $number, 0777)`, + `os.$name($file, 0777)`, + `os.$name(0777)`, + `os.$name($file, $number, os.ModePerm)`, + `os.$name($file, os.ModePerm)`, + `os.$name(os.ModePerm)`, + ).Report("os.$name called with file mode 0777 (ModePerm)") } func ForbidPanicsRule(m dsl.Matcher) { m.Match(`panic($_)`).Report("panics should not be manually used") } + +func NoUseOfTimeNowWithoutUTCRule(m dsl.Matcher) { + m.Match(`$_ = time.Now()`, + `$_ := time.Now()`, + `$_.$_ = time.Now()`). + Suggest("time.Now().UTC()"). + Report(`use UTC time when calling time.Now`) + + m.Match(`time.Now().$f()`, + `time.Now().$f($_)`, + `time.Now().$f($_, $_)`, + `time.Now().$f($_, $_, $_)`). + Where(!m["f"].Text.Matches("UTC")). + Suggest("time.Now().UTC()"). + Report(`use UTC time when calling time.Now`) +} diff --git a/gorules/testdata/rules_test_data.go b/gorules/testdata/rules_test_data.go index b78a3ec779..0b85725ffd 100644 --- a/gorules/testdata/rules_test_data.go +++ b/gorules/testdata/rules_test_data.go @@ -4,25 +4,59 @@ import ( "errors" "fmt" "os" + "time" ) +type testLintStruct struct { + testTime time.Time +} + func testLintError() { err := errors.New("test") + testStruct := &testLintStruct{} + var timeNow time.Time + + os.Mkdir("test", 0777) // want `OsFilePermissionRule: os.Mkdir called with file mode 0777` + os.Mkdir("test", os.ModePerm) // want `OsFilePermissionRule: os.Mkdir called with file mode 0777` + os.MkdirAll("test", 0777) // want `OsFilePermissionRule: os.MkdirAll called with file mode 0777` + os.MkdirAll("test", os.ModePerm) // want `OsFilePermissionRule: os.MkdirAll called with file mode 0777` + os.Chmod("test", 0777) // want `OsFilePermissionRule: os.Chmod called with file mode 0777` + os.Chmod("test", os.ModePerm) // want `OsFilePermissionRule: os.Chmod called with file mode 0777` + os.OpenFile("test", 0, 0777) // want `OsFilePermissionRule: os.OpenFile called with file mode 0777` + os.OpenFile("test", 0, os.ModePerm) // want `OsFilePermissionRule: os.OpenFile called with file mode 0777` + panic("test") // want `ForbidPanicsRule: panics should not be manually used` + panic(fmt.Sprintf("test error")) // want `ForbidPanicsRule: panics should not be manually used` + panic(err) // want `ForbidPanicsRule: panics should not be manually used` + testStruct.testTime = time.Now() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + timeNow = time.Now() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + v := time.Now() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Unix() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().UnixMicro() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().UnixMilli() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().UnixNano() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Add(time.Hour) // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().AddDate(1, 1, 1) // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().After(time.Date(1, 1, 1, 1, 1, 1, 1, time.Local)) // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().AppendFormat([]byte{0, 1}, "dd.mm.yyyy") // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Before(time.Date(1, 1, 1, 1, 1, 1, 1, time.Local)) // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Clock() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Date() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Day() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Equal(time.Date(1, 1, 1, 1, 1, 1, 1, time.Local)) // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Hour() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Minute() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Hour() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Year() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` + time.Now().Add(time.Hour).Unix() // want `NoUseOfTimeNowWithoutUTCRule: use UTC time when calling time.Now` - os.Mkdir("test", 0777) // want `OsFilePermissionRule: os.Mkdir called with file mode` - os.Mkdir("test", os.ModePerm) // want `OsFilePermissionRule: os.Mkdir called with file mode` - os.MkdirAll("test", 0777) // want `OsFilePermissionRule: os.MkdirAll called with file mode` - os.MkdirAll("test", os.ModePerm) // want `OsFilePermissionRule: os.MkdirAll called with file mode` - os.Chmod("test", 0777) // want `OsFilePermissionRule: os.Chmod called with file mode` - os.Chmod("test", os.ModePerm) // want `OsFilePermissionRule: os.Chmod called with file mode` - os.OpenFile("test", 0, 0777) // want `OsFilePermissionRule: os.OpenFile called with file mode` - os.OpenFile("test", 0, os.ModePerm) // want `OsFilePermissionRule: os.OpenFile called with file mode` - panic("test") // want `ForbidPanicsRule: panics should not be manually used` - panic(fmt.Sprintf("test error")) // want `ForbidPanicsRule: panics should not be manually used` - panic(err) // want `ForbidPanicsRule: panics should not be manually used` + v.Day() + timeNow.Day() } func testNoLintError() { + testStruct := &testLintStruct{} + var timeNow time.Time + os.Mkdir("test", 67108864) os.Mkdir("test", os.ModeDevice) os.MkdirAll("test", 67108864) @@ -31,4 +65,28 @@ func testNoLintError() { os.Chmod("test", os.ModeDevice) os.OpenFile("test", 0, 67108864) os.OpenFile("test", 0, os.ModeDevice) + testStruct.testTime = time.Now().UTC() + timeNow = time.Now().UTC() + v := time.Now().UTC() + time.Now().UTC().Unix() + time.Now().UTC().UnixMicro() + time.Now().UTC().UnixMilli() + time.Now().UTC().UnixNano() + time.Now().UTC().Add(time.Hour) + time.Now().UTC().AddDate(1, 1, 1) + time.Now().UTC().After(time.Date(1, 1, 1, 1, 1, 1, 1, time.Local)) + time.Now().UTC().AppendFormat([]byte{0, 1}, "dd.mm.yyyy") + time.Now().UTC().Before(time.Date(1, 1, 1, 1, 1, 1, 1, time.Local)) + time.Now().UTC().Clock() + time.Now().UTC().Date() + time.Now().UTC().Day() + time.Now().UTC().Equal(time.Date(1, 1, 1, 1, 1, 1, 1, time.Local)) + time.Now().UTC().Hour() + time.Now().UTC().Minute() + time.Now().UTC().Hour() + time.Now().UTC().Year() + time.Now().UTC().Add(time.Hour).Unix() + + v.Day() + timeNow.Day() } diff --git a/jsonrpc/filter_manager.go b/jsonrpc/filter_manager.go index 670bdc02c3..8f463d68ba 100644 --- a/jsonrpc/filter_manager.go +++ b/jsonrpc/filter_manager.go @@ -570,7 +570,7 @@ func (f *FilterManager) refreshFilterTimeout(filter *filterBase) { // addFilterTimeout set timeout and add to heap func (f *FilterManager) addFilterTimeout(filter *filterBase) { - filter.expiresAt = time.Now().Add(f.timeout) + filter.expiresAt = time.Now().UTC().Add(f.timeout) f.timeouts.addFilter(filter) f.emitSignalToUpdateCh() } diff --git a/jsonrpc/filter_manager_test.go b/jsonrpc/filter_manager_test.go index 578a57e810..a8658f0ed7 100644 --- a/jsonrpc/filter_manager_test.go +++ b/jsonrpc/filter_manager_test.go @@ -542,7 +542,7 @@ func TestHeadStream_Concurrent(t *testing.T) { // Write co-routine with jitter go func() { - seed := time.Now().UnixNano() + seed := time.Now().UTC().UnixNano() t.Logf("Using seed %d", seed) z := rand.NewZipf(rand.New(rand.NewSource(seed)), 1.5, 1.5, 50) diff --git a/network/gossip_test.go b/network/gossip_test.go index f48c228235..57dd16c224 100644 --- a/network/gossip_test.go +++ b/network/gossip_test.go @@ -31,7 +31,7 @@ func WaitForSubscribers(ctx context.Context, srv *Server, topic string, expected func TestSimpleGossip(t *testing.T) { numServers := 10 - sentMessage := fmt.Sprintf("%d", time.Now().Unix()) + sentMessage := fmt.Sprintf("%d", time.Now().UTC().Unix()) servers, createErr := createServers(numServers, nil) if createErr != nil { diff --git a/txpool/event_subscription_test.go b/txpool/event_subscription_test.go index ac52a9aeb0..4a0abb8389 100644 --- a/txpool/event_subscription_test.go +++ b/txpool/event_subscription_test.go @@ -70,7 +70,7 @@ func shuffleTxPoolEvents( } // Shuffle the events - mathRand.Seed(time.Now().UnixNano()) + mathRand.Seed(time.Now().UTC().UnixNano()) mathRand.Shuffle(len(events), func(i, j int) { events[i], events[j] = events[j], events[i] })