Skip to content

Commit

Permalink
Synchronized inform field state.batch.closing_reason from DB
Browse files Browse the repository at this point in the history
* Add reasons to the field `state.batch.closing_reason` (issue #3243)
  • Loading branch information
joanestebanr authored Feb 26, 2024
1 parent 54b7fcd commit f08d413
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 14 deletions.
17 changes: 17 additions & 0 deletions state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type ProcessingContext struct {
GlobalExitRoot common.Hash
ForcedBatchNum *uint64
BatchL2Data *[]byte
ClosingReason ClosingReason
}

// ClosingReason represents the reason why a batch is closed.
Expand All @@ -82,6 +83,22 @@ const (
MaxDeltaTimestampClosingReason ClosingReason = "Max delta timestamp"
// NoTxFitsClosingReason is the closing reason used when any of the txs in the pool (worker) fits in the remaining resources of the batch
NoTxFitsClosingReason ClosingReason = "No transaction fits"

// Reason due Synchronizer
// ------------------------------------------------------------------------------------------

// SyncL1EventInitialBatchClosingReason is the closing reason used when a batch is closed by the synchronizer due to an initial batch (first batch mode forced)
SyncL1EventInitialBatchClosingReason ClosingReason = "Sync L1: initial"
// SyncL1EventSequencedBatchClosingReason is the closing reason used when a batch is closed by the synchronizer due to a sequenced batch event from L1
SyncL1EventSequencedBatchClosingReason ClosingReason = "Sync L1: sequenced"
// SyncL1EventSequencedForcedBatchClosingReason is the closing reason used when a batch is closed by the synchronizer due to a sequenced forced batch event from L1
SyncL1EventSequencedForcedBatchClosingReason ClosingReason = "Sync L1: forced"
// SyncL1EventUpdateEtrogSequenceClosingReason is the closing reason used when a batch is closed by the synchronizer due to an UpdateEtrogSequence event from L1 that inject txs
SyncL1EventUpdateEtrogSequenceClosingReason ClosingReason = "Sync L1: injected"
// SyncL2TrustedBatchClosingReason is the closing reason used when a batch is closed by the synchronizer due to a trusted batch from L2
SyncL2TrustedBatchClosingReason ClosingReason = "Sync L2: trusted"
// SyncGenesisBatchClosingReason is the closing reason used when genesis batch is created by synchronizer
SyncGenesisBatchClosingReason ClosingReason = "Sync: genesis"
)

// ProcessingReceipt indicates the outcome (StateRoot, AccInputHash) of processing a batch
Expand Down
2 changes: 2 additions & 0 deletions state/batchV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ProcessingContextV2 struct {
SkipVerifyL1InfoRoot uint32
GlobalExitRoot common.Hash // GlobalExitRoot is not use for execute but use to OpenBatch (data on DB)
ExecutionMode uint64
ClosingReason ClosingReason
}

// ProcessBatchV2 processes a batch for forkID >= ETROG
Expand Down Expand Up @@ -415,5 +416,6 @@ func (s *State) ProcessAndStoreClosedBatchV2(ctx context.Context, processingCtx
LocalExitRoot: processedBatch.NewLocalExitRoot,
AccInputHash: processedBatch.NewAccInputHash,
BatchL2Data: *BatchL2Data,
ClosingReason: processingCtx.ClosingReason,
}, dbTx)
}
1 change: 1 addition & 0 deletions state/convertersV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func convertProcessingContext(p *ProcessingContextV2) (*ProcessingContext, error
BatchL2Data: p.BatchL2Data,
Timestamp: tstamp,
GlobalExitRoot: p.GlobalExitRoot,
ClosingReason: p.ClosingReason,
}
return &result, nil
}
2 changes: 1 addition & 1 deletion state/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, m
ForcedBatchNum: nil,
}

