From 53be0cefe90f9e2a3aaa4a3377a18948b503fbce Mon Sep 17 00:00:00 2001 From: maskpp Date: Mon, 8 Apr 2024 08:46:33 +0800 Subject: [PATCH] add syncmode --- bindings/.githead | 2 +- cmd/flags/driver.go | 15 ++++++++ .../beaconsync/progress_tracker.go | 36 +++++++++---------- .../beaconsync/progress_tracker_test.go | 14 ++++---- driver/chain_syncer/beaconsync/syncer.go | 4 +-- driver/chain_syncer/calldata/syncer.go | 4 +-- driver/chain_syncer/chain_syncer.go | 32 +++++++++++++---- driver/chain_syncer/chain_syncer_test.go | 2 ++ driver/config.go | 2 ++ driver/driver.go | 9 +++++ go.mod | 2 +- go.sum | 8 ++--- 12 files changed, 86 insertions(+), 44 deletions(-) diff --git a/bindings/.githead b/bindings/.githead index 52773f9ff..aeae30be4 100644 --- a/bindings/.githead +++ b/bindings/.githead @@ -1 +1 @@ -a05e7b209611404d64579982d6769ebbf70506c1 +b341a68d53b01087a6ce56a629b064d0b808b0bb diff --git a/cmd/flags/driver.go b/cmd/flags/driver.go index ef52c69fc..6b9cccf1c 100644 --- a/cmd/flags/driver.go +++ b/cmd/flags/driver.go @@ -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, " + @@ -43,6 +57,7 @@ var DriverFlags = MergeFlags(CommonFlags, []cli.Flag{ L2WSEndpoint, L2AuthEndpoint, JWTSecret, + SyncModeFlag, P2PSyncVerifiedBlocks, P2PSyncTimeout, CheckPointSyncURL, diff --git a/driver/chain_syncer/beaconsync/progress_tracker.go b/driver/chain_syncer/beaconsync/progress_tracker.go index 1948b8ddf..6135f8f9f 100644 --- a/driver/chain_syncer/beaconsync/progress_tracker.go +++ b/driver/chain_syncer/beaconsync/progress_tracker.go @@ -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 @@ -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 } @@ -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. @@ -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 } @@ -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. @@ -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. diff --git a/driver/chain_syncer/beaconsync/progress_tracker_test.go b/driver/chain_syncer/beaconsync/progress_tracker_test.go index 9d95df111..aec59e245 100644 --- a/driver/chain_syncer/beaconsync/progress_tracker_test.go +++ b/driver/chain_syncer/beaconsync/progress_tracker_test.go @@ -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) { diff --git a/driver/chain_syncer/beaconsync/syncer.go b/driver/chain_syncer/beaconsync/syncer.go index 6b5027d89..e239aabff 100644 --- a/driver/chain_syncer/beaconsync/syncer.go +++ b/driver/chain_syncer/beaconsync/syncer.go @@ -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, ) } @@ -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 diff --git a/driver/chain_syncer/calldata/syncer.go b/driver/chain_syncer/calldata/syncer.go index 7fa5ec4f8..a79aa3a71 100644 --- a/driver/chain_syncer/calldata/syncer.go +++ b/driver/chain_syncer/calldata/syncer.go @@ -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) } diff --git a/driver/chain_syncer/chain_syncer.go b/driver/chain_syncer/chain_syncer.go index 39e114752..cff727d48 100644 --- a/driver/chain_syncer/chain_syncer.go +++ b/driver/chain_syncer/chain_syncer.go @@ -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" @@ -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 @@ -38,6 +42,7 @@ func New( ctx context.Context, rpc *rpc.Client, state *state.State, + syncMode string, p2pSyncVerifiedBlocks bool, p2pSyncTimeout time.Duration, maxRetrieveExponent uint64, @@ -58,6 +63,7 @@ func New( beaconSyncer: beaconSyncer, calldataSyncer: calldataSyncer, progressTracker: tracker, + syncMode: syncMode, p2pSyncVerifiedBlocks: p2pSyncVerifiedBlocks, }, nil } @@ -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. @@ -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 @@ -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 } diff --git a/driver/chain_syncer/chain_syncer_test.go b/driver/chain_syncer/chain_syncer_test.go index ede2bc863..a7f1c322d 100644 --- a/driver/chain_syncer/chain_syncer_test.go +++ b/driver/chain_syncer/chain_syncer_test.go @@ -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" @@ -38,6 +39,7 @@ func (s *ChainSyncerTestSuite) SetupTest() { context.Background(), s.RPCClient, state, + downloader.FullSync.String(), false, 1*time.Hour, 0, diff --git a/driver/config.go b/driver/config.go index a1d906365..a3310e0f6 100644 --- a/driver/config.go +++ b/driver/config.go @@ -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 @@ -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), diff --git a/driver/driver.go b/driver/driver.go index 753707c35..0fde82544 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -2,6 +2,7 @@ package driver import ( "context" + "errors" "sync" "time" @@ -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 } @@ -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, diff --git a/go.mod b/go.mod index bdcebe3d1..9d1f9f4eb 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c79a70fc4..dc9b2bd93 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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=