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/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..9f0921007 100644 --- a/driver/chain_syncer/beaconsync/syncer.go +++ b/driver/chain_syncer/beaconsync/syncer.go @@ -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" @@ -20,6 +21,7 @@ type Syncer struct { ctx context.Context rpc *rpc.Client state *state.State + syncMode string progressTracker *SyncProgressTracker // Sync progress tracker } @@ -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 @@ -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, ) } @@ -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 @@ -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[:]), ) } diff --git a/driver/chain_syncer/calldata/syncer.go b/driver/chain_syncer/calldata/syncer.go index 73116e748..92c2cd724 100644 --- a/driver/chain_syncer/calldata/syncer.go +++ b/driver/chain_syncer/calldata/syncer.go @@ -220,11 +220,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 8a90f6f81..ea87f6ec8 100644 --- a/driver/chain_syncer/chain_syncer.go +++ b/driver/chain_syncer/chain_syncer.go @@ -7,6 +7,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" @@ -29,6 +30,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 @@ -48,7 +52,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, blobServerEndpoint) if err != nil { return nil, err @@ -61,6 +69,7 @@ func New( beaconSyncer: beaconSyncer, calldataSyncer: calldataSyncer, progressTracker: tracker, + syncMode: syncMode, p2pSyncVerifiedBlocks: p2pSyncVerifiedBlocks, }, nil } @@ -101,8 +110,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. @@ -139,8 +148,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 @@ -158,17 +167,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 } 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= diff --git a/prover/event_handler/transition_proved_test.go b/prover/event_handler/transition_proved_test.go index 37c17b079..edc5c15ba 100644 --- a/prover/event_handler/transition_proved_test.go +++ b/prover/event_handler/transition_proved_test.go @@ -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" @@ -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 {