Skip to content

Commit

Permalink
fix GetRawBatchTimestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
agnusmor committed Aug 6, 2024
1 parent 8ceeda2 commit 6588af8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
16 changes: 13 additions & 3 deletions state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,9 @@ func (s *State) GetLastBatch(ctx context.Context, dbTx pgx.Tx) (*Batch, error) {
}

// GetBatchTimestamp returns the batch timestamp
// If batch >= etrog and it's virtualized it will return virtual_batch.timestamp field value, otherwise it will return batchTimestamp (trusted state)
// If batch >= etrog and it's virtualized it will return virtual_batch.timestamp_batch_etrog field value, otherwise it will return batchTimestamp (trusted state)
// If batch < etrog it will return batchTimestamp value
// The function GetRawBatchTimestamps will return as batchTimestamp the timestamp of the last L2 block of the batch,
// if the batch doesn't have blocks it will return batch.timestamp field value
// The function GetRawBatchTimestamps will return as batchTimestamp the timestamp of the last L2 block of the batch
func (s *State) GetBatchTimestamp(ctx context.Context, batchNumber uint64, forcedForkId *uint64, dbTx pgx.Tx) (*time.Time, error) {
var forkid uint64
if forcedForkId != nil {
Expand All @@ -586,6 +585,17 @@ func (s *State) GetBatchTimestamp(ctx context.Context, batchNumber uint64, force
}
if forkid >= FORKID_ETROG {
if virtualTimestamp == nil {
lastL2Block, err := s.GetLastL2BlockByBatchNumber(ctx, batchNumber, dbTx)
if err != nil && !errors.Is(err, ErrNotFound) {
return nil, err
}

// If the batch has L2 blocks we will return the timestamp of the last L2 block as the timestamp of the batch
// else we will return the batchTimestamp value (timestamp of batch creation)
if lastL2Block != nil {
return &lastL2Block.ReceivedAt, nil
}

return batchTimestamp, nil
}
return virtualTimestamp, nil
Expand Down
14 changes: 0 additions & 14 deletions state/pgstatestorage/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,21 +1008,7 @@ func (p *PostgresStorage) GetRawBatchTimestamps(ctx context.Context, batchNumber
err := e.QueryRow(ctx, sql, batchNumber).Scan(&batchTimestamp, &virtualBatchTimestamp)
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil, nil
} else if err != nil {
return nil, nil, err
}

lastL2Block, err := p.GetLastL2BlockByBatchNumber(ctx, batchNumber, dbTx)
if err != nil && !errors.Is(err, state.ErrNotFound) {
return nil, nil, err
}

// If the batch has L2 blocks we will return the timestamp of the last L2 block as the timestamp of the batch
// else we will return the batch.timestamp value (timestamp of batch creation)
if lastL2Block != nil {
batchTimestamp = &lastL2Block.ReceivedAt
}

return batchTimestamp, virtualBatchTimestamp, nil
}

Expand Down

0 comments on commit 6588af8

Please sign in to comment.