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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package attributes
import (
"context"
"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 TestAttributesHandler(t *testing.T) {
rng := rand.New(rand.NewSource(1234))
refA := testutils.RandomBlockRef(rng)
refB := eth.L1BlockRef{
Hash: testutils.RandomHash(rng),
Number: refA.Number + 1,
ParentHash: refA.Hash,
Time: refA.Time + 12,
}
// Copy with different hash, as alternative where the alt-L2 block may come from
refBAlt := refB
refBAlt.Hash = testutils.RandomHash(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,
}
refA0Alt := 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{31: 123},
Scalar: [32]byte{0: 0, 31: 42},
GasLimit: 20_000_000,
},
},
BlockTime: 1,
SeqWindowSize: 2,
RegolithTime: new(uint64),
CanyonTime: new(uint64),
EcotoneTime: new(uint64),
}
a1L1Info, err := derive.L1InfoDepositBytes(cfg, cfg.Genesis.SystemConfig, 1, aL1Info, refA0.Time+cfg.BlockTime)
require.NoError(t, err)
parentBeaconBlockRoot := testutils.RandomHash(rng)
payloadA1 := ð.ExecutionPayloadEnvelope{ExecutionPayload: ð.ExecutionPayload{
ParentHash: refA0.Hash,
FeeRecipient: common.Address{},
StateRoot: eth.Bytes32{},
ReceiptsRoot: eth.Bytes32{},
LogsBloom: eth.Bytes256{},
PrevRandao: eth.Bytes32{},
BlockNumber: eth.Uint64Quantity(refA0.Number + 1),
GasLimit: gasLimit,
GasUsed: 0,
Timestamp: eth.Uint64Quantity(refA0.Time + cfg.BlockTime),
ExtraData: nil,
BaseFeePerGas: eth.Uint256Quantity(*uint256.NewInt(7)),
BlockHash: common.Hash{},
Transactions: []eth.Data{a1L1Info},
}, ParentBeaconBlockRoot: &parentBeaconBlockRoot}
// fix up the block-hash
payloadA1.ExecutionPayload.BlockHash, _ = payloadA1.CheckBlockHash()
attrA1 := &derive.AttributesWithParent{
Attributes: ð.PayloadAttributes{
Timestamp: payloadA1.ExecutionPayload.Timestamp,
PrevRandao: payloadA1.ExecutionPayload.PrevRandao,
SuggestedFeeRecipient: payloadA1.ExecutionPayload.FeeRecipient,
Withdrawals: payloadA1.ExecutionPayload.Withdrawals,
ParentBeaconBlockRoot: payloadA1.ParentBeaconBlockRoot,
Transactions: []eth.Data{a1L1Info},
NoTxPool: false,
GasLimit: &payloadA1.ExecutionPayload.GasLimit,
},
Parent: refA0,
Concluding: true,
DerivedFrom: refB,
}
refA1, err := derive.PayloadToBlockRef(cfg, payloadA1.ExecutionPayload)
require.NoError(t, err)
payloadA1Alt := ð.ExecutionPayloadEnvelope{ExecutionPayload: ð.ExecutionPayload{
ParentHash: refA0.Hash,
FeeRecipient: common.Address{0xde, 0xea}, // change of the alternative payload
StateRoot: eth.Bytes32{},
ReceiptsRoot: eth.Bytes32{},
LogsBloom: eth.Bytes256{},
PrevRandao: eth.Bytes32{},
BlockNumber: eth.Uint64Quantity(refA0.Number + 1),
GasLimit: gasLimit,
GasUsed: 0,
Timestamp: eth.Uint64Quantity(refA0.Time + cfg.BlockTime),
ExtraData: nil,
BaseFeePerGas: eth.Uint256Quantity(*uint256.NewInt(7)),
BlockHash: common.Hash{},
Transactions: []eth.Data{a1L1Info},
}, ParentBeaconBlockRoot: &parentBeaconBlockRoot}
// fix up the block-hash
payloadA1Alt.ExecutionPayload.BlockHash, _ = payloadA1Alt.CheckBlockHash()
attrA1Alt := &derive.AttributesWithParent{
Attributes: ð.PayloadAttributes{
Timestamp: payloadA1Alt.ExecutionPayload.Timestamp,
PrevRandao: payloadA1Alt.ExecutionPayload.PrevRandao,
SuggestedFeeRecipient: payloadA1Alt.ExecutionPayload.FeeRecipient,
Withdrawals: payloadA1Alt.ExecutionPayload.Withdrawals,
ParentBeaconBlockRoot: payloadA1Alt.ParentBeaconBlockRoot,
Transactions: []eth.Data{a1L1Info},
NoTxPool: false,
GasLimit: &payloadA1Alt.ExecutionPayload.GasLimit,
},
Parent: refA0,
Concluding: true,
DerivedFrom: refBAlt,
}
refA1Alt, err := derive.PayloadToBlockRef(cfg, payloadA1Alt.ExecutionPayload)
require.NoError(t, err)
t.Run("drop invalid attributes", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelInfo)
l2 := &testutils.MockL2Client{}
emitter := &testutils.MockEmitter{}
ah := NewAttributesHandler(logger, cfg, context.Background(), l2)
ah.AttachEmitter(emitter)
emitter.ExpectOnce(derive.ConfirmReceivedAttributesEvent{})
emitter.ExpectOnce(engine.PendingSafeRequestEvent{})
ah.OnEvent(derive.DerivedAttributesEvent{
Attributes: attrA1,
})
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes, "queue the invalid attributes")
emitter.ExpectOnce(engine.PendingSafeRequestEvent{})
ah.OnEvent(engine.InvalidPayloadAttributesEvent{
Attributes: attrA1,
})
emitter.AssertExpectations(t)
require.Nil(t, ah.attributes, "drop the invalid attributes")
})
t.Run("drop stale attributes", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelInfo)
l2 := &testutils.MockL2Client{}
emitter := &testutils.MockEmitter{}
ah := NewAttributesHandler(logger, cfg, context.Background(), l2)
ah.AttachEmitter(emitter)
emitter.ExpectOnce(derive.ConfirmReceivedAttributesEvent{})
emitter.ExpectOnce(engine.PendingSafeRequestEvent{})
ah.OnEvent(derive.DerivedAttributesEvent{
Attributes: attrA1,
})
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes)
// New attributes will have to get generated after processing the last ones
emitter.ExpectOnce(derive.PipelineStepEvent{PendingSafe: refA1Alt})
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA1Alt,
Unsafe: refA1Alt,
})
l2.AssertExpectations(t)
emitter.AssertExpectations(t)
require.Nil(t, ah.attributes, "drop stale attributes")
})
t.Run("pending gets reorged", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelInfo)
l2 := &testutils.MockL2Client{}
emitter := &testutils.MockEmitter{}
ah := NewAttributesHandler(logger, cfg, context.Background(), l2)
ah.AttachEmitter(emitter)
emitter.ExpectOnce(derive.ConfirmReceivedAttributesEvent{})
emitter.ExpectOnce(engine.PendingSafeRequestEvent{})
ah.OnEvent(derive.DerivedAttributesEvent{
Attributes: attrA1,
})
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes)
emitter.ExpectOnceType("ResetEvent")
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA0Alt,
Unsafe: refA0Alt,
})
l2.AssertExpectations(t)
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes, "detected reorg does not clear state, reset is required")
})
t.Run("pending older than unsafe", func(t *testing.T) {
t.Run("consolidation fails", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelInfo)
l2 := &testutils.MockL2Client{}
emitter := &testutils.MockEmitter{}
ah := NewAttributesHandler(logger, cfg, context.Background(), l2)
ah.AttachEmitter(emitter)
// attrA1Alt does not match block A1, so will cause force-reorg.
emitter.ExpectOnce(derive.ConfirmReceivedAttributesEvent{})
emitter.ExpectOnce(engine.PendingSafeRequestEvent{})
ah.OnEvent(derive.DerivedAttributesEvent{Attributes: attrA1Alt})
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes, "queued up derived attributes")
// Call during consolidation.
// The payloadA1 is going to get reorged out in favor of attrA1Alt (turns into payloadA1Alt)
l2.ExpectPayloadByNumber(refA1.Number, payloadA1, nil)
// fail consolidation, perform force reorg
emitter.ExpectOnce(engine.BuildStartEvent{Attributes: attrA1Alt})
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA0,
Unsafe: refA1,
})
l2.AssertExpectations(t)
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes, "still have attributes, processing still unconfirmed")
emitter.ExpectOnce(derive.PipelineStepEvent{PendingSafe: refA1Alt})
// recognize reorg as complete
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA1Alt,
Unsafe: refA1Alt,
})
emitter.AssertExpectations(t)
require.Nil(t, ah.attributes, "drop when attributes are successful")
})
t.Run("consolidation passes", func(t *testing.T) {
fn := func(t *testing.T, concluding bool) {
logger := testlog.Logger(t, log.LevelInfo)
l2 := &testutils.MockL2Client{}
emitter := &testutils.MockEmitter{}
ah := NewAttributesHandler(logger, cfg, context.Background(), l2)
ah.AttachEmitter(emitter)
attr := &derive.AttributesWithParent{
Attributes: attrA1.Attributes, // attributes will match, passing consolidation
Parent: attrA1.Parent,
Concluding: concluding,
DerivedFrom: refB,
}
emitter.ExpectOnce(derive.ConfirmReceivedAttributesEvent{})
emitter.ExpectOnce(engine.PendingSafeRequestEvent{})
ah.OnEvent(derive.DerivedAttributesEvent{Attributes: attr})
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes, "queued up derived attributes")
// Call during consolidation.
l2.ExpectPayloadByNumber(refA1.Number, payloadA1, nil)
emitter.ExpectOnce(engine.PromotePendingSafeEvent{
Ref: refA1,
Concluding: concluding,
DerivedFrom: refB,
})
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA0,
Unsafe: refA1,
})
l2.AssertExpectations(t)
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes, "still have attributes, processing still unconfirmed")
emitter.ExpectOnce(derive.PipelineStepEvent{PendingSafe: refA1})
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA1,
Unsafe: refA1,
})
emitter.AssertExpectations(t)
require.Nil(t, ah.attributes, "drop when attributes are successful")
}
t.Run("is last span", func(t *testing.T) {
fn(t, true)
})
t.Run("is not last span", func(t *testing.T) {
fn(t, false)
})
})
})
t.Run("pending equals unsafe", func(t *testing.T) {
// no consolidation to do, just force next attributes on tip of chain
logger := testlog.Logger(t, log.LevelInfo)
l2 := &testutils.MockL2Client{}
emitter := &testutils.MockEmitter{}
ah := NewAttributesHandler(logger, cfg, context.Background(), l2)
ah.AttachEmitter(emitter)
emitter.ExpectOnce(derive.ConfirmReceivedAttributesEvent{})
emitter.ExpectOnce(engine.PendingSafeRequestEvent{})
ah.OnEvent(derive.DerivedAttributesEvent{Attributes: attrA1Alt})
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes, "queued up derived attributes")
// sanity check test setup
require.True(t, attrA1Alt.Concluding, "must be concluding attributes")
// attrA1Alt will fit right on top of A0
emitter.ExpectOnce(engine.BuildStartEvent{Attributes: attrA1Alt})
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA0,
Unsafe: refA0,
})
l2.AssertExpectations(t)
emitter.AssertExpectations(t)
require.NotNil(t, ah.attributes)
emitter.ExpectOnce(derive.PipelineStepEvent{PendingSafe: refA1Alt})
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA1Alt,
Unsafe: refA1Alt,
})
emitter.AssertExpectations(t)
require.Nil(t, ah.attributes, "clear attributes after successful processing")
})
t.Run("pending ahead of unsafe", func(t *testing.T) {
// Legacy test case: if attributes fit on top of the pending safe block as expected,
// but if the unsafe block is older, then we can recover by resetting.
logger := testlog.Logger(t, log.LevelInfo)
l2 := &testutils.MockL2Client{}
emitter := &testutils.MockEmitter{}
ah := NewAttributesHandler(logger, cfg, context.Background(), l2)
ah.AttachEmitter(emitter)
emitter.ExpectOnceType("ResetEvent")
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA1,
Unsafe: refA0,
})
emitter.AssertExpectations(t)
l2.AssertExpectations(t)
})
t.Run("no attributes", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelInfo)
l2 := &testutils.MockL2Client{}
emitter := &testutils.MockEmitter{}
ah := NewAttributesHandler(logger, cfg, context.Background(), l2)
ah.AttachEmitter(emitter)
// If there are no attributes, we expect the pipeline to be requested to generate attributes.
emitter.ExpectOnce(derive.PipelineStepEvent{PendingSafe: refA1})
ah.OnEvent(engine.PendingSafeUpdateEvent{
PendingSafe: refA1,
Unsafe: refA1,
})
// no calls to L2 or emitter when there is nothing to process
l2.AssertExpectations(t)
emitter.AssertExpectations(t)
})
}