Skip to content

Commit

Permalink
add type conversion abstraction to Batch interface
Browse files Browse the repository at this point in the history
Since we're dealing now with wrapped types around Batch implementations,
this let's us transparently unwrap the underlying batch types. It makes
sense to add this to the interface, because getting the underlying types
from the interface is done in several places, so it's part of the
interface's contract.
  • Loading branch information
sebastianst committed May 22, 2024
1 parent bdcc910 commit 88e9a09
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions op-node/rollup/derive/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Batch interface {
GetBatchType() int
GetTimestamp() uint64
LogContext(log.Logger) log.Logger
AsSingularBatch() (*SingularBatch, bool)
AsSpanBatch() (*SpanBatch, bool)
}

type batchWithMetadata struct {
Expand Down
8 changes: 4 additions & 4 deletions op-node/rollup/derive/batch_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ func (bq *BatchQueue) NextBatch(ctx context.Context, parent eth.L2BlockRef) (*Si
}

var nextBatch *SingularBatch
switch batch.GetBatchType() {
switch typ := batch.GetBatchType(); typ {
case SingularBatchType:
singularBatch, ok := batch.(*SingularBatch)
singularBatch, ok := batch.AsSingularBatch()
if !ok {
return nil, false, NewCriticalError(errors.New("failed type assertion to SingularBatch"))
}
nextBatch = singularBatch
case SpanBatchType:
spanBatch, ok := batch.(*SpanBatch)
spanBatch, ok := batch.AsSpanBatch()
if !ok {
return nil, false, NewCriticalError(errors.New("failed type assertion to SpanBatch"))
}
Expand All @@ -198,7 +198,7 @@ func (bq *BatchQueue) NextBatch(ctx context.Context, parent eth.L2BlockRef) (*Si
// span-batches are non-empty, so the below pop is safe.
nextBatch = bq.popNextBatch(parent)
default:
return nil, false, NewCriticalError(fmt.Errorf("unrecognized batch type: %d", batch.GetBatchType()))
return nil, false, NewCriticalError(fmt.Errorf("unrecognized batch type: %d", typ))
}

// If the nextBatch is derived from the span batch, len(bq.nextSpan) == 0 means it's the last batch of the span.
Expand Down
10 changes: 5 additions & 5 deletions op-node/rollup/derive/batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

type BatchWithL1InclusionBlock struct {
Batch
L1InclusionBlock eth.L1BlockRef
Batch Batch
}

type BatchValidity uint8
Expand All @@ -34,23 +34,23 @@ const (
func CheckBatch(ctx context.Context, cfg *rollup.Config, log log.Logger, l1Blocks []eth.L1BlockRef,
l2SafeHead eth.L2BlockRef, batch *BatchWithL1InclusionBlock, l2Fetcher SafeBlockFetcher,
) BatchValidity {
switch batch.Batch.GetBatchType() {
switch typ := batch.GetBatchType(); typ {
case SingularBatchType:
singularBatch, ok := batch.Batch.(*SingularBatch)
singularBatch, ok := batch.AsSingularBatch()
if !ok {
log.Error("failed type assertion to SingularBatch")
return BatchDrop
}
return checkSingularBatch(cfg, log, l1Blocks, l2SafeHead, singularBatch, batch.L1InclusionBlock)
case SpanBatchType:
spanBatch, ok := batch.Batch.(*SpanBatch)
spanBatch, ok := batch.AsSpanBatch()
if !ok {
log.Error("failed type assertion to SpanBatch")
return BatchDrop
}
return checkSpanBatch(ctx, cfg, log, l1Blocks, l2SafeHead, spanBatch, batch.L1InclusionBlock, l2Fetcher)
default:
log.Warn("Unrecognized batch type: %d", batch.Batch.GetBatchType())
log.Warn("Unrecognized batch type: %d", typ)
return BatchDrop
}
}
Expand Down
3 changes: 3 additions & 0 deletions op-node/rollup/derive/singular_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type SingularBatch struct {
Transactions []hexutil.Bytes
}

func (b *SingularBatch) AsSingularBatch() (*SingularBatch, bool) { return b, true }
func (b *SingularBatch) AsSpanBatch() (*SpanBatch, bool) { return nil, false }

// GetBatchType returns its batch type (batch_version)
func (b *SingularBatch) GetBatchType() int {
return SingularBatchType
Expand Down
3 changes: 3 additions & 0 deletions op-node/rollup/derive/span_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ type SpanBatch struct {
sbtxs *spanBatchTxs
}

func (b *SpanBatch) AsSingularBatch() (*SingularBatch, bool) { return nil, false }
func (b *SpanBatch) AsSpanBatch() (*SpanBatch, bool) { return b, true }

// spanBatchMarshaling is a helper type used for JSON marshaling.
type spanBatchMarshaling struct {
ParentCheck []hexutil.Bytes `json:"parent_check"`
Expand Down

0 comments on commit 88e9a09

Please sign in to comment.