From 1c9d70a8cc8494944a0c40b4833eaf36d4111292 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 26 Oct 2023 20:00:20 +0800 Subject: [PATCH] test: more driver tests --- driver/chain_syncer/chain_syncer.go | 12 +-- driver/driver_test.go | 4 +- driver/state/l1_current.go | 80 ++++--------------- driver/state/l1_current_test.go | 14 +--- driver/state/state.go | 11 --- driver/state/state_test.go | 6 -- .../prover_selector/eth_fee_eoa_selector.go | 8 +- 7 files changed, 28 insertions(+), 107 deletions(-) diff --git a/driver/chain_syncer/chain_syncer.go b/driver/chain_syncer/chain_syncer.go index 6b0cded7f..61a9fca11 100644 --- a/driver/chain_syncer/chain_syncer.go +++ b/driver/chain_syncer/chain_syncer.go @@ -98,21 +98,13 @@ func (s *L2ChainSyncer) Sync(l1End *types.Header) error { "lastSyncedVerifiedBlockHash", s.progressTracker.LastSyncedVerifiedBlockHash(), ) - heightOrID := &state.HeightOrID{Height: l2Head.Number} - - // If the L2 execution engine has synced to latest verified block. - if l2Head.Hash() == s.progressTracker.LastSyncedVerifiedBlockHash() { - heightOrID.ID = s.progressTracker.LastSyncedVerifiedBlockID() - } - // Reset the L1Current cursor. - _, blockID, err := s.state.ResetL1Current(s.ctx, heightOrID) - if err != nil { + if _, err := s.state.ResetL1Current(s.ctx, l2Head.Number); err != nil { return err } // Reset to the latest L2 execution engine's chain status. - s.progressTracker.UpdateMeta(blockID, heightOrID.Height, l2Head.Hash()) + s.progressTracker.UpdateMeta(l2Head.Number, l2Head.Number, l2Head.Hash()) } // Insert the proposed block one by one. diff --git a/driver/driver_test.go b/driver/driver_test.go index e39137e42..4ae933e72 100644 --- a/driver/driver_test.go +++ b/driver/driver_test.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/suite" "github.com/taikoxyz/taiko-client/bindings/encoding" - "github.com/taikoxyz/taiko-client/driver/state" "github.com/taikoxyz/taiko-client/pkg/jwt" "github.com/taikoxyz/taiko-client/proposer" "github.com/taikoxyz/taiko-client/testutils" @@ -308,8 +307,7 @@ func (s *DriverTestSuite) TestL1Current() { // propose and insert a block testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer()) // reset L1 current with increased height - _, id, err := s.d.state.ResetL1Current(s.d.ctx, &state.HeightOrID{ID: common.Big1}) - s.Equal(common.Big1, id) + _, err := s.d.state.ResetL1Current(s.d.ctx, common.Big1) s.Nil(err) } diff --git a/driver/state/l1_current.go b/driver/state/l1_current.go index e40587d84..bbda4e8cc 100644 --- a/driver/state/l1_current.go +++ b/driver/state/l1_current.go @@ -31,72 +31,21 @@ func (s *State) SetL1Current(h *types.Header) { // BlockProposed event with given blockID / blockHash. func (s *State) ResetL1Current( ctx context.Context, - heightOrID *HeightOrID, -) (*bindings.TaikoL1ClientBlockProposed, *big.Int, error) { - if !heightOrID.NotEmpty() { - return nil, nil, fmt.Errorf("empty input %v", heightOrID) + blockID *big.Int, +) (*bindings.TaikoL1ClientBlockProposed, error) { + if blockID == nil { + return nil, fmt.Errorf("empty block ID") } - log.Info("Reset L1 current cursor", "heightOrID", heightOrID) + log.Info("Reset L1 current cursor", "blockID", blockID) - var ( - err error - ) - - if (heightOrID.ID != nil && heightOrID.ID.Cmp(common.Big0) == 0) || - (heightOrID.Height != nil && heightOrID.Height.Cmp(common.Big0) == 0) { + if blockID.Cmp(common.Big0) == 0 { l1Current, err := s.rpc.L1.HeaderByNumber(ctx, s.GenesisL1Height) if err != nil { - return nil, nil, err + return nil, err } s.SetL1Current(l1Current) - return nil, common.Big0, nil - } - - // Need to find the block ID at first, before filtering the BlockProposed events. - if heightOrID.ID == nil { - header, err := s.rpc.L2.HeaderByNumber(context.Background(), heightOrID.Height) - if err != nil { - return nil, nil, err - } - targetHash := header.Hash() - - iter, err := eventIterator.NewTransitionProvedIterator( - ctx, - &eventIterator.TransitionProvenIteratorConfig{ - Client: s.rpc.L1, - TaikoL1: s.rpc.TaikoL1, - StartHeight: s.GenesisL1Height, - EndHeight: s.GetL1Head().Number, - FilterQuery: []*big.Int{}, - Reverse: true, - OnTransitionProved: func( - ctx context.Context, - e *bindings.TaikoL1ClientTransitionProved, - end eventIterator.EndTransitionProvedEventIterFunc, - ) error { - log.Debug("Filtered TransitionProved event", "ID", e.BlockId, "hash", common.Hash(e.BlockHash)) - if e.BlockHash == targetHash { - heightOrID.ID = e.BlockId - end() - } - - return nil - }, - }, - ) - - if err != nil { - return nil, nil, err - } - - if err := iter.Iter(); err != nil { - return nil, nil, err - } - - if heightOrID.ID == nil { - return nil, nil, fmt.Errorf("TransitionProved event not found, hash: %s", targetHash) - } + return nil, nil } var event *bindings.TaikoL1ClientBlockProposed @@ -107,7 +56,7 @@ func (s *State) ResetL1Current( TaikoL1: s.rpc.TaikoL1, StartHeight: s.GenesisL1Height, EndHeight: s.GetL1Head().Number, - FilterQuery: []*big.Int{heightOrID.ID}, + FilterQuery: []*big.Int{blockID}, Reverse: true, OnBlockProposedEvent: func( ctx context.Context, @@ -120,26 +69,25 @@ func (s *State) ResetL1Current( }, }, ) - if err != nil { - return nil, nil, err + return nil, err } if err := iter.Iter(); err != nil { - return nil, nil, err + return nil, err } if event == nil { - return nil, nil, fmt.Errorf("BlockProposed event not found, blockID: %s", heightOrID.ID) + return nil, fmt.Errorf("BlockProposed event not found, blockID: %s", blockID) } l1Current, err := s.rpc.L1.HeaderByNumber(ctx, new(big.Int).SetUint64(event.Raw.BlockNumber)) if err != nil { - return nil, nil, err + return nil, err } s.SetL1Current(l1Current) log.Info("Reset L1 current cursor", "height", s.GetL1Current().Number) - return event, heightOrID.ID, nil + return event, nil } diff --git a/driver/state/l1_current_test.go b/driver/state/l1_current_test.go index b55f52e4a..d7fcdda02 100644 --- a/driver/state/l1_current_test.go +++ b/driver/state/l1_current_test.go @@ -24,27 +24,21 @@ func (s *DriverStateTestSuite) TestSetL1Current() { } func (s *DriverStateTestSuite) TestResetL1CurrentEmptyHeight() { - _, l1Current, err := s.s.ResetL1Current(context.Background(), &HeightOrID{ID: common.Big0}) + _, err := s.s.ResetL1Current(context.Background(), common.Big0) s.Nil(err) - s.Zero(l1Current.Uint64()) - _, _, err = s.s.ResetL1Current(context.Background(), &HeightOrID{Height: common.Big0}) + _, err = s.s.ResetL1Current(context.Background(), common.Big0) s.Nil(err) } func (s *DriverStateTestSuite) TestResetL1CurrentEmptyID() { - _, _, err := s.s.ResetL1Current(context.Background(), &HeightOrID{Height: common.Big1}) + _, err := s.s.ResetL1Current(context.Background(), common.Big1) s.ErrorContains(err, "not found") } -func (s *DriverStateTestSuite) TestResetL1CurrentEmptyHeightAndID() { - _, _, err := s.s.ResetL1Current(context.Background(), &HeightOrID{}) - s.ErrorContains(err, "empty input") -} - func (s *DriverStateTestSuite) TestResetL1CurrentCtxErr() { ctx, cancel := context.WithCancel(context.Background()) cancel() - _, _, err := s.s.ResetL1Current(ctx, &HeightOrID{Height: common.Big0}) + _, err := s.s.ResetL1Current(ctx, common.Big0) s.ErrorContains(err, "context canceled") } diff --git a/driver/state/state.go b/driver/state/state.go index c6aec80eb..d563f6e1f 100644 --- a/driver/state/state.go +++ b/driver/state/state.go @@ -17,17 +17,6 @@ import ( "github.com/taikoxyz/taiko-client/pkg/rpc" ) -// HeightOrID contains a block height or a block ID. -type HeightOrID struct { - Height *big.Int - ID *big.Int -} - -// NotEmpty checks whether this is an empty struct. -func (h *HeightOrID) NotEmpty() bool { - return h.Height != nil || h.ID != nil -} - // State contains all states which will be used by driver. type State struct { // Subscriptions, will automatically resubscribe on errors diff --git a/driver/state/state_test.go b/driver/state/state_test.go index de7987e98..a9d969c67 100644 --- a/driver/state/state_test.go +++ b/driver/state/state_test.go @@ -45,12 +45,6 @@ func (s *DriverStateTestSuite) TestGetHeadBlockID() { s.Equal(uint64(0), s.s.GetHeadBlockID().Uint64()) } -func (s *DriverStateTestSuite) TestHeightOrIDNotEmpty() { - s.False((&HeightOrID{}).NotEmpty()) - s.True((&HeightOrID{Height: common.Big0}).NotEmpty()) - s.True((&HeightOrID{ID: common.Big0}).NotEmpty()) -} - func (s *DriverStateTestSuite) TestClose() { s.NotPanics(s.s.Close) } diff --git a/proposer/prover_selector/eth_fee_eoa_selector.go b/proposer/prover_selector/eth_fee_eoa_selector.go index 4106e087d..aa42982f8 100644 --- a/proposer/prover_selector/eth_fee_eoa_selector.go +++ b/proposer/prover_selector/eth_fee_eoa_selector.go @@ -133,7 +133,13 @@ func (s *ETHFeeEOASelector) AssignProver( continue } - ok, err := rpc.CheckProverBalance(ctx, s.rpc, proverAddress, s.taikoL1Address, s.protocolConfigs.LivenessBond) + ok, err := rpc.CheckProverBalance( + ctx, + s.rpc, + proverAddress, + s.taikoL1Address, + s.protocolConfigs.LivenessBond, + ) if err != nil { log.Warn("Failed to check prover balance", "endpoint", endpoint, "error", err) continue