forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdamgr_test.go
More file actions
446 lines (359 loc) · 14.9 KB
/
damgr_test.go
File metadata and controls
446 lines (359 loc) · 14.9 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package altda
import (
"context"
"math/big"
"math/rand"
"testing"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func RandomData(rng *rand.Rand, size int) []byte {
out := make([]byte, size)
rng.Read(out)
return out
}
func RandomCommitment(rng *rand.Rand) CommitmentData {
return NewKeccak256Commitment(RandomData(rng, 32))
}
func l1Ref(n uint64) eth.L1BlockRef {
return eth.L1BlockRef{Number: n}
}
func bID(n uint64) eth.BlockID {
return eth.BlockID{Number: n}
}
// TestFinalization checks that the finalized L1 block ref is returned correctly when pruning with and without challenges
func TestFinalization(t *testing.T) {
logger := testlog.Logger(t, log.LevelInfo)
cfg := Config{
ResolveWindow: 6,
ChallengeWindow: 6,
}
rng := rand.New(rand.NewSource(1234))
state := NewState(logger, &NoopMetrics{}, cfg)
c1 := RandomCommitment(rng)
bn1 := uint64(2)
// Track a commitment without a challenge
state.TrackCommitment(c1, l1Ref(bn1))
require.NoError(t, state.ExpireCommitments(bID(7)))
require.Empty(t, state.expiredCommitments)
require.NoError(t, state.ExpireCommitments(bID(8)))
require.Empty(t, state.commitments)
state.Prune(bID(bn1))
require.Equal(t, eth.L1BlockRef{}, state.lastPrunedCommitment)
state.Prune(bID(7))
require.Equal(t, eth.L1BlockRef{}, state.lastPrunedCommitment)
state.Prune(bID(8))
require.Equal(t, l1Ref(bn1), state.lastPrunedCommitment)
// Track a commitment, challenge it, & then resolve it
c2 := RandomCommitment(rng)
bn2 := uint64(20)
state.TrackCommitment(c2, l1Ref(bn2))
require.Equal(t, ChallengeUninitialized, state.GetChallengeStatus(c2, bn2))
state.CreateChallenge(c2, bID(24), bn2)
require.Equal(t, ChallengeActive, state.GetChallengeStatus(c2, bn2))
require.NoError(t, state.ResolveChallenge(c2, bID(30), bn2, nil))
require.Equal(t, ChallengeResolved, state.GetChallengeStatus(c2, bn2))
// Expire Challenges & Comms after challenge period but before resolve end & assert they are not expired yet
require.NoError(t, state.ExpireCommitments(bID(28)))
require.Empty(t, state.expiredCommitments)
state.ExpireChallenges(bID(28))
require.Empty(t, state.expiredChallenges)
// Now fully expire them
require.NoError(t, state.ExpireCommitments(bID(30)))
require.Empty(t, state.commitments)
state.ExpireChallenges(bID(30))
require.Empty(t, state.challenges)
// Now finalize everything
state.Prune(bID(20))
require.Equal(t, l1Ref(bn1), state.lastPrunedCommitment)
state.Prune(bID(28))
require.Equal(t, l1Ref(bn1), state.lastPrunedCommitment)
state.Prune(bID(32))
require.Equal(t, l1Ref(bn2), state.lastPrunedCommitment)
}
// TestExpireChallenges expires challenges and prunes the state for longer windows
// with commitments every 6 blocks.
func TestExpireChallenges(t *testing.T) {
logger := testlog.Logger(t, log.LevelInfo)
cfg := Config{
ResolveWindow: 90,
ChallengeWindow: 90,
}
rng := rand.New(rand.NewSource(1234))
state := NewState(logger, &NoopMetrics{}, cfg)
comms := make(map[uint64]CommitmentData)
i := uint64(3713854)
// increment new commitments every 6 blocks
for ; i < 3713948; i += 6 {
comm := RandomCommitment(rng)
comms[i] = comm
logger.Info("set commitment", "block", i, "comm", comm)
state.TrackCommitment(comm, l1Ref(i))
require.NoError(t, state.ExpireCommitments(bID(i)))
state.ExpireChallenges(bID(i))
}
// activate a couple of subsequent challenges
state.CreateChallenge(comms[3713926], bID(3713948), 3713926)
state.CreateChallenge(comms[3713932], bID(3713950), 3713932)
// continue incrementing commitments
for ; i < 3714038; i += 6 {
comm := RandomCommitment(rng)
comms[i] = comm
logger.Info("set commitment", "block", i)
state.TrackCommitment(comm, l1Ref(i))
require.NoError(t, state.ExpireCommitments(bID(i)))
state.ExpireChallenges(bID(i))
}
// Jump ahead to the end of the resolve window for comm included in block 3713926 which triggers a reorg
state.ExpireChallenges(bID(3714106))
require.ErrorIs(t, state.ExpireCommitments(bID(3714106)), ErrReorgRequired)
}
// TestDAChallengeDetached tests the lookahead + reorg handling of the da state
func TestDAChallengeDetached(t *testing.T) {
logger := testlog.Logger(t, log.LevelWarn)
cfg := Config{
ResolveWindow: 6,
ChallengeWindow: 6,
}
rng := rand.New(rand.NewSource(1234))
state := NewState(logger, &NoopMetrics{}, cfg)
c1 := RandomCommitment(rng)
c2 := RandomCommitment(rng)
// c1 at bn1 is missing, pipeline stalls
state.TrackCommitment(c1, l1Ref(1))
// c2 at bn2 is challenged at bn3
state.CreateChallenge(c2, bID(3), uint64(2))
require.Equal(t, ChallengeActive, state.GetChallengeStatus(c2, uint64(2)))
// c1 is finally challenged at bn5
state.CreateChallenge(c1, bID(5), uint64(1))
// c2 expires but should not trigger a reset because we're waiting for c1 to expire
state.ExpireChallenges(bID(10))
err := state.ExpireCommitments(bID(10))
require.NoError(t, err)
// c1 expires finally
state.ExpireChallenges(bID(11))
err = state.ExpireCommitments(bID(11))
require.ErrorIs(t, err, ErrReorgRequired)
// pruning finalized block is safe. It should not prune any commitments yet.
state.Prune(bID(1))
require.Equal(t, eth.L1BlockRef{}, state.lastPrunedCommitment)
// Perform reorg back to bn2
state.ClearCommitments()
// pipeline discovers c2 at bn2
state.TrackCommitment(c2, l1Ref(2))
// it is already marked as expired so it will be skipped without needing a reorg
require.Equal(t, ChallengeExpired, state.GetChallengeStatus(c2, uint64(2)))
// later when we get to finalizing block 10 + margin, the pending challenge is safely pruned
// Note: We need to go through the expire then prune steps
state.ExpireChallenges(bID(201))
err = state.ExpireCommitments(bID(201))
require.ErrorIs(t, err, ErrReorgRequired)
state.Prune(bID(201))
require.True(t, state.NoCommitments())
}
// cannot import from testutils at this time because of import cycle
type mockL1Fetcher struct {
mock.Mock
}
func (m *mockL1Fetcher) InfoAndTxsByHash(ctx context.Context, hash common.Hash) (eth.BlockInfo, types.Transactions, error) {
out := m.Mock.Called(hash)
return out.Get(0).(eth.BlockInfo), out.Get(1).(types.Transactions), out.Error(2)
}
func (m *mockL1Fetcher) ExpectInfoAndTxsByHash(hash common.Hash, info eth.BlockInfo, transactions types.Transactions, err error) {
m.Mock.On("InfoAndTxsByHash", hash).Once().Return(info, transactions, err)
}
func (m *mockL1Fetcher) FetchReceipts(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, types.Receipts, error) {
out := m.Mock.Called(blockHash)
return *out.Get(0).(*eth.BlockInfo), out.Get(1).(types.Receipts), out.Error(2)
}
func (m *mockL1Fetcher) ExpectFetchReceipts(hash common.Hash, info eth.BlockInfo, receipts types.Receipts, err error) {
m.Mock.On("FetchReceipts", hash).Once().Return(&info, receipts, err)
}
func (m *mockL1Fetcher) L1BlockRefByNumber(ctx context.Context, num uint64) (eth.L1BlockRef, error) {
out := m.Mock.Called(num)
return out.Get(0).(eth.L1BlockRef), out.Error(1)
}
func (m *mockL1Fetcher) ExpectL1BlockRefByNumber(num uint64, ref eth.L1BlockRef, err error) {
m.Mock.On("L1BlockRefByNumber", num).Once().Return(ref, err)
}
// TestUpdateFinalizedHead tests the updateFinalizedHead behavior with and without commitments.
// When there are no commitments, updateFinalizedHead preserves the existing finalizedHead,
// which is managed by updateFinalizedFromL1 called from AdvanceL1Origin.
func TestUpdateFinalizedHead(t *testing.T) {
logger := testlog.Logger(t, log.LevelInfo)
cfg := Config{
ResolveWindow: 6,
ChallengeWindow: 6,
}
t.Run("no commitments preserves existing finalizedHead unchanged", func(t *testing.T) {
state := NewState(logger, &NoopMetrics{}, cfg)
storage := NewMockDAClient(logger)
da := NewAltDAWithState(logger, cfg, storage, &NoopMetrics{}, state)
// Verify state has no commitments
require.True(t, state.NoCommitments())
// Set an initial finalizedHead (simulating what updateFinalizedFromL1 would do)
initialFinalizedHead := l1Ref(50)
da.finalizedHead = initialFinalizedHead
// Call Finalize with l1Finalized
l1Finalized := l1Ref(100)
da.Finalize(l1Finalized)
// finalizedHead should be preserved (not overwritten) since there are no commitments
require.Equal(t, initialFinalizedHead, da.finalizedHead)
// l1FinalizedHead should be updated
require.Equal(t, l1Finalized, da.l1FinalizedHead)
})
t.Run("no commitments after all pruned preserves existing finalizedHead", func(t *testing.T) {
rng := rand.New(rand.NewSource(1234))
state := NewState(logger, &NoopMetrics{}, cfg)
storage := NewMockDAClient(logger)
da := NewAltDAWithState(logger, cfg, storage, &NoopMetrics{}, state)
// Track and expire a commitment to set lastPrunedCommitment
c1 := RandomCommitment(rng)
bn1 := uint64(10)
state.TrackCommitment(c1, l1Ref(bn1))
require.NoError(t, state.ExpireCommitments(bID(bn1+cfg.ChallengeWindow)))
state.Prune(bID(bn1 + cfg.ChallengeWindow))
// Verify lastPrunedCommitment is set and no more commitments
require.Equal(t, l1Ref(bn1), state.lastPrunedCommitment)
require.True(t, state.NoCommitments())
// Simulate updateFinalizedFromL1 having set the finalizedHead
initialFinalizedHead := l1Ref(80)
da.finalizedHead = initialFinalizedHead
// Call Finalize with l1Finalized
l1Finalized := l1Ref(100)
da.Finalize(l1Finalized)
// finalizedHead should be preserved since there are no commitments
require.Equal(t, initialFinalizedHead, da.finalizedHead)
})
t.Run("with pending commitments prunes and uses lastPrunedCommitment", func(t *testing.T) {
rng := rand.New(rand.NewSource(1234))
state := NewState(logger, &NoopMetrics{}, cfg)
storage := NewMockDAClient(logger)
da := NewAltDAWithState(logger, cfg, storage, &NoopMetrics{}, state)
// Track a commitment that will be pruned
c1 := RandomCommitment(rng)
bn1 := uint64(10)
state.TrackCommitment(c1, l1Ref(bn1))
require.NoError(t, state.ExpireCommitments(bID(bn1+cfg.ChallengeWindow)))
state.Prune(bID(bn1 + cfg.ChallengeWindow))
// Track another commitment that won't be expired/pruned
c2 := RandomCommitment(rng)
bn2 := uint64(50)
state.TrackCommitment(c2, l1Ref(bn2))
// Verify state has pending commitments
require.False(t, state.NoCommitments())
require.Equal(t, l1Ref(bn1), state.lastPrunedCommitment)
// Call Finalize with l1Finalized higher than lastPrunedCommitment
l1Finalized := l1Ref(100)
da.Finalize(l1Finalized)
// finalizedHead should be lastPrunedCommitment because there are pending commitments
require.Equal(t, l1Ref(bn1), da.finalizedHead)
})
t.Run("with commitments prunes up to l1Finalized and updates finalizedHead", func(t *testing.T) {
rng := rand.New(rand.NewSource(1234))
state := NewState(logger, &NoopMetrics{}, cfg)
storage := NewMockDAClient(logger)
da := NewAltDAWithState(logger, cfg, storage, &NoopMetrics{}, state)
// Track and expire multiple commitments
c1 := RandomCommitment(rng)
bn1 := uint64(10)
state.TrackCommitment(c1, l1Ref(bn1))
c2 := RandomCommitment(rng)
bn2 := uint64(20)
state.TrackCommitment(c2, l1Ref(bn2))
// Expire both commitments
require.NoError(t, state.ExpireCommitments(bID(bn2+cfg.ChallengeWindow)))
// Verify we have expired commitments ready to prune
require.False(t, state.NoCommitments())
// Call Finalize - this should prune up to l1Finalized
l1Finalized := l1Ref(bn2 + cfg.ChallengeWindow)
da.Finalize(l1Finalized)
// Both commitments should be pruned, finalizedHead should be the last pruned one
require.Equal(t, l1Ref(bn2), da.finalizedHead)
})
t.Run("finalized head signal handler is called with correct value", func(t *testing.T) {
state := NewState(logger, &NoopMetrics{}, cfg)
storage := NewMockDAClient(logger)
da := NewAltDAWithState(logger, cfg, storage, &NoopMetrics{}, state)
// Set initial finalizedHead (simulating updateFinalizedFromL1)
initialFinalizedHead := l1Ref(50)
da.finalizedHead = initialFinalizedHead
var receivedHead eth.L1BlockRef
da.OnFinalizedHeadSignal(func(ref eth.L1BlockRef) {
receivedHead = ref
})
l1Finalized := l1Ref(100)
da.Finalize(l1Finalized)
// Handler should receive the preserved finalizedHead (since no commitments)
require.Equal(t, initialFinalizedHead, receivedHead)
})
}
func TestAdvanceChallengeOrigin(t *testing.T) {
logger := testlog.Logger(t, log.LevelWarn)
ctx := context.Background()
l1F := &mockL1Fetcher{}
defer l1F.AssertExpectations(t)
storage := NewMockDAClient(logger)
daddr := common.HexToAddress("0x978e3286eb805934215a88694d80b09aded68d90")
pcfg := Config{
ChallengeWindow: 90, ResolveWindow: 90, DAChallengeContractAddress: daddr,
}
bhash := common.HexToHash("0xd438144ffab918b1349e7cd06889c26800c26d8edc34d64f750e3e097166a09c")
bhash2 := common.HexToHash("0xd000004ffab918b1349e7cd06889c26800c26d8edc34d64f750e3e097166a09c")
bn := uint64(19)
comm := Keccak256Commitment(common.FromHex("eed82c1026bdd0f23461dd6ca515ef677624e63e6fc0ff91e3672af8eddf579d"))
state := NewState(logger, &NoopMetrics{}, pcfg)
da := NewAltDAWithState(logger, pcfg, storage, &NoopMetrics{}, state)
receipts := types.Receipts{&types.Receipt{
Type: 2,
Status: 1,
Logs: []*types.Log{
{
BlockNumber: bn,
Address: daddr,
Topics: []common.Hash{
common.HexToHash("0xa448afda7ea1e3a7a10fcab0c29fe9a9dd85791503bf0171f281521551c7ec05"),
},
},
{
BlockNumber: bn,
Address: daddr,
Topics: []common.Hash{
common.HexToHash("0xc5d8c630ba2fdacb1db24c4599df78c7fb8cf97b5aecde34939597f6697bb1ad"),
common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000000e"),
},
Data: common.FromHex("0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002100eed82c1026bdd0f23461dd6ca515ef677624e63e6fc0ff91e3672af8eddf579d00000000000000000000000000000000000000000000000000000000000000"),
},
},
BlockNumber: big.NewInt(int64(bn)),
}}
id := eth.BlockID{
Number: bn,
Hash: bhash,
}
l1F.ExpectFetchReceipts(bhash, nil, receipts, nil)
// Advance the challenge origin & ensure that we track the challenge
err := da.AdvanceChallengeOrigin(ctx, l1F, id)
require.NoError(t, err)
c, has := state.GetChallenge(comm, 14)
require.True(t, has)
require.Equal(t, ChallengeActive, c.challengeStatus)
// Advance the challenge origin until the challenge should be expired
for i := bn + 1; i < bn+1+pcfg.ChallengeWindow; i++ {
id2 := eth.BlockID{
Number: i,
Hash: bhash2,
}
l1F.ExpectFetchReceipts(bhash2, nil, nil, nil)
err = da.AdvanceChallengeOrigin(ctx, l1F, id2)
require.NoError(t, err)
}
state.Prune(bID(bn + 1 + pcfg.ChallengeWindow + pcfg.ResolveWindow))
_, has = state.GetChallenge(comm, 14)
require.False(t, has)
}