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

Commit

Permalink
test(testutils): update testutils (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp authored Feb 23, 2024
1 parent f5d1146 commit 069b8a5
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 90 deletions.
8 changes: 4 additions & 4 deletions driver/chain_syncer/calldata/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s *CalldataSyncerTestSuite) TestProcessL1Blocks() {

func (s *CalldataSyncerTestSuite) TestProcessL1BlocksReorg() {
head, err := s.s.rpc.L1.HeaderByNumber(context.Background(), nil)
testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.s)
s.ProposeAndInsertEmptyBlocks(s.p, s.s)
s.Nil(err)
s.Nil(s.s.ProcessL1Blocks(context.Background(), head))
}
Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *CalldataSyncerTestSuite) TestTreasuryIncomeAllAnchors() {
headBefore, err := s.RPCClient.L2.BlockNumber(context.Background())
s.Nil(err)

testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.s)
s.ProposeAndInsertEmptyBlocks(s.p, s.s)

headAfter, err := s.RPCClient.L2.BlockNumber(context.Background())
s.Nil(err)
Expand All @@ -178,8 +178,8 @@ func (s *CalldataSyncerTestSuite) TestTreasuryIncome() {
headBefore, err := s.RPCClient.L2.BlockNumber(context.Background())
s.Nil(err)

testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.s)
testutils.ProposeAndInsertValidBlock(&s.ClientTestSuite, s.p, s.s)
s.ProposeAndInsertEmptyBlocks(s.p, s.s)
s.ProposeAndInsertValidBlock(s.p, s.s)

