Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.
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
16 changes: 12 additions & 4 deletions state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,16 @@ func (s *State) CloseBatch(ctx context.Context, receipt ProcessingReceipt, dbTx
// ProcessAndStoreClosedBatch is used by the Synchronizer to add a closed batch into the data base. Values returned are the new stateRoot,
// the flushID (incremental value returned by executor),
// the ProverID (executor running ID) the result of closing the batch.
func (s *State) ProcessAndStoreClosedBatch(ctx context.Context, processingCtx ProcessingContext, encodedTxs []byte, dbTx pgx.Tx, caller metrics.CallerLabel) (common.Hash, uint64, string, error) {
func (s *State) ProcessAndStoreClosedBatch(ctx context.Context, processingCtx ProcessingContext, dbTx pgx.Tx, caller metrics.CallerLabel) (common.Hash, uint64, string, error) {
BatchL2Data := processingCtx.BatchL2Data
if BatchL2Data == nil {
log.Warnf("Batch %v: ProcessAndStoreClosedBatch: processingCtx.BatchL2Data is nil, assuming is empty", processingCtx.BatchNumber)
var BatchL2DataEmpty []byte
BatchL2Data = &BatchL2DataEmpty
}
// Decode transactions
forkID := s.GetForkIDByBatchNumber(processingCtx.BatchNumber)
decodedTransactions, _, _, err := DecodeTxs(encodedTxs, forkID)
decodedTransactions, _, _, err := DecodeTxs(*BatchL2Data, forkID)
if err != nil && !errors.Is(err, ErrInvalidData) {
log.Debugf("error decoding transactions: %v", err)
return common.Hash{}, noFlushID, noProverID, err
Expand All @@ -423,10 +429,12 @@ func (s *State) ProcessAndStoreClosedBatch(ctx context.Context, processingCtx Pr
if dbTx == nil {
return common.Hash{}, noFlushID, noProverID, ErrDBTxNil
}
// Avoid writing twice to the DB the BatchL2Data that is going to be written also in the call closeBatch
processingCtx.BatchL2Data = nil
if err := s.OpenBatch(ctx, processingCtx, dbTx); err != nil {
return common.Hash{}, noFlushID, noProverID, err
}
processed, err := s.processBatch(ctx, processingCtx.BatchNumber, encodedTxs, caller, dbTx)
processed, err := s.processBatch(ctx, processingCtx.BatchNumber, *BatchL2Data, caller, dbTx)
if err != nil {
return common.Hash{}, noFlushID, noProverID, err
}
Expand Down Expand Up @@ -476,7 +484,7 @@ func (s *State) ProcessAndStoreClosedBatch(ctx context.Context, processingCtx Pr
StateRoot: processedBatch.NewStateRoot,
LocalExitRoot: processedBatch.NewLocalExitRoot,
AccInputHash: processedBatch.NewAccInputHash,
BatchL2Data: encodedTxs,
BatchL2Data: *BatchL2Data,
}, dbTx)
}

Expand Down
2 changes: 1 addition & 1 deletion synchronizer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type stateInterface interface {
AddVirtualBatch(ctx context.Context, virtualBatch *state.VirtualBatch, dbTx pgx.Tx) error
GetNextForcedBatches(ctx context.Context, nextForcedBatches int, dbTx pgx.Tx) ([]state.ForcedBatch, error)
AddVerifiedBatch(ctx context.Context, verifiedBatch *state.VerifiedBatch, dbTx pgx.Tx) error
ProcessAndStoreClosedBatch(ctx context.Context, processingCtx state.ProcessingContext, encodedTxs []byte, dbTx pgx.Tx, caller metrics.CallerLabel) (common.Hash, uint64, string, error)
ProcessAndStoreClosedBatch(ctx context.Context, processingCtx state.ProcessingContext, dbTx pgx.Tx, caller metrics.CallerLabel) (common.Hash, uint64, string, error)
SetGenesis(ctx context.Context, block state.Block, genesis state.Genesis, dbTx pgx.Tx) ([]byte, error)
OpenBatch(ctx context.Context, processingContext state.ProcessingContext, dbTx pgx.Tx) error
CloseBatch(ctx context.Context, receipt state.ProcessingReceipt, dbTx pgx.Tx) error
Expand Down
35 changes: 17 additions & 18 deletions synchronizer/mock_state.go

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

8 changes: 5 additions & 3 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
Timestamp: batch.Timestamp,
GlobalExitRoot: batch.GlobalExitRoot,
ForcedBatchNum: batch.ForcedBatchNum,
BatchL2Data: &batch.BatchL2Data,
}

var newRoot common.Hash
Expand All @@ -853,7 +854,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
if errors.Is(err, state.ErrNotFound) || errors.Is(err, state.ErrStateNotSynchronized) {
log.Debugf("BatchNumber: %d, not found in trusted state. Storing it...", batch.BatchNumber)
// If it is not found, store batch
newStateRoot, flushID, proverID, err := s.state.ProcessAndStoreClosedBatch(s.ctx, processCtx, batch.BatchL2Data, dbTx, stateMetrics.SynchronizerCallerLabel)
newStateRoot, flushID, proverID, err := s.state.ProcessAndStoreClosedBatch(s.ctx, processCtx, dbTx, stateMetrics.SynchronizerCallerLabel)
if err != nil {
log.Errorf("error storing trustedBatch. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
Expand Down Expand Up @@ -935,7 +936,7 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
log.Errorf("error resetting trusted state. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, blockNumber, err)
return err
}
_, flushID, proverID, err := s.state.ProcessAndStoreClosedBatch(s.ctx, processCtx, batch.BatchL2Data, dbTx, stateMetrics.SynchronizerCallerLabel)
_, flushID, proverID, err := s.state.ProcessAndStoreClosedBatch(s.ctx, processCtx, dbTx, stateMetrics.SynchronizerCallerLabel)
if err != nil {
log.Errorf("error storing trustedBatch. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, blockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
Expand Down Expand Up @@ -1058,9 +1059,10 @@ func (s *ClientSynchronizer) processSequenceForceBatch(sequenceForceBatch []ethe
Timestamp: block.ReceivedAt,
Coinbase: fbatch.Coinbase,
ForcedBatchNum: &forcedBatches[i].ForcedBatchNumber,
BatchL2Data: &forcedBatches[i].RawTxsData,
}
// Process batch
_, flushID, proverID, err := s.state.ProcessAndStoreClosedBatch(s.ctx, batch, forcedBatches[i].RawTxsData, dbTx, stateMetrics.SynchronizerCallerLabel)
_, flushID, proverID, err := s.state.ProcessAndStoreClosedBatch(s.ctx, batch, dbTx, stateMetrics.SynchronizerCallerLabel)
if err != nil {
log.Errorf("error processing batch in processSequenceForceBatch. BatchNumber: %d, BlockNumber: %d, error: %v", batch.BatchNumber, block.BlockNumber, err)
rollbackErr := dbTx.Rollback(s.ctx)
Expand Down
3 changes: 2 additions & 1 deletion synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,11 @@ func TestSequenceForcedBatch(t *testing.T) {
Timestamp: ethBlock.ReceivedAt,
GlobalExitRoot: sequencedForceBatch.GlobalExitRoot,
ForcedBatchNum: &f,
BatchL2Data: &sequencedForceBatch.Transactions,
}

m.State.
On("ProcessAndStoreClosedBatch", ctx, processingContext, sequencedForceBatch.Transactions, m.DbTx, metrics.SynchronizerCallerLabel).
On("ProcessAndStoreClosedBatch", ctx, processingContext, m.DbTx, metrics.SynchronizerCallerLabel).
Return(common.Hash{}, uint64(1), cProverIDExecution, nil).
Once()

Expand Down