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

Commit

Permalink
test: more driver tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Oct 26, 2023
1 parent 73287a5 commit 1c9d70a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 107 deletions.
12 changes: 2 additions & 10 deletions driver/chain_syncer/chain_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
80 changes: 14 additions & 66 deletions driver/state/l1_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
}
14 changes: 4 additions & 10 deletions driver/state/l1_current_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
11 changes: 0 additions & 11 deletions driver/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions driver/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion proposer/prover_selector/eth_fee_eoa_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1c9d70a

Please sign in to comment.