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

Commit

Permalink
test(all): increase test coverage (#353)
Browse files Browse the repository at this point in the history
Co-authored-by: David <[email protected]>
  • Loading branch information
RogerLamTd and davidtaikocha authored Aug 25, 2023
1 parent e0adf17 commit cac555f
Show file tree
Hide file tree
Showing 17 changed files with 497 additions and 60 deletions.
4 changes: 2 additions & 2 deletions driver/chain_syncer/calldata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (s *Syncer) createExecutionPayloads(
l1Origin *rawdb.L1Origin,
headBlockID *big.Int,
txListBytes []byte,
baseFeee *big.Int,
baseFee *big.Int,
withdrawals types.Withdrawals,
) (payloadData *engine.ExecutableData, err error) {
fc := &engine.ForkchoiceStateV1{HeadBlockHash: parentHash}
Expand All @@ -428,7 +428,7 @@ func (s *Syncer) createExecutionPayloads(
MixHash: event.Meta.MixHash,
Treasury: event.Meta.Treasury,
},
BaseFeePerGas: baseFeee,
BaseFeePerGas: baseFee,
L1Origin: l1Origin,
}

Expand Down
2 changes: 1 addition & 1 deletion driver/chain_syncer/calldata/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *CalldataSyncerTestSuite) SetupTest() {

s.p = prop
}
func (s *CalldataSyncerTestSuite) TestNewSyncer() {
func (s *CalldataSyncerTestSuite) TestCancelNewSyncer() {
ctx, cancel := context.WithCancel(context.Background())
cancel()
syncer, err := NewSyncer(
Expand Down
99 changes: 94 additions & 5 deletions driver/chain_syncer/chain_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ package chainSyncer

import (
"context"

"os"
"testing"
"time"

"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/log"
"github.com/stretchr/testify/suite"
"github.com/taikoxyz/taiko-client/driver/state"
"github.com/taikoxyz/taiko-client/pkg/rpc"
"github.com/taikoxyz/taiko-client/proposer"
"github.com/taikoxyz/taiko-client/testutils"
)

type ChainSyncerTestSuite struct {
testutils.ClientTestSuite
s *L2ChainSyncer
s *L2ChainSyncer
snapshotID string
p testutils.Proposer
}

func (s *ChainSyncerTestSuite) SetupTest() {
Expand All @@ -33,6 +41,24 @@ func (s *ChainSyncerTestSuite) SetupTest() {
)
s.Nil(err)
s.s = syncer

prop := new(proposer.Proposer)
l1ProposerPrivKey, err := crypto.ToECDSA(common.Hex2Bytes(os.Getenv("L1_PROPOSER_PRIVATE_KEY")))
s.Nil(err)
proposeInterval := 1024 * time.Hour // No need to periodically propose transactions list in unit tests
s.Nil(proposer.InitFromConfig(context.Background(), prop, (&proposer.Config{
L1Endpoint: os.Getenv("L1_NODE_WS_ENDPOINT"),
L2Endpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"),
TaikoL1Address: common.HexToAddress(os.Getenv("TAIKO_L1_ADDRESS")),
TaikoL2Address: common.HexToAddress(os.Getenv("TAIKO_L2_ADDRESS")),
L1ProposerPrivKey: l1ProposerPrivKey,
L2SuggestedFeeRecipient: common.HexToAddress(os.Getenv("L2_SUGGESTED_FEE_RECIPIENT")),
ProposeInterval: &proposeInterval,
MaxProposedTxListsPerEpoch: 1,
WaitReceiptTimeout: 10 * time.Second,
})))

s.p = prop
}

func (s *ChainSyncerTestSuite) TestGetInnerSyncers() {
Expand All @@ -46,15 +72,78 @@ func (s *ChainSyncerTestSuite) TestSync() {
s.Nil(s.s.Sync(head))
}

// func (s *ChainSyncerTestSuite) TestSyncTriggerBeaconSync() {
// s.s.p2pSyncVerifiedBlocks = true
// s.s.state.setLatestVerifiedBlockHash(common.Hash{})
// }
func (s *ChainSyncerTestSuite) TestAheadOfProtocolVerifiedHead2() {
s.TakeSnapshot()
// propose a couple blocks
testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.s.calldataSyncer)

// NOTE: need to prove the proposed blocks to be verified, writing helper function

// generate transactopts to interact with TaikoL1 contract with.
privKey, err := crypto.ToECDSA(common.Hex2Bytes(os.Getenv("L1_PROVER_PRIVATE_KEY")))
s.Nil(err)
opts, err := bind.NewKeyedTransactorWithChainID(privKey, s.RpcClient.L1ChainID)
s.Nil(err)

head, err := s.RpcClient.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)

l2Head, err := s.RpcClient.L2.HeaderByNumber(context.Background(), nil)
s.Nil(err)

log.Info("L1HeaderByNumber head:", "number", head.Number)
// (equiv to s.state.GetL2Head().Number)
log.Info("L2HeaderByNumber head:", "number", l2Head.Number)
log.Info("LatestVerifiedBlock number:", "number", s.s.state.GetLatestVerifiedBlock().ID.Uint64())

config, err := s.s.rpc.TaikoL1.GetConfig(&bind.CallOpts{})
s.Nil(err)

// increase evm time to make blocks verifiable.
var result uint64
s.Nil(s.RpcClient.L1RawRPC.CallContext(
context.Background(),
&result,
"evm_increaseTime",
config.ProofRegularCooldown.Uint64()))
s.NotNil(result)
log.Info("evm time increase:", "number", result)

// interact with TaikoL1 contract to allow for verification of L2 blocks
tx, err := s.s.rpc.TaikoL1.VerifyBlocks(opts, common.Big3)
s.Nil(err)
s.NotNil(tx)

head2, err := s.RpcClient.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)

l2Head2, err := s.RpcClient.L2.HeaderByNumber(context.Background(), nil)
s.Nil(err)

log.Info("L1HeaderByNumber head2:", "number", head2.Number)
log.Info("L2HeaderByNumber head:", "number", l2Head2.Number)
log.Info("LatestVerifiedBlock number:", "number", s.s.state.GetLatestVerifiedBlock().ID.Uint64())

s.RevertSnapshot()
}

func TestChainSyncerTestSuite(t *testing.T) {
suite.Run(t, new(ChainSyncerTestSuite))
}

func (s *ChainSyncerTestSuite) TakeSnapshot() {
// record snapshot state to revert to before changes
s.Nil(s.RpcClient.L1RawRPC.CallContext(context.Background(), &s.snapshotID, "evm_snapshot"))
}

func (s *ChainSyncerTestSuite) RevertSnapshot() {
// revert to the snapshot state so protocol configs are unaffected
var revertRes bool
s.Nil(s.RpcClient.L1RawRPC.CallContext(context.Background(), &revertRes, "evm_revert", s.snapshotID))
s.True(revertRes)
s.Nil(rpc.SetHead(context.Background(), s.RpcClient.L2RawRPC, common.Big0))
}

func (s *ChainSyncerTestSuite) TestAheadOfProtocolVerifiedHead() {
s.True(s.s.AheadOfProtocolVerifiedHead())
}
65 changes: 48 additions & 17 deletions driver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,18 @@ import (
"github.com/urfave/cli/v2"
)

var (
l1Endpoint = os.Getenv("L1_NODE_WS_ENDPOINT")
l2Endpoint = os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT")
l2EngineEndpoint = os.Getenv("L2_EXECUTION_ENGINE_AUTH_ENDPOINT")
taikoL1 = os.Getenv("TAIKO_L1_ADDRESS")
taikoL2 = os.Getenv("TAIKO_L2_ADDRESS")
rpcTimeout = 5 * time.Second
)

func (s *DriverTestSuite) TestNewConfigFromCliContext() {
l1Endpoint := os.Getenv("L1_NODE_WS_ENDPOINT")
l2Endpoint := os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT")
l2EngineEndpoint := os.Getenv("L2_EXECUTION_ENGINE_AUTH_ENDPOINT")
taikoL1 := os.Getenv("TAIKO_L1_ADDRESS")
taikoL2 := os.Getenv("TAIKO_L2_ADDRESS")
rpcTimeout := 5 * time.Second
app := s.SetupApp()

app := cli.NewApp()
app.Flags = []cli.Flag{
&cli.StringFlag{Name: flags.L1WSEndpoint.Name},
&cli.StringFlag{Name: flags.L2WSEndpoint.Name},
&cli.StringFlag{Name: flags.L2AuthEndpoint.Name},
&cli.StringFlag{Name: flags.TaikoL1Address.Name},
&cli.StringFlag{Name: flags.TaikoL2Address.Name},
&cli.StringFlag{Name: flags.JWTSecret.Name},
&cli.UintFlag{Name: flags.P2PSyncTimeout.Name},
&cli.UintFlag{Name: flags.RPCTimeout.Name},
}
app.Action = func(ctx *cli.Context) error {
c, err := NewConfigFromCliContext(ctx)
s.Nil(err)
Expand Down Expand Up @@ -56,3 +49,41 @@ func (s *DriverTestSuite) TestNewConfigFromCliContext() {
"-" + flags.RPCTimeout.Name, "5",
}))
}

