Skip to content

Commit 9f43c21

Browse files
committed
reviews
1 parent 80467b2 commit 9f43c21

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

miner/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
149149
chainExtra = params.GetExtra(w.chainConfig)
150150
)
151151

152-
timestamp, timestampMS, err := customheader.GetNextTimestamp(parent, chainExtra, tstart)
152+
timestamp, timestampMS, err := customheader.GetNextTimestamp(chainExtra, parent, tstart)
153153
if err != nil {
154154
return nil, fmt.Errorf("failed to get next timestamp: %w", err)
155155
}

plugin/evm/customheader/min_delay_excess.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func VerifyMinDelayExcess(
7777
}
7878
default:
7979
// Prior to Granite there was no expected min delay excess.
80-
// TODO (ceyonur): this can be removed after Granite is activated.
80+
// TODO Cleanup (ceyonur): this can be removed after Granite is activated.
8181
if customtypes.GetHeaderExtra(header).MinDelayExcess != nil {
8282
return fmt.Errorf("%w: %s", errRemoteMinDelayExcessSet, header.Hash())
8383
}

plugin/evm/customheader/time.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,28 @@ var (
3030

3131
// GetNextTimestamp calculates the timestamp (in seconds and milliseconds) for the header based on the parent's timestamp and the current time.
3232
// First return value is the timestamp in seconds, second return value is the timestamp in milliseconds.
33-
func GetNextTimestamp(parent *types.Header, config *extras.ChainConfig, now time.Time) (uint64, uint64, error) {
33+
func GetNextTimestamp(config *extras.ChainConfig, parent *types.Header, now time.Time) (uint64, uint64, error) {
3434
var (
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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,23 @@ func TestGetNextTimestamp(t *testing.T) {
359359
now: now,
360360
expectedErr: ErrGraniteClockBehindParent,
361361
},
362+
{
363+
name: "current_timesec_equals_parent_time_with_different_milliseconds_granite",
364+
parent: generateHeader(nowSeconds, utils.NewUint64(nowMillis-1000)),
365+
extraConfig: extras.TestGraniteChainConfig,
366+
now: now,
367+
expectedErr: nil,
368+
expectedSec: nowSeconds,
369+
expectedMillis: nowMillis,
370+
},
362371
}
363372

364373
for _, test := range tests {
365374
t.Run(test.name, func(t *testing.T) {
366375
if test.extraConfig == nil {
367376
test.extraConfig = extras.TestChainConfig
368377
}
369-
sec, millis, err := GetNextTimestamp(test.parent, test.extraConfig, test.now)
378+
sec, millis, err := GetNextTimestamp(test.extraConfig, test.parent, test.now)
370379
require.ErrorIs(t, err, test.expectedErr)
371380
require.Equal(t, test.expectedSec, sec)
372381
require.Equal(t, test.expectedMillis, millis)

0 commit comments

Comments
 (0)