batch_queue.go 13.4 KB
Newer Older
protolambda's avatar
protolambda committed
1 2 3 4
package derive

import (
	"context"
5
	"errors"
protolambda's avatar
protolambda committed
6 7 8
	"fmt"
	"io"

9 10
	"github.com/ethereum/go-ethereum/log"

protolambda's avatar
protolambda committed
11
	"github.com/ethereum-optimism/optimism/op-node/rollup"
12
	"github.com/ethereum-optimism/optimism/op-service/eth"
protolambda's avatar
protolambda committed
13 14
)

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// The batch queue is responsible for ordering unordered batches & generating empty batches
// when the sequence window has passed. This is a very stateful stage.
//
// It receives batches that are tagged with the L1 Inclusion block of the batch. It only considers
// batches that are inside the sequencing window of a specific L1 Origin.
// It tries to eagerly pull batches based on the current L2 safe head.
// Otherwise it filters/creates an entire epoch's worth of batches at once.
//
// This stage tracks a range of L1 blocks with the assumption that all batches with an L1 inclusion
// block inside that range have been added to the stage by the time that it attempts to advance a
// full epoch.
//
// It is internally responsible for making sure that batches with L1 inclusions block outside it's
// working range are not considered or pruned.

30 31
type NextBatchProvider interface {
	Origin() eth.L1BlockRef
32 33 34 35 36
	NextBatch(ctx context.Context) (Batch, error)
}

type SafeBlockFetcher interface {
	L2BlockRefByNumber(context.Context, uint64) (eth.L2BlockRef, error)
37
	PayloadByNumber(context.Context, uint64) (*eth.ExecutionPayloadEnvelope, error)
38 39
}

protolambda's avatar
protolambda committed
40 41 42
// BatchQueue contains a set of batches for every L1 block.
// L1 blocks are contiguous and this does not support reorgs.
type BatchQueue struct {
43 44 45 46
	log    log.Logger
	config *rollup.Config
	prev   NextBatchProvider
	origin eth.L1BlockRef
47

48 49 50 51 52 53
	// l1Blocks contains consecutive eth.L1BlockRef sorted by time.
	// Every L1 origin of unsafe L2 blocks must be eventually included in l1Blocks.
	// Batch queue's job is to ensure below two rules:
	//  If every L2 block corresponding to single L1 block becomes safe, it will be popped from l1Blocks.
	//  If new L2 block's L1 origin is not included in l1Blocks, fetch and push to l1Blocks.
	// length of l1Blocks never exceeds SequencerWindowSize
54 55
	l1Blocks []eth.L1BlockRef

56 57 58 59 60 61 62
	// batches in order of when we've first seen them
	batches []*BatchWithL1InclusionBlock

	// nextSpan is cached SingularBatches derived from SpanBatch
	nextSpan []*SingularBatch

	l2 SafeBlockFetcher
protolambda's avatar
protolambda committed
63 64 65
}

// NewBatchQueue creates a BatchQueue, which should be Reset(origin) before use.
66
func NewBatchQueue(log log.Logger, cfg *rollup.Config, prev NextBatchProvider, l2 SafeBlockFetcher) *BatchQueue {
protolambda's avatar
protolambda committed
67
	return &BatchQueue{
68 69
		log:    log,
		config: cfg,
70
		prev:   prev,
71
		l2:     l2,
protolambda's avatar
protolambda committed
72 73 74
	}
}

75 76
func (bq *BatchQueue) Origin() eth.L1BlockRef {
	return bq.prev.Origin()
protolambda's avatar
protolambda committed
77 78
}

79 80
// popNextBatch pops the next batch from the current queued up span-batch nextSpan.
// The queue must be non-empty, or the function will panic.
Tei Im's avatar
Tei Im committed
81
func (bq *BatchQueue) popNextBatch(parent eth.L2BlockRef) *SingularBatch {
82 83 84
	if len(bq.nextSpan) == 0 {
		panic("popping non-existent span-batch, invalid state")
	}
85 86
	nextBatch := bq.nextSpan[0]
	bq.nextSpan = bq.nextSpan[1:]
Tei Im's avatar
Tei Im committed
87 88
	// Must set ParentHash before return. we can use parent because the parentCheck is verified in CheckBatch().
	nextBatch.ParentHash = parent.Hash
Tei Im's avatar
Tei Im committed
89
	bq.log.Debug("pop next batch from the cached span batch")
90 91 92
	return nextBatch
}

