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

Commit e3cd415

Browse files
authored
fix #3613 timestamp needs to be greater or equal (#3614)
syncrhonizer update the tstamp from table state.batch when the batch is sequenced
1 parent 8d5cf96 commit e3cd415

File tree

8 files changed

+132
-2
lines changed

8 files changed

+132
-2
lines changed

state/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,5 @@ type storage interface {
163163
GetNotCheckedBatches(ctx context.Context, dbTx pgx.Tx) ([]*Batch, error)
164164
GetLastL2BlockByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*L2Block, error)
165165
GetPreviousBlockToBlockNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*Block, error)
166+
UpdateBatchTimestamp(ctx context.Context, batchNumber uint64, timestamp time.Time, dbTx pgx.Tx) error
166167
}

state/mocks/mock_storage.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

state/pgstatestorage/batch.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,3 +1092,11 @@ func (p *PostgresStorage) GetNotCheckedBatches(ctx context.Context, dbTx pgx.Tx)
10921092

10931093
return batches, nil
10941094
}
1095+
1096+
// UpdateBatchTimestamp updates the timestamp of the state.batch with the given number.
1097+
func (p *PostgresStorage) UpdateBatchTimestamp(ctx context.Context, batchNumber uint64, timestamp time.Time, dbTx pgx.Tx) error {
1098+
const sql = "UPDATE state.batch SET timestamp = $1 WHERE batch_num = $2"
1099+
e := p.getExecQuerier(dbTx)
1100+
_, err := e.Exec(ctx, sql, timestamp.UTC(), batchNumber)
1101+
return err
1102+
}

synchronizer/actions/etrog/processor_l1_sequence_batches.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type stateProcessSequenceBatches interface {
3232
AddVirtualBatch(ctx context.Context, virtualBatch *state.VirtualBatch, dbTx pgx.Tx) error
3333
AddTrustedReorg(ctx context.Context, trustedReorg *state.TrustedReorg, dbTx pgx.Tx) error
3434
GetL1InfoTreeDataFromBatchL2Data(ctx context.Context, batchL2Data []byte, dbTx pgx.Tx) (map[uint32]state.L1DataV2, common.Hash, common.Hash, error)
35+
UpdateBatchTimestamp(ctx context.Context, batchNumber uint64, timestamp time.Time, dbTx pgx.Tx) error
3536
}
3637

