batch_queue_test.go 8.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
package derive

import (
	"context"
	"io"
	"math/rand"
	"testing"

	"github.com/ethereum-optimism/optimism/op-node/eth"
	"github.com/ethereum-optimism/optimism/op-node/rollup"
	"github.com/ethereum-optimism/optimism/op-node/testlog"
	"github.com/ethereum-optimism/optimism/op-node/testutils"
	"github.com/ethereum/go-ethereum"
	"github.com/ethereum/go-ethereum/common/hexutil"
	"github.com/ethereum/go-ethereum/log"
	"github.com/stretchr/testify/require"
)

// fakeBatchQueueOutput fakes the next stage (receive only) for the batch queue
// It tracks the progress state of the next stage.
// Upon receiving a batch, relevant characteristic of safeL2Head are immediately advanced.
type fakeBatchQueueOutput struct {
	progress   Progress
	batches    []*BatchData
	safeL2Head eth.L2BlockRef
}

var _ BatchQueueOutput = (*fakeBatchQueueOutput)(nil)

func (f *fakeBatchQueueOutput) AddBatch(batch *BatchData) {
	f.batches = append(f.batches, batch)
	// Advance SafeL2Head
	f.safeL2Head.Time = batch.Timestamp
	f.safeL2Head.L1Origin.Number = uint64(batch.EpochNum)
}

func (f *fakeBatchQueueOutput) SafeL2Head() eth.L2BlockRef {
	return f.safeL2Head
}

func (f *fakeBatchQueueOutput) Progress() Progress {
	return f.progress
}

func b(timestamp uint64, epoch eth.L1BlockRef) *BatchData {
	rng := rand.New(rand.NewSource(int64(timestamp)))
	data := testutils.RandomData(rng, 20)
	return &BatchData{BatchV1{
		Timestamp:    timestamp,
		EpochNum:     rollup.Epoch(epoch.Number),
		EpochHash:    epoch.Hash,
		Transactions: []hexutil.Bytes{data},
	}}
}

func L1Chain(l1Times []uint64) []eth.L1BlockRef {
	var out []eth.L1BlockRef
	var parentHash [32]byte
	for i, time := range l1Times {
		hash := [32]byte{byte(i)}
		out = append(out, eth.L1BlockRef{
			Hash:       hash,
			Number:     uint64(i),
			ParentHash: parentHash,
			Time:       time,
		})
		parentHash = hash
	}
	return out
}

type fakeL1Fetcher struct {
	l1 []eth.L1BlockRef
}

func (f *fakeL1Fetcher) L1BlockRefByNumber(_ context.Context, n uint64) (eth.L1BlockRef, error) {
	if n >= uint64(len(f.l1)) {
		return eth.L1BlockRef{}, ethereum.NotFound
	}
	return f.l1[int(n)], nil
}

func TestBatchQueueEager(t *testing.T) {
	log := testlog.Logger(t, log.LvlTrace)
	next := &fakeBatchQueueOutput{
		safeL2Head: eth.L2BlockRef{
			Number:   0,
			Time:     10,
			L1Origin: eth.BlockID{Number: 0},
		},
	}
	cfg := &rollup.Config{
		Genesis: rollup.Genesis{
			L2Time: 10,
		},
		BlockTime:         2,
		MaxSequencerDrift: 600,
		SeqWindowSize:     30,
	}

	l1 := L1Chain([]uint64{10, 20, 30})

	fetcher := fakeL1Fetcher{l1: l1}
	bq := NewBatchQueue(log, cfg, &fetcher, next)

	prevProgress := Progress{
		Origin: l1[0],
		Closed: false,
	}

	// Setup progress
	bq.progress.Closed = true
	err := bq.Step(context.Background(), prevProgress)
	require.Nil(t, err)

	// Add batches
	batches := []*BatchData{b(12, l1[0]), b(14, l1[0])}
	for _, batch := range batches {
119
		bq.AddBatch(batch)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	}
	// Step
	for {
		if err := bq.Step(context.Background(), prevProgress); err == io.EOF {
			break
		} else {
			require.Nil(t, err)
		}
	}
	// Verify Output
	require.Equal(t, batches, next.batches)
}

