Skip to content

Commit 3385ef1

Browse files
committed
refactor GetNextTimestamp and add test for same sec different ms
1 parent 6d05981 commit 3385ef1

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

plugin/evm/customheader/time.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,23 @@ func GetNextTimestamp(parent *types.Header, config *extras.ChainConfig, now time
3535
timestamp = uint64(now.Unix())
3636
timestampMS = uint64(now.UnixMilli())
3737
)
38-
// Note: in order to support asynchronous block production, blocks are allowed to have
39-
// the same timestamp as their parent. This allows more than one block to be produced
40-
// per second.
41-
parentExtra := customtypes.GetHeaderExtra(parent)
42-
if parent.Time >= timestamp || (parentExtra.TimeMilliseconds != nil && *parentExtra.TimeMilliseconds >= timestampMS) {
43-
// In pre-Granite, blocks are allowed to have the same timestamp as their parent.
44-
// In Granite, there is a minimum delay enforced, and if the timestamp is the same as the parent,
45-
// the block will be rejected.
46-
// The block builder should have already waited enough time to meet the minimum delay.
47-
// This is to early-exit from the block building.
48-
if config.IsGranite(timestamp) {
38+
if parent.Time < timestamp {
39+
return timestamp, timestampMS, nil
40+
}
41+
// In Granite, there is a minimum delay enforced, and if the timestamp is the same as the parent,
42+
// the block will be rejected.
43+
// The block builder should have already waited enough time to meet the minimum delay.
44+
// This is to early-exit from the block building.
45+
if config.IsGranite(timestamp) {
46+
if uint64(customtypes.BlockTime(parent).UnixMilli()) >= timestampMS {
4947
return 0, 0, ErrGraniteClockBehindParent
5048
}
51-
// Add the delay to the parent timestamp with seconds precision.
52-
timestamp = parent.Time
53-
// Actually we don't need to set timestampMS, because it will be not be set if this is not
54-
// Granite, but setting here for consistency.
55-
timestampMS = parent.Time * 1000
49+
return timestamp, timestampMS, nil
5650
}
57-
58-
return timestamp, timestampMS, nil
51+
// In pre-Granite, blocks are allowed to have the same timestamp as their parent
52+
// Actually we don't need to return modified timestampMS, because it will be not be set if this is not
53+
// Granite, but setting here for consistency.
54+
return parent.Time, parent.Time * 1000, nil
5955
}
6056

6157
// VerifyTime verifies that the header's Time and TimeMilliseconds fields are

plugin/evm/customheader/time_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ func TestGetNextTimestamp(t *testing.T) {
225225
now: now,
226226
expectedErr: ErrGraniteClockBehindParent,
227227
},
228+
{
229+
name: "current_timesec_equals_parent_time_with_different_milliseconds_granite",
230+
parent: generateHeader(nowSeconds, utils.NewUint64(nowMillis-1000)),
231+
extraConfig: extras.TestGraniteChainConfig,
232+
now: now,
233+
expectedErr: nil,
234+
expectedSec: nowSeconds,
235+
expectedMillis: nowMillis,
236+
},
228237
}
229238

230239
for _, test := range tests {

0 commit comments

Comments
 (0)