3738
type syncProcessSequenceBatchesInterface interface {
@@ -158,7 +159,7 @@ func (p *ProcessorL1SequenceBatchesEtrog) ProcessSequenceBatches(ctx context.Con
158159
SkipVerifyL1InfoRoot: 1,
159160
ClosingReason: state.SyncL1EventSequencedForcedBatchClosingReason,
160161
}
161-
} else if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber == 1 {
162+
} else if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber == 1 { // This is the initial batch (injected)
162163
log.Debug("Processing initial batch")
163164
batch.GlobalExitRoot = sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot
164165
var fBHL1 common.Hash = sbatch.PolygonRollupBaseEtrogBatchData.ForcedBlockHashL1
@@ -251,6 +252,17 @@ func (p *ProcessorL1SequenceBatchesEtrog) ProcessSequenceBatches(ctx context.Con
251252
return err
252253
}
253254
} else {
255+
// Batch already exists
256+
// We update the timestamp of the batch to match the timestamp
257+
err := p.state.UpdateBatchTimestamp(ctx, batch.BatchNumber, *processCtx.Timestamp, dbTx)
258+
if err != nil {
259+
log.Errorf("error updating batch timestamp %s. BatchNumber: %d, BlockNumber: %d, error: %v", processCtx.Timestamp, batch.BatchNumber, blockNumber, err)
260+
rollbackErr := dbTx.Rollback(ctx)
261+
if rollbackErr != nil {
262+
log.Errorf("error rolling back state because error updating batch timestamp. BatchNumber: %d, BlockNumber: %d, rollbackErr: %s, error : %v", batch.BatchNumber, blockNumber, rollbackErr.Error(), err)
263+
return rollbackErr
264+
}
265+
}
254266
// Reprocess batch to compare the stateRoot with tBatch.StateRoot and get accInputHash
255267
batchRespose, err := p.state.ExecuteBatchV2(ctx, batch, processCtx.L1InfoRoot, leaves, *processCtx.Timestamp, false, processCtx.SkipVerifyL1InfoRoot, processCtx.ForcedBlockHashL1, dbTx)
256268
if err != nil {

synchronizer/actions/etrog/processor_l1_sequence_batches_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ func TestL1SequenceBatchesTrustedBatchSequencedThatAlreadyExistsHappyPath(t *tes
101101
expectationsPreExecution(t, mocks, ctx, batch, nil)
102102
executionResponse := newProcessBatchResponseV2(batch)
103103
expectationsForExecution(t, mocks, ctx, l1Block.SequencedBatches[1][0], l1Block.ReceivedAt, executionResponse)
104+
mocks.State.EXPECT().UpdateBatchTimestamp(ctx, batch.BatchNumber, l1Block.ReceivedAt, mocks.DbTx).Return(nil)
104105
mocks.State.EXPECT().AddAccumulatedInputHash(ctx, executionResponse.NewBatchNum, common.BytesToHash(executionResponse.NewAccInputHash), mocks.DbTx).Return(nil)
105106
expectationsAddSequencedBatch(t, mocks, ctx, executionResponse)
107+
106108
err := sut.Process(ctx, etherman.Order{Pos: 1}, l1Block, mocks.DbTx)
109+
107110
require.NoError(t, err)
108111
}
109112

@@ -117,9 +120,12 @@ func TestL1SequenceBatchesPermissionlessBatchSequencedThatAlreadyExistsHappyPath
117120
expectationsPreExecution(t, mocks, ctx, batch, nil)
118121
executionResponse := newProcessBatchResponseV2(batch)
119122
expectationsForExecution(t, mocks, ctx, l1Block.SequencedBatches[1][0], l1Block.ReceivedAt, executionResponse)
123+
mocks.State.EXPECT().UpdateBatchTimestamp(ctx, batch.BatchNumber, l1Block.ReceivedAt, mocks.DbTx).Return(nil)
120124
mocks.State.EXPECT().AddAccumulatedInputHash(ctx, executionResponse.NewBatchNum, common.BytesToHash(executionResponse.NewAccInputHash), mocks.DbTx).Return(nil)
121125
expectationsAddSequencedBatch(t, mocks, ctx, executionResponse)
126+
122127
err := sut.Process(ctx, etherman.Order{Pos: 1}, l1Block, mocks.DbTx)
128+
123129
require.NoError(t, err)
124130
}
125131

@@ -139,6 +145,7 @@ func TestL1SequenceBatchesPermissionlessBatchSequencedThatAlreadyExistsMismatch(
139145
executionResponse := newProcessBatchResponseV2(batch)
140146
executionResponse.NewStateRoot = common.HexToHash(hashExamplesValues[2]).Bytes()
141147
expectationsForExecution(t, mocks, ctx, l1Block.SequencedBatches[1][0], l1Block.ReceivedAt, executionResponse)
148+
mocks.State.EXPECT().UpdateBatchTimestamp(ctx, batch.BatchNumber, l1Block.ReceivedAt, mocks.DbTx).Return(nil)
142149
mocks.State.EXPECT().AddAccumulatedInputHash(ctx, executionResponse.NewBatchNum, common.BytesToHash(executionResponse.NewAccInputHash), mocks.DbTx).Return(nil)
143150
mocks.Synchronizer.EXPECT().IsTrustedSequencer().Return(false)
144151
mocks.State.EXPECT().AddTrustedReorg(ctx, mock.Anything, mocks.DbTx).Return(nil)
@@ -177,6 +184,7 @@ func TestL1SequenceBatchesTrustedBatchSequencedThatAlreadyExistsMismatch(t *test
177184
executionResponse := newProcessBatchResponseV2(batch)
178185
executionResponse.NewStateRoot = common.HexToHash(hashExamplesValues[2]).Bytes()
179186
expectationsForExecution(t, mocks, ctx, l1Block.SequencedBatches[1][0], l1Block.ReceivedAt, executionResponse)
187+
mocks.State.EXPECT().UpdateBatchTimestamp(ctx, batch.BatchNumber, l1Block.ReceivedAt, mocks.DbTx).Return(nil)
180188
mocks.State.EXPECT().AddAccumulatedInputHash(ctx, executionResponse.NewBatchNum, common.BytesToHash(executionResponse.NewAccInputHash), mocks.DbTx).Return(nil)
181189
mocks.Synchronizer.EXPECT().IsTrustedSequencer().Return(true)
182190

@@ -295,7 +303,7 @@ func newL1Block(mocks *mocksEtrogProcessorL1, batch *state.Batch, l1InfoRoot com
295303
func newComposedL1Block(mocks *mocksEtrogProcessorL1, forcedBatch *etherman.SequencedBatch, l1InfoRoot common.Hash) *etherman.Block {
296304
l1Block := etherman.Block{
297305
BlockNumber: 123,
298-
ReceivedAt: mocks.TimeProvider.Now(),
306+
ReceivedAt: time.Date(2024, 1, 1, 1, 0, 0, 0, time.UTC),
299307
SequencedBatches: [][]etherman.SequencedBatch{},
300308
}
301309
l1Block.SequencedBatches = append(l1Block.SequencedBatches, []etherman.SequencedBatch{})

synchronizer/common/syncinterfaces/mocks/state_full_interface.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

synchronizer/common/syncinterfaces/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ type StateFullInterface interface {
7878
GetL2BlockByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*state.L2Block, error)
7979
GetUncheckedBlocks(ctx context.Context, fromBlockNumber uint64, toBlockNumber uint64, dbTx pgx.Tx) ([]*state.Block, error)
8080
GetPreviousBlockToBlockNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*state.Block, error)
81+
UpdateBatchTimestamp(ctx context.Context, batchNumber uint64, timestamp time.Time, dbTx pgx.Tx) error
8182
}

synchronizer/synchronizer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ func TestForcedBatchEtrog(t *testing.T) {
368368
Return(nil).
369369
Once()
370370

371+
m.State.EXPECT().UpdateBatchTimestamp(ctx, sequencedBatch.BatchNumber, fb[0].ForcedAt, m.DbTx).Return(nil)
372+
371373
m.State.
372374
On("AddAccumulatedInputHash", ctx, sequencedBatch.BatchNumber, common.Hash{}, m.DbTx).
373375
Return(nil).

0 commit comments

Comments
 (0)