func TestBatchQueueFull(t *testing.T) {
	log := testlog.Logger(t, log.LvlTrace)
	next := &fakeBatchQueueOutput{
		safeL2Head: eth.L2BlockRef{
			Number:   0,
			Time:     10,
			L1Origin: eth.BlockID{Number: 0},
		},
	}
	cfg := &rollup.Config{
		Genesis: rollup.Genesis{
			L2Time: 10,
		},
		BlockTime:         2,
		MaxSequencerDrift: 600,
		SeqWindowSize:     2,
	}

	l1 := L1Chain([]uint64{10, 15, 20})

	fetcher := fakeL1Fetcher{l1: l1}
	bq := NewBatchQueue(log, cfg, &fetcher, next)

	// Start with open previous & closed self.
	// Then this stage is opened at the first step.
	bq.progress.Closed = true
	prevProgress := Progress{
		Origin: l1[0],
		Closed: false,
	}

	// Do the bq open
	err := bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, false)

	// Add batches
	batches := []*BatchData{b(14, l1[0]), b(16, l1[0]), b(18, l1[1])}
	for _, batch := range batches {
172
		bq.AddBatch(batch)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	}
	// Missing first batch
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, io.EOF)

	// Close previous to close bq
	prevProgress.Closed = true
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, true)

	// Open previous to open bq with the new inclusion block
	prevProgress.Closed = false
	prevProgress.Origin = l1[1]
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, false)

	// Close previous to close bq (for epoch 2)
	prevProgress.Closed = true
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, true)

	// Open previous to open bq with the new inclusion block (epoch 2)
	prevProgress.Closed = false
	prevProgress.Origin = l1[2]
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, false)

	// Finally add batch
	firstBatch := b(12, l1[0])
206
	bq.AddBatch(firstBatch)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

	// Close the origin
	prevProgress.Closed = true
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, true)

	// Step, but should have full epoch now
	for {
		if err := bq.Step(context.Background(), prevProgress); err == io.EOF {
			break
		} else {
			require.Nil(t, err)
		}
	}
	// Verify Output
	var final []*BatchData
	final = append(final, firstBatch)
	final = append(final, batches...)
	require.Equal(t, final, next.batches)
}

func TestBatchQueueMissing(t *testing.T) {
	log := testlog.Logger(t, log.LvlTrace)
	next := &fakeBatchQueueOutput{
		safeL2Head: eth.L2BlockRef{
			Number:   0,
			Time:     10,
			L1Origin: eth.BlockID{Number: 0},
		},
	}
	cfg := &rollup.Config{
		Genesis: rollup.Genesis{
			L2Time: 10,
		},
		BlockTime:         2,
		MaxSequencerDrift: 600,
		SeqWindowSize:     2,
	}

	l1 := L1Chain([]uint64{10, 15, 20})

	fetcher := fakeL1Fetcher{l1: l1}
	bq := NewBatchQueue(log, cfg, &fetcher, next)

	// Start with open previous & closed self.
	// Then this stage is opened at the first step.
	bq.progress.Closed = true
	prevProgress := Progress{
		Origin: l1[0],
		Closed: false,
	}

	// Do the bq open
	err := bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, false)

	// Add batches
	// NB: The batch at 18 is skipped to skip over the ability to
	// do eager batch processing for that batch. This test checks
	// that batch timestamp 12 & 14 is created & 16 is used.
	batches := []*BatchData{b(16, l1[0]), b(20, l1[1])}
	for _, batch := range batches {
271
		bq.AddBatch(batch)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	}
	// Missing first batch
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, io.EOF)

	// Close previous to close bq
	prevProgress.Closed = true
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, true)

	// Open previous to open bq with the new inclusion block
	prevProgress.Closed = false
	prevProgress.Origin = l1[1]
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, false)

	// Close previous to close bq (for epoch 2)
	prevProgress.Closed = true
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, true)

	// Open previous to open bq with the new inclusion block (epoch 2)
	prevProgress.Closed = false
	prevProgress.Origin = l1[2]
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, false)

	// Close the origin
	prevProgress.Closed = true
	err = bq.Step(context.Background(), prevProgress)
	require.Equal(t, err, nil)
	require.Equal(t, bq.progress.Closed, true)

	// Step, but should have full epoch now + fill missing
	for {
		if err := bq.Step(context.Background(), prevProgress); err == io.EOF {
			break
		} else {
			require.Nil(t, err)
		}
	}
	// TODO: Maybe check actuall batch validity better
	require.Equal(t, 3, len(next.batches))
}