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

Commit

Permalink
add syncmode
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp committed Apr 8, 2024
1 parent ba40ced commit 53be0ce
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 44 deletions.
2 changes: 1 addition & 1 deletion bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a05e7b209611404d64579982d6769ebbf70506c1
b341a68d53b01087a6ce56a629b064d0b808b0bb
15 changes: 15 additions & 0 deletions cmd/flags/driver.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package flags

import (
"errors"
"time"

"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/urfave/cli/v2"
)

// Optional flags used by driver.
var (
SyncModeFlag = &cli.StringFlag{
Name: "syncmode",
Usage: `Blockchain sync mode ("snap" or "full")`,
Value: "snap",
Category: driverCategory,
Action: func(_ *cli.Context, s string) error {
if s != downloader.SnapSync.String() && s != downloader.FullSync.String() {
return errors.New("invalid sync mode")
}
return nil
},
}
P2PSyncVerifiedBlocks = &cli.BoolFlag{
Name: "p2p.syncVerifiedBlocks",
Usage: "Try P2P syncing verified blocks between L2 execution engines, " +
Expand Down Expand Up @@ -43,6 +57,7 @@ var DriverFlags = MergeFlags(CommonFlags, []cli.Flag{
L2WSEndpoint,
L2AuthEndpoint,
JWTSecret,
SyncModeFlag,
P2PSyncVerifiedBlocks,
P2PSyncTimeout,
CheckPointSyncURL,
Expand Down
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
4 changes: 2 additions & 2 deletions driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 +83,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 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
32 changes: 25 additions & 7 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 @@ -38,6 +42,7 @@ func New(
ctx context.Context,
rpc *rpc.Client,
state *state.State,
syncMode string,
p2pSyncVerifiedBlocks bool,
p2pSyncTimeout time.Duration,
maxRetrieveExponent uint64,
Expand All @@ -58,6 +63,7 @@ func New(
beaconSyncer: beaconSyncer,
calldataSyncer: calldataSyncer,
progressTracker: tracker,
syncMode: syncMode,
p2pSyncVerifiedBlocks: p2pSyncVerifiedBlocks,
}, nil
}
Expand Down Expand Up @@ -98,8 +104,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 +142,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 @@ -160,12 +166,24 @@ func (s *L2ChainSyncer) needNewBeaconSyncTriggered() (uint64, bool, error) {
return 0, false, err
}

// If the protocol's latest verified block head is zero, we simply return false.
if stateVars.B.LastVerifiedBlockId == 0 {
// full sync mode will use the verified block head.
// snap sync mode will use the latest block head.
var blockID uint64
switch s.syncMode {
case downloader.SnapSync.String():
blockID = stateVars.B.NumBlocks
case downloader.FullSync.String():
blockID = stateVars.B.LastVerifiedBlockId
default:
return 0, false, fmt.Errorf("invalid sync mode: %s", s.syncMode)
}

// 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: 2 additions & 0 deletions driver/chain_syncer/chain_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/suite"

Expand All @@ -38,6 +39,7 @@ func (s *ChainSyncerTestSuite) SetupTest() {
context.Background(),
s.RPCClient,
state,
downloader.FullSync.String(),
false,
1*time.Hour,
0,
Expand Down
2 changes: 2 additions & 0 deletions driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// Config contains the configurations to initialize a Taiko driver.
type Config struct {
*rpc.ClientConfig
SyncMode string
P2PSyncVerifiedBlocks bool
P2PSyncTimeout time.Duration
RPCTimeout time.Duration
Expand Down Expand Up @@ -57,6 +58,7 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
JwtSecret: string(jwtSecret),
Timeout: timeout,
},
SyncMode: c.String(flags.SyncModeFlag.Name),
RetryInterval: c.Duration(flags.BackOffRetryInterval.Name),
P2PSyncVerifiedBlocks: p2pSyncVerifiedBlocks,
P2PSyncTimeout: c.Duration(flags.P2PSyncTimeout.Name),
Expand Down
9 changes: 9 additions & 0 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver

import (
"context"
"errors"
"sync"
"time"

Expand Down Expand Up @@ -60,6 +61,13 @@ func (d *Driver) InitFromConfig(ctx context.Context, cfg *Config) (err error) {
return err
}

// check the sync mode of the L2 node.
if syncMode, err := d.rpc.L2.GetSyncMode(d.ctx); err != nil {
return err
} else if syncMode != d.SyncMode {
return errors.New("the type is inconsistent with taiko-geth's sync mode")
}

if d.state, err = state.New(d.ctx, d.rpc); err != nil {
return err
}
Expand All @@ -77,6 +85,7 @@ func (d *Driver) InitFromConfig(ctx context.Context, cfg *Config) (err error) {
d.ctx,
d.rpc,
d.state,
d.SyncMode,
cfg.P2PSyncVerifiedBlocks,
cfg.P2PSyncTimeout,
cfg.MaxExponent,
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

0 comments on commit 53be0ce

Please sign in to comment.