Skip to content

Commit

Permalink
fix #3613 timestamp needs to be greater or equal (#3614)
Browse files Browse the repository at this point in the history
syncrhonizer update the tstamp from table state.batch when the batch is sequenced
  • Loading branch information
joanestebanr authored May 3, 2024
1 parent 8d5cf96 commit e3cd415
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,5 @@ type storage interface {
GetNotCheckedBatches(ctx context.Context, dbTx pgx.Tx) ([]*Batch, error)
GetLastL2BlockByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*L2Block, error)
GetPreviousBlockToBlockNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*Block, error)
UpdateBatchTimestamp(ctx context.Context, batchNumber uint64, timestamp time.Time, dbTx pgx.Tx) error
}
49 changes: 49 additions & 0 deletions state/mocks/mock_storage.go

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

8 changes: 8 additions & 0 deletions state/pgstatestorage/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,11 @@ func (p *PostgresStorage) GetNotCheckedBatches(ctx context.Context, dbTx pgx.Tx)

return batches, nil
}

// UpdateBatchTimestamp updates the timestamp of the state.batch with the given number.
func (p *PostgresStorage) UpdateBatchTimestamp(ctx context.Context, batchNumber uint64, timestamp time.Time, dbTx pgx.Tx) error {
const sql = "UPDATE state.batch SET timestamp = $1 WHERE batch_num = $2"
e := p.getExecQuerier(dbTx)
_, err := e.Exec(ctx, sql, timestamp.UTC(), batchNumber)
return err
}
14 changes: 13 additions & 1 deletion synchronizer/actions/etrog/processor_l1_sequence_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type stateProcessSequenceBatches interface {
AddVirtualBatch(ctx context.Context, virtualBatch *state.VirtualBatch, dbTx pgx.Tx) error
AddTrustedReorg(ctx context.Context, trustedReorg *state.TrustedReorg, dbTx pgx.Tx) error
GetL1InfoTreeDataFromBatchL2Data(ctx context.Context, batchL2Data []byte, dbTx pgx.Tx) (map[uint32]state.L1DataV2, common.Hash, common.Hash, error)
UpdateBatchTimestamp(ctx context.Context, batchNumber uint64, timestamp time.Time, dbTx pgx.Tx) error
}

type syncProcessSequenceBatchesInterface interface {
Expand Down Expand Up @@ -158,7 +159,7 @@ func (p *ProcessorL1SequenceBatchesEtrog) ProcessSequenceBatches(ctx context.Con
SkipVerifyL1InfoRoot: 1,
ClosingReason: state.SyncL1EventSequencedForcedBatchClosingReason,
}
} else if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber == 1 {
} else if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber == 1 { // This is the initial batch (injected)
log.Debug("Processing initial batch")
batch.GlobalExitRoot = sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot
var fBHL1 common.Hash = sbatch.PolygonRollupBaseEtrogBatchData.ForcedBlockHashL1
Expand Down Expand Up @@ -251,6 +252,17 @@ func (p *ProcessorL1SequenceBatchesEtrog) ProcessSequenceBatches(ctx context.Con
return err
}
} else {
// Batch already exists
// We update the timestamp of the batch to match the timestamp
err := p.state.UpdateBatchTimestamp(ctx, batch.BatchNumber, *processCtx.Timestamp, dbTx)
if err != nil {
log.Errorf("error updating batch timestamp %s. BatchNumber: %d, BlockNumber: %d, error: %v", processCtx.Timestamp, batch.BatchNumber, blockNumber, err)
rollbackErr := dbTx.Rollback(ctx)
if rollbackErr != nil {
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)
return rollbackErr
}
}
// Reprocess batch to compare the stateRoot with tBatch.StateRoot and get accInputHash
batchRespose, err := p.state.ExecuteBatchV2(ctx, batch, processCtx.L1InfoRoot, leaves, *processCtx.Timestamp, false, processCtx.SkipVerifyL1InfoRoot, processCtx.ForcedBlockHashL1, dbTx)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ func TestL1SequenceBatchesTrustedBatchSequencedThatAlreadyExistsHappyPath(t *tes
expectationsPreExecution(t, mocks, ctx, batch, nil)
executionResponse := newProcessBatchResponseV2(batch)
expectationsForExecution(t, mocks, ctx, l1Block.SequencedBatches[1][0], l1Block.ReceivedAt, executionResponse)
mocks.State.EXPECT().UpdateBatchTimestamp(ctx, batch.BatchNumber, l1Block.ReceivedAt, mocks.DbTx).Return(nil)
mocks.State.EXPECT().AddAccumulatedInputHash(ctx, executionResponse.NewBatchNum, common.BytesToHash(executionResponse.NewAccInputHash), mocks.DbTx).Return(nil)
expectationsAddSequencedBatch(t, mocks, ctx, executionResponse)

err := sut.Process(ctx, etherman.Order{Pos: 1}, l1Block, mocks.DbTx)

require.NoError(t, err)
}

