Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

feat(driver): add syncmode #695

Merged
merged 10 commits into from
Apr 8, 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
2 changes: 1 addition & 1 deletion bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a05e7b209611404d64579982d6769ebbf70506c1
b341a68d53b01087a6ce56a629b064d0b808b0bb
36 changes: 18 additions & 18 deletions driver/chain_syncer/beaconsync/progress_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type SyncProgressTracker struct {
client *rpc.EthClient

// Meta data
triggered bool
lastSyncedVerifiedBlockID *big.Int
lastSyncedVerifiedBlockHash common.Hash
triggered bool
lastSyncedBlockID *big.Int
lastSyncedBlockHash common.Hash

// Out-of-sync check related
lastSyncProgress *ethereum.SyncProgress
Expand Down Expand Up @@ -94,13 +94,13 @@ func (t *SyncProgressTracker) track(ctx context.Context) {
return
}

if new(big.Int).SetUint64(headHeight).Cmp(t.lastSyncedVerifiedBlockID) >= 0 {
if new(big.Int).SetUint64(headHeight).Cmp(t.lastSyncedBlockID) >= 0 {
t.lastProgressedTime = time.Now()
log.Info(
"L2 execution engine has finished the P2P sync work, all verified blocks synced, "+
"will switch to insert pending blocks one by one",
"lastSyncedVerifiedBlockID", t.lastSyncedVerifiedBlockID,
"lastSyncedVerifiedBlockHash", t.lastSyncedVerifiedBlockHash,
"lastSyncedBlockID", t.lastSyncedBlockID,
"lastSyncedBlockHash", t.lastSyncedBlockHash,
)
return
}
Expand Down Expand Up @@ -142,8 +142,8 @@ func (t *SyncProgressTracker) UpdateMeta(id *big.Int, blockHash common.Hash) {
}

t.triggered = true
t.lastSyncedVerifiedBlockID = id
t.lastSyncedVerifiedBlockHash = blockHash
t.lastSyncedBlockID = id
t.lastSyncedBlockHash = blockHash
}

// ClearMeta cleans the inner beacon sync metadata.
Expand All @@ -154,8 +154,8 @@ func (t *SyncProgressTracker) ClearMeta() {
log.Debug("Clear sync progress tracker meta")

t.triggered = false
t.lastSyncedVerifiedBlockID = nil
t.lastSyncedVerifiedBlockHash = common.Hash{}
t.lastSyncedBlockID = nil
t.lastSyncedBlockHash = common.Hash{}
t.outOfSync = false
}

Expand All @@ -168,7 +168,7 @@ func (t *SyncProgressTracker) HeadChanged(newID *big.Int) bool {
return true
}

return t.lastSyncedVerifiedBlockID != nil && t.lastSyncedVerifiedBlockID != newID
return t.lastSyncedBlockID != nil && t.lastSyncedBlockID != newID
}

// OutOfSync tells whether the L2 execution engine is marked as out of sync.
Expand All @@ -187,24 +187,24 @@ func (t *SyncProgressTracker) Triggered() bool {
return t.triggered
}

