Skip to content

Commit

Permalink
chore(taiko-client): make full sync to sync to the latest header (#18771
Browse files Browse the repository at this point in the history
)
  • Loading branch information
davidtaikocha authored Jan 16, 2025
1 parent c634425 commit 65f763b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 66 deletions.
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

0 comments on commit 65f763b

Please sign in to comment.