Skip to content

Commit

Permalink
improve: storing 'batch_resources' and 'closing_reason' in 'state.bat…
Browse files Browse the repository at this point in the history
…ch' on closing a batch.

Signed-off-by: Nikolay Nedkov <[email protected]>
Psykepro committed Apr 12, 2023

Unverified

This user has not yet uploaded their public signing key.
1 parent c32f2d4 commit f72e0d7
Showing 20 changed files with 325 additions and 253 deletions.
9 changes: 9 additions & 0 deletions db/migrations/state/0006.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +migrate Up
ALTER TABLE state.batch
ADD COLUMN batch_resources JSONB,
ADD COLUMN closing_reason VARCHAR;

-- +migrate Down
ALTER TABLE state.batch
DROP COLUMN batch_resources,
DROP COLUMN closing_reason;
28 changes: 0 additions & 28 deletions sequencer/batchresources.go

This file was deleted.

36 changes: 26 additions & 10 deletions sequencer/dbmanager.go
Original file line number Diff line number Diff line change
@@ -36,11 +36,13 @@ func (d *dbManager) GetBatchByNumber(ctx context.Context, batchNumber uint64, db

// ClosingBatchParameters contains the necessary parameters to close a batch
type ClosingBatchParameters struct {
BatchNumber uint64
StateRoot common.Hash
LocalExitRoot common.Hash
AccInputHash common.Hash
Txs []types.Transaction
BatchNumber uint64
StateRoot common.Hash
LocalExitRoot common.Hash
AccInputHash common.Hash
Txs []types.Transaction
BatchResources state.BatchResources
ClosingReason state.ClosingReason
}

func newDBManager(ctx context.Context, config DBManagerCfg, txPool txPool, state dbManagerStateInterface, worker *Worker, closingSignalCh ClosingSignalCh, txsStore TxsStore, batchConstraints batchConstraints) *dbManager {
@@ -357,7 +359,7 @@ func (d *dbManager) GetWIPBatch(ctx context.Context) (*WipBatch, error) {
}
}

wipBatch.remainingResources = batchResources{zKCounters: batchZkCounters, bytes: totalBytes}
wipBatch.remainingResources = state.BatchResources{ZKCounters: batchZkCounters, Bytes: totalBytes}
return wipBatch, nil
}