// LastSyncedVerifiedBlockID returns tracker.lastSyncedVerifiedBlockID.
func (t *SyncProgressTracker) LastSyncedVerifiedBlockID() *big.Int {
// LastSyncedBlockID returns tracker.lastSyncedBlockID.
func (t *SyncProgressTracker) LastSyncedBlockID() *big.Int {
t.mutex.RLock()
defer t.mutex.RUnlock()

if t.lastSyncedVerifiedBlockID == nil {
if t.lastSyncedBlockID == nil {
return nil
}

return new(big.Int).Set(t.lastSyncedVerifiedBlockID)
return new(big.Int).Set(t.lastSyncedBlockID)
}

// LastSyncedVerifiedBlockHash returns tracker.lastSyncedVerifiedBlockHash.
func (t *SyncProgressTracker) LastSyncedVerifiedBlockHash() common.Hash {
// LastSyncedBlockHash returns tracker.lastSyncedBlockHash.
func (t *SyncProgressTracker) LastSyncedBlockHash() common.Hash {
t.mutex.RLock()
defer t.mutex.RUnlock()

return t.lastSyncedVerifiedBlockHash
return t.lastSyncedBlockHash
}

// syncProgressed checks whether there is any new progress since last sync progress check.
Expand Down
14 changes: 7 additions & 7 deletions driver/chain_syncer/beaconsync/progress_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ func (s *BeaconSyncProgressTrackerTestSuite) TestTriggered() {
s.False(s.t.Triggered())
}

func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedVerifiedBlockID() {
s.Nil(s.t.LastSyncedVerifiedBlockID())
s.t.lastSyncedVerifiedBlockID = common.Big1
s.Equal(common.Big1.Uint64(), s.t.LastSyncedVerifiedBlockID().Uint64())
func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedBlockID() {
s.Nil(s.t.LastSyncedBlockID())
s.t.lastSyncedBlockID = common.Big1
s.Equal(common.Big1.Uint64(), s.t.LastSyncedBlockID().Uint64())
}

func (s *BeaconSyncProgressTrackerTestSuite) TestLastSyncedVerifiedBlockHash() {
s.Equal(
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
s.t.LastSyncedVerifiedBlockHash(),
s.t.LastSyncedBlockHash(),
)
randomHash := testutils.RandomHash()
s.t.lastSyncedVerifiedBlockHash = randomHash
s.Equal(randomHash, s.t.LastSyncedVerifiedBlockHash())
s.t.lastSyncedBlockHash = randomHash
s.Equal(randomHash, s.t.LastSyncedBlockHash())
}

func TestBeaconSyncProgressTrackerTestSuite(t *testing.T) {
Expand Down
15 changes: 10 additions & 5 deletions driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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

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

// TriggerBeaconSync triggers the L2 execution engine to start performing a beacon sync, if the
Expand All @@ -50,7 +53,7 @@ func (s *Syncer) TriggerBeaconSync(blockID uint64) error {
if s.progressTracker.lastSyncProgress == nil {
log.Info(
"Syncing beacon headers, please check L2 execution engine logs for progress",
"currentSyncHead", s.progressTracker.LastSyncedVerifiedBlockID(),
"currentSyncHead", s.progressTracker.LastSyncedBlockID(),
"newBlockID", blockID,
)
}
Expand Down Expand Up @@ -83,7 +86,7 @@ func (s *Syncer) TriggerBeaconSync(blockID uint64) error {
log.Info(
"⛓️ Beacon sync triggered",
"newHeadID", blockID,
"newHeadHash", s.progressTracker.LastSyncedVerifiedBlockHash(),
"newHeadHash", s.progressTracker.LastSyncedBlockHash(),
)

return nil
Expand All @@ -102,9 +105,11 @@ func (s *Syncer) getVerifiedBlockPayload(ctx context.Context, blockID uint64) (*
return nil, err
}

if header.Hash() != blockInfo.Ts.BlockHash {
if s.syncMode == downloader.FullSync.String() && header.Hash() != blockInfo.Ts.BlockHash {
return nil, fmt.Errorf(
"latest verified block hash mismatch: %s != %s", header.Hash(), common.BytesToHash(blockInfo.Ts.BlockHash[:]),
"latest verified block hash mismatch: %s != %s",
header.Hash(),
common.BytesToHash(blockInfo.Ts.BlockHash[:]),
)
}

Expand Down
4 changes: 2 additions & 2 deletions driver/chain_syncer/calldata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ func (s *Syncer) onBlockProposed(
)
if s.progressTracker.Triggered() {
// Already synced through beacon sync, just skip this event.
if event.BlockId.Cmp(s.progressTracker.LastSyncedVerifiedBlockID()) <= 0 {
if event.BlockId.Cmp(s.progressTracker.LastSyncedBlockID()) <= 0 {
return nil
}

parent, err = s.rpc.L2.HeaderByHash(ctx, s.progressTracker.LastSyncedVerifiedBlockHash())
parent, err = s.rpc.L2.HeaderByHash(ctx, s.progressTracker.LastSyncedBlockHash())
} else {
parent, err = s.rpc.L2ParentByBlockID(ctx, event.BlockId)
}
Expand Down
47 changes: 36 additions & 11 deletions driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"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-client/driver/chain_syncer/beaconsync"
Expand All @@ -28,6 +29,9 @@ 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
p2pSyncVerifiedBlocks bool
Expand All @@ -45,7 +49,11 @@ func New(
tracker := beaconsync.NewSyncProgressTracker(rpc.L2, p2pSyncTimeout)
go tracker.Track(ctx)

beaconSyncer := beaconsync.NewSyncer(ctx, rpc, state, tracker)
syncMode, err := rpc.L2.GetSyncMode(ctx)
if err != nil {
return nil, err
}
beaconSyncer := beaconsync.NewSyncer(ctx, rpc, state, syncMode, tracker)
calldataSyncer, err := calldata.NewSyncer(ctx, rpc, state, tracker, maxRetrieveExponent)
if err != nil {
return nil, err
Expand All @@ -58,6 +66,7 @@ func New(
beaconSyncer: beaconSyncer,
calldataSyncer: calldataSyncer,
progressTracker: tracker,
syncMode: syncMode,
p2pSyncVerifiedBlocks: p2pSyncVerifiedBlocks,
}, nil
}
Expand Down Expand Up @@ -98,8 +107,8 @@ func (s *L2ChainSyncer) Sync() error {
"L2 head information",
"number", l2Head.Number,
"hash", l2Head.Hash(),
"lastSyncedVerifiedBlockID", s.progressTracker.LastSyncedVerifiedBlockID(),
"lastSyncedVerifiedBlockHash", s.progressTracker.LastSyncedVerifiedBlockHash(),
"lastSyncedVerifiedBlockID", s.progressTracker.LastSyncedBlockID(),
"lastSyncedVerifiedBlockHash", s.progressTracker.LastSyncedBlockHash(),
)

// Reset the L1Current cursor.
Expand Down Expand Up @@ -136,8 +145,8 @@ func (s *L2ChainSyncer) AheadOfProtocolVerifiedHead(verifiedHeightToCompare uint
return false
}

if s.progressTracker.LastSyncedVerifiedBlockID() != nil {
return s.state.GetL2Head().Number.Uint64() >= s.progressTracker.LastSyncedVerifiedBlockID().Uint64()
if s.progressTracker.LastSyncedBlockID() != nil {
return s.state.GetL2Head().Number.Uint64() >= s.progressTracker.LastSyncedBlockID().Uint64()
}

return true
Expand All @@ -155,17 +164,33 @@ func (s *L2ChainSyncer) needNewBeaconSyncTriggered() (uint64, bool, error) {
return 0, false, nil
}

stateVars, err := s.rpc.GetProtocolStateVariables(&bind.CallOpts{Context: s.ctx})
if err != nil {
return 0, false, err
// full sync mode will use the verified block head.
// snap sync mode 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)
}

// If the protocol's latest verified block head is zero, we simply return false.
if stateVars.B.LastVerifiedBlockId == 0 {
// If the protocol's block head is zero, we simply return false.
if blockID == 0 {
return 0, false, nil
}

return stateVars.B.LastVerifiedBlockId, !s.AheadOfProtocolVerifiedHead(stateVars.B.LastVerifiedBlockId) &&
return blockID, !s.AheadOfProtocolVerifiedHead(blockID) &&
!s.progressTracker.OutOfSync(), nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,6 @@ require (
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/ethereum/go-ethereum v1.13.14 => github.com/taikoxyz/taiko-geth v0.0.0-20240403070732-2eac3d10ea69
replace github.com/ethereum/go-ethereum v1.13.14 => github.com/taikoxyz/taiko-geth v0.0.0-20240408001118-a58b085df11d

replace github.com/ethereum-optimism/optimism v1.7.0 => github.com/taikoxyz/optimism v0.0.0-20240402022152-070fc9dba2ec
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,6 @@ github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtB
github.com/raulk/go-watchdog v1.3.0/go.mod h1:fIvOnLbF0b0ZwkB9YU4mOW9Did//4vPZtDqv66NfsMU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down Expand Up @@ -962,8 +960,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d h1:vfofYNRScrDd
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d/go.mod h1:RRCYJbIwD5jmqPI9XoAFR0OcDxqUctll6zUj/+B4S48=
github.com/taikoxyz/optimism v0.0.0-20240402022152-070fc9dba2ec h1:3CwBNzTe8gl+enXcbsQrbh4RObS/UCWvZ3dtMrdiuEg=
github.com/taikoxyz/optimism v0.0.0-20240402022152-070fc9dba2ec/go.mod h1:X4jEuxN69o7ZVG20Yt3joIOaCDKb1G/dPYVjnR3XxrU=
github.com/taikoxyz/taiko-geth v0.0.0-20240403070732-2eac3d10ea69 h1:kl830bUZwaIC+csxgFYBlovSjc+3cW2DVmNn1WqNaso=
github.com/taikoxyz/taiko-geth v0.0.0-20240403070732-2eac3d10ea69/go.mod h1:nqByouVW0a0qx5KKgvYgoXba+pYEHznAAQp6LhZilgM=
github.com/taikoxyz/taiko-geth v0.0.0-20240408001118-a58b085df11d h1:7XFzQm//BpLXNvbOfur0/WeKavCgyuX2E5h8WapUka8=
github.com/taikoxyz/taiko-geth v0.0.0-20240408001118-a58b085df11d/go.mod h1:nqByouVW0a0qx5KKgvYgoXba+pYEHznAAQp6LhZilgM=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
Expand Down Expand Up @@ -1578,8 +1576,6 @@ k8s.io/utils v0.0.0-20201110183641-67b214c5f920 h1:CbnUZsM497iRC5QMVkHwyl8s2tB3g
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
lukechampine.com/blake3 v1.2.1 h1:YuqqRuaqsGV71BV/nm9xlI0MKUv4QC54jQnBChWbGnI=
lukechampine.com/blake3 v1.2.1/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
4 changes: 2 additions & 2 deletions prover/event_handler/transition_proved_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"testing"
"time"

proofProducer "github.com/taikoxyz/taiko-client/prover/proof_producer"

"github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/suite"

"github.com/taikoxyz/taiko-client/bindings"
"github.com/taikoxyz/taiko-client/driver"
"github.com/taikoxyz/taiko-client/driver/chain_syncer/beaconsync"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/taikoxyz/taiko-client/pkg/jwt"
"github.com/taikoxyz/taiko-client/pkg/rpc"
"github.com/taikoxyz/taiko-client/proposer"
proofProducer "github.com/taikoxyz/taiko-client/prover/proof_producer"
)

type EventHandlerTestSuite struct {
Expand Down
Loading