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

Commit

Permalink
add tests for config.go
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerLamTd committed Aug 1, 2023
1 parent a8c1d33 commit 7550b6e
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 15 deletions.
134 changes: 121 additions & 13 deletions proposer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/taikoxyz/taiko-client/cmd/flags"
"github.com/taikoxyz/taiko-client/testutils"
"github.com/urfave/cli/v2"
)

Expand All @@ -25,19 +26,7 @@ func (s *ProposerTestSuite) TestNewConfigFromCliContext() {
goldenTouchPrivKey, err := s.RpcClient.TaikoL2.GOLDENTOUCHPRIVATEKEY(nil)
s.Nil(err)

app := cli.NewApp()
app.Flags = []cli.Flag{
&cli.StringFlag{Name: flags.L1WSEndpoint.Name},
&cli.StringFlag{Name: flags.L2HTTPEndpoint.Name},
&cli.StringFlag{Name: flags.TaikoL1Address.Name},
&cli.StringFlag{Name: flags.TaikoL2Address.Name},
&cli.StringFlag{Name: flags.L1ProposerPrivKey.Name},
&cli.StringFlag{Name: flags.L2SuggestedFeeRecipient.Name},
&cli.StringFlag{Name: flags.ProposeInterval.Name},
&cli.Uint64Flag{Name: flags.CommitSlot.Name},
&cli.StringFlag{Name: flags.TxPoolLocals.Name},
&cli.Uint64Flag{Name: flags.ProposeBlockTxReplacementMultiplier.Name},
}
app := s.SetupApp()
app.Action = func(ctx *cli.Context) error {
c, err := NewConfigFromCliContext(ctx)
s.Nil(err)
Expand Down Expand Up @@ -71,3 +60,122 @@ func (s *ProposerTestSuite) TestNewConfigFromCliContext() {
"-" + flags.ProposeBlockTxReplacementMultiplier.Name, "5",
}))
}

func (s *ProposerTestSuite) TestNewConfigFromCliContextPrivKeyErr() {
app := s.SetupApp()

s.NotNil(app.Run([]string{
"TestNewConfigFromCliContextPrivKeyErr",
"-" + flags.L1ProposerPrivKey.Name, string(common.FromHex("0x")),
}))
}

func (s *ProposerTestSuite) TestNewConfigFromCliContextPropIntervalErr() {
goldenTouchPrivKey, err := s.RpcClient.TaikoL2.GOLDENTOUCHPRIVATEKEY(nil)
s.Nil(err)

app := s.SetupApp()

s.NotNil(app.Run([]string{
"TestNewConfigFromCliContextProposeIntervalErr",
"-" + flags.L1ProposerPrivKey.Name, common.Bytes2Hex(goldenTouchPrivKey.Bytes()),
"-" + flags.ProposeInterval.Name, "",
}))
}

func (s *ProposerTestSuite) TestNewConfigFromCliContextEmptyPropoIntervalErr() {
proposeInterval := "10s"

goldenTouchPrivKey, err := s.RpcClient.TaikoL2.GOLDENTOUCHPRIVATEKEY(nil)
s.Nil(err)

app := s.SetupApp()

s.NotNil(app.Run([]string{
"TestNewConfigFromCliContextEmptyProposalIntervalErr",
"-" + flags.L1ProposerPrivKey.Name, common.Bytes2Hex(goldenTouchPrivKey.Bytes()),
"-" + flags.ProposeInterval.Name, proposeInterval,
"-" + flags.ProposeEmptyBlocksInterval.Name, "",
}))
}

func (s *ProposerTestSuite) TestNewConfigFromCliContextL2RecipErr() {
proposeInterval := "10s"

goldenTouchPrivKey, err := s.RpcClient.TaikoL2.GOLDENTOUCHPRIVATEKEY(nil)
s.Nil(err)

app := s.SetupApp()

s.NotNil(app.Run([]string{
"TestNewConfigFromCliContextL2RecipErr",
"-" + flags.L1ProposerPrivKey.Name, common.Bytes2Hex(goldenTouchPrivKey.Bytes()),
"-" + flags.ProposeInterval.Name, proposeInterval,
"-" + flags.ProposeEmptyBlocksInterval.Name, proposeInterval,
"-" + flags.L2SuggestedFeeRecipient.Name, common.Bytes2Hex(testutils.RandomBytes(20)),
}))
}

