@@ -227,8 +227,42 @@ func (s *SystemContract) VerifyUncles(chain consensus.ChainReader, block *types.
227227 return nil
228228}
229229
230+ // CalcBlocksPerSecond returns the number of blocks per second
231+ // Uses the BlocksPerSecond configuration parameter directly
232+ // Default is 1 block per second if not specified
233+ func CalcBlocksPerSecond (blocksPerSecond uint64 ) uint64 {
234+ if blocksPerSecond == 0 {
235+ return 1 // Default to 1 block per second
236+ }
237+ return blocksPerSecond
238+ }
239+
240+ // CalcPeriodMs calculates the period in milliseconds between blocks
241+ // based on the blocks per second configuration
242+ func CalcPeriodMs (blocksPerSecond uint64 ) uint64 {
243+ bps := CalcBlocksPerSecond (blocksPerSecond )
244+ return 1000 / bps
245+ }
246+
230247func (s * SystemContract ) CalcTimestamp (parent * types.Header ) uint64 {
231- timestamp := parent .Time + s .config .Period
248+ var timestamp uint64
249+ if s .config .Period == 1 {
250+ // Get the base timestamp (in seconds)
251+ timestamp = parent .Time
252+
253+ blocksPerSecond := CalcBlocksPerSecond (s .config .BlocksPerSecond )
254+
255+ // Calculate the block index within the current period for the next block
256+ blockIndex := parent .Number .Uint64 () % blocksPerSecond
257+
258+ // If this block is the last one in the current second, increment the timestamp
259+ // We compare with blocksPerSecond-1 because blockIndex is 0-based
260+ if blockIndex == blocksPerSecond - 1 {
261+ timestamp ++
262+ }
263+ } else {
264+ timestamp = parent .Time + s .config .Period
265+ }
232266
233267 // If RelaxedPeriod is enabled, always set the header timestamp to now (ie the time we start building it) as
234268 // we don't know when it will be sealed
0 commit comments