From 37f442c8319c436f02098847c1c2bc6ba8bb6aca Mon Sep 17 00:00:00 2001 From: maskpp Date: Fri, 4 Oct 2024 23:02:23 +0800 Subject: [PATCH 1/2] correct finalized and safe block hash --- .../driver/chain_syncer/beaconsync/syncer.go | 13 +++++++++---- .../taiko-client/driver/chain_syncer/blob/syncer.go | 9 +++++++-- 2 files changed, 16 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 3290cd7f00..03cc81c825 100644 --- a/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go +++ b/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go @@ -5,13 +5,13 @@ import ( "fmt" "math/big" - "github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings" - + "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" "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" @@ -72,10 +72,15 @@ func (s *Syncer) TriggerBeaconSync(blockID uint64) error { return fmt.Errorf("unexpected NewPayload response status: %s", status.Status) } + verifiedBlock, err := s.rpc.TaikoL1.GetLastVerifiedBlock(&bind.CallOpts{Context: s.ctx}) + if err != nil { + return err + } + fcRes, err := s.rpc.L2Engine.ForkchoiceUpdate(s.ctx, &engine.ForkchoiceStateV1{ HeadBlockHash: headPayload.BlockHash, - SafeBlockHash: headPayload.BlockHash, - FinalizedBlockHash: headPayload.BlockHash, + SafeBlockHash: verifiedBlock.BlockHash, + FinalizedBlockHash: verifiedBlock.BlockHash, }, nil) if err != nil { return err diff --git a/packages/taiko-client/driver/chain_syncer/blob/syncer.go b/packages/taiko-client/driver/chain_syncer/blob/syncer.go index 47f2d997a1..c3dad62f48 100644 --- a/packages/taiko-client/driver/chain_syncer/blob/syncer.go +++ b/packages/taiko-client/driver/chain_syncer/blob/syncer.go @@ -409,10 +409,15 @@ func (s *Syncer) insertNewHead( return nil, fmt.Errorf("failed to create execution payloads: %w", err) } + verifiedBlock, err := s.rpc.TaikoL1.GetLastVerifiedBlock(&bind.CallOpts{Context: s.ctx}) + if err != nil { + return nil, err + } + fc := &engine.ForkchoiceStateV1{ HeadBlockHash: payload.BlockHash, - SafeBlockHash: payload.BlockHash, - FinalizedBlockHash: payload.BlockHash, + SafeBlockHash: verifiedBlock.BlockHash, + FinalizedBlockHash: verifiedBlock.BlockHash, } // Update the fork choice From 00fa50388a812dab9720759c33c85b85c604d627 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 5 Oct 2024 08:46:44 +0800 Subject: [PATCH 2/2] feat: improve code --- .../driver/chain_syncer/beaconsync/syncer.go | 9 ++++----- .../taiko-client/driver/chain_syncer/blob/syncer.go | 8 ++++---- packages/taiko-client/pkg/rpc/methods.go | 13 +++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go b/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go index 03cc81c825..cc2c65a515 100644 --- a/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go +++ b/packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go @@ -5,7 +5,6 @@ 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" @@ -72,15 +71,15 @@ func (s *Syncer) TriggerBeaconSync(blockID uint64) error { return fmt.Errorf("unexpected NewPayload response status: %s", status.Status) } - verifiedBlock, err := s.rpc.TaikoL1.GetLastVerifiedBlock(&bind.CallOpts{Context: s.ctx}) + lastVerifiedBlockHash, err := s.rpc.GetLastVerifiedBlockHash(s.ctx) if err != nil { - return err + 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, - SafeBlockHash: verifiedBlock.BlockHash, - FinalizedBlockHash: verifiedBlock.BlockHash, + SafeBlockHash: lastVerifiedBlockHash, + FinalizedBlockHash: lastVerifiedBlockHash, }, nil) if err != nil { return err diff --git a/packages/taiko-client/driver/chain_syncer/blob/syncer.go b/packages/taiko-client/driver/chain_syncer/blob/syncer.go index c3dad62f48..c850bb52ad 100644 --- a/packages/taiko-client/driver/chain_syncer/blob/syncer.go +++ b/packages/taiko-client/driver/chain_syncer/blob/syncer.go @@ -409,15 +409,15 @@ func (s *Syncer) insertNewHead( return nil, fmt.Errorf("failed to create execution payloads: %w", err) } - verifiedBlock, err := s.rpc.TaikoL1.GetLastVerifiedBlock(&bind.CallOpts{Context: s.ctx}) + lastVerifiedBlockHash, err := s.rpc.GetLastVerifiedBlockHash(ctx) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to fetch the last verified block hash: %w", err) } fc := &engine.ForkchoiceStateV1{ HeadBlockHash: payload.BlockHash, - SafeBlockHash: verifiedBlock.BlockHash, - FinalizedBlockHash: verifiedBlock.BlockHash, + SafeBlockHash: lastVerifiedBlockHash, + FinalizedBlockHash: lastVerifiedBlockHash, } // Update the fork choice diff --git a/packages/taiko-client/pkg/rpc/methods.go b/packages/taiko-client/pkg/rpc/methods.go index d89b1457a7..7a747c419e 100644 --- a/packages/taiko-client/pkg/rpc/methods.go +++ b/packages/taiko-client/pkg/rpc/methods.go @@ -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)