From c9c3502fb616e4fe2e71ec794ce0fac34e996010 Mon Sep 17 00:00:00 2001 From: Sebastian Stammler Date: Sat, 11 Mar 2023 21:23:17 +0100 Subject: [PATCH] op-batcher: Test requeuing of transactions Regression test for #4937 --- op-batcher/batcher/channel_manager_test.go | 49 ++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/op-batcher/batcher/channel_manager_test.go b/op-batcher/batcher/channel_manager_test.go index 2ddbcede3de7c..36f06dc073ff9 100644 --- a/op-batcher/batcher/channel_manager_test.go +++ b/op-batcher/batcher/channel_manager_test.go @@ -3,12 +3,15 @@ package batcher_test import ( "io" "math/big" + "math/rand" "testing" + "time" "github.com/ethereum-optimism/optimism/op-batcher/batcher" "github.com/ethereum-optimism/optimism/op-node/eth" "github.com/ethereum-optimism/optimism/op-node/rollup/derive" "github.com/ethereum-optimism/optimism/op-node/testlog" + "github.com/ethereum-optimism/optimism/op-node/testutils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" @@ -54,15 +57,15 @@ func TestChannelManagerReturnsErrReorgWhenDrained(t *testing.T) { log := testlog.Logger(t, log.LvlCrit) m := batcher.NewChannelManager(log, batcher.ChannelConfig{ TargetFrameSize: 0, - MaxFrameSize: 100, + MaxFrameSize: 120_000, ApproxComprRatio: 1.0, }) - lBlock := types.NewBlock(&types.Header{ + l1Block := types.NewBlock(&types.Header{ BaseFee: big.NewInt(10), Difficulty: common.Big0, Number: big.NewInt(100), }, nil, nil, nil, trie.NewStackTrie(nil)) - l1InfoTx, err := derive.L1InfoDeposit(0, lBlock, eth.SystemConfig{}, false) + l1InfoTx, err := derive.L1InfoDeposit(0, l1Block, eth.SystemConfig{}, false) require.NoError(t, err) txs := []*types.Transaction{types.NewTx(l1InfoTx)} @@ -81,6 +84,46 @@ func TestChannelManagerReturnsErrReorgWhenDrained(t *testing.T) { require.NoError(t, err) _, err = m.TxData(eth.BlockID{}) require.ErrorIs(t, err, io.EOF) + err = m.AddL2Block(x) require.ErrorIs(t, err, batcher.ErrReorg) } + +func TestChannelManager_TxResend(t *testing.T) { + require := require.New(t) + rng := rand.New(rand.NewSource(time.Now().UnixNano())) + log := testlog.Logger(t, log.LvlError) + m := batcher.NewChannelManager(log, batcher.ChannelConfig{ + TargetFrameSize: 0, + MaxFrameSize: 120_000, + ApproxComprRatio: 1.0, + }) + + a, _ := testutils.RandomL2Block(rng, 4) + + err := m.AddL2Block(a) + require.NoError(err) + + txdata0, err := m.TxData(eth.BlockID{}) + require.NoError(err) + txdata0bytes := txdata0.Bytes() + data0 := make([]byte, len(txdata0bytes)) + // make sure we have a clone for later comparison + copy(data0, txdata0bytes) + + // ensure channel is drained + _, err = m.TxData(eth.BlockID{}) + require.ErrorIs(err, io.EOF) + + // requeue frame + m.TxFailed(txdata0.ID()) + + txdata1, err := m.TxData(eth.BlockID{}) + require.NoError(err) + + data1 := txdata1.Bytes() + require.Equal(data1, data0) + fs, err := derive.ParseFrames(data1) + require.NoError(err) + require.Len(fs, 1) +}