Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct timestamps in zkevm node to avoid decreasing #81

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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