func (s *DriverTestSuite) TestNewConfigFromCliContextJWTError() {
app := s.SetupApp()
s.ErrorContains(app.Run([]string{
"TestNewConfigFromCliContext",
"-" + flags.JWTSecret.Name, "wrongsecretfile.txt",
}), "invalid JWT secret file")
}

func (s *DriverTestSuite) TestNewConfigFromCliContextEmptyL2CheckPoint() {
app := s.SetupApp()
s.ErrorContains(app.Run([]string{
"TestNewConfigFromCliContext",
"-" + flags.JWTSecret.Name, os.Getenv("JWT_SECRET"),
"-" + flags.P2PSyncVerifiedBlocks.Name, "true",
"-" + flags.L2WSEndpoint.Name, "",
}), "empty L2 check point URL")
}

func (s *DriverTestSuite) SetupApp() *cli.App {
app := cli.NewApp()
app.Flags = []cli.Flag{
&cli.StringFlag{Name: flags.L1WSEndpoint.Name},
&cli.StringFlag{Name: flags.L2WSEndpoint.Name},
&cli.StringFlag{Name: flags.L2AuthEndpoint.Name},
&cli.StringFlag{Name: flags.TaikoL1Address.Name},
&cli.StringFlag{Name: flags.TaikoL2Address.Name},
&cli.StringFlag{Name: flags.JWTSecret.Name},
&cli.BoolFlag{Name: flags.P2PSyncVerifiedBlocks.Name},
&cli.UintFlag{Name: flags.P2PSyncTimeout.Name},
&cli.UintFlag{Name: flags.RPCTimeout.Name},
}
app.Action = func(ctx *cli.Context) error {
_, err := NewConfigFromCliContext(ctx)
return err
}
return app
}
138 changes: 138 additions & 0 deletions pkg/chain_iterator/block_batch_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -113,6 +114,143 @@ func (s *BlockBatchIteratorTestSuite) TestIterEndFunc() {
s.Equal(lastEnd.Uint64(), maxBlocksReadPerEpoch)
}

