Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(taiko-client): correct finalized and safe block hash #18211

Merged
merged 3 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"fmt"
"math/big"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings"

"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/log"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/driver/state"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/pkg/rpc"
Expand Down Expand Up @@ -72,8 +71,15 @@ 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)
}

fcRes, err := s.rpc.L2Engine.ForkchoiceUpdate(s.ctx, &engine.ForkchoiceStateV1{
HeadBlockHash: headPayload.BlockHash,
HeadBlockHash: headPayload.BlockHash,
SafeBlockHash: lastVerifiedBlockHash,
FinalizedBlockHash: lastVerifiedBlockHash,
}, nil)
if err != nil {
return err
Expand Down
11 changes: 10 additions & 1 deletion packages/taiko-client/driver/chain_syncer/blob/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,16 @@ func (s *Syncer) insertNewHead(
return nil, fmt.Errorf("failed to create execution payloads: %w", err)
}

fc := &engine.ForkchoiceStateV1{HeadBlockHash: payload.BlockHash}
lastVerifiedBlockHash, err := s.rpc.GetLastVerifiedBlockHash(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch the last verified block hash: %w", err)
}

fc := &engine.ForkchoiceStateV1{
HeadBlockHash: payload.BlockHash,
SafeBlockHash: lastVerifiedBlockHash,
FinalizedBlockHash: lastVerifiedBlockHash,
}

// Update the fork choice
fcRes, err := s.rpc.L2Engine.ForkchoiceUpdate(ctx, fc, nil)
Expand Down
13 changes: 13 additions & 0 deletions packages/taiko-client/pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,19 @@ 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) {
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
}

// GetL2BlockInfo fetches the L2 block information from the protocol.
func (c *Client) GetL2BlockInfo(ctx context.Context, blockID *big.Int) (bindings.TaikoDataBlockV2, error) {
ctxWithTimeout, cancel := CtxWithTimeoutOrDefault(ctx, defaultTimeout)
Expand Down
Loading