This repository was archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathsyncer.go
590 lines (521 loc) · 18.1 KB
/
syncer.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package calldata
import (
"context"
"errors"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/taikoxyz/taiko-client/bindings"
"github.com/taikoxyz/taiko-client/bindings/encoding"
anchorTxConstructor "github.com/taikoxyz/taiko-client/driver/anchor_tx_constructor"
"github.com/taikoxyz/taiko-client/driver/chain_syncer/beaconsync"
"github.com/taikoxyz/taiko-client/driver/state"
txlistfetcher "github.com/taikoxyz/taiko-client/driver/txlist_fetcher"
"github.com/taikoxyz/taiko-client/internal/metrics"
eventIterator "github.com/taikoxyz/taiko-client/pkg/chain_iterator/event_iterator"
"github.com/taikoxyz/taiko-client/pkg/rpc"
txListValidator "github.com/taikoxyz/taiko-client/pkg/txlist_validator"
)
// Syncer responsible for letting the L2 execution engine catching up with protocol's latest
// pending block through deriving L1 calldata.
type Syncer struct {
ctx context.Context
rpc *rpc.Client
state *state.State
progressTracker *beaconsync.SyncProgressTracker // Sync progress tracker
anchorConstructor *anchorTxConstructor.AnchorTxConstructor // TaikoL2.anchor transactions constructor
txListValidator *txListValidator.TxListValidator // Transactions list validator
// Used by BlockInserter
lastInsertedBlockID *big.Int
reorgDetectedFlag bool
}
// NewSyncer creates a new syncer instance.
func NewSyncer(
ctx context.Context,
client *rpc.Client,
state *state.State,
progressTracker *beaconsync.SyncProgressTracker,
) (*Syncer, error) {
configs, err := client.TaikoL1.GetConfig(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, fmt.Errorf("failed to get protocol configs: %w", err)
}
constructor, err := anchorTxConstructor.New(client)
if err != nil {
return nil, fmt.Errorf("failed to initialize anchor constructor: %w", err)
}
return &Syncer{
ctx: ctx,
rpc: client,
state: state,
progressTracker: progressTracker,
anchorConstructor: constructor,
txListValidator: txListValidator.NewTxListValidator(
uint64(configs.BlockMaxGasLimit),
rpc.BlockMaxTxListBytes,
client.L2.ChainID,
),
}, nil
}
// ProcessL1Blocks fetches all `TaikoL1.BlockProposed` events between given
// L1 block heights, and then tries inserting them into L2 execution engine's blockchain.
func (s *Syncer) ProcessL1Blocks(ctx context.Context, l1End *types.Header) error {
for {
if err := s.processL1Blocks(ctx, l1End); err != nil {
return err
}
// If the L1 chain has been reorged, we process the new L1 blocks again with
// the new L1Current cursor.
if s.reorgDetectedFlag {
s.reorgDetectedFlag = false
continue
}
return nil
}
}
// processL1Blocks is the inner method which responsible for processing
// all new L1 blocks.
func (s *Syncer) processL1Blocks(ctx context.Context, l1End *types.Header) error {
startL1Current := s.state.GetL1Current()
// If there is a L1 reorg, sometimes this will happen.
if startL1Current.Number.Uint64() >= l1End.Number.Uint64() && startL1Current.Hash() != l1End.Hash() {
newL1Current, err := s.rpc.L1.HeaderByNumber(ctx, new(big.Int).Sub(l1End.Number, common.Big1))
if err != nil {
return err
}
log.Info(
"Reorg detected",
"oldL1CurrentHeight", startL1Current.Number,
"oldL1CurrentHash", startL1Current.Hash(),
"newL1CurrentHeight", newL1Current.Number,
"newL1CurrentHash", newL1Current.Hash(),
"l1Head", l1End.Number,
)
s.state.SetL1Current(newL1Current)
s.lastInsertedBlockID = nil
}
iter, err := eventIterator.NewBlockProposedIterator(ctx, &eventIterator.BlockProposedIteratorConfig{
Client: s.rpc.L1,
TaikoL1: s.rpc.TaikoL1,
StartHeight: s.state.GetL1Current().Number,
EndHeight: l1End.Number,
FilterQuery: nil,
OnBlockProposedEvent: s.onBlockProposed,
})
if err != nil {
return err
}
if err := iter.Iter(); err != nil {
return err
}
// If there is a L1 reorg, we don't update the L1Current cursor.
if !s.reorgDetectedFlag {
s.state.SetL1Current(l1End)
metrics.DriverL1CurrentHeightGauge.Update(s.state.GetL1Current().Number.Int64())
}
return nil
}
// OnBlockProposed is a `BlockProposed` event callback which responsible for
// inserting the proposed block one by one to the L2 execution engine.
func (s *Syncer) onBlockProposed(
ctx context.Context,
event *bindings.TaikoL1ClientBlockProposed,
endIter eventIterator.EndBlockProposedEventIterFunc,
) error {
// We simply ignore the genesis block's `BlockProposed` event.
if event.BlockId.Cmp(common.Big0) == 0 {
return nil
}
// If we are not inserting a block whose parent block is the latest verified block in protocol,
// and the node hasn't just finished the P2P sync, we check if the L1 chain has been reorged.
if !s.progressTracker.Triggered() {
reorgCheckResult, err := s.checkReorg(ctx, event)
if err != nil {
return err
}
if reorgCheckResult.IsReorged {
log.Info(
"Reset L1Current cursor due to L1 reorg",
"l1CurrentHeightOld", s.state.GetL1Current().Number,
"l1CurrentHashOld", s.state.GetL1Current().Hash(),
"l1CurrentHeightNew", reorgCheckResult.L1CurrentToReset.Number,
"l1CurrentHashNew", reorgCheckResult.L1CurrentToReset.Hash(),
"lastInsertedBlockIDOld", s.lastInsertedBlockID,
"lastInsertedBlockIDNew", reorgCheckResult.LastHandledBlockIDToReset,
)
s.state.SetL1Current(reorgCheckResult.L1CurrentToReset)
s.lastInsertedBlockID = reorgCheckResult.LastHandledBlockIDToReset
s.reorgDetectedFlag = true
endIter()
return nil
}
}
// Ignore those already inserted blocks.
if s.lastInsertedBlockID != nil && event.BlockId.Cmp(s.lastInsertedBlockID) <= 0 {
return nil
}
log.Info(
"New BlockProposed event",
"l1Height", event.Raw.BlockNumber,
"l1Hash", event.Raw.BlockHash,
"blockID", event.BlockId,
"removed", event.Raw.Removed,
)
// If the event's timestamp is in the future, we wait until the timestamp is reached, should
// only happen when testing.
if event.Meta.Timestamp > uint64(time.Now().Unix()) {
log.Warn("Future L2 block, waiting", "L2BlockTimestamp", event.Meta.Timestamp, "now", time.Now().Unix())
time.Sleep(time.Until(time.Unix(int64(event.Meta.Timestamp), 0)))
}
// Fetch the L2 parent block, if the node is just finished a P2P sync, we simply use the tracker's
// last synced verified block as the parent, otherwise, we fetch the parent block from L2 EE.
var (
parent *types.Header
err error
)
if s.progressTracker.Triggered() {
// Already synced through beacon sync, just skip this event.
if event.BlockId.Cmp(s.progressTracker.LastSyncedVerifiedBlockID()) <= 0 {
return nil
}
parent, err = s.rpc.L2.HeaderByHash(ctx, s.progressTracker.LastSyncedVerifiedBlockHash())
} else {
parent, err = s.rpc.L2ParentByBlockID(ctx, event.BlockId)
}
if err != nil {
return fmt.Errorf("failed to fetch L2 parent block: %w", err)
}
log.Debug(
"Parent block",
"height", parent.Number,
"hash", parent.Hash(),
"beaconSyncTriggered", s.progressTracker.Triggered(),
)
tx, err := s.rpc.L1.TransactionInBlock(ctx, event.Raw.BlockHash, event.Raw.TxIndex)
if err != nil {
return fmt.Errorf("failed to fetch original TaikoL1.proposeBlock transaction: %w", err)
}
// Decode transactions list.
var txListDecoder txlistfetcher.TxListFetcher
if event.Meta.BlobUsed {
txListDecoder = txlistfetcher.NewBlobTxListFetcher(s.rpc)
} else {
txListDecoder = new(txlistfetcher.CalldataFetcher)
}
txListBytes, err := txListDecoder.Fetch(ctx, tx, &event.Meta)
if err != nil {
if errors.Is(err, rpc.ErrBlobInvalid) {
log.Info("Invalid blob detected", "blockID", event.BlockId)
txListBytes = []byte{}
} else {
return fmt.Errorf("failed to decode tx list: %w", err)
}
}
// If the transactions list is invalid, we simply insert an empty L2 block.
if !s.txListValidator.ValidateTxList(event.BlockId, txListBytes, event.Meta.BlobUsed) {
log.Info("Invalid transactions list, insert an empty L2 block instead", "blockID", event.BlockId)
txListBytes = []byte{}
}
payloadData, err := s.insertNewHead(
ctx,
event,
parent,
s.state.GetHeadBlockID(),
txListBytes,
&rawdb.L1Origin{
BlockID: event.BlockId,
L2BlockHash: common.Hash{}, // Will be set by taiko-geth.
L1BlockHeight: new(big.Int).SetUint64(event.Raw.BlockNumber),
L1BlockHash: event.Raw.BlockHash,
},
)
if err != nil {
return fmt.Errorf("failed to insert new head to L2 execution engine: %w", err)
}
log.Debug("Payload data", "hash", payloadData.BlockHash, "txs", len(payloadData.Transactions))
log.Info(
"🔗 New L2 block inserted",
"blockID", event.BlockId,
"height", payloadData.Number,
"hash", payloadData.BlockHash,
"transactions", len(payloadData.Transactions),
"baseFee", payloadData.BaseFeePerGas,
"withdrawals", len(payloadData.Withdrawals),
)
metrics.DriverL1CurrentHeightGauge.Update(int64(event.Raw.BlockNumber))
s.lastInsertedBlockID = event.BlockId
if s.progressTracker.Triggered() {
s.progressTracker.ClearMeta()
}
return nil
}
// insertNewHead tries to insert a new head block to the L2 execution engine's local
// block chain through Engine APIs.
func (s *Syncer) insertNewHead(
ctx context.Context,
event *bindings.TaikoL1ClientBlockProposed,
parent *types.Header,
headBlockID *big.Int,
txListBytes []byte,
l1Origin *rawdb.L1Origin,
) (*engine.ExecutableData, error) {
log.Debug(
"Try to insert a new L2 head block",
"parentNumber", parent.Number,
"parentHash", parent.Hash(),
"headBlockID", headBlockID,
"l1Origin", l1Origin,
)
// Insert a TaikoL2.anchor transaction at transactions list head
var txList []*types.Transaction
if len(txListBytes) != 0 {
if err := rlp.DecodeBytes(txListBytes, &txList); err != nil {
log.Error("Invalid txList bytes", "blockID", event.BlockId)
return nil, err
}
}
// Get L2 baseFee
baseFee, err := s.rpc.TaikoL2.GetBasefee(
&bind.CallOpts{BlockNumber: parent.Number, Context: ctx},
event.Meta.L1Height,
uint32(parent.GasUsed),
)
if err != nil {
return nil, fmt.Errorf("failed to get L2 baseFee: %w", encoding.TryParsingCustomError(err))
}
log.Info(
"L2 baseFee",
"blockID", event.BlockId,
"baseFee", baseFee,
"syncedL1Height", event.Meta.L1Height,
"parentGasUsed", parent.GasUsed,
)
// Get withdrawals
withdrawals := make(types.Withdrawals, len(event.DepositsProcessed))
for i, d := range event.DepositsProcessed {
withdrawals[i] = &types.Withdrawal{Address: d.Recipient, Amount: d.Amount.Uint64(), Index: d.Id}
}
// Assemble a TaikoL2.anchor transaction
anchorTx, err := s.anchorConstructor.AssembleAnchorTx(
ctx,
new(big.Int).SetUint64(event.Meta.L1Height),
event.Meta.L1Hash,
new(big.Int).Add(parent.Number, common.Big1),
baseFee,
parent.GasUsed,
)
if err != nil {
return nil, fmt.Errorf("failed to create TaikoL2.anchor transaction: %w", err)
}
// Insert the anchor transaction at the head of the transactions list
txList = append([]*types.Transaction{anchorTx}, txList...)
if txListBytes, err = rlp.EncodeToBytes(txList); err != nil {
log.Error("Encode txList error", "blockID", event.BlockId, "error", err)
return nil, err
}
payload, err := s.createExecutionPayloads(
ctx,
event,
parent.Hash(),
l1Origin,
headBlockID,
txListBytes,
baseFee,
withdrawals,
)
if err != nil {
return nil, fmt.Errorf("failed to create execution payloads: %w", err)
}
fc := &engine.ForkchoiceStateV1{HeadBlockHash: payload.BlockHash}
if err = s.fillForkchoiceState(ctx, event, fc); err != nil {
return nil, err
}
// Update the fork choice
fcRes, err := s.rpc.L2Engine.ForkchoiceUpdate(ctx, fc, nil)
if err != nil {
return nil, err
}
if fcRes.PayloadStatus.Status != engine.VALID {
return nil, fmt.Errorf("unexpected ForkchoiceUpdate response status: %s", fcRes.PayloadStatus.Status)
}
return payload, nil
}
// fillForkchoiceState fills the forkchoice state with the finalized block hash and the safe block hash.
func (s *Syncer) fillForkchoiceState(
ctx context.Context,
event *bindings.TaikoL1ClientBlockProposed,
fc *engine.ForkchoiceStateV1,
) error {
// If the event is emitted from the genesis block, we don't need to fill the forkchoice state,
// should only happen when testing.
if event.Raw.BlockNumber == 0 {
return nil
}
// Fetch the latest verified block's header from protocol.
variables, err := s.rpc.GetProtocolStateVariables(
&bind.CallOpts{Context: ctx, BlockNumber: new(big.Int).SetUint64(event.Raw.BlockNumber - 1)},
)
if err != nil {
return err
}
finalizeHeader, err := s.rpc.L2.HeaderByNumber(ctx, new(big.Int).SetUint64(variables.B.LastVerifiedBlockId))
if err != nil {
return err
}
// Fill the forkchoice state.
fc.FinalizedBlockHash = finalizeHeader.Hash()
fc.SafeBlockHash = finalizeHeader.ParentHash
return nil
}
// createExecutionPayloads creates a new execution payloads through
// Engine APIs.
func (s *Syncer) createExecutionPayloads(
ctx context.Context,
event *bindings.TaikoL1ClientBlockProposed,
parentHash common.Hash,
l1Origin *rawdb.L1Origin,
headBlockID *big.Int,
txListBytes []byte,
baseFee *big.Int,
withdrawals types.Withdrawals,
) (payloadData *engine.ExecutableData, err error) {
fc := &engine.ForkchoiceStateV1{HeadBlockHash: parentHash}
attributes := &engine.PayloadAttributes{
Timestamp: event.Meta.Timestamp,
Random: event.Meta.Difficulty,
SuggestedFeeRecipient: event.Meta.Coinbase,
Withdrawals: withdrawals,
BlockMetadata: &engine.BlockMetadata{
HighestBlockID: headBlockID,
Beneficiary: event.Meta.Coinbase,
GasLimit: uint64(event.Meta.GasLimit) + anchorTxConstructor.AnchorGasLimit,
Timestamp: event.Meta.Timestamp,
TxList: txListBytes,
MixHash: event.Meta.Difficulty,
ExtraData: event.Meta.ExtraData[:],
},
BaseFeePerGas: baseFee,
L1Origin: l1Origin,
}
log.Debug(
"PayloadAttributes",
"blockID", event.BlockId,
"timestamp", attributes.Timestamp,
"random", attributes.Random,
"suggestedFeeRecipient", attributes.SuggestedFeeRecipient,
"withdrawals", len(attributes.Withdrawals),
"highestBlockID", attributes.BlockMetadata.HighestBlockID,
"gasLimit", attributes.BlockMetadata.GasLimit,
"timestamp", attributes.BlockMetadata.Timestamp,
"mixHash", attributes.BlockMetadata.MixHash,
"baseFee", attributes.BaseFeePerGas,
"extraData", string(attributes.BlockMetadata.ExtraData),
"l1OriginHeight", attributes.L1Origin.L1BlockHeight,
"l1OriginHash", attributes.L1Origin.L1BlockHash,
)
// Step 1, prepare a payload
fcRes, err := s.rpc.L2Engine.ForkchoiceUpdate(ctx, fc, attributes)
if err != nil {
return nil, fmt.Errorf("failed to update fork choice: %w", err)
}
if fcRes.PayloadStatus.Status != engine.VALID {
return nil, fmt.Errorf("unexpected ForkchoiceUpdate response status: %s", fcRes.PayloadStatus.Status)
}
if fcRes.PayloadID == nil {
return nil, errors.New("empty payload ID")
}
// Step 2, get the payload
payload, err := s.rpc.L2Engine.GetPayload(ctx, fcRes.PayloadID)
if err != nil {
return nil, fmt.Errorf("failed to get payload: %w", err)
}
log.Debug(
"Payload",
"blockID", event.BlockId,
"baseFee", payload.BaseFeePerGas,
"number", payload.Number,
"hash", payload.BlockHash,
"gasLimit", payload.GasLimit,
"gasUsed", payload.GasUsed,
"timestamp", payload.Timestamp,
"withdrawalsHash", payload.WithdrawalsHash,
)
// Step 3, execute the payload
execStatus, err := s.rpc.L2Engine.NewPayload(ctx, payload)
if err != nil {
return nil, fmt.Errorf("failed to create a new payload: %w", err)
}
if execStatus.Status != engine.VALID {
return nil, fmt.Errorf("unexpected NewPayload response status: %s", execStatus.Status)
}
return payload, nil
}
// checkLastVerifiedBlockMismatch checks if there is a mismatch between protocol's last verified block hash and
// the corresponding L2 EE block hash.
func (s *Syncer) checkLastVerifiedBlockMismatch(ctx context.Context) (bool, error) {
stateVars, err := s.rpc.GetProtocolStateVariables(&bind.CallOpts{Context: ctx})
if err != nil {
return false, err
}
if s.state.GetL2Head().Number.Uint64() < stateVars.B.LastVerifiedBlockId {
return false, nil
}
blockInfo, err := s.rpc.GetL2BlockInfo(ctx, new(big.Int).SetUint64(stateVars.B.LastVerifiedBlockId))
if err != nil {
return false, err
}
l2Header, err := s.rpc.L2.HeaderByNumber(ctx, new(big.Int).SetUint64(stateVars.B.LastVerifiedBlockId))
if err != nil {
return false, err
}
return blockInfo.Ts.BlockHash != l2Header.Hash(), nil
}
// checkReorg checks whether the L1 chain has been reorged, and resets the L1Current cursor if necessary.
func (s *Syncer) checkReorg(
ctx context.Context,
event *bindings.TaikoL1ClientBlockProposed,
) (*rpc.ReorgCheckResult, error) {
var (
reorgCheckResult = new(rpc.ReorgCheckResult)
err error
)
// If the L2 chain is at genesis, we don't need to check L1 reorg.
if s.state.GetL1Current().Number == s.state.GenesisL1Height {
return reorgCheckResult, nil
}
// 1. The latest verified block
mismatch, err := s.checkLastVerifiedBlockMismatch(ctx)
if err != nil {
return nil, fmt.Errorf("failed to check if last verified block in L2 EE has been reorged: %w", err)
}
// If the latest verified block in chain is mismatched, we reset the L2 chain to genesis, and restart
// the calldata sync process.
// TODO(Gavin): improve this approach.
if mismatch {
log.Warn("The latest verified block mismatch detected, reset L2 chain to genesis")
genesisL1Header, err := s.rpc.GetGenesisL1Header(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch genesis L1 header: %w", err)
}
reorgCheckResult.IsReorged = true
reorgCheckResult.L1CurrentToReset = genesisL1Header
reorgCheckResult.LastHandledBlockIDToReset = common.Big0
} else {
// 2. Parent block
reorgCheckResult, err = s.rpc.CheckL1Reorg(
ctx,
new(big.Int).Sub(event.BlockId, common.Big1),
)
if err != nil {
return nil, fmt.Errorf("failed to check whether L1 chain has been reorged: %w", err)
}
}
return reorgCheckResult, nil
}