From 55717c8ff8c9541640c5368c2e937451ae6af7aa Mon Sep 17 00:00:00 2001 From: maskpp Date: Tue, 2 Apr 2024 13:51:30 +0800 Subject: [PATCH] feat(driver): improve driver implementation (#672) --- cmd/utils/sub_command.go | 15 +++----- driver/chain_syncer/beaconsync/syncer.go | 28 ++++++-------- driver/chain_syncer/calldata/syncer.go | 7 ++-- driver/chain_syncer/calldata/syncer_test.go | 8 +--- driver/chain_syncer/chain_syncer.go | 19 +++++----- driver/chain_syncer/chain_syncer_test.go | 4 +- driver/config_test.go | 3 +- driver/driver.go | 41 ++++++++++----------- driver/driver_test.go | 15 +++----- driver/state/state.go | 16 +++----- integration_test/deploy_l1_contract.sh | 16 ++++---- integration_test/l1_env.sh | 2 +- internal/metrics/metrics.go | 5 +-- internal/testutils/helper.go | 4 +- internal/testutils/interfaces.go | 4 +- internal/utils/test_utils.go | 16 ++++++++ proposer/config_test.go | 3 +- proposer/proposer.go | 6 +-- proposer/proposer_test.go | 2 +- prover/config_test.go | 3 +- prover/prover.go | 8 ++-- 21 files changed, 104 insertions(+), 121 deletions(-) create mode 100644 internal/utils/test_utils.go diff --git a/cmd/utils/sub_command.go b/cmd/utils/sub_command.go index d4149a19e..787c6aac6 100644 --- a/cmd/utils/sub_command.go +++ b/cmd/utils/sub_command.go @@ -1,7 +1,6 @@ package utils import ( - "context" "os" "os/signal" "syscall" @@ -14,20 +13,17 @@ import ( ) type SubcommandApplication interface { - InitFromCli(context.Context, *cli.Context) error + InitFromCli(*cli.Context) error Name() string Start() error - Close(context.Context) + Close() } func SubcommandAction(app SubcommandApplication) cli.ActionFunc { return func(c *cli.Context) error { logger.InitLogger(c) - ctx, ctxClose := context.WithCancel(context.Background()) - defer ctxClose() - - if err := app.InitFromCli(ctx, c); err != nil { + if err := app.InitFromCli(c); err != nil { return err } @@ -38,14 +34,13 @@ func SubcommandAction(app SubcommandApplication) cli.ActionFunc { return err } - if err := metrics.Serve(ctx, c); err != nil { + if err := metrics.Serve(c); err != nil { log.Error("Starting metrics server error", "error", err) return err } defer func() { - ctxClose() - app.Close(ctx) + app.Close() log.Info("Application stopped", "name", app.Name()) }() diff --git a/driver/chain_syncer/beaconsync/syncer.go b/driver/chain_syncer/beaconsync/syncer.go index d263edc7b..6b5027d89 100644 --- a/driver/chain_syncer/beaconsync/syncer.go +++ b/driver/chain_syncer/beaconsync/syncer.go @@ -5,7 +5,6 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/beacon/engine" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" @@ -36,13 +35,13 @@ func NewSyncer( // TriggerBeaconSync triggers the L2 execution engine to start performing a beacon sync, if the // latest verified block has changed. -func (s *Syncer) TriggerBeaconSync() error { - blockID, latestVerifiedHeadPayload, err := s.getVerifiedBlockPayload(s.ctx) +func (s *Syncer) TriggerBeaconSync(blockID uint64) error { + latestVerifiedHeadPayload, err := s.getVerifiedBlockPayload(s.ctx, blockID) if err != nil { return err } - if !s.progressTracker.HeadChanged(blockID) { + if !s.progressTracker.HeadChanged(new(big.Int).SetUint64(blockID)) { log.Debug("Verified head has not changed", "blockID", blockID, "hash", latestVerifiedHeadPayload.BlockHash) return nil } @@ -79,7 +78,7 @@ func (s *Syncer) TriggerBeaconSync() error { } // Update sync status. - s.progressTracker.UpdateMeta(blockID, latestVerifiedHeadPayload.BlockHash) + s.progressTracker.UpdateMeta(new(big.Int).SetUint64(blockID), latestVerifiedHeadPayload.BlockHash) log.Info( "⛓️ Beacon sync triggered", @@ -92,29 +91,24 @@ func (s *Syncer) TriggerBeaconSync() error { // getVerifiedBlockPayload fetches the latest verified block's header, and converts it to an Engine API executable data, // which will be used to let the node start beacon syncing. -func (s *Syncer) getVerifiedBlockPayload(ctx context.Context) (*big.Int, *engine.ExecutableData, error) { - stateVars, err := s.rpc.GetProtocolStateVariables(&bind.CallOpts{Context: ctx}) +func (s *Syncer) getVerifiedBlockPayload(ctx context.Context, blockID uint64) (*engine.ExecutableData, error) { + blockInfo, err := s.rpc.GetL2BlockInfo(ctx, new(big.Int).SetUint64(blockID)) if err != nil { - return nil, nil, err + return nil, err } - blockInfo, err := s.rpc.GetL2BlockInfo(ctx, new(big.Int).SetUint64(stateVars.B.LastVerifiedBlockId)) + header, err := s.rpc.L2CheckPoint.HeaderByNumber(s.ctx, new(big.Int).SetUint64(blockID)) if err != nil { - return nil, nil, err - } - - header, err := s.rpc.L2CheckPoint.HeaderByNumber(s.ctx, new(big.Int).SetUint64(stateVars.B.LastVerifiedBlockId)) - if err != nil { - return nil, nil, err + return nil, err } if header.Hash() != blockInfo.Ts.BlockHash { - return nil, nil, fmt.Errorf( + return nil, fmt.Errorf( "latest verified block hash mismatch: %s != %s", header.Hash(), common.BytesToHash(blockInfo.Ts.BlockHash[:]), ) } log.Info("Latest verified block header retrieved", "hash", header.Hash()) - return new(big.Int).SetUint64(stateVars.B.LastVerifiedBlockId), encoding.ToExecutableData(header), nil + return encoding.ToExecutableData(header), nil } diff --git a/driver/chain_syncer/calldata/syncer.go b/driver/chain_syncer/calldata/syncer.go index 6b678ffe2..36480e9a6 100644 --- a/driver/chain_syncer/calldata/syncer.go +++ b/driver/chain_syncer/calldata/syncer.go @@ -79,9 +79,9 @@ func NewSyncer( // ProcessL1Blocks fetches all `TaikoL1.BlockProposed` events between given // L1 block heights, and then tries inserting them into L2 execution engine's blockchain. -func (s *Syncer) ProcessL1Blocks(ctx context.Context, l1End *types.Header) error { +func (s *Syncer) ProcessL1Blocks(ctx context.Context) error { for { - if err := s.processL1Blocks(ctx, l1End); err != nil { + if err := s.processL1Blocks(ctx); err != nil { return err } @@ -98,7 +98,8 @@ func (s *Syncer) ProcessL1Blocks(ctx context.Context, l1End *types.Header) error // processL1Blocks is the inner method which responsible for processing // all new L1 blocks. -func (s *Syncer) processL1Blocks(ctx context.Context, l1End *types.Header) error { +func (s *Syncer) processL1Blocks(ctx context.Context) error { + l1End := s.state.GetL1Head() startL1Current := s.state.GetL1Current() // If there is a L1 reorg, sometimes this will happen. if startL1Current.Number.Uint64() >= l1End.Number.Uint64() && startL1Current.Hash() != l1End.Hash() { diff --git a/driver/chain_syncer/calldata/syncer_test.go b/driver/chain_syncer/calldata/syncer_test.go index 4032c9f33..b7fe7077f 100644 --- a/driver/chain_syncer/calldata/syncer_test.go +++ b/driver/chain_syncer/calldata/syncer_test.go @@ -61,16 +61,12 @@ func (s *CalldataSyncerTestSuite) TestCancelNewSyncer() { } func (s *CalldataSyncerTestSuite) TestProcessL1Blocks() { - head, err := s.s.rpc.L1.HeaderByNumber(context.Background(), nil) - s.Nil(err) - s.Nil(s.s.ProcessL1Blocks(context.Background(), head)) + s.Nil(s.s.ProcessL1Blocks(context.Background())) } func (s *CalldataSyncerTestSuite) TestProcessL1BlocksReorg() { - head, err := s.s.rpc.L1.HeaderByNumber(context.Background(), nil) s.ProposeAndInsertEmptyBlocks(s.p, s.s) - s.Nil(err) - s.Nil(s.s.ProcessL1Blocks(context.Background(), head)) + s.Nil(s.s.ProcessL1Blocks(context.Background())) } func (s *CalldataSyncerTestSuite) TestOnBlockProposed() { diff --git a/driver/chain_syncer/chain_syncer.go b/driver/chain_syncer/chain_syncer.go index 903175f6b..39e114752 100644 --- a/driver/chain_syncer/chain_syncer.go +++ b/driver/chain_syncer/chain_syncer.go @@ -6,7 +6,6 @@ import ( "time" "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" "github.com/taikoxyz/taiko-client/driver/chain_syncer/beaconsync" @@ -64,8 +63,8 @@ func New( } // Sync performs a sync operation to L2 execution engine's local chain. -func (s *L2ChainSyncer) Sync(l1End *types.Header) error { - needNewBeaconSyncTriggered, err := s.needNewBeaconSyncTriggered() +func (s *L2ChainSyncer) Sync() error { + blockID, needNewBeaconSyncTriggered, err := s.needNewBeaconSyncTriggered() if err != nil { return err } @@ -73,7 +72,7 @@ func (s *L2ChainSyncer) Sync(l1End *types.Header) error { // `P2PSyncVerifiedBlocks` flag is set, try triggering a beacon sync in L2 execution engine to catch up the // latest verified block head. if needNewBeaconSyncTriggered { - if err := s.beaconSyncer.TriggerBeaconSync(); err != nil { + if err := s.beaconSyncer.TriggerBeaconSync(blockID); err != nil { return fmt.Errorf("trigger beacon sync error: %w", err) } @@ -113,7 +112,7 @@ func (s *L2ChainSyncer) Sync(l1End *types.Header) error { } // Insert the proposed block one by one. - return s.calldataSyncer.ProcessL1Blocks(s.ctx, l1End) + return s.calldataSyncer.ProcessL1Blocks(s.ctx) } // AheadOfProtocolVerifiedHead checks whether the L2 chain is ahead of verified head in protocol. @@ -150,23 +149,23 @@ func (s *L2ChainSyncer) AheadOfProtocolVerifiedHead(verifiedHeightToCompare uint // 2. The protocol's latest verified block head is not zero. // 3. The L2 execution engine's chain is behind of the protocol's latest verified block head. // 4. The L2 execution engine's chain have met a sync timeout issue. -func (s *L2ChainSyncer) needNewBeaconSyncTriggered() (bool, error) { +func (s *L2ChainSyncer) needNewBeaconSyncTriggered() (uint64, bool, error) { // If the flag is not set, we simply return false. if !s.p2pSyncVerifiedBlocks { - return false, nil + return 0, false, nil } stateVars, err := s.rpc.GetProtocolStateVariables(&bind.CallOpts{Context: s.ctx}) if err != nil { - return false, err + return 0, false, err } // If the protocol's latest verified block head is zero, we simply return false. if stateVars.B.LastVerifiedBlockId == 0 { - return false, nil + return 0, false, nil } - return !s.AheadOfProtocolVerifiedHead(stateVars.B.LastVerifiedBlockId) && + return stateVars.B.LastVerifiedBlockId, !s.AheadOfProtocolVerifiedHead(stateVars.B.LastVerifiedBlockId) && !s.progressTracker.OutOfSync(), nil } diff --git a/driver/chain_syncer/chain_syncer_test.go b/driver/chain_syncer/chain_syncer_test.go index da8524bbe..9d9baff58 100644 --- a/driver/chain_syncer/chain_syncer_test.go +++ b/driver/chain_syncer/chain_syncer_test.go @@ -96,9 +96,7 @@ func (s *ChainSyncerTestSuite) TestGetInnerSyncers() { } func (s *ChainSyncerTestSuite) TestSync() { - head, err := s.RPCClient.L1.HeaderByNumber(context.Background(), nil) - s.Nil(err) - s.Nil(s.s.Sync(head)) + s.Nil(s.s.Sync()) } func (s *ChainSyncerTestSuite) TestAheadOfProtocolVerifiedHead2() { diff --git a/driver/config_test.go b/driver/config_test.go index 5232aa03f..888e0557f 100644 --- a/driver/config_test.go +++ b/driver/config_test.go @@ -1,7 +1,6 @@ package driver import ( - "context" "os" "time" @@ -38,7 +37,7 @@ func (s *DriverTestSuite) TestNewConfigFromCliContext() { s.NotEmpty(c.JwtSecret) s.True(c.P2PSyncVerifiedBlocks) s.Equal(l2CheckPoint, c.L2CheckPoint) - s.NotNil(new(Driver).InitFromCli(context.Background(), ctx)) + s.NotNil(new(Driver).InitFromCli(ctx)) return err } diff --git a/driver/driver.go b/driver/driver.go index 0f2561270..753707c35 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -33,28 +33,26 @@ type Driver struct { l2ChainSyncer *chainSyncer.L2ChainSyncer state *state.State - l1HeadCh chan *types.Header - l1HeadSub event.Subscription - syncNotify chan struct{} + l1HeadCh chan *types.Header + l1HeadSub event.Subscription ctx context.Context wg sync.WaitGroup } // InitFromCli initializes the given driver instance based on the command line flags. -func (d *Driver) InitFromCli(ctx context.Context, c *cli.Context) error { +func (d *Driver) InitFromCli(c *cli.Context) error { cfg, err := NewConfigFromCliContext(c) if err != nil { return err } - return d.InitFromConfig(ctx, cfg) + return d.InitFromConfig(c.Context, cfg) } // InitFromConfig initializes the driver instance based on the given configurations. func (d *Driver) InitFromConfig(ctx context.Context, cfg *Config) (err error) { d.l1HeadCh = make(chan *types.Header, 1024) - d.syncNotify = make(chan struct{}, 1) d.ctx = ctx d.Config = cfg @@ -101,7 +99,7 @@ func (d *Driver) Start() error { } // Close closes the driver instance. -func (d *Driver) Close(_ context.Context) { +func (d *Driver) Close() { d.l1HeadSub.Unsubscribe() d.state.Close() d.wg.Wait() @@ -112,11 +110,12 @@ func (d *Driver) eventLoop() { d.wg.Add(1) defer d.wg.Done() + syncNotify := make(chan struct{}, 1) // reqSync requests performing a synchronising operation, won't block // if we are already synchronising. reqSync := func() { select { - case d.syncNotify <- struct{}{}: + case syncNotify <- struct{}{}: default: } } @@ -138,7 +137,7 @@ func (d *Driver) eventLoop() { select { case <-d.ctx.Done(): return - case <-d.syncNotify: + case <-syncNotify: doSyncWithBackoff() case <-d.l1HeadCh: reqSync() @@ -156,7 +155,7 @@ func (d *Driver) doSync() error { return nil } - if err := d.l2ChainSyncer.Sync(d.state.GetL1Head()); err != nil { + if err := d.l2ChainSyncer.Sync(); err != nil { log.Error("Process new L1 blocks error", "error", err) return err } @@ -239,18 +238,16 @@ func (d *Driver) exchangeTransitionConfigLoop() { case <-d.ctx.Done(): return case <-ticker.C: - func() { - tc, err := d.rpc.L2Engine.ExchangeTransitionConfiguration(d.ctx, &engine.TransitionConfigurationV1{ - TerminalTotalDifficulty: (*hexutil.Big)(common.Big0), - TerminalBlockHash: common.Hash{}, - TerminalBlockNumber: 0, - }) - if err != nil { - log.Error("Failed to exchange Transition Configuration", "error", err) - } else { - log.Debug("Exchanged transition config", "transitionConfig", tc) - } - }() + tc, err := d.rpc.L2Engine.ExchangeTransitionConfiguration(d.ctx, &engine.TransitionConfigurationV1{ + TerminalTotalDifficulty: (*hexutil.Big)(common.Big0), + TerminalBlockHash: common.Hash{}, + TerminalBlockNumber: 0, + }) + if err != nil { + log.Error("Failed to exchange Transition Configuration", "error", err) + } else { + log.Debug("Exchanged transition config", "transitionConfig", tc) + } } } } diff --git a/driver/driver_test.go b/driver/driver_test.go index 780b0b483..165ef7b05 100644 --- a/driver/driver_test.go +++ b/driver/driver_test.go @@ -58,13 +58,10 @@ func (s *DriverTestSuite) TestName() { } func (s *DriverTestSuite) TestProcessL1Blocks() { - l1Head1, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil) - s.Nil(err) - l2Head1, err := s.d.rpc.L2.HeaderByNumber(context.Background(), nil) s.Nil(err) - s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background(), l1Head1)) + s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background())) // Propose a valid L2 block s.ProposeAndInsertValidBlock(s.p, s.d.ChainSyncer().CalldataSyncer()) @@ -143,7 +140,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() { s.Greater(l1Head4.Number.Uint64(), l1Head2.Number.Uint64()) - s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background(), l1Head4)) + s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background())) l2Head3, err := s.d.rpc.L2.HeaderByNumber(context.Background(), nil) s.Nil(err) @@ -201,7 +198,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() { s.Greater(l1Head4.Number.Uint64(), l1Head3.Number.Uint64()) s.Less(l1Head4.Number.Uint64(), l1Head2.Number.Uint64()) - s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background(), l1Head4)) + s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background())) l2Head3, err := s.d.rpc.L2.HeaderByNumber(context.Background(), nil) s.Nil(err) @@ -258,7 +255,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() { s.Greater(l1Head4.Number.Uint64(), l1Head3.Number.Uint64()) - s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background(), l1Head4)) + s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background())) l2Head3, err := s.d.rpc.L2.HeaderByNumber(context.Background(), nil) s.Nil(err) @@ -271,13 +268,13 @@ func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() { } func (s *DriverTestSuite) TestDoSyncNoNewL2Blocks() { - s.Nil(s.d.doSync()) + s.Nil(s.d.l2ChainSyncer.Sync()) } func (s *DriverTestSuite) TestStartClose() { s.Nil(s.d.Start()) s.cancel() - s.d.Close(context.Background()) + s.d.Close() } func (s *DriverTestSuite) TestL1Current() { diff --git a/driver/state/state.go b/driver/state/state.go index 6e8e632c7..48f0351e6 100644 --- a/driver/state/state.go +++ b/driver/state/state.go @@ -22,10 +22,10 @@ type State struct { // Feeds l1HeadsFeed event.Feed // L1 new heads notification feed - l1Head *atomic.Value // Latest known L1 head - l2Head *atomic.Value // Current L2 execution engine's local chain head - l2HeadBlockID *atomic.Value // Latest known L2 block ID in protocol - l1Current *atomic.Value // Current L1 block sync cursor + l1Head atomic.Value // Latest known L1 head + l2Head atomic.Value // Current L2 execution engine's local chain head + l2HeadBlockID atomic.Value // Latest known L2 block ID in protocol + l1Current atomic.Value // Current L1 block sync cursor // Constants GenesisL1Height *big.Int @@ -40,12 +40,8 @@ type State struct { // New creates a new driver state instance. func New(ctx context.Context, rpc *rpc.Client) (*State, error) { s := &State{ - rpc: rpc, - l1Head: new(atomic.Value), - l2Head: new(atomic.Value), - l2HeadBlockID: new(atomic.Value), - l1Current: new(atomic.Value), - stopCh: make(chan struct{}), + rpc: rpc, + stopCh: make(chan struct{}), } if err := s.init(ctx); err != nil { diff --git a/integration_test/deploy_l1_contract.sh b/integration_test/deploy_l1_contract.sh index 4e3f44942..694c57452 100755 --- a/integration_test/deploy_l1_contract.sh +++ b/integration_test/deploy_l1_contract.sh @@ -9,11 +9,11 @@ source integration_test/l1_env.sh check_env "TAIKO_MONO_DIR" cd "$TAIKO_MONO_DIR"/packages/protocol && - forge script script/DeployOnL1.s.sol:DeployOnL1 \ - --fork-url "$L1_NODE_HTTP_ENDPOINT" \ - --broadcast \ - --ffi \ - -vvvvv \ - --evm-version cancun \ - --private-key "$PRIVATE_KEY" \ - --block-gas-limit 100000000 + forge script script/DeployOnL1.s.sol:DeployOnL1 \ + --fork-url "$L1_NODE_HTTP_ENDPOINT" \ + --broadcast \ + --ffi \ + -vvvvv \ + --evm-version cancun \ + --private-key "$PRIVATE_KEY" \ + --block-gas-limit 100000000 diff --git a/integration_test/l1_env.sh b/integration_test/l1_env.sh index 76b1fc923..7ef412926 100755 --- a/integration_test/l1_env.sh +++ b/integration_test/l1_env.sh @@ -27,5 +27,5 @@ export GUARDIAN_PROVERS=${GUARDIAN_PROVERS_ADDRESSES:1} export MIN_GUARDIANS=${#GUARDIAN_PROVERS_ADDRESSES_LIST[@]} # Get the hash of L2 genesis. -export L2_GENESIS_HASH=$(cast block --rpc-url "$L2_EXECUTION_ENGINE_HTTP_ENDPOINT" 0x0 -f hash) +export L2_GENESIS_HASH=$(cast block --rpc-url "$L2_EXECUTION_ENGINE_WS_ENDPOINT" 0x0 -f hash) echo "L2_GENESIS_HASH: $L2_GENESIS_HASH" diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 9b82b87a7..4ab7164a8 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -1,7 +1,6 @@ package metrics import ( - "context" "net" "net/http" "strconv" @@ -56,7 +55,7 @@ var ( // Serve starts the metrics server on the given address, will be closed when the given // context is cancelled. -func Serve(ctx context.Context, c *cli.Context) error { +func Serve(c *cli.Context) error { if !c.Bool(flags.MetricsEnabled.Name) { return nil } @@ -73,7 +72,7 @@ func Serve(ctx context.Context, c *cli.Context) error { } go func() { - <-ctx.Done() + <-c.Context.Done() if err := server.Close(); err != nil { log.Error("Failed to close metrics server", "error", err) } diff --git a/internal/testutils/helper.go b/internal/testutils/helper.go index 0a72da2d6..d127ef853 100644 --- a/internal/testutils/helper.go +++ b/internal/testutils/helper.go @@ -74,7 +74,7 @@ func (s *ClientTestSuite) ProposeAndInsertEmptyBlocks( defer cancel() s.Nil(backoff.Retry(func() error { - return calldataSyncer.ProcessL1Blocks(ctx, newL1Head) + return calldataSyncer.ProcessL1Blocks(ctx) }, backoff.NewExponentialBackOff())) s.Nil(s.RPCClient.WaitTillL2ExecutionEngineSynced(context.Background())) @@ -142,7 +142,7 @@ func (s *ClientTestSuite) ProposeAndInsertValidBlock( defer cancel() s.Nil(backoff.Retry(func() error { - return calldataSyncer.ProcessL1Blocks(ctx, newL1Head) + return calldataSyncer.ProcessL1Blocks(ctx) }, backoff.NewExponentialBackOff())) s.Nil(s.RPCClient.WaitTillL2ExecutionEngineSynced(context.Background())) diff --git a/internal/testutils/interfaces.go b/internal/testutils/interfaces.go index a6be5ef48..08ea83d29 100644 --- a/internal/testutils/interfaces.go +++ b/internal/testutils/interfaces.go @@ -3,13 +3,11 @@ package testutils import ( "context" - "github.com/ethereum/go-ethereum/core/types" - "github.com/taikoxyz/taiko-client/cmd/utils" ) type CalldataSyncer interface { - ProcessL1Blocks(ctx context.Context, l1End *types.Header) error + ProcessL1Blocks(ctx context.Context) error } type Proposer interface { diff --git a/internal/utils/test_utils.go b/internal/utils/test_utils.go new file mode 100644 index 000000000..a1ca5db35 --- /dev/null +++ b/internal/utils/test_utils.go @@ -0,0 +1,16 @@ +package utils + +import ( + "context" + "testing" + + "github.com/ethereum/go-ethereum/rpc" + "github.com/stretchr/testify/assert" +) + +// MineL1Block mines a block on the L1 chain. +func MineL1Block(t *testing.T, l1Client *rpc.Client) { + var blockID string + assert.Nil(t, l1Client.CallContext(context.Background(), &blockID, "evm_mine")) + assert.NotEmpty(t, blockID) +} diff --git a/proposer/config_test.go b/proposer/config_test.go index e2c9fa858..d30213585 100644 --- a/proposer/config_test.go +++ b/proposer/config_test.go @@ -1,7 +1,6 @@ package proposer import ( - "context" "fmt" "os" "strings" @@ -58,7 +57,7 @@ func (s *ProposerTestSuite) TestNewConfigFromCliContext() { s.Equal(c.ProverEndpoints[i].String(), e) } - s.Nil(new(Proposer).InitFromCli(context.Background(), ctx)) + s.Nil(new(Proposer).InitFromCli(ctx)) return nil } diff --git a/proposer/proposer.go b/proposer/proposer.go index 04e24c8cf..0a3ad20db 100644 --- a/proposer/proposer.go +++ b/proposer/proposer.go @@ -70,13 +70,13 @@ type Proposer struct { } // InitFromCli New initializes the given proposer instance based on the command line flags. -func (p *Proposer) InitFromCli(ctx context.Context, c *cli.Context) error { +func (p *Proposer) InitFromCli(c *cli.Context) error { cfg, err := NewConfigFromCliContext(c) if err != nil { return err } - return p.InitFromConfig(ctx, cfg) + return p.InitFromConfig(c.Context, cfg) } // InitFromConfig initializes the proposer instance based on the given configurations. @@ -209,7 +209,7 @@ func (p *Proposer) eventLoop() { } // Close closes the proposer instance. -func (p *Proposer) Close(_ context.Context) { +func (p *Proposer) Close() { p.wg.Wait() } diff --git a/proposer/proposer_test.go b/proposer/proposer_test.go index 56f58bebf..bbc21cf6c 100644 --- a/proposer/proposer_test.go +++ b/proposer/proposer_test.go @@ -184,7 +184,7 @@ func (s *ProposerTestSuite) TestUpdateProposingTicker() { func (s *ProposerTestSuite) TestStartClose() { s.Nil(s.p.Start()) s.cancel() - s.NotPanics(func() { s.p.Close(context.Background()) }) + s.NotPanics(func() { s.p.Close() }) } func TestProposerTestSuite(t *testing.T) { diff --git a/prover/config_test.go b/prover/config_test.go index a6214060d..2d1e15296 100644 --- a/prover/config_test.go +++ b/prover/config_test.go @@ -1,7 +1,6 @@ package prover import ( - "context" "fmt" "os" "time" @@ -53,7 +52,7 @@ func (s *ProverTestSuite) TestNewConfigFromCliContextGuardianProver() { s.Equal(uint64(minTierFee), c.MinSgxTierFee.Uint64()) s.Equal(c.L1NodeVersion, l1NodeVersion) s.Equal(c.L2NodeVersion, l2NodeVersion) - s.Nil(new(Prover).InitFromCli(context.Background(), ctx)) + s.Nil(new(Prover).InitFromCli(ctx)) s.True(c.ProveUnassignedBlocks) s.Equal(uint64(100), c.MaxProposedIn) s.Equal(os.Getenv("ASSIGNMENT_HOOK_ADDRESS"), c.AssignmentHookAddress.String()) diff --git a/prover/prover.go b/prover/prover.go index e5b98feb6..83ec015c6 100644 --- a/prover/prover.go +++ b/prover/prover.go @@ -78,13 +78,13 @@ type Prover struct { } // InitFromCli initializes the given prover instance based on the command line flags. -func (p *Prover) InitFromCli(ctx context.Context, c *cli.Context) error { +func (p *Prover) InitFromCli(c *cli.Context) error { cfg, err := NewConfigFromCliContext(c) if err != nil { return err } - return InitFromConfig(ctx, p, cfg) + return InitFromConfig(c.Context, p, cfg) } // InitFromConfig initializes the prover instance based on the given configurations. @@ -312,8 +312,8 @@ func (p *Prover) eventLoop() { } // Close closes the prover instance. -func (p *Prover) Close(ctx context.Context) { - if err := p.server.Shutdown(ctx); err != nil { +func (p *Prover) Close() { + if err := p.server.Shutdown(p.ctx); err != nil { log.Error("Failed to shut down prover server", "error", err) } p.wg.Wait()