Skip to content

Commit 4fde65a

Browse files
authored
fix: fix genesis volta related helper function (#295)
1 parent 9e00f11 commit 4fde65a

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

op-node/rollup/types.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,12 @@ func (c *Config) IsVolta(timestamp uint64) bool {
188188
return c.VoltaTime != nil && timestamp >= *c.VoltaTime
189189
}
190190

191-
func (c *Config) VoltaBlockNumber() uint64 {
192-
if c.VoltaTime == nil || *c.VoltaTime == 0 {
193-
return 0
191+
// VoltaBlockNumber return volta block number, return -1 when volta time is not set or < gensis L2 time.
192+
func (c *Config) VoltaBlockNumber() int64 {
193+
if c.VoltaTime == nil || *c.VoltaTime < c.Genesis.L2Time {
194+
return -1
194195
}
195-
return (*c.VoltaTime-c.Genesis.L2Time)/c.BlockTime + c.Genesis.L2.Number
196+
return int64((*c.VoltaTime-c.Genesis.L2Time)/c.BlockTime + c.Genesis.L2.Number)
196197
}
197198

198199
func (c *Config) IsVoltaActivationBlock(l2BlockMillisecondTime uint64) bool {
@@ -240,16 +241,20 @@ func (cfg *Config) ValidateL2Config(ctx context.Context, client L2Client, skipL2
240241

241242
func (cfg *Config) MillisecondTimestampForBlock(blockNumber uint64) uint64 {
242243
voltaBlockNumber := cfg.VoltaBlockNumber()
243-
if voltaBlockNumber == 0 || blockNumber <= voltaBlockNumber {
244+
if voltaBlockNumber < 0 { // not active volta hardfork
245+
return cfg.Genesis.L2Time*1000 + (blockNumber-cfg.Genesis.L2.Number)*cfg.BlockTime*1000
246+
} else if voltaBlockNumber == 0 { // active volta hardfork in genesis
247+
return *cfg.VoltaTime*1000 + blockNumber*MillisecondBlockIntervalVolta
248+
} else if blockNumber <= uint64(voltaBlockNumber) { // block number before volta hardfork
244249
return cfg.Genesis.L2Time*1000 + (blockNumber-cfg.Genesis.L2.Number)*cfg.BlockTime*1000
245250
} else {
246-
return *cfg.VoltaTime*1000 + (blockNumber-voltaBlockNumber)*MillisecondBlockIntervalVolta
251+
return *cfg.VoltaTime*1000 + (blockNumber-uint64(voltaBlockNumber))*MillisecondBlockIntervalVolta
247252
}
248253
}
249254

250255
func (cfg *Config) TargetBlockNumber(milliTimestamp uint64) (num uint64, err error) {
251256
voltaBlockNumber := cfg.VoltaBlockNumber()
252-
if voltaBlockNumber == 0 || milliTimestamp <= *cfg.VoltaTime*1000 {
257+
if voltaBlockNumber < 0 || milliTimestamp < *cfg.VoltaTime*1000 {
253258
// subtract genesis time from timestamp to get the time elapsed since genesis, and then divide that
254259
// difference by the block time to get the expected L2 block number at the current time. If the
255260
// unsafe head does not have this block number, then there is a gap in the queue.
@@ -265,7 +270,7 @@ func (cfg *Config) TargetBlockNumber(milliTimestamp uint64) (num uint64, err err
265270
voltaMilliTimestamp := *cfg.VoltaTime * 1000
266271
wallClockGenesisDiff := milliTimestamp - voltaMilliTimestamp
267272
blocksSinceVolta := wallClockGenesisDiff / MillisecondBlockIntervalVolta
268-
return voltaBlockNumber + blocksSinceVolta, nil
273+
return uint64(voltaBlockNumber) + blocksSinceVolta, nil
269274
}
270275
}
271276

0 commit comments

Comments
 (0)