93 94
// NextBatch return next valid batch upon the given safe head.
// It also returns the boolean that indicates if the batch is the last block in the batch.
Tei Im's avatar
Tei Im committed
95
func (bq *BatchQueue) NextBatch(ctx context.Context, parent eth.L2BlockRef) (*SingularBatch, bool, error) {
96
	if len(bq.nextSpan) > 0 {
Tei Im's avatar
Tei Im committed
97 98
		// There are cached singular batches derived from the span batch.
		// Check if the next cached batch matches the given parent block.
Tei Im's avatar
Tei Im committed
99
		if bq.nextSpan[0].Timestamp == parent.Time+bq.config.BlockTime {
Tei Im's avatar
Tei Im committed
100
			// Pop first one and return.
Tei Im's avatar
Tei Im committed
101
			nextBatch := bq.popNextBatch(parent)
Tei Im's avatar
Tei Im committed
102
			// len(bq.nextSpan) == 0 means it's the last batch of the span.
103 104
			return nextBatch, len(bq.nextSpan) == 0, nil
		} else {
Tei Im's avatar
Tei Im committed
105 106
			// Given parent block does not match the next batch. It means the previously returned batch is invalid.
			// Drop cached batches and find another batch.
Tei Im's avatar
Tei Im committed
107
			bq.log.Warn("parent block does not match the next batch. dropped cached batches", "parent", parent.ID(), "nextBatchTime", bq.nextSpan[0].GetTimestamp())
108 109
			bq.nextSpan = bq.nextSpan[:0]
		}
110 111
	}

112
	// If the epoch is advanced, update bq.l1Blocks
113 114 115
	// Advancing epoch must be done after the pipeline successfully apply the entire span batch to the chain.
	// Because the span batch can be reverted during processing the batch, then we must preserve existing l1Blocks
	// to verify the epochs of the next candidate batch.
Tei Im's avatar
Tei Im committed
116
	if len(bq.l1Blocks) > 0 && parent.L1Origin.Number > bq.l1Blocks[0].Number {
117
		for i, l1Block := range bq.l1Blocks {
Tei Im's avatar
Tei Im committed
118
			if parent.L1Origin.Number == l1Block.Number {
119
				bq.l1Blocks = bq.l1Blocks[i:]
Tei Im's avatar
Tei Im committed
120 121 122 123 124
				if len(bq.l1Blocks) > 0 {
					bq.log.Debug("Advancing internal L1 blocks", "next_epoch", bq.l1Blocks[0].ID(), "next_epoch_time", bq.l1Blocks[0].Time)
				} else {
					bq.log.Debug("Advancing internal L1 blocks. No L1 blocks left")
				}
125 126 127
				break
			}
		}
Tei Im's avatar
Tei Im committed
128
		// If we can't find the origin of parent block, we have to advance bq.origin.
129 130
	}

131 132 133 134
	// Note: We use the origin that we will have to determine if it's behind. This is important
	// because it's the future origin that gets saved into the l1Blocks array.
	// We always update the origin of this stage if it is not the same so after the update code
	// runs, this is consistent.
Tei Im's avatar
Tei Im committed
135
	originBehind := bq.prev.Origin().Number < parent.L1Origin.Number
136 137 138 139

	// Advance origin if needed
	// Note: The entire pipeline has the same origin
	// We just don't accept batches prior to the L1 origin of the L2 safe head
140 141
	if bq.origin != bq.prev.Origin() {
		bq.origin = bq.prev.Origin()
142
		if !originBehind {
143
			bq.l1Blocks = append(bq.l1Blocks, bq.origin)
144
		} else {
145 146 147 148
			// This is to handle the special case of startup. At startup we call Reset & include
			// the L1 origin. That is the only time where immediately after `Reset` is called
			// originBehind is false.
			bq.l1Blocks = bq.l1Blocks[:0]
149
		}
150
		bq.log.Info("Advancing bq origin", "origin", bq.origin, "originBehind", originBehind)
151 152 153 154 155 156 157
	}

	// Load more data into the batch queue
	outOfData := false
	if batch, err := bq.prev.NextBatch(ctx); err == io.EOF {
		outOfData = true
	} else if err != nil {
158
		return nil, false, err
159
	} else if !originBehind {
Tei Im's avatar
Tei Im committed
160
		bq.AddBatch(ctx, batch, parent)
161 162
	}

163 164
	// Skip adding data unless we are up to date with the origin, but do fully
	// empty the previous stages
165
	if originBehind {
166
		if outOfData {
167
			return nil, false, io.EOF
168
		} else {
169
			return nil, false, NotEnoughData
170 171 172
		}
	}

173
	// Finally attempt to derive more batches
Tei Im's avatar
Tei Im committed
174
	batch, err := bq.deriveNextBatch(ctx, outOfData, parent)
175
	if err == io.EOF && outOfData {
176
		return nil, false, io.EOF
177
	} else if err == io.EOF {
178
		return nil, false, NotEnoughData
179
	} else if err != nil {
180
		return nil, false, err
181
	}
182 183 184 185 186 187

	var nextBatch *SingularBatch
	switch batch.GetBatchType() {
	case SingularBatchType:
		singularBatch, ok := batch.(*SingularBatch)
		if !ok {
188
			return nil, false, NewCriticalError(errors.New("failed type assertion to SingularBatch"))
189 190 191 192 193
		}
		nextBatch = singularBatch
	case SpanBatchType:
		spanBatch, ok := batch.(*SpanBatch)
		if !ok {
194
			return nil, false, NewCriticalError(errors.New("failed type assertion to SpanBatch"))
195 196
		}
		// If next batch is SpanBatch, convert it to SingularBatches.
Tei Im's avatar
Tei Im committed
197
		singularBatches, err := spanBatch.GetSingularBatches(bq.l1Blocks, parent)
198
		if err != nil {
199
			return nil, false, NewCriticalError(err)
200 201
		}
		bq.nextSpan = singularBatches
202
		// span-batches are non-empty, so the below pop is safe.
Tei Im's avatar
Tei Im committed
203
		nextBatch = bq.popNextBatch(parent)
204
	default:
205
		return nil, false, NewCriticalError(fmt.Errorf("unrecognized batch type: %d", batch.GetBatchType()))
206 207
	}

Tei Im's avatar
Tei Im committed
208 209
	// If the nextBatch is derived from the span batch, len(bq.nextSpan) == 0 means it's the last batch of the span.
	// For singular batches, len(bq.nextSpan) == 0 is always true.
210
	return nextBatch, len(bq.nextSpan) == 0, nil
protolambda's avatar
protolambda committed
211 212
}

