From 8512f456f033130ecb0e5493a3c36be025908228 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 22 Oct 2024 14:52:33 +0800 Subject: [PATCH] fix(taiko-client): fix `lastVerifiedBlockHash` fetch (#18277) --- .../driver/chain_syncer/beaconsync/syncer.go | 22 ++++++++++++++++--- .../driver/chain_syncer/blob/syncer.go | 21 +++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go b/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go index cc2c65a515..b901b356c2 100644 --- a/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go +++ b/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go @@ -5,6 +5,7 @@ import ( "fmt" "math/big" + "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/eth/downloader" @@ -71,9 +72,24 @@ func (s *Syncer) TriggerBeaconSync(blockID uint64) error { return fmt.Errorf("unexpected NewPayload response status: %s", status.Status) } - lastVerifiedBlockHash, err := s.rpc.GetLastVerifiedBlockHash(s.ctx) - if err != nil { - return fmt.Errorf("failed to fetch the last verified block hash: %w", err) + var lastVerifiedBlockHash common.Hash + if lastVerifiedBlockHash, err = s.rpc.GetLastVerifiedBlockHash(s.ctx); err != nil { + log.Debug("Failed to fetch the last verified block hash", "err", err) + + stateVars, err := s.rpc.GetProtocolStateVariables(&bind.CallOpts{Context: s.ctx}) + if err != nil { + return fmt.Errorf("failed to fetch protocol state variables: %w", err) + } + + lastVerifiedBlockHeader, err := s.rpc.L2CheckPoint.HeaderByNumber( + s.ctx, + new(big.Int).SetUint64(stateVars.B.LastVerifiedBlockId), + ) + if err != nil { + return fmt.Errorf("failed to fetch the last verified block hash: %w", err) + } + + lastVerifiedBlockHash = lastVerifiedBlockHeader.Hash() } fcRes, err := s.rpc.L2Engine.ForkchoiceUpdate(s.ctx, &engine.ForkchoiceStateV1{ diff --git a/packages/taiko-client/driver/chain_syncer/blob/syncer.go b/packages/taiko-client/driver/chain_syncer/blob/syncer.go index c850bb52ad..53a49961a0 100644 --- a/packages/taiko-client/driver/chain_syncer/blob/syncer.go +++ b/packages/taiko-client/driver/chain_syncer/blob/syncer.go @@ -409,9 +409,24 @@ func (s *Syncer) insertNewHead( return nil, fmt.Errorf("failed to create execution payloads: %w", err) } - lastVerifiedBlockHash, err := s.rpc.GetLastVerifiedBlockHash(ctx) - if err != nil { - return nil, fmt.Errorf("failed to fetch the last verified block hash: %w", err) + var lastVerifiedBlockHash common.Hash + if lastVerifiedBlockHash, err = s.rpc.GetLastVerifiedBlockHash(ctx); err != nil { + log.Debug("Failed to fetch last verified block hash", "error", err) + + stateVars, err := s.rpc.GetProtocolStateVariables(&bind.CallOpts{Context: ctx}) + if err != nil { + return nil, fmt.Errorf("failed to fetch protocol state variables: %w", err) + } + + lastVerifiedBlockHeader, err := s.rpc.L2.HeaderByNumber( + ctx, + new(big.Int).SetUint64(stateVars.B.LastVerifiedBlockId), + ) + if err != nil { + return nil, fmt.Errorf("failed to fetch last verified block: %w", err) + } + + lastVerifiedBlockHash = lastVerifiedBlockHeader.Hash() } fc := &engine.ForkchoiceStateV1{