Skip to content

Commit

Permalink
fix(taiko-client): check the blockID of the last verified block bef…
Browse files Browse the repository at this point in the history
…ore using it as `FinalizedBlockHash` (#18739)
  • Loading branch information
davidtaikocha authored Jan 9, 2025
1 parent 01ebba3 commit 8c364b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/taiko-client/driver/chain_syncer/blob/soft_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (s *Syncer) InsertSoftBlockFromTransactionsBatch(
return nil, fmt.Errorf("unexpected NewPayload response status: %s", execStatus.Status)
}

lastVerifiedBlockHash, err := s.rpc.GetLastVerifiedBlockHash(ctx)
lastVerifiedBlockInfo, err := s.rpc.GetLastVerifiedBlock(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch last verified block hash: %w", err)
}
Expand All @@ -245,7 +245,7 @@ func (s *Syncer) InsertSoftBlockFromTransactionsBatch(
fc = &engine.ForkchoiceStateV1{
HeadBlockHash: payload.BlockHash,
SafeBlockHash: canonicalHead.L2BlockHash,
FinalizedBlockHash: lastVerifiedBlockHash,
FinalizedBlockHash: lastVerifiedBlockInfo.BlockHash,
}
fcRes, err = s.rpc.L2Engine.ForkchoiceUpdate(ctx, fc, nil)
if err != nil {
Expand Down
23 changes: 6 additions & 17 deletions packages/taiko-client/driver/chain_syncer/blob/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,23 +409,12 @@ func (s *Syncer) insertNewHead(
}

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()
lastVerifiedBlockInfo, err := s.rpc.GetLastVerifiedBlock(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch last verified block: %w", err)
}
if payload.Number > lastVerifiedBlockInfo.BlockId {
lastVerifiedBlockHash = lastVerifiedBlockInfo.BlockHash
}

fc := &engine.ForkchoiceStateV1{
Expand Down
16 changes: 8 additions & 8 deletions packages/taiko-client/pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,17 @@ func (c *Client) GetProtocolStateVariables(opts *bind.CallOpts) (*struct {
return GetProtocolStateVariables(c.TaikoL1, opts)
}

// GetLastVerifiedBlockHash gets the last verified block hash from TaikoL1 contract.
func (c *Client) GetLastVerifiedBlockHash(ctx context.Context) (common.Hash, error) {
// GetLastVerifiedBlock gets the last verified block from TaikoL1 contract.
func (c *Client) GetLastVerifiedBlock(ctx context.Context) (struct {
BlockId uint64 //nolint:stylecheck
BlockHash [32]byte
StateRoot [32]byte
VerifiedAt uint64
}, error) {
ctxWithTimeout, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()

b, err := c.TaikoL1.GetLastVerifiedBlock(&bind.CallOpts{Context: ctxWithTimeout})
if err != nil {
return common.Hash{}, err
}

return b.BlockHash, nil
return c.TaikoL1.GetLastVerifiedBlock(&bind.CallOpts{Context: ctxWithTimeout})
}

// GetL2BlockInfo fetches the L2 block information from the protocol.
Expand Down

0 comments on commit 8c364b1

Please sign in to comment.