Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit a774dc9

Browse files
authored
Fix timestamp when querying zkevm_getBatchByNumer and the batch is only in trusted state (#3750)
* fix timestamp when querying zkevm_getBatchByNumer and the batch is only in trusted state * control timestamp to be returned for a batch in GetBatchTimestamp * fix ErrNotFound * fix state.ErrNotFound check * fix comments * fix GetRawBatchTimestamps * fix comments * fix GetRawBatchTimestamps return err
1 parent 45a7413 commit a774dc9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

state/batch.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,13 @@ func (s *State) GetLastBatch(ctx context.Context, dbTx pgx.Tx) (*Batch, error) {
568568
return batches[0], nil
569569
}
570570

571-
// GetBatchTimestamp returns the batch timestamp.
571+
// GetBatchTimestamp returns the batch timestamp
572+
// If batch >= etrog
572573
//
573-
// for >= etrog is stored on virtual_batch.batch_timestamp
574-
// previous batches is stored on batch.timestamp
574+
// if the batch it's virtualized it will return virtual_batch.timestamp_batch_etrog field value
575+
// if the batch if's only trusted and it has L2 blocks it will return the timestamp of the last L2 block, otherwise it will return batchTimestamp
576+
//
577+
// If batch < etrog it will return batchTimestamp value
575578
func (s *State) GetBatchTimestamp(ctx context.Context, batchNumber uint64, forcedForkId *uint64, dbTx pgx.Tx) (*time.Time, error) {
576579
var forkid uint64
577580
if forcedForkId != nil {
@@ -584,6 +587,20 @@ func (s *State) GetBatchTimestamp(ctx context.Context, batchNumber uint64, force
584587
return nil, err
585588
}
586589
if forkid >= FORKID_ETROG {
590+
if virtualTimestamp == nil {
591+
lastL2Block, err := s.GetLastL2BlockByBatchNumber(ctx, batchNumber, dbTx)
592+
if err != nil && !errors.Is(err, ErrNotFound) {
593+
return nil, err
594+
}
595+
596+
// If the batch has L2 blocks we will return the timestamp of the last L2 block as the timestamp of the batch
597+
// else we will return the batchTimestamp value (timestamp of batch creation)
598+
if lastL2Block != nil {
599+
return &lastL2Block.ReceivedAt, nil
600+
}
601+
602+
return batchTimestamp, nil
603+
}
587604
return virtualTimestamp, nil
588605
}
589606
return batchTimestamp, nil

state/pgstatestorage/batch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ func (p *PostgresStorage) BuildChangeL2Block(deltaTimestamp uint32, l1InfoTreeIn
995995
}
996996

997997
// GetRawBatchTimestamps returns the timestamp of the batch with the given number.
998-
// it returns batch_num.tstamp and virtual_batch.batch_timestamp
998+
// it returns batch.timestamp and virtual_batch.timestamp_batch_etrog
999999
func (p *PostgresStorage) GetRawBatchTimestamps(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*time.Time, *time.Time, error) {
10001000
const sql = `
10011001
SELECT b.timestamp AS batch_timestamp, v.timestamp_batch_etrog AS virtual_batch_timestamp

0 commit comments

Comments
 (0)