Skip to content

Commit a2e02d2

Browse files
feat(taiko-client): check if the block is preconfirmed before calling setHead (#18863)
1 parent 47584e7 commit a2e02d2

File tree

5 files changed

+88
-51
lines changed

5 files changed

+88
-51
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
285e03b07ba7c880e2c99b8314f6fa0d9078b49a
1+
1713be26d6226695f393da883a8cf22d4152dcc9

packages/taiko-client/bindings/pacaya/gen_taiko_inbox.go

Lines changed: 33 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/taiko-client/driver/chain_syncer/blob/blocks_inserter/common.go

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"math/big"
78

89
"github.com/ethereum/go-ethereum/beacon/engine"
910
"github.com/ethereum/go-ethereum/common"
@@ -31,35 +32,47 @@ func createPayloadAndSetHead(
3132
"l1Origin", meta.L1Origin,
3233
)
3334

34-
payload, err := createExecutionPayloads(
35-
ctx,
36-
rpc,
37-
meta.createExecutionPayloadsMetaData,
38-
anchorTx,
39-
)
40-
if err != nil {
41-
return nil, fmt.Errorf("failed to create execution payloads: %w", err)
42-
}
43-
44-
var (
45-
lastVerifiedBlockHash common.Hash
46-
)
35+
var lastVerifiedBlockHash common.Hash
4736
lastVerifiedTS, err := rpc.GetLastVerifiedTransitionPacaya(ctx)
4837
if err != nil {
4938
lastVerifiedBlockInfo, err := rpc.GetLastVerifiedBlockOntake(ctx)
5039
if err != nil {
5140
return nil, fmt.Errorf("failed to fetch last verified block: %w", err)
5241
}
5342

54-
if payload.Number > lastVerifiedBlockInfo.BlockId {
43+
if meta.BlockID.Uint64() > lastVerifiedBlockInfo.BlockId {
5544
lastVerifiedBlockHash = lastVerifiedBlockInfo.BlockHash
5645
}
5746
} else {
58-
if payload.Number > lastVerifiedTS.BlockId {
47+
if meta.BlockID.Uint64() > lastVerifiedTS.BlockId {
5948
lastVerifiedBlockHash = lastVerifiedTS.Ts.BlockHash
6049
}
6150
}
6251

52+
payload, err := createExecutionPayloads(
53+
ctx,
54+
rpc,
55+
meta.createExecutionPayloadsMetaData,
56+
anchorTx,
57+
lastVerifiedBlockHash,
58+
)
59+
if err != nil {
60+
return nil, fmt.Errorf("failed to create execution payloads: %w", err)
61+
}
62+
63+
// If the Pacaya block is preconfirmed, we don't need to insert it again.
64+
if meta.BlockID.Cmp(new(big.Int).SetUint64(rpc.PacayaClients.ForkHeight)) >= 0 {
65+
preconfirmed, err := isBlockPreconfirmed(ctx, rpc, payload)
66+
if err != nil {
67+
log.Debug("Failed to check if the block is preconfirmed", "error", err)
68+
} else {
69+
if preconfirmed {
70+
log.Info("The block is preconfirmed", "blockID", meta.BlockID, "hash", payload.BlockHash)
71+
return payload, nil
72+
}
73+
}
74+
}
75+
6376
fc := &engine.ForkchoiceStateV1{
6477
HeadBlockHash: payload.BlockHash,
6578
SafeBlockHash: lastVerifiedBlockHash,
@@ -85,6 +98,7 @@ func createExecutionPayloads(
8598
rpc *rpc.Client,
8699
meta *createExecutionPayloadsMetaData,
87100
anchorTx *types.Transaction,
101+
lastVerfiiedBlockHash common.Hash,
88102
) (payloadData *engine.ExecutableData, err error) {
89103
// Insert a TaikoL2.anchor / TaikoL2.anchorV2 transaction at transactions list head
90104
txListBytes, err := rlp.EncodeToBytes(append([]*types.Transaction{anchorTx}, meta.Txs...))
@@ -93,7 +107,11 @@ func createExecutionPayloads(
93107
return nil, err
94108
}
95109

96-
fc := &engine.ForkchoiceStateV1{HeadBlockHash: meta.ParentHash}
110+
fc := &engine.ForkchoiceStateV1{
111+
HeadBlockHash: meta.ParentHash,
112+
SafeBlockHash: lastVerfiiedBlockHash,
113+
FinalizedBlockHash: lastVerfiiedBlockHash,
114+
}
97115
attributes := &engine.PayloadAttributes{
98116
Timestamp: meta.Timestamp,
99117
Random: meta.Difficulty,
@@ -168,3 +186,17 @@ func createExecutionPayloads(
168186

169187
return payload, nil
170188
}
189+
190+
// isBlockPreconfirmed checks if the block is preconfirmed.
191+
func isBlockPreconfirmed(ctx context.Context, rpc *rpc.Client, payload *engine.ExecutableData) (bool, error) {
192+
header, err := rpc.L2.HeaderByNumber(ctx, new(big.Int).SetUint64(payload.Number))
193+
if err != nil {
194+
return false, fmt.Errorf("failed to get header by number %d: %w", payload.Number, err)
195+
}
196+
197+
if header == nil {
198+
return false, fmt.Errorf("header not found for block number %d", payload.Number)
199+
}
200+
201+
return header.Hash() == payload.BlockHash, nil
202+
}

packages/taiko-client/driver/chain_syncer/blob/blocks_inserter/pacaya.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ func (i *BlocksInserterPacaya) InsertBlocks(
191191
// Decompress the transactions list and try to insert a new head block to L2 EE.
192192
if lastPayloadData, err = createPayloadAndSetHead(
193193
ctx,
194-
i.rpc,
195-
&createPayloadAndSetHeadMetaData{
194+
i.rpc, &createPayloadAndSetHeadMetaData{
196195
createExecutionPayloadsMetaData: &createExecutionPayloadsMetaData{
197196
BlockID: blockID,
198197
ExtraData: meta.GetExtraData(),

packages/taiko-client/pkg/rpc/client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,11 @@ func (c *Client) initForkHeightConfigs(ctx context.Context) error {
302302
c.PacayaClients.ForkHeight = pacayaForkHeightDevnet
303303
}
304304

305-
log.Info("Pacaya fork client fork height", "chainID", c.L2.ChainID.Uint64(), "forkHeight", c.PacayaClients.ForkHeight)
305+
log.Info(
306+
"Pacaya fork client fork height",
307+
"chainID", c.L2.ChainID.Uint64(),
308+
"forkHeight", c.PacayaClients.ForkHeight,
309+
)
306310

307311
ontakeProtocolConfigs, err := c.OntakeClients.TaikoL1.GetConfig(&bind.CallOpts{Context: ctx})
308312
if err != nil {

0 commit comments

Comments
 (0)