func (s *ProposerTestSuite) TestNewConfigFromCliContextTxPoolLocalsErr() {
proposeInterval := "10s"

goldenTouchAddress, err := s.RpcClient.TaikoL2.GOLDENTOUCHADDRESS(nil)
s.Nil(err)

goldenTouchPrivKey, err := s.RpcClient.TaikoL2.GOLDENTOUCHPRIVATEKEY(nil)
s.Nil(err)

app := s.SetupApp()

s.NotNil(app.Run([]string{
"TestNewConfigFromCliContextL2RecipErr",
"-" + flags.L1ProposerPrivKey.Name, common.Bytes2Hex(goldenTouchPrivKey.Bytes()),
"-" + flags.ProposeInterval.Name, proposeInterval,
"-" + flags.ProposeEmptyBlocksInterval.Name, proposeInterval,
"-" + flags.L2SuggestedFeeRecipient.Name, goldenTouchAddress.Hex(),
"-" + flags.TxPoolLocals.Name, common.Bytes2Hex(testutils.RandomBytes(20)),
}))
}

func (s *ProposerTestSuite) TestNewConfigFromCliContextReplMultErr() {
proposeInterval := "10s"

goldenTouchAddress, err := s.RpcClient.TaikoL2.GOLDENTOUCHADDRESS(nil)
s.Nil(err)

goldenTouchPrivKey, err := s.RpcClient.TaikoL2.GOLDENTOUCHPRIVATEKEY(nil)
s.Nil(err)

app := s.SetupApp()

s.NotNil(app.Run([]string{
"TestNewConfigFromCliContextL2RecipErr",
"-" + flags.L1ProposerPrivKey.Name, common.Bytes2Hex(goldenTouchPrivKey.Bytes()),
"-" + flags.ProposeInterval.Name, proposeInterval,
"-" + flags.ProposeEmptyBlocksInterval.Name, proposeInterval,
"-" + flags.L2SuggestedFeeRecipient.Name, goldenTouchAddress.Hex(),
"-" + flags.TxPoolLocals.Name, goldenTouchAddress.Hex(),
"-" + flags.ProposeBlockTxReplacementMultiplier.Name, "0",
}))
}

func (s *ProposerTestSuite) SetupApp() *cli.App {
app := cli.NewApp()
app.Flags = []cli.Flag{
&cli.StringFlag{Name: flags.L1WSEndpoint.Name},
&cli.StringFlag{Name: flags.L2HTTPEndpoint.Name},
&cli.StringFlag{Name: flags.TaikoL1Address.Name},
&cli.StringFlag{Name: flags.TaikoL2Address.Name},
&cli.StringFlag{Name: flags.L1ProposerPrivKey.Name},
&cli.StringFlag{Name: flags.L2SuggestedFeeRecipient.Name},
&cli.StringFlag{Name: flags.ProposeInterval.Name},
&cli.Uint64Flag{Name: flags.CommitSlot.Name},
&cli.StringFlag{Name: flags.TxPoolLocals.Name},
&cli.Uint64Flag{Name: flags.ProposeBlockTxReplacementMultiplier.Name},
}
app.Action = func(ctx *cli.Context) error {
_, err := NewConfigFromCliContext(ctx)
return err
}
return app
}
5 changes: 3 additions & 2 deletions proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,16 @@ func (p *Proposer) eventLoop() {
select {
case <-p.ctx.Done():
return
// proposing interval timer has been reached
case <-p.proposingTimer.C:
metrics.ProposerProposeEpochCounter.Inc(1)

// attempt propose operation
if err := p.ProposeOp(p.ctx); err != nil {
if !errors.Is(err, errNoNewTxs) {
log.Error("Proposing operation error", "error", err)
continue
}

// if no new transactions and empty block interval has passed, propose an empty block
if p.proposeEmptyBlocksInterval != nil {
if time.Now().Before(lastNonEmptyBlockProposedAt.Add(*p.proposeEmptyBlocksInterval)) {
continue
Expand Down
16 changes: 16 additions & 0 deletions proposer/proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ func (s *ProposerTestSuite) TestStartClose() {
s.NotPanics(s.p.Close)
}

// TODO: not working
// func (s *ProposerTestSuite) TestEventLoopEmptyBlock() {
// fiveSecs := 5 * time.Second
// s.p.proposingInterval = &fiveSecs
// s.p.proposeEmptyBlocksInterval = &fiveSecs
// s.p.Start()
// time.Sleep(30 * time.Second)
// s.cancel()
// s.p.Close()
// // check if empty blocks have been proposed? query TaikoL1 contract?
// block, err := s.p.rpc.L2.BlockByNumber(context.Background(), nil)
// s.Nil(err)
// s.Equal(uint64(block.GasLimit()), uint64(21000))
// s.Equal(block.TxHash(), common.Hash(crypto.Keccak256Hash([]byte{})))
// }

func TestProposerTestSuite(t *testing.T) {
suite.Run(t, new(ProposerTestSuite))
}

0 comments on commit 7550b6e

Please sign in to comment.