err = s.StoreGenesisBatch(ctx, batch, dbTx)
err = s.StoreGenesisBatch(ctx, batch, string(SyncGenesisBatchClosingReason), dbTx)
if err != nil {
return common.Hash{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type storage interface {
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
Begin(ctx context.Context) (pgx.Tx, error)
StoreGenesisBatch(ctx context.Context, batch Batch, dbTx pgx.Tx) error
StoreGenesisBatch(ctx context.Context, batch Batch, closingReason string, dbTx pgx.Tx) error
ResetToL1BlockNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) error
ResetForkID(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error
ResetTrustedState(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error
Expand Down
21 changes: 11 additions & 10 deletions state/mocks/mock_storage.go

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

5 changes: 3 additions & 2 deletions state/pgstatestorage/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,8 @@ func (p *PostgresStorage) GetVirtualBatch(ctx context.Context, batchNumber uint6
return &virtualBatch, nil
}

func (p *PostgresStorage) StoreGenesisBatch(ctx context.Context, batch state.Batch, dbTx pgx.Tx) error {
const addGenesisBatchSQL = "INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num, wip) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, FALSE)"
func (p *PostgresStorage) StoreGenesisBatch(ctx context.Context, batch state.Batch, closingReason string, dbTx pgx.Tx) error {
const addGenesisBatchSQL = "INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num,closing_reason, wip) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9,$10, FALSE)"

if batch.BatchNumber != 0 {
return fmt.Errorf("%w. Got %d, should be 0", state.ErrUnexpectedBatch, batch.BatchNumber)
Expand All @@ -577,6 +577,7 @@ func (p *PostgresStorage) StoreGenesisBatch(ctx context.Context, batch state.Bat
batch.Coinbase.String(),
batch.BatchL2Data,
batch.ForcedBatchNum,
closingReason,
)

return err
Expand Down
3 changes: 3 additions & 0 deletions synchronizer/actions/etrog/processor_l1_sequence_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func (p *ProcessorL1SequenceBatchesEtrog) ProcessSequenceBatches(ctx context.Con
ForcedBlockHashL1: forcedBlockHashL1,
SkipVerifyL1InfoRoot: 1,
ExecutionMode: executor.ExecutionMode1,
ClosingReason: state.SyncL1EventSequencedForcedBatchClosingReason,
}
} else if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber == 1 {
log.Debug("Processing initial batch")
Expand All @@ -174,6 +175,7 @@ func (p *ProcessorL1SequenceBatchesEtrog) ProcessSequenceBatches(ctx context.Con
ForcedBlockHashL1: forcedBlockHashL1,
SkipVerifyL1InfoRoot: 1,
ExecutionMode: executor.ExecutionMode1,
ClosingReason: state.SyncL1EventInitialBatchClosingReason,
}
} else {
var maxGER common.Hash
Expand All @@ -199,6 +201,7 @@ func (p *ProcessorL1SequenceBatchesEtrog) ProcessSequenceBatches(ctx context.Con
SkipVerifyL1InfoRoot: 1,
GlobalExitRoot: batch.GlobalExitRoot,
ExecutionMode: executor.ExecutionMode1,
ClosingReason: state.SyncL1EventSequencedBatchClosingReason,
}
if batch.GlobalExitRoot == (common.Hash{}) {
if len(leaves) > 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (g *ProcessorL1UpdateEtrogSequence) processUpdateEtrogSequence(ctx context.
SkipVerifyL1InfoRoot: 1,
GlobalExitRoot: updateEtrogSequence.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
ExecutionMode: executor.ExecutionMode1,
ClosingReason: state.SyncL1EventUpdateEtrogSequenceClosingReason,
}

virtualBatch := state.VirtualBatch{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ func (b *SyncTrustedBatchExecutorForEtrog) CloseBatch(ctx context.Context, trust
LocalExitRoot: trustedBatch.LocalExitRoot,
BatchL2Data: trustedBatch.BatchL2Data,
AccInputHash: trustedBatch.AccInputHash,
ClosingReason: state.SyncL2TrustedBatchClosingReason,
}
log.Debugf("%s closing batch %v", debugStr, trustedBatch.Number)
// This update SET state_root = $1, local_exit_root = $2, acc_input_hash = $3, raw_txs_data = $4, batch_resources = $5, closing_reason = $6, wip = FALSE
Expand Down

0 comments on commit f08d413

Please sign in to comment.