-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathclsync_test.go
311 lines (266 loc) · 9.22 KB
/
clsync_test.go
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
package clsync
import (
"context"
"errors"
"io"
"math/big"
"math/rand"
"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-service/eth"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum-optimism/optimism/op-service/testutils"
)
type fakeEngine struct {
unsafe, safe, finalized eth.L2BlockRef
err error
}
func (f *fakeEngine) Finalized() eth.L2BlockRef {
return f.finalized
}
func (f *fakeEngine) UnsafeL2Head() eth.L2BlockRef {
return f.unsafe
}
func (f *fakeEngine) SafeL2Head() eth.L2BlockRef {
return f.safe
}
func (f *fakeEngine) InsertUnsafePayload(ctx context.Context, payload *eth.ExecutionPayloadEnvelope, ref eth.L2BlockRef) error {
if f.err != nil {
return f.err
}
f.unsafe = ref
return nil
}
var _ Engine = (*fakeEngine)(nil)
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)
eng := &fakeEngine{
unsafe: refA2,
safe: refA0,
finalized: refA0,
}
cl := NewCLSync(logger, cfg, metrics, eng)
cl.AddUnsafePayload(payloadA1)
require.NoError(t, cl.Proceed(context.Background()))
require.Nil(t, cl.unsafePayloads.Peek(), "pop because too old")
require.Equal(t, refA2, eng.unsafe, "keep unsafe head")
})
// 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)
eng := &fakeEngine{
unsafe: refA1,
safe: refA0,
finalized: refA0,
}
cl := NewCLSync(logger, cfg, metrics, eng)
cl.AddUnsafePayload(payloadA1)
require.NoError(t, cl.Proceed(context.Background()))
require.Nil(t, cl.unsafePayloads.Peek(), "pop because seen")
require.Equal(t, refA1, eng.unsafe, "keep unsafe head")
})
// 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)
eng := &fakeEngine{
unsafe: altRefA1,
safe: refA0,
finalized: refA0,
}
cl := NewCLSync(logger, cfg, metrics, eng)
cl.AddUnsafePayload(payloadA1)
require.NoError(t, cl.Proceed(context.Background()))
require.Nil(t, cl.unsafePayloads.Peek(), "pop because alternative")
require.Equal(t, altRefA1, eng.unsafe, "keep unsafe head")
})
t.Run("ignore unsafe reorg", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
eng := &fakeEngine{
unsafe: altRefA1,
safe: refA0,
finalized: refA0,
}
cl := NewCLSync(logger, cfg, metrics, eng)
cl.AddUnsafePayload(payloadA2)
require.ErrorIs(t, cl.Proceed(context.Background()), io.EOF, "payload2 does not fit onto alt1, thus retrieve next input from L1")
require.Nil(t, cl.unsafePayloads.Peek(), "pop because not applicable")
require.Equal(t, altRefA1, eng.unsafe, "keep unsafe head")
})
t.Run("success", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
eng := &fakeEngine{
unsafe: refA0,
safe: refA0,
finalized: refA0,
}
cl := NewCLSync(logger, cfg, metrics, eng)
require.ErrorIs(t, cl.Proceed(context.Background()), io.EOF, "nothing to process yet")
require.Nil(t, cl.unsafePayloads.Peek(), "no payloads yet")
cl.AddUnsafePayload(payloadA1)
lowest := cl.LowestQueuedUnsafeBlock()
require.Equal(t, refA1, lowest, "expecting A1 next")
require.NoError(t, cl.Proceed(context.Background()))
require.Nil(t, cl.unsafePayloads.Peek(), "pop because applied")
require.Equal(t, refA1, eng.unsafe, "new unsafe head")
cl.AddUnsafePayload(payloadA2)
lowest = cl.LowestQueuedUnsafeBlock()
require.Equal(t, refA2, lowest, "expecting A2 next")
require.NoError(t, cl.Proceed(context.Background()))
require.Nil(t, cl.unsafePayloads.Peek(), "pop because applied")
require.Equal(t, refA2, eng.unsafe, "new unsafe head")
})
t.Run("double buffer", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
eng := &fakeEngine{
unsafe: refA0,
safe: refA0,
finalized: refA0,
}
cl := NewCLSync(logger, cfg, metrics, eng)
cl.AddUnsafePayload(payloadA1)
cl.AddUnsafePayload(payloadA2)
lowest := cl.LowestQueuedUnsafeBlock()
require.Equal(t, refA1, lowest, "expecting A1 next")
require.NoError(t, cl.Proceed(context.Background()))
require.NotNil(t, cl.unsafePayloads.Peek(), "next is ready")
require.Equal(t, refA1, eng.unsafe, "new unsafe head")
require.NoError(t, cl.Proceed(context.Background()))
require.Nil(t, cl.unsafePayloads.Peek(), "done")
require.Equal(t, refA2, eng.unsafe, "new unsafe head")
})
t.Run("temporary error", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
eng := &fakeEngine{
unsafe: refA0,
safe: refA0,
finalized: refA0,
}
cl := NewCLSync(logger, cfg, metrics, eng)
testErr := derive.NewTemporaryError(errors.New("test error"))
eng.err = testErr
cl.AddUnsafePayload(payloadA1)
require.ErrorIs(t, cl.Proceed(context.Background()), testErr)
require.Equal(t, refA0, eng.unsafe, "old unsafe head after error")
require.NotNil(t, cl.unsafePayloads.Peek(), "no pop because temporary error")
eng.err = nil
require.NoError(t, cl.Proceed(context.Background()))
require.Equal(t, refA1, eng.unsafe, "new unsafe head after resolved error")
require.Nil(t, cl.unsafePayloads.Peek(), "pop because valid")
})
t.Run("invalid payload error", func(t *testing.T) {
logger := testlog.Logger(t, log.LevelError)
eng := &fakeEngine{
unsafe: refA0,
safe: refA0,
finalized: refA0,
}
cl := NewCLSync(logger, cfg, metrics, eng)
testErr := errors.New("test error")
eng.err = testErr
cl.AddUnsafePayload(payloadA1)
require.ErrorIs(t, cl.Proceed(context.Background()), testErr)
require.Equal(t, refA0, eng.unsafe, "old unsafe head after error")
require.Nil(t, cl.unsafePayloads.Peek(), "pop because invalid")
})
}