Skip to content

Commit

Permalink
feat(taiko-client): check if the block is preconfirmed before calling…
Browse files Browse the repository at this point in the history
… `setHead` (#18863)
  • Loading branch information
davidtaikocha authored Feb 3, 2025
1 parent 47584e7 commit a2e02d2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/taiko-client/bindings/pacaya/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
285e03b07ba7c880e2c99b8314f6fa0d9078b49a
1713be26d6226695f393da883a8cf22d4152dcc9
64 changes: 33 additions & 31 deletions packages/taiko-client/bindings/pacaya/gen_taiko_inbox.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -31,35 +32,47 @@ func createPayloadAndSetHead(
"l1Origin", meta.L1Origin,
)

payload, err := createExecutionPayloads(
ctx,
rpc,
meta.createExecutionPayloadsMetaData,
anchorTx,
)
if err != nil {
return nil, fmt.Errorf("failed to create execution payloads: %w", err)
}

var (
lastVerifiedBlockHash common.Hash
)
var lastVerifiedBlockHash common.Hash
lastVerifiedTS, err := rpc.GetLastVerifiedTransitionPacaya(ctx)
if err != nil {
lastVerifiedBlockInfo, err := rpc.GetLastVerifiedBlockOntake(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch last verified block: %w", err)
}

if payload.Number > lastVerifiedBlockInfo.BlockId {
if meta.BlockID.Uint64() > lastVerifiedBlockInfo.BlockId {
lastVerifiedBlockHash = lastVerifiedBlockInfo.BlockHash
}
} else {
if payload.Number > lastVerifiedTS.BlockId {
if meta.BlockID.Uint64() > lastVerifiedTS.BlockId {
lastVerifiedBlockHash = lastVerifiedTS.Ts.BlockHash
}
}

payload, err := createExecutionPayloads(
ctx,
rpc,
meta.createExecutionPayloadsMetaData,
anchorTx,
lastVerifiedBlockHash,
)
if err != nil {
return nil, fmt.Errorf("failed to create execution payloads: %w", err)
}

// If the Pacaya block is preconfirmed, we don't need to insert it again.
if meta.BlockID.Cmp(new(big.Int).SetUint64(rpc.PacayaClients.ForkHeight)) >= 0 {
preconfirmed, err := isBlockPreconfirmed(ctx, rpc, payload)
if err != nil {
log.Debug("Failed to check if the block is preconfirmed", "error", err)
} else {
if preconfirmed {
log.Info("The block is preconfirmed", "blockID", meta.BlockID, "hash", payload.BlockHash)
return payload, nil
}
}
}

fc := &engine.ForkchoiceStateV1{
HeadBlockHash: payload.BlockHash,
SafeBlockHash: lastVerifiedBlockHash,
Expand All @@ -85,6 +98,7 @@ func createExecutionPayloads(
rpc *rpc.Client,
meta *createExecutionPayloadsMetaData,
anchorTx *types.Transaction,
lastVerfiiedBlockHash common.Hash,
) (payloadData *engine.ExecutableData, err error) {
// Insert a TaikoL2.anchor / TaikoL2.anchorV2 transaction at transactions list head
txListBytes, err := rlp.EncodeToBytes(append([]*types.Transaction{anchorTx}, meta.Txs...))
Expand All @@ -93,7 +107,11 @@ func createExecutionPayloads(
return nil, err
}

fc := &engine.ForkchoiceStateV1{HeadBlockHash: meta.ParentHash}
fc := &engine.ForkchoiceStateV1{
HeadBlockHash: meta.ParentHash,
SafeBlockHash: lastVerfiiedBlockHash,
FinalizedBlockHash: lastVerfiiedBlockHash,
}
attributes := &engine.PayloadAttributes{
Timestamp: meta.Timestamp,
Random: meta.Difficulty,
Expand Down Expand Up @@ -168,3 +186,17 @@ func createExecutionPayloads(

return payload, nil
}

// isBlockPreconfirmed checks if the block is preconfirmed.
func isBlockPreconfirmed(ctx context.Context, rpc *rpc.Client, payload *engine.ExecutableData) (bool, error) {
header, err := rpc.L2.HeaderByNumber(ctx, new(big.Int).SetUint64(payload.Number))
if err != nil {
return false, fmt.Errorf("failed to get header by number %d: %w", payload.Number, err)
}

if header == nil {
return false, fmt.Errorf("header not found for block number %d", payload.Number)
}

return header.Hash() == payload.BlockHash, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ func (i *BlocksInserterPacaya) InsertBlocks(
// Decompress the transactions list and try to insert a new head block to L2 EE.
if lastPayloadData, err = createPayloadAndSetHead(
ctx,
i.rpc,
&createPayloadAndSetHeadMetaData{
i.rpc, &createPayloadAndSetHeadMetaData{
createExecutionPayloadsMetaData: &createExecutionPayloadsMetaData{
BlockID: blockID,
ExtraData: meta.GetExtraData(),
Expand Down
6 changes: 5 additions & 1 deletion packages/taiko-client/pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ func (c *Client) initForkHeightConfigs(ctx context.Context) error {
c.PacayaClients.ForkHeight = pacayaForkHeightDevnet
}

log.Info("Pacaya fork client fork height", "chainID", c.L2.ChainID.Uint64(), "forkHeight", c.PacayaClients.ForkHeight)
log.Info(
"Pacaya fork client fork height",
"chainID", c.L2.ChainID.Uint64(),
"forkHeight", c.PacayaClients.ForkHeight,
)

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

0 comments on commit a2e02d2

Please sign in to comment.