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

chore(taiko-client): make full sync to sync to the latest header #18771

Merged
merged 2 commits into from
Jan 16, 2025
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
37 changes: 1 addition & 36 deletions packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import (
"math/big"

"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 All @@ -22,7 +19,6 @@ type Syncer struct {
ctx context.Context
rpc *rpc.Client
state *state.State
syncMode string
progressTracker *SyncProgressTracker // Sync progress tracker
}

Expand All @@ -31,10 +27,9 @@ func NewSyncer(
ctx context.Context,
rpc *rpc.Client,
state *state.State,
syncMode string,
progressTracker *SyncProgressTracker,
) *Syncer {
return &Syncer{ctx, rpc, state, syncMode, progressTracker}
return &Syncer{ctx, rpc, state, progressTracker}
}

// TriggerBeaconSync triggers the L2 execution engine to start performing a beacon sync, if the
Expand Down Expand Up @@ -101,36 +96,6 @@ func (s *Syncer) getBlockPayload(ctx context.Context, blockID uint64) (*engine.E
return nil, err
}

// If the sync mode is `full`, we need to verify the protocol verified block hash before syncing.
if s.syncMode == downloader.FullSync.String() {
blockNum := new(big.Int).SetUint64(blockID)
var blockInfo bindings.TaikoDataBlockV2
if s.state.IsOnTake(blockNum) {
blockInfo, err = s.rpc.GetL2BlockInfoV2(ctx, blockNum)
} else {
blockInfo, err = s.rpc.GetL2BlockInfo(ctx, blockNum)
}
if err != nil {
return nil, err
}
ts, err := s.rpc.GetTransition(
ctx,
new(big.Int).SetUint64(blockInfo.BlockId),

uint32(blockInfo.VerifiedTransitionId.Uint64()),
)
if err != nil {
return nil, err
}
if header.Hash() != ts.BlockHash {
return nil, fmt.Errorf(
"latest verified block hash mismatch: %s != %s",
header.Hash(),
common.BytesToHash(ts.BlockHash[:]),
)
}
}

log.Info("Block header to sync retrieved", "hash", header.Hash())

return encoding.ToExecutableData(header), nil
Expand Down
34 changes: 4 additions & 30 deletions packages/taiko-client/driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"net/url"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/log"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/driver/chain_syncer/beaconsync"
Expand All @@ -30,9 +28,6 @@ type L2ChainSyncer struct {
// Monitors
progressTracker *beaconsync.SyncProgressTracker

// Sync mode
syncMode string

// If this flag is activated, will try P2P beacon sync if current node is behind of the protocol's
// the latest verified block head
p2pSync bool
Expand All @@ -52,11 +47,7 @@ func New(
tracker := beaconsync.NewSyncProgressTracker(rpc.L2, p2pSyncTimeout)
go tracker.Track(ctx)

syncMode, err := rpc.L2.GetSyncMode(ctx)
if err != nil {
return nil, err
}
beaconSyncer := beaconsync.NewSyncer(ctx, rpc, state, syncMode, tracker)
beaconSyncer := beaconsync.NewSyncer(ctx, rpc, state, tracker)
blobSyncer, err := blob.NewSyncer(
ctx,
rpc,
Expand All @@ -77,7 +68,6 @@ func New(
beaconSyncer: beaconSyncer,
blobSyncer: blobSyncer,
progressTracker: tracker,
syncMode: syncMode,
p2pSync: p2pSync,
}, nil
}
Expand Down Expand Up @@ -193,25 +183,9 @@ func (s *L2ChainSyncer) needNewBeaconSyncTriggered() (uint64, bool, error) {
return 0, false, nil
}

// For full sync mode, we will use the verified block head,
// and for snap sync mode, we will use the latest block head.
var (
blockID uint64
err error
)
switch s.syncMode {
case downloader.SnapSync.String():
if blockID, err = s.rpc.L2CheckPoint.BlockNumber(s.ctx); err != nil {
return 0, false, err
}
case downloader.FullSync.String():
stateVars, err := s.rpc.GetProtocolStateVariables(&bind.CallOpts{Context: s.ctx})
if err != nil {
return 0, false, err
}
blockID = stateVars.B.LastVerifiedBlockId
default:
return 0, false, fmt.Errorf("invalid sync mode: %s", s.syncMode)
blockID, err := s.rpc.L2CheckPoint.BlockNumber(s.ctx)
if err != nil {
return 0, false, err
}

// If the protocol's block head is zero, we simply return false.
Expand Down
Loading