headAfter, err := s.RPCClient.L2.BlockNumber(context.Background())
s.Nil(err)
Expand Down
8 changes: 3 additions & 5 deletions driver/chain_syncer/chain_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (s *ChainSyncerTestSuite) TestSync() {
func (s *ChainSyncerTestSuite) TestAheadOfProtocolVerifiedHead2() {
s.TakeSnapshot()
// propose a couple blocks
testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.s.calldataSyncer)
s.ProposeAndInsertEmptyBlocks(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.
Expand Down Expand Up @@ -136,14 +136,12 @@ func TestChainSyncerTestSuite(t *testing.T) {

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

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

Expand Down
50 changes: 19 additions & 31 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ func (s *DriverTestSuite) TestProcessL1Blocks() {
s.Nil(s.d.ChainSyncer().CalldataSyncer().ProcessL1Blocks(context.Background(), l1Head1))

// Propose a valid L2 block
testutils.ProposeAndInsertValidBlock(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertValidBlock(s.p, s.d.ChainSyncer().CalldataSyncer())

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

s.Greater(l2Head2.Number.Uint64(), l2Head1.Number.Uint64())

// Empty blocks
testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertEmptyBlocks(s.p, s.d.ChainSyncer().CalldataSyncer())
s.Nil(err)

l2Head3, err := s.d.rpc.L2.HeaderByNumber(context.Background(), nil)
Expand All @@ -129,19 +129,17 @@ func (s *DriverTestSuite) TestProcessL1Blocks() {
}

func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {
var testnetL1SnapshotID string
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.NotEmpty(testnetL1SnapshotID)
var testnetL1SnapshotID = s.SetL1Snapshot()

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)

// Propose two L2 blocks
testutils.ProposeAndInsertValidBlock(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertValidBlock(s.p, s.d.ChainSyncer().CalldataSyncer())

testutils.ProposeAndInsertValidBlock(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertValidBlock(s.p, s.d.ChainSyncer().CalldataSyncer())

l1Head2, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
Expand All @@ -159,9 +157,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {
s.False(reorged)

// Reorg back to l2Head1
var revertRes bool
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.True(revertRes)
s.RevertL1Snapshot(testnetL1SnapshotID)

l1Head3, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
Expand All @@ -170,7 +166,7 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {

// Propose ten blocks on another fork
for i := 0; i < 10; i++ {
testutils.ProposeInvalidTxListBytes(&s.ClientTestSuite, s.p)
s.ProposeInvalidTxListBytes(s.p)
}

l1Head4, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
Expand All @@ -192,19 +188,17 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {
}

func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {
var testnetL1SnapshotID string
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.NotEmpty(testnetL1SnapshotID)
var testnetL1SnapshotID = s.SetL1Snapshot()

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)

// Propose two L2 blocks
testutils.ProposeAndInsertValidBlock(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertValidBlock(s.p, s.d.ChainSyncer().CalldataSyncer())
time.Sleep(3 * time.Second)
testutils.ProposeAndInsertValidBlock(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertValidBlock(s.p, s.d.ChainSyncer().CalldataSyncer())

l1Head2, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
Expand All @@ -222,17 +216,15 @@ func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {
s.False(reorged)

// Reorg back to l2Head1
var revertRes bool
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.True(revertRes)
s.RevertL1Snapshot(testnetL1SnapshotID)

l1Head3, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
s.Equal(l1Head3.Number.Uint64(), l1Head1.Number.Uint64())
s.Equal(l1Head3.Hash(), l1Head1.Hash())

// Propose one blocks on another fork
testutils.ProposeInvalidTxListBytes(&s.ClientTestSuite, s.p)
s.ProposeInvalidTxListBytes(s.p)

l1Head4, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
Expand All @@ -252,19 +244,17 @@ func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {
}

func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() {
var testnetL1SnapshotID string
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &testnetL1SnapshotID, "evm_snapshot"))
s.NotEmpty(testnetL1SnapshotID)
var testnetL1SnapshotID = s.SetL1Snapshot()

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)

// Propose two L2 blocks
testutils.ProposeAndInsertValidBlock(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertValidBlock(s.p, s.d.ChainSyncer().CalldataSyncer())
time.Sleep(3 * time.Second)
testutils.ProposeAndInsertValidBlock(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertValidBlock(s.p, s.d.ChainSyncer().CalldataSyncer())

l1Head2, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
Expand All @@ -282,19 +272,17 @@ func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() {
s.False(reorged)

// Reorg back to l2Head1
var revertRes bool
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", testnetL1SnapshotID))
s.True(revertRes)
s.RevertL1Snapshot(testnetL1SnapshotID)

l1Head3, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
s.Equal(l1Head3.Number.Uint64(), l1Head1.Number.Uint64())
s.Equal(l1Head3.Hash(), l1Head1.Hash())

// Propose two blocks on another fork
testutils.ProposeInvalidTxListBytes(&s.ClientTestSuite, s.p)
s.ProposeInvalidTxListBytes(s.p)
time.Sleep(3 * time.Second)
testutils.ProposeInvalidTxListBytes(&s.ClientTestSuite, s.p)
s.ProposeInvalidTxListBytes(s.p)

l1Head4, err := s.d.rpc.L1.HeaderByNumber(context.Background(), nil)
s.Nil(err)
Expand Down Expand Up @@ -326,7 +314,7 @@ func (s *DriverTestSuite) TestStartClose() {

func (s *DriverTestSuite) TestL1Current() {
// propose and insert a block
testutils.ProposeAndInsertEmptyBlocks(&s.ClientTestSuite, s.p, s.d.ChainSyncer().CalldataSyncer())
s.ProposeAndInsertEmptyBlocks(s.p, s.d.ChainSyncer().CalldataSyncer())
// reset L1 current with increased height
s.Nil(s.d.state.ResetL1Current(s.d.ctx, common.Big1))
}
Expand Down
13 changes: 5 additions & 8 deletions internal/testutils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import (
"github.com/taikoxyz/taiko-client/prover/server"
)

func ProposeInvalidTxListBytes(s *ClientTestSuite, proposer Proposer) {
func (s *ClientTestSuite) ProposeInvalidTxListBytes(proposer Proposer) {
invalidTxListBytes := RandomBytes(256)

s.Nil(proposer.ProposeTxList(context.Background(), invalidTxListBytes, 1, nil))
}

func ProposeAndInsertEmptyBlocks(
s *ClientTestSuite,
func (s *ClientTestSuite) ProposeAndInsertEmptyBlocks(
proposer Proposer,
calldataSyncer CalldataSyncer,
) []*bindings.TaikoL1ClientBlockProposed {
Expand All @@ -56,7 +55,7 @@ func ProposeAndInsertEmptyBlocks(

s.Nil(proposer.ProposeTxList(context.Background(), encoded, 0, nil))

ProposeInvalidTxListBytes(s, proposer)
s.ProposeInvalidTxListBytes(proposer)

// Random bytes txList
s.Nil(proposer.ProposeEmptyBlockOp(context.Background()))
Expand Down Expand Up @@ -85,8 +84,7 @@ func ProposeAndInsertEmptyBlocks(

// ProposeAndInsertValidBlock proposes an valid tx list and then insert it
// into L2 execution engine's local chain.
func ProposeAndInsertValidBlock(
s *ClientTestSuite,
func (s *ClientTestSuite) ProposeAndInsertValidBlock(
proposer Proposer,
calldataSyncer CalldataSyncer,
) *bindings.TaikoL1ClientBlockProposed {
Expand Down Expand Up @@ -156,8 +154,7 @@ func ProposeAndInsertValidBlock(

// NewTestProverServer starts a new prover server that has channel listeners to respond and react
// to requests for capacity, which provers can call.
func NewTestProverServer(
s *ClientTestSuite,
func (s *ClientTestSuite) NewTestProverServer(
proverPrivKey *ecdsa.PrivateKey,
url *url.URL,
) *server.ProverServer {
Expand Down
22 changes: 16 additions & 6 deletions internal/testutils/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *ClientTestSuite) SetupTest() {
s.Nil(err)

s.ProverEndpoints = []*url.URL{LocalRandomProverEndpoint()}
s.proverServer = NewTestProverServer(s, l1ProverPrivKey, s.ProverEndpoints[0])
s.proverServer = s.NewTestProverServer(l1ProverPrivKey, s.ProverEndpoints[0])

balance, err := rpcCli.TaikoToken.BalanceOf(nil, crypto.PubkeyToAddress(l1ProverPrivKey.PublicKey))
s.Nil(err)
Expand Down Expand Up @@ -92,8 +92,7 @@ func (s *ClientTestSuite) SetupTest() {
s.setAllowance(l1ProverPrivKey)
s.setAllowance(ownerPrivKey)
}
s.Nil(rpcCli.L1.CallContext(context.Background(), &s.testnetL1SnapshotID, "evm_snapshot"))
s.NotEmpty(s.testnetL1SnapshotID)
s.testnetL1SnapshotID = s.SetL1Snapshot()
}

func (s *ClientTestSuite) setAllowance(key *ecdsa.PrivateKey) {
Expand Down Expand Up @@ -124,9 +123,7 @@ func (s *ClientTestSuite) setAllowance(key *ecdsa.PrivateKey) {
}

func (s *ClientTestSuite) TearDownTest() {
var revertRes bool
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", s.testnetL1SnapshotID))
s.True(revertRes)
s.RevertL1Snapshot(s.testnetL1SnapshotID)

s.Nil(rpc.SetHead(context.Background(), s.RPCClient.L2, common.Big0))
s.Nil(s.proverServer.Shutdown(context.Background()))
Expand All @@ -141,3 +138,16 @@ func (s *ClientTestSuite) IncreaseTime(time uint64) {
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &result, "evm_increaseTime", time))
s.NotNil(result)
}

func (s *ClientTestSuite) SetL1Snapshot() string {
var snapshotID string
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &snapshotID, "evm_snapshot"))
s.NotEmpty(snapshotID)
return snapshotID
}

func (s *ClientTestSuite) RevertL1Snapshot(snapshotID string) {
var revertRes bool
s.Nil(s.RPCClient.L1.CallContext(context.Background(), &revertRes, "evm_revert", snapshotID))
s.True(revertRes)
}
19 changes: 17 additions & 2 deletions pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ func (c *Client) WaitL1Origin(ctx context.Context, blockID *big.Int) (*rawdb.L1O
func (c *Client) GetPoolContent(
ctx context.Context,
beneficiary common.Address,
baseFee *big.Int,
blockMaxGasLimit uint32,
maxBytesPerTxList uint64,
locals []common.Address,
Expand All @@ -247,13 +246,29 @@ func (c *Client) GetPoolContent(
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

l2Head, err := c.L2.HeaderByNumber(ctx, nil)
if err != nil {
return nil, err
}

baseFee, err := c.TaikoL2.GetBasefee(
&bind.CallOpts{Context: ctx},
uint64(time.Now().Unix())-l2Head.Time,
uint32(l2Head.GasUsed),
)
if err != nil {
return nil, err
}

log.Info("Current base fee", "fee", baseFee)

var localsArg []string
for _, local := range locals {
localsArg = append(localsArg, local.Hex())
}

var result []types.Transactions
err := c.L2.CallContext(
err = c.L2.CallContext(
ctxWithTimeout,
&result,
"taiko_txPoolContent",
Expand Down
5 changes: 0 additions & 5 deletions pkg/rpc/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ func TestGetPoolContentValid(t *testing.T) {
require.Nil(t, err)
goldenTouchAddress, err := client.TaikoL2.GOLDENTOUCHADDRESS(nil)
require.Nil(t, err)
parent, err := client.L2.BlockByNumber(context.Background(), nil)
require.Nil(t, err)
baseFee, err := client.TaikoL2.GetBasefee(nil, 1, uint32(parent.GasUsed()))
require.Nil(t, err)
gasLimit := configs.BlockMaxGasLimit
maxBytes := configs.BlockMaxTxListBytes

Expand All @@ -170,7 +166,6 @@ func TestGetPoolContentValid(t *testing.T) {
_, err2 := client.GetPoolContent(
context.Background(),
goldenTouchAddress,
baseFee,
gasLimit,
maxBytes.Uint64(),
txPools,
Expand Down
17 changes: 0 additions & 17 deletions proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,9 @@ func (p *Proposer) ProposeOp(ctx context.Context) error {

log.Info("Start fetching L2 execution engine's transaction pool content")

l2Head, err := p.rpc.L2.HeaderByNumber(ctx, nil)
if err != nil {
return err
}

baseFee, err := p.rpc.TaikoL2.GetBasefee(
&bind.CallOpts{Context: ctx},
uint64(time.Now().Unix())-l2Head.Time,
uint32(l2Head.GasUsed),
)
if err != nil {
return err
}

log.Info("Current base fee", "fee", baseFee)

txLists, err := p.rpc.GetPoolContent(
ctx,
p.proposerAddress,
baseFee,
p.protocolConfigs.BlockMaxGasLimit,
p.protocolConfigs.BlockMaxTxListBytes.Uint64(),
p.LocalAddresses,
Expand Down
Loading

0 comments on commit 069b8a5

Please sign in to comment.