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
119
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
172
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
206
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
271
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package clsync
import (
"errors"
"math/big"
"math/rand" // nosemgrep
"testing"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-node/rollup/engine"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum-optimism/optimism/op-service/testutils"
)
func TestCLSync(t *testing.T) {
rng := rand.New(rand.NewSource(1234))
refA := testutils.RandomBlockRef(rng)
aL1Info := &testutils.MockBlockInfo{
InfoParentHash: refA.ParentHash,
InfoNum: refA.Number,
InfoTime: refA.Time,
InfoHash: refA.Hash,
InfoBaseFee: big.NewInt(1),
InfoBlobBaseFee: big.NewInt(1),
InfoReceiptRoot: types.EmptyRootHash,
InfoRoot: testutils.RandomHash(rng),
InfoGasUsed: rng.Uint64(),
}
refA0 := eth.L2BlockRef{
Hash: testutils.RandomHash(rng),
Number: 0,
ParentHash: common.Hash{},
Time: refA.Time,
L1Origin: refA.ID(),
SequenceNumber: 0,
}
gasLimit := eth.Uint64Quantity(20_000_000)
cfg := &rollup.Config{
Genesis: rollup.Genesis{
L1: refA.ID(),
L2: refA0.ID(),
L2Time: refA0.Time,
SystemConfig: eth.SystemConfig{
BatcherAddr: common.Address{42},
Overhead: [32]byte{123},
Scalar: [32]byte{42},
GasLimit: 20_000_000,
},
},
BlockTime: 1,
SeqWindowSize: 2,
}
refA1 := eth.L2BlockRef{
Hash: testutils.RandomHash(rng),
Number: refA0.Number + 1,
ParentHash: refA0.Hash,
Time: refA0.Time + cfg.BlockTime,
L1Origin: refA.ID(),
SequenceNumber: 1,
}
altRefA1 := refA1
altRefA1.Hash = testutils.RandomHash(rng)
refA2 := eth.L2BlockRef{
Hash: testutils.RandomHash(rng),
Number: refA1.Number + 1,
ParentHash: refA1.Hash,
Time: refA1.Time + cfg.BlockTime,
L1Origin: refA.ID(),
SequenceNumber: 2,
}
a1L1Info, err := derive.L1InfoDepositBytes(cfg, cfg.Genesis.SystemConfig, refA1.SequenceNumber, aL1Info, refA1.Time)
require.NoError(t, err)
payloadA1 := ð.ExecutionPayloadEnvelope{ExecutionPayload: ð.ExecutionPayload{
ParentHash: refA1.ParentHash,
FeeRecipient: common.Address{},
StateRoot: eth.Bytes32{},
ReceiptsRoot: eth.Bytes32{},
LogsBloom: eth.Bytes256{},
PrevRandao: eth.Bytes32{},
BlockNumber: eth.Uint64Quantity(refA1.Number),
GasLimit: gasLimit,
GasUsed: 0,
Timestamp: eth.Uint64Quantity(refA1.Time),
ExtraData: nil,
BaseFeePerGas: eth.Uint256Quantity(*uint256.NewInt(7)),
BlockHash: refA1.Hash,
Transactions: []eth.Data{a1L1Info},
}}
a2L1Info, err := derive.L1InfoDepositBytes(cfg, cfg.Genesis.SystemConfig, refA2.SequenceNumber, aL1Info, refA2.Time)
require.NoError(t, err)
payloadA2 := ð.ExecutionPayloadEnvelope{ExecutionPayload: ð.ExecutionPayload{
ParentHash: refA2.ParentHash,
FeeRecipient: common.Address{},
StateRoot: eth.Bytes32{},
ReceiptsRoot: eth.Bytes32{},
LogsBloom: eth.Bytes256{},
PrevRandao: eth.Bytes32{},
BlockNumber: eth.Uint64Quantity(refA2.Number),
GasLimit: gasLimit,
GasUsed: 0,
Timestamp: eth.Uint64Quantity(refA2.Time),
ExtraData: nil,
BaseFeePerGas: eth.Uint256Quantity(*uint256.NewInt(7)),
BlockHash: refA2.Hash,
Transactions: []eth.Data{a2L1Info},
}}
metrics := &testutils.TestDerivationMetrics{}
// When a previously received unsafe block is older than the tip of the chain, we want to drop it.
t.Run("drop old", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
emitter := &testutils.MockEmitter{}
cl := NewCLSync(logger, cfg, metrics)
cl.AttachEmitter(emitter)
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA1})
emitter.AssertExpectations(t)
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA2,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t) // no new events expected to be emitted
require.Nil(t, cl.unsafePayloads.Peek(), "pop because too old")
})
// When we already have the exact payload as tip, then no need to process it
t.Run("drop equal", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
emitter := &testutils.MockEmitter{}
cl := NewCLSync(logger, cfg, metrics)
cl.AttachEmitter(emitter)
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA1})
emitter.AssertExpectations(t)
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA1,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t) // no new events expected to be emitted
require.Nil(t, cl.unsafePayloads.Peek(), "pop because seen")
})
// When we have a different payload, at the same height, then we want to keep it.
// The unsafe chain consensus preserves the first-seen payload.
t.Run("ignore conflict", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
emitter := &testutils.MockEmitter{}
cl := NewCLSync(logger, cfg, metrics)
cl.AttachEmitter(emitter)
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA1})
emitter.AssertExpectations(t)
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: altRefA1,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t) // no new events expected to be emitted
require.Nil(t, cl.unsafePayloads.Peek(), "pop because alternative")
})
t.Run("ignore unsafe reorg", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
emitter := &testutils.MockEmitter{}
cl := NewCLSync(logger, cfg, metrics)
cl.AttachEmitter(emitter)
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA2})
emitter.AssertExpectations(t)
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: altRefA1,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t) // no new events expected, since A2 does not fit onto altA1
require.Nil(t, cl.unsafePayloads.Peek(), "pop because not applicable")
})
t.Run("success", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
emitter := &testutils.MockEmitter{}
cl := NewCLSync(logger, cfg, metrics)
cl.AttachEmitter(emitter)
emitter.AssertExpectations(t) // nothing to process yet
require.Nil(t, cl.unsafePayloads.Peek(), "no payloads yet")
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA1})
emitter.AssertExpectations(t)
lowest := cl.LowestQueuedUnsafeBlock()
require.Equal(t, refA1, lowest, "expecting A1 next")
// payload A1 should be possible to process on top of A0
emitter.ExpectOnce(engine.ProcessUnsafePayloadEvent{Envelope: payloadA1})
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA0,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t)
// now pretend the payload was processed: we can drop A1 now
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA1,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
require.Nil(t, cl.unsafePayloads.Peek(), "pop because applied")
// repeat for A2
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA2})
emitter.AssertExpectations(t)
lowest = cl.LowestQueuedUnsafeBlock()
require.Equal(t, refA2, lowest, "expecting A2 next")
emitter.ExpectOnce(engine.ProcessUnsafePayloadEvent{Envelope: payloadA2})
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA1,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t)
// now pretend the payload was processed: we can drop A2 now
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA2,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
require.Nil(t, cl.unsafePayloads.Peek(), "pop because applied")
})
t.Run("double buffer", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
emitter := &testutils.MockEmitter{}
cl := NewCLSync(logger, cfg, metrics)
cl.AttachEmitter(emitter)
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA1})
emitter.AssertExpectations(t)
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA2})
emitter.AssertExpectations(t)
lowest := cl.LowestQueuedUnsafeBlock()
require.Equal(t, refA1, lowest, "expecting A1 next")
emitter.ExpectOnce(engine.ProcessUnsafePayloadEvent{Envelope: payloadA1})
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA0,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t)
require.Equal(t, 2, cl.unsafePayloads.Len(), "still holding on to A1, and queued A2")
// Now pretend the payload was processed: we can drop A1 now.
// The CL-sync will try to immediately continue with A2.
emitter.ExpectOnce(engine.ProcessUnsafePayloadEvent{Envelope: payloadA2})
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA1,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t)
// now pretend the payload was processed: we can drop A2 now
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA2,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
require.Nil(t, cl.unsafePayloads.Peek(), "done")
})
t.Run("temporary error", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
emitter := &testutils.MockEmitter{}
cl := NewCLSync(logger, cfg, metrics)
cl.AttachEmitter(emitter)
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA1})
emitter.AssertExpectations(t)
emitter.ExpectOnce(engine.ProcessUnsafePayloadEvent{Envelope: payloadA1})
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA0,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t)
// On temporary errors we don't need any feedback from the engine.
// We just hold on to what payloads there are in the queue.
require.NotNil(t, cl.unsafePayloads.Peek(), "no pop because temporary error")
// Pretend we are still stuck on the same forkchoice. The CL-sync will retry sneding the payload.
emitter.ExpectOnce(engine.ProcessUnsafePayloadEvent{Envelope: payloadA1})
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA0,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t)
require.NotNil(t, cl.unsafePayloads.Peek(), "no pop because retry still unconfirmed")
// Now confirm we got the payload this time
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA1,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
require.Nil(t, cl.unsafePayloads.Peek(), "pop because valid")
})
t.Run("invalid payload error", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
emitter := &testutils.MockEmitter{}
cl := NewCLSync(logger, cfg, metrics)
cl.AttachEmitter(emitter)
// CLSync gets payload and requests engine state, to later determine if payload should be forwarded
emitter.ExpectOnce(engine.ForkchoiceRequestEvent{})
cl.OnEvent(ReceivedUnsafePayloadEvent{Envelope: payloadA1})
emitter.AssertExpectations(t)
// Engine signals, CLSync sends the payload
emitter.ExpectOnce(engine.ProcessUnsafePayloadEvent{Envelope: payloadA1})
cl.OnEvent(engine.ForkchoiceUpdateEvent{
UnsafeL2Head: refA0,
SafeL2Head: refA0,
FinalizedL2Head: refA0,
})
emitter.AssertExpectations(t)
// Pretend the payload is bad. It should not be retried after this.
cl.OnEvent(engine.PayloadInvalidEvent{Envelope: payloadA1, Err: errors.New("test err")})
emitter.AssertExpectations(t)
require.Nil(t, cl.unsafePayloads.Peek(), "pop because invalid")
})
}