func (s *BlockBatchIteratorTestSuite) TestIterCtxCancel() {
lastEnd := common.Big0
headHeight, err := s.RpcClient.L1.BlockNumber(context.Background())
s.Nil(err)
ctx, cancel := context.WithCancel(context.Background())
retry := 5 * time.Second

itr, err := NewBlockBatchIterator(ctx, &BlockBatchIteratorConfig{
Client: s.RpcClient.L1,
MaxBlocksReadPerEpoch: nil,
RetryInterval: &retry,
StartHeight: common.Big0,
EndHeight: new(big.Int).SetUint64(headHeight),
OnBlocks: func(
ctx context.Context,
start, end *types.Header,
updateCurrentFunc UpdateCurrentFunc,
endIterFunc EndIterFunc,
) error {
s.Equal(lastEnd.Uint64(), start.Number.Uint64())
lastEnd = end.Number
endIterFunc()
return nil
},
})

s.Nil(err)
cancel()
// should output a log.Warn and context cancel error
err8 := itr.Iter()
s.ErrorContains(err8, "context canceled")
}

func (s *BlockBatchIteratorTestSuite) TestBlockBatchIteratorConfig() {
_, err := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: nil,
})
s.ErrorContains(err, "invalid RPC client")

_, err2 := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RpcClient.L1,
OnBlocks: nil,
})
s.ErrorContains(err2, "invalid callback")

lastEnd := common.Big0
_, err3 := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RpcClient.L1,
OnBlocks: func(
ctx context.Context,
start, end *types.Header,
updateCurrentFunc UpdateCurrentFunc,
endIterFunc EndIterFunc,
) error {
s.Equal(lastEnd.Uint64(), start.Number.Uint64())
lastEnd = end.Number
endIterFunc()
return nil
},
StartHeight: nil,
})
s.ErrorContains(err3, "invalid start height")

_, err4 := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RpcClient.L1,
OnBlocks: func(
ctx context.Context,
start, end *types.Header,
updateCurrentFunc UpdateCurrentFunc,
endIterFunc EndIterFunc,
) error {
s.Equal(lastEnd.Uint64(), start.Number.Uint64())
lastEnd = end.Number
endIterFunc()
return nil
},
StartHeight: common.Big2,
EndHeight: common.Big0,
})
s.ErrorContains(err4, "start height (2) > end height (0)")

_, err5 := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RpcClient.L1,
OnBlocks: func(
ctx context.Context,
start, end *types.Header,
updateCurrentFunc UpdateCurrentFunc,
endIterFunc EndIterFunc,
) error {
s.Equal(lastEnd.Uint64(), start.Number.Uint64())
lastEnd = end.Number
endIterFunc()
return nil
},
StartHeight: common.Big0,
Reverse: true,
EndHeight: nil,
})
s.ErrorContains(err5, "missing end height")

_, err6 := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RpcClient.L1,
OnBlocks: func(
ctx context.Context,
start, end *types.Header,
updateCurrentFunc UpdateCurrentFunc,
endIterFunc EndIterFunc,
) error {
s.Equal(lastEnd.Uint64(), start.Number.Uint64())
lastEnd = end.Number
endIterFunc()
return nil
},
StartHeight: common.Big256,
EndHeight: common.Big256,
})
s.ErrorContains(err6, "failed to get start header")

_, err7 := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RpcClient.L1,
OnBlocks: func(
ctx context.Context,
start, end *types.Header,
updateCurrentFunc UpdateCurrentFunc,
endIterFunc EndIterFunc,
) error {
s.Equal(lastEnd.Uint64(), start.Number.Uint64())
lastEnd = end.Number
endIterFunc()
return nil
},
StartHeight: common.Big0,
EndHeight: common.Big256,
})
s.ErrorContains(err7, "failed to get end header")
}

func TestBlockBatchIteratorTestSuite(t *testing.T) {
suite.Run(t, new(BlockBatchIteratorTestSuite))
}
Loading

0 comments on commit cac555f

Please sign in to comment.