Expand All @@ -117,9 +120,12 @@ func TestL1SequenceBatchesPermissionlessBatchSequencedThatAlreadyExistsHappyPath
expectationsPreExecution(t, mocks, ctx, batch, nil)
executionResponse := newProcessBatchResponseV2(batch)
expectationsForExecution(t, mocks, ctx, l1Block.SequencedBatches[1][0], l1Block.ReceivedAt, executionResponse)
mocks.State.EXPECT().UpdateBatchTimestamp(ctx, batch.BatchNumber, l1Block.ReceivedAt, mocks.DbTx).Return(nil)
mocks.State.EXPECT().AddAccumulatedInputHash(ctx, executionResponse.NewBatchNum, common.BytesToHash(executionResponse.NewAccInputHash), mocks.DbTx).Return(nil)
expectationsAddSequencedBatch(t, mocks, ctx, executionResponse)

err := sut.Process(ctx, etherman.Order{Pos: 1}, l1Block, mocks.DbTx)

require.NoError(t, err)
}

Expand All @@ -139,6 +145,7 @@ func TestL1SequenceBatchesPermissionlessBatchSequencedThatAlreadyExistsMismatch(
executionResponse := newProcessBatchResponseV2(batch)
executionResponse.NewStateRoot = common.HexToHash(hashExamplesValues[2]).Bytes()
expectationsForExecution(t, mocks, ctx, l1Block.SequencedBatches[1][0], l1Block.ReceivedAt, executionResponse)
mocks.State.EXPECT().UpdateBatchTimestamp(ctx, batch.BatchNumber, l1Block.ReceivedAt, mocks.DbTx).Return(nil)
mocks.State.EXPECT().AddAccumulatedInputHash(ctx, executionResponse.NewBatchNum, common.BytesToHash(executionResponse.NewAccInputHash), mocks.DbTx).Return(nil)
mocks.Synchronizer.EXPECT().IsTrustedSequencer().Return(false)
mocks.State.EXPECT().AddTrustedReorg(ctx, mock.Anything, mocks.DbTx).Return(nil)
Expand Down Expand Up @@ -177,6 +184,7 @@ func TestL1SequenceBatchesTrustedBatchSequencedThatAlreadyExistsMismatch(t *test
executionResponse := newProcessBatchResponseV2(batch)
executionResponse.NewStateRoot = common.HexToHash(hashExamplesValues[2]).Bytes()
expectationsForExecution(t, mocks, ctx, l1Block.SequencedBatches[1][0], l1Block.ReceivedAt, executionResponse)
mocks.State.EXPECT().UpdateBatchTimestamp(ctx, batch.BatchNumber, l1Block.ReceivedAt, mocks.DbTx).Return(nil)
mocks.State.EXPECT().AddAccumulatedInputHash(ctx, executionResponse.NewBatchNum, common.BytesToHash(executionResponse.NewAccInputHash), mocks.DbTx).Return(nil)
mocks.Synchronizer.EXPECT().IsTrustedSequencer().Return(true)

Expand Down Expand Up @@ -295,7 +303,7 @@ func newL1Block(mocks *mocksEtrogProcessorL1, batch *state.Batch, l1InfoRoot com
func newComposedL1Block(mocks *mocksEtrogProcessorL1, forcedBatch *etherman.SequencedBatch, l1InfoRoot common.Hash) *etherman.Block {
l1Block := etherman.Block{
BlockNumber: 123,
ReceivedAt: mocks.TimeProvider.Now(),
ReceivedAt: time.Date(2024, 1, 1, 1, 0, 0, 0, time.UTC),
SequencedBatches: [][]etherman.SequencedBatch{},
}
l1Block.SequencedBatches = append(l1Block.SequencedBatches, []etherman.SequencedBatch{})
Expand Down
49 changes: 49 additions & 0 deletions synchronizer/common/syncinterfaces/mocks/state_full_interface.go

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

1 change: 1 addition & 0 deletions synchronizer/common/syncinterfaces/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ type StateFullInterface interface {
GetL2BlockByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*state.L2Block, error)
GetUncheckedBlocks(ctx context.Context, fromBlockNumber uint64, toBlockNumber uint64, dbTx pgx.Tx) ([]*state.Block, error)
GetPreviousBlockToBlockNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*state.Block, error)
UpdateBatchTimestamp(ctx context.Context, batchNumber uint64, timestamp time.Time, dbTx pgx.Tx) error
}
2 changes: 2 additions & 0 deletions synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ func TestForcedBatchEtrog(t *testing.T) {
Return(nil).
Once()

m.State.EXPECT().UpdateBatchTimestamp(ctx, sequencedBatch.BatchNumber, fb[0].ForcedAt, m.DbTx).Return(nil)

m.State.
On("AddAccumulatedInputHash", ctx, sequencedBatch.BatchNumber, common.Hash{}, m.DbTx).
Return(nil).
Expand Down

0 comments on commit e3cd415

Please sign in to comment.