213
func (bq *BatchQueue) Reset(ctx context.Context, base eth.L1BlockRef, _ eth.SystemConfig) error {
214
	// Copy over the Origin from the next stage
215
	// It is set in the engine queue (two stages away) such that the L2 Safe Head origin is the progress
216
	bq.origin = base
217
	bq.batches = []*BatchWithL1InclusionBlock{}
218
	// Include the new origin as an origin to build on
219 220
	// Note: This is only for the initialization case. During normal resets we will later
	// throw out this block.
221
	bq.l1Blocks = bq.l1Blocks[:0]
222
	bq.l1Blocks = append(bq.l1Blocks, base)
223
	bq.nextSpan = bq.nextSpan[:0]
224 225 226
	return io.EOF
}

Tei Im's avatar
Tei Im committed
227
func (bq *BatchQueue) AddBatch(ctx context.Context, batch Batch, parent eth.L2BlockRef) {
228
	if len(bq.l1Blocks) == 0 {
229
		panic(fmt.Errorf("cannot add batch with timestamp %d, no origin was prepared", batch.GetTimestamp()))
protolambda's avatar
protolambda committed
230
	}
231
	data := BatchWithL1InclusionBlock{
232
		L1InclusionBlock: bq.origin,
233
		Batch:            batch,
protolambda's avatar
protolambda committed
234
	}
Tei Im's avatar
Tei Im committed
235
	validity := CheckBatch(ctx, bq.config, bq.log, bq.l1Blocks, parent, &data, bq.l2)
236 237
	if validity == BatchDrop {
		return // if we do drop the batch, CheckBatch will log the drop reason with WARN level.
protolambda's avatar
protolambda committed
238
	}
239 240
	batch.LogContext(bq.log).Debug("Adding batch")
	bq.batches = append(bq.batches, &data)
241 242
}

