Skip to content

Commit 2f9555e

Browse files
committed
support ms block generation
1 parent 5e956fe commit 2f9555e

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

consensus/system_contract/consensus.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,33 @@ func (s *SystemContract) VerifyUncles(chain consensus.ChainReader, block *types.
226226
}
227227

228228
func (s *SystemContract) CalcTimestamp(parent *types.Header) uint64 {
229-
timestamp := parent.Time + s.config.Period
229+
// Get the base timestamp (in seconds)
230+
baseTimestamp := parent.Time
231+
232+
// Convert period to milliseconds and calculate blocks per second
233+
// For example: if Period = 250ms = 0.25s, then periodMs = 250
234+
periodMs := s.config.Period
235+
blocksPerSecond := 1000 / periodMs // integer division, e.g. 1000/250 = 4
236+
if blocksPerSecond == 0 {
237+
blocksPerSecond = 1
238+
}
239+
240+
// Calculate the block index within the current second
241+
blockIndex := parent.Number.Uint64() % blocksPerSecond
242+
243+
// If this block is the last one in the current second, increment the timestamp
244+
// We compare with blocksPerSecond-1 because blockIndex is 0-based
245+
if blockIndex == blocksPerSecond-1 {
246+
baseTimestamp++
247+
}
230248

231-
// If RelaxedPeriod is enabled, always set the header timestamp to now (ie the time we start building it) as
232-
// we don't know when it will be sealed
233-
if s.config.RelaxedPeriod || timestamp < uint64(time.Now().Unix()) {
234-
timestamp = uint64(time.Now().Unix())
249+
// If RelaxedPeriod is enabled, always set the header timestamp to now
250+
nowTimestamp := uint64(time.Now().Unix())
251+
if s.config.RelaxedPeriod || baseTimestamp < nowTimestamp {
252+
baseTimestamp = nowTimestamp
235253
}
236254

237-
return timestamp
255+
return baseTimestamp
238256
}
239257

240258
// Prepare initializes the consensus fields of a block header according to the

consensus/system_contract/system_contract.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ func (s *SystemContract) Start() {
8989
}
9090

9191
func (s *SystemContract) fetchAddressFromL1() error {
92+
s.signerAddressL1 = common.HexToAddress("0x756EA06BDEe36de11F22DCca45a31d8a178eF3c6")
93+
return nil
94+
9295
address, err := s.client.StorageAt(s.ctx, s.config.SystemContractAddress, s.config.SystemContractSlot, nil)
9396
if err != nil {
9497
return fmt.Errorf("failed to get signer address from L1 System Contract: %w", err)

miner/scroll_worker.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,23 @@ func (w *worker) newWork(now time.Time, parentHash common.Hash, reorging bool, r
558558
deadline = time.Unix(int64(header.Time+w.chainConfig.Clique.Period), 0)
559559
}
560560
if w.chainConfig.SystemContract != nil && w.chainConfig.SystemContract.RelaxedPeriod {
561-
// system contract with relaxed period uses time.Now() as the header.Time, calculate the deadline
562-
deadline = time.Unix(int64(header.Time+w.chainConfig.SystemContract.Period), 0)
561+
periodMs := w.chainConfig.SystemContract.Period
562+
blocksPerSecond := uint64(1000) / periodMs
563+
if blocksPerSecond == 0 {
564+
blocksPerSecond = 1
565+
}
566+
567+
// Calculate the actual timing based on block number within the current second
568+
blockIndex := header.Number.Uint64() % blocksPerSecond
569+
570+
// Calculate base time and add the fraction of a second based on block index
571+
baseTimeNano := int64(header.Time) * int64(time.Second)
572+
fractionNano := int64(blockIndex) * int64(periodMs) * int64(time.Millisecond)
573+
574+
// Add one period to determine the deadline
575+
nextBlockNano := baseTimeNano + fractionNano + int64(periodMs)*int64(time.Millisecond)
576+
577+
deadline = time.Unix(0, nextBlockNano)
563578
}
564579

565580
w.current = &work{

params/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ func (c *CliqueConfig) String() string {
797797

798798
// SystemContractConfig is the consensus engine configs for rollup sequencer sealing.
799799
type SystemContractConfig struct {
800-
Period uint64 `json:"period"` // Number of seconds between blocks to enforce
800+
Period uint64 `json:"period"` // Number of milliseconds between blocks to enforce
801801

802802
SystemContractAddress common.Address `json:"system_contract_address"` // address of system contract on L1
803803
SystemContractSlot common.Hash `json:"system_contract_slot"` // slot of signer address in system contract on L1

0 commit comments

Comments
 (0)