Skip to content

Commit

Permalink
Merge pull request #81 from EspressoSystems/fix/timestamps
Browse files Browse the repository at this point in the history
Correct timestamps in zkevm node to avoid decreasing
  • Loading branch information
sveitser authored Sep 29, 2023
2 parents adb1e47 + 0825ca6 commit 57bd549
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions synchronizer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package synchronizer
import (
"context"
"math/big"
"time"

"github.com/0xPolygonHermez/zkevm-node/etherman"
"github.com/0xPolygonHermez/zkevm-node/state"
Expand Down Expand Up @@ -32,6 +33,7 @@ type stateInterface interface {
Reset(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) error
GetPreviousBlock(ctx context.Context, offset uint64, dbTx pgx.Tx) (*state.Block, error)
GetLastBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLastBatchTime(ctx context.Context, dbTx pgx.Tx) (time.Time, error)
GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
ResetTrustedState(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error
AddVirtualBatch(ctx context.Context, virtualBatch *state.VirtualBatch, dbTx pgx.Tx) error
Expand Down
22 changes: 22 additions & 0 deletions synchronizer/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,12 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
log.Warn("Empty sequencedBatches array detected, ignoring...")
return nil
}

prevTimestamp, err := s.state.GetLastBatchTime(s.ctx, dbTx)
if err != nil {
log.Warn("Error fetching previous timestamp")
return err
}
for _, sbatch := range sequencedBatches {
virtualBatch := state.VirtualBatch{
BatchNumber: sbatch.BatchNumber,
Expand All @@ -620,6 +626,16 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
BatchL2Data: sbatch.Transactions,
}

// This should not be necessary, since HotShot should enforce non-decreasing timestamps.
// However, since HotShot does not currently support the ValidatedState API, timestamps
// proposed by leaders are not checked by replicas, and may occasionally decrease. In this
// case, just use the previous timestamp, to avoid breaking the rest of the execution
// pipeline.
if batch.Timestamp.Before(prevTimestamp) {
batch.Timestamp = prevTimestamp
}
prevTimestamp = batch.Timestamp

// Forced batches no longer supported, don't need to be handled

// Now we need to check the batch. ForcedBatches should be already stored in the batch table because this is done by the sequencer
Expand Down Expand Up @@ -703,7 +719,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
FromBatchNumber: sequencedBatches[0].BatchNumber,
ToBatchNumber: sequencedBatches[len(sequencedBatches)-1].BatchNumber,
}
err := s.state.AddSequence(s.ctx, seq, dbTx)
err = s.state.AddSequence(s.ctx, seq, dbTx)
if err != nil {
log.Errorf("error adding sequence. Sequence: %+v", seq)
rollbackErr := dbTx.Rollback(s.ctx)
Expand Down

0 comments on commit 57bd549

Please sign in to comment.