243 244 245 246
// deriveNextBatch derives the next batch to apply on top of the current L2 safe head,
// following the validity rules imposed on consecutive batches,
// based on currently available buffered batch and L1 origin information.
// If no batch can be derived yet, then (nil, io.EOF) is returned.
Tei Im's avatar
Tei Im committed
247
func (bq *BatchQueue) deriveNextBatch(ctx context.Context, outOfData bool, parent eth.L2BlockRef) (Batch, error) {
248
	if len(bq.l1Blocks) == 0 {
249
		return nil, NewCriticalError(errors.New("cannot derive next batch, no origin was prepared"))
250 251
	}
	epoch := bq.l1Blocks[0]
Tei Im's avatar
Tei Im committed
252
	bq.log.Trace("Deriving the next batch", "epoch", epoch, "parent", parent, "outOfData", outOfData)
253

254 255 256
	// Note: epoch origin can now be one block ahead of the L2 Safe Head
	// This is in the case where we auto generate all batches in an epoch & advance the epoch
	// but don't advance the L2 Safe Head's epoch
Tei Im's avatar
Tei Im committed
257 258
	if parent.L1Origin != epoch.ID() && parent.L1Origin.Number != epoch.Number-1 {
		return nil, NewResetError(fmt.Errorf("buffered L1 chain epoch %s in batch queue does not match safe head origin %s", epoch, parent.L1Origin))
259 260 261 262 263
	}

	// Find the first-seen batch that matches all validity conditions.
	// We may not have sufficient information to proceed filtering, and then we stop.
	// There may be none: in that case we force-create an empty batch
Tei Im's avatar
Tei Im committed
264
	nextTimestamp := parent.Time + bq.config.BlockTime
265 266 267 268 269 270
	var nextBatch *BatchWithL1InclusionBlock

	// Go over all batches, in order of inclusion, and find the first batch we can accept.
	// We filter in-place by only remembering the batches that may be processed in the future, or those we are undecided on.
	var remaining []*BatchWithL1InclusionBlock
batchLoop:
271
	for i, batch := range bq.batches {
Tei Im's avatar
Tei Im committed
272
		validity := CheckBatch(ctx, bq.config, bq.log.New("batch_index", i), bq.l1Blocks, parent, batch, bq.l2)
273 274
		switch validity {
		case BatchFuture:
275 276
			remaining = append(remaining, batch)
			continue
277
		case BatchDrop:
278
			batch.Batch.LogContext(bq.log).Warn("Dropping batch",
Tei Im's avatar
Tei Im committed
279 280
				"parent", parent.ID(),
				"parent_time", parent.Time,
281 282 283 284 285 286
			)
			continue
		case BatchAccept:
			nextBatch = batch
			// don't keep the current batch in the remaining items since we are processing it now,
			// but retain every batch we didn't get to yet.
287
			remaining = append(remaining, bq.batches[i+1:]...)
288 289
			break batchLoop
		case BatchUndecided:
290 291
			remaining = append(remaining, bq.batches[i:]...)
			bq.batches = remaining
292 293 294
			return nil, io.EOF
		default:
			return nil, NewCriticalError(fmt.Errorf("unknown batch validity type: %d", validity))
protolambda's avatar
protolambda committed
295
		}
296
	}
297
	bq.batches = remaining
298 299

	if nextBatch != nil {
300
		nextBatch.Batch.LogContext(bq.log).Info("Found next batch")
301
		return nextBatch.Batch, nil
protolambda's avatar
protolambda committed
302
	}
303

304
	// If the current epoch is too old compared to the L1 block we are at,
305
	// i.e. if the sequence window expired, we create empty batches for the current epoch
306
	expiryEpoch := epoch.Number + bq.config.SeqWindowSize
307
	forceEmptyBatches := (expiryEpoch == bq.origin.Number && outOfData) || expiryEpoch < bq.origin.Number
Tei Im's avatar
Tei Im committed
308
	firstOfEpoch := epoch.Number == parent.L1Origin.Number+1
309

310 311
	bq.log.Trace("Potentially generating an empty batch",
		"expiryEpoch", expiryEpoch, "forceEmptyBatches", forceEmptyBatches, "nextTimestamp", nextTimestamp,
312
		"epoch_time", epoch.Time, "len_l1_blocks", len(bq.l1Blocks), "firstOfEpoch", firstOfEpoch)
313

314
	if !forceEmptyBatches {
315 316
		// sequence window did not expire yet, still room to receive batches for the current epoch,
		// no need to force-create empty batch(es) towards the next epoch yet.
317
		return nil, io.EOF
protolambda's avatar
protolambda committed
318
	}
319 320
	if len(bq.l1Blocks) < 2 {
		// need next L1 block to proceed towards
321
		return nil, io.EOF
protolambda's avatar
protolambda committed
322 323
	}

324 325
	nextEpoch := bq.l1Blocks[1]
	// Fill with empty L2 blocks of the same epoch until we meet the time of the next L1 origin,
326 327 328
	// to preserve that L2 time >= L1 time. If this is the first block of the epoch, always generate a
	// batch to ensure that we at least have one batch per epoch.
	if nextTimestamp < nextEpoch.Time || firstOfEpoch {
Joshua Gutow's avatar
Joshua Gutow committed
329
		bq.log.Info("Generating next batch", "epoch", epoch, "timestamp", nextTimestamp)
330
		return &SingularBatch{
Tei Im's avatar
Tei Im committed
331
			ParentHash:   parent.Hash,
332 333 334 335 336
			EpochNum:     rollup.Epoch(epoch.Number),
			EpochHash:    epoch.Hash,
			Timestamp:    nextTimestamp,
			Transactions: nil,
		}, nil
337
	}
338 339 340

	// At this point we have auto generated every batch for the current epoch
	// that we can, so we can advance to the next epoch.
341
	bq.log.Trace("Advancing internal L1 blocks", "next_timestamp", nextTimestamp, "next_epoch_time", nextEpoch.Time)
342
	bq.l1Blocks = bq.l1Blocks[1:]
343
	return nil, io.EOF
protolambda's avatar
protolambda committed
344
}