@@ -393,10 +395,12 @@ func (d *dbManager) GetLatestGer(ctx context.Context, gerFinalityNumberOfBlocks
// CloseBatch closes a batch in the state
func (d *dbManager) CloseBatch(ctx context.Context, params ClosingBatchParameters) error {
processingReceipt := state.ProcessingReceipt{
BatchNumber: params.BatchNumber,
StateRoot: params.StateRoot,
LocalExitRoot: params.LocalExitRoot,
AccInputHash: params.AccInputHash,
BatchNumber: params.BatchNumber,
StateRoot: params.StateRoot,
LocalExitRoot: params.LocalExitRoot,
AccInputHash: params.AccInputHash,
BatchResources: params.BatchResources,
ClosingReason: params.ClosingReason,
}

batchL2Data, err := state.EncodeTransactions(params.Txs)
@@ -477,12 +481,24 @@ func (d *dbManager) ProcessForcedBatch(forcedBatchNum uint64, request state.Proc
}

// Close Batch
txsBytes := uint64(0)
for _, resp := range processBatchResponse.Responses {
if !resp.IsProcessed {
continue
}
txsBytes += resp.Tx.Size()
}
processingReceipt := state.ProcessingReceipt{
BatchNumber: request.BatchNumber,
StateRoot: processBatchResponse.NewStateRoot,
LocalExitRoot: processBatchResponse.NewLocalExitRoot,
AccInputHash: processBatchResponse.NewAccInputHash,
BatchL2Data: forcedBatch.RawTxsData,
BatchResources: state.BatchResources{
ZKCounters: processBatchResponse.UsedZkCounters,
Bytes: txsBytes,
},
ClosingReason: state.ForcedBatchClosingReason,
}

isClosed := false
37 changes: 0 additions & 37 deletions sequencer/errors.go

This file was deleted.

96 changes: 58 additions & 38 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
@@ -64,8 +64,9 @@ type WipBatch struct {
localExitRoot common.Hash
timestamp uint64
globalExitRoot common.Hash // 0x000...0 (ZeroHash) means to not update
remainingResources batchResources
remainingResources state.BatchResources
countOfTxs int
closingReason state.ClosingReason
}

func (w *WipBatch) isEmpty() bool {
@@ -238,6 +239,7 @@ func (f *finalizer) finalizeBatches(ctx context.Context) {
func (f *finalizer) isBatchFull() bool {
if f.batch.countOfTxs >= int(f.batchConstraints.MaxTxsPerBatch) {
log.Infof("Closing batch: %d, because it's full.", f.batch.batchNumber)
f.batch.closingReason = state.BatchFullClosingReason
return true
}
return false
@@ -416,7 +418,7 @@ func (f *finalizer) handleTxProcessResp(ctx context.Context, tx *TxTracker, resu
}

// Check remaining resources
err := f.checkRemainingResources(ctx, result, tx)
err := f.checkRemainingResources(result, tx)
if err != nil {
return err
}
@@ -685,11 +687,14 @@ func (f *finalizer) closeBatch(ctx context.Context) error {
for i, tx := range transactions {
log.Infof("closeBatch: BatchNum: %d, Tx position: %d, txHash: %s", f.batch.batchNumber, i, tx.Hash().String())
}
usedResources := getUsedBatchResources(f.batchConstraints, f.batch.remainingResources)
receipt := ClosingBatchParameters{
BatchNumber: f.batch.batchNumber,
StateRoot: f.batch.stateRoot,
LocalExitRoot: f.batch.localExitRoot,
Txs: transactions,
BatchNumber: f.batch.batchNumber,
StateRoot: f.batch.stateRoot,
LocalExitRoot: f.batch.localExitRoot,
Txs: transactions,
BatchResources: usedResources,
ClosingReason: f.batch.closingReason,
}
return f.dbManager.CloseBatch(ctx, receipt)
}
@@ -804,35 +809,38 @@ func (f *finalizer) isDeadlineEncountered() bool {
if f.nextForcedBatchDeadline != 0 && now().Unix() >= f.nextForcedBatchDeadline {
log.Infof("Closing batch: %d, forced batch deadline encountered.", f.batch.batchNumber)
f.setNextSendingToL1Deadline()
f.batch.closingReason = state.ForcedBatchDeadlineClosingReason
return true
}
// Global Exit Root deadline
if f.nextGERDeadline != 0 && now().Unix() >= f.nextGERDeadline {
log.Infof("Closing batch: %d, Global Exit Root deadline encountered.", f.batch.batchNumber)
f.setNextSendingToL1Deadline()
f.batch.closingReason = state.GlobalExitRootDeadlineClosingReason
return true
}
// Delayed batch deadline
if f.nextSendingToL1Deadline != 0 && now().Unix() >= f.nextSendingToL1Deadline && !f.batch.isEmpty() {
log.Infof("Closing batch: %d, Sending to L1 deadline encountered.", f.batch.batchNumber)
f.setNextSendingToL1Deadline()
f.batch.closingReason = state.SendingToL1DeadlineClosingReason
return true
}
return false
}

// checkRemainingResources checks if the transaction uses less resources than the remaining ones in the batch.
func (f *finalizer) checkRemainingResources(ctx context.Context, result *state.ProcessBatchResponse, tx *TxTracker) error {
usedResources := batchResources{
zKCounters: result.UsedZkCounters,
bytes: uint64(len(tx.RawTx)),
func (f *finalizer) checkRemainingResources(result *state.ProcessBatchResponse, tx *TxTracker) error {
usedResources := state.BatchResources{
ZKCounters: result.UsedZkCounters,
Bytes: uint64(len(tx.RawTx)),
}

err := f.batch.remainingResources.sub(usedResources)
err := f.batch.remainingResources.Sub(usedResources)
if err != nil {
log.Infof("current transaction exceeds the batch limit, updating metadata for tx in worker and continuing")
start := time.Now()
f.worker.UpdateTx(result.Responses[0].TxHash, tx.From, usedResources.zKCounters)
f.worker.UpdateTx(result.Responses[0].TxHash, tx.From, usedResources.ZKCounters)
metrics.WorkerProcessingTime(time.Since(start))
return err
}
@@ -843,32 +851,28 @@ func (f *finalizer) checkRemainingResources(ctx context.Context, result *state.P
// isBatchAlmostFull checks if the current batch remaining resources are under the constraints threshold for most efficient moment to close a batch
func (f *finalizer) isBatchAlmostFull() bool {
resources := f.batch.remainingResources
zkCounters := resources.zKCounters
if resources.bytes <= f.getConstraintThresholdUint64(f.batchConstraints.MaxBatchBytesSize) {
return true
}
if zkCounters.UsedSteps <= f.getConstraintThresholdUint32(f.batchConstraints.MaxSteps) {
return true
}
if zkCounters.UsedPoseidonPaddings <= f.getConstraintThresholdUint32(f.batchConstraints.MaxPoseidonPaddings) {
return true
}
if zkCounters.UsedBinaries <= f.getConstraintThresholdUint32(f.batchConstraints.MaxBinaries) {
return true
}
if zkCounters.UsedKeccakHashes <= f.getConstraintThresholdUint32(f.batchConstraints.MaxKeccakHashes) {
return true
}
if zkCounters.UsedArithmetics <= f.getConstraintThresholdUint32(f.batchConstraints.MaxArithmetics) {
return true
}
if zkCounters.UsedMemAligns <= f.getConstraintThresholdUint32(f.batchConstraints.MaxMemAligns) {
return true
}
if zkCounters.CumulativeGasUsed <= f.getConstraintThresholdUint64(f.batchConstraints.MaxCumulativeGasUsed) {
return true
}
return false
zkCounters := resources.ZKCounters
result := false
if resources.Bytes <= f.getConstraintThresholdUint64(f.batchConstraints.MaxBatchBytesSize) {
result = true
} else if zkCounters.UsedSteps <= f.getConstraintThresholdUint32(f.batchConstraints.MaxSteps) {
result = true
} else if zkCounters.UsedPoseidonPaddings <= f.getConstraintThresholdUint32(f.batchConstraints.MaxPoseidonPaddings) {
result = true
} else if zkCounters.UsedBinaries <= f.getConstraintThresholdUint32(f.batchConstraints.MaxBinaries) {
result = true
} else if zkCounters.UsedKeccakHashes <= f.getConstraintThresholdUint32(f.batchConstraints.MaxKeccakHashes) {
result = true
} else if zkCounters.UsedArithmetics <= f.getConstraintThresholdUint32(f.batchConstraints.MaxArithmetics) {
result = true
} else if zkCounters.UsedMemAligns <= f.getConstraintThresholdUint32(f.batchConstraints.MaxMemAligns) {
result = true
} else if zkCounters.CumulativeGasUsed <= f.getConstraintThresholdUint64(f.batchConstraints.MaxCumulativeGasUsed) {
result = true
}
f.batch.closingReason = state.BatchAlmostFullClosingReason

return result
}

func (f *finalizer) setNextForcedBatchDeadline() {
@@ -890,3 +894,19 @@ func (f *finalizer) getConstraintThresholdUint64(input uint64) uint64 {
func (f *finalizer) getConstraintThresholdUint32(input uint32) uint32 {
return uint32(input*f.cfg.ResourcePercentageToCloseBatch) / oneHundred
}

func getUsedBatchResources(constraints batchConstraints, remainingResources state.BatchResources) state.BatchResources {
return state.BatchResources{
ZKCounters: state.ZKCounters{
CumulativeGasUsed: constraints.MaxCumulativeGasUsed - remainingResources.ZKCounters.CumulativeGasUsed,
UsedKeccakHashes: constraints.MaxKeccakHashes - remainingResources.ZKCounters.UsedKeccakHashes,
UsedPoseidonHashes: constraints.MaxPoseidonHashes - remainingResources.ZKCounters.UsedPoseidonHashes,
UsedPoseidonPaddings: constraints.MaxPoseidonPaddings - remainingResources.ZKCounters.UsedPoseidonPaddings,
UsedMemAligns: constraints.MaxMemAligns - remainingResources.ZKCounters.UsedMemAligns,
UsedArithmetics: constraints.MaxArithmetics - remainingResources.ZKCounters.UsedArithmetics,
UsedBinaries: constraints.MaxBinaries - remainingResources.ZKCounters.UsedBinaries,
UsedSteps: constraints.MaxSteps - remainingResources.ZKCounters.UsedSteps,
},
Bytes: constraints.MaxBatchBytesSize - remainingResources.Bytes,
}
}
Loading

0 comments on commit f72e0d7

Please sign in to comment.