Commit 53d9465e authored by Andreas Bigger's avatar Andreas Bigger

more channel builder tests :test_tube:

parent 98a800f8
...@@ -207,7 +207,7 @@ func (c *channelBuilder) updateTimeout(timeoutBlockNum uint64, reason error) { ...@@ -207,7 +207,7 @@ func (c *channelBuilder) updateTimeout(timeoutBlockNum uint64, reason error) {
} }
// checkTimeout checks if the channel is timed out at the given block number and // checkTimeout checks if the channel is timed out at the given block number and
// in this case marks the channel as full, if it wasn't full alredy. // in this case marks the channel as full, if it wasn't full already.
func (c *channelBuilder) checkTimeout(blockNum uint64) { func (c *channelBuilder) checkTimeout(blockNum uint64) {
if !c.IsFull() && c.TimedOut(blockNum) { if !c.IsFull() && c.TimedOut(blockNum) {
c.setFullErr(c.timeoutReason) c.setFullErr(c.timeoutReason)
......
...@@ -2,9 +2,15 @@ package batcher ...@@ -2,9 +2,15 @@ package batcher
import ( import (
"bytes" "bytes"
"math/big"
"testing" "testing"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive" "github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
...@@ -91,3 +97,148 @@ func (testSuite *ChannelBuilderTestSuite) TestBuilderWrongFramePanic() { ...@@ -91,3 +97,148 @@ func (testSuite *ChannelBuilderTestSuite) TestBuilderWrongFramePanic() {
// If we get here, `PushFrame` did not panic as expected // If we get here, `PushFrame` did not panic as expected
testSuite.T().Errorf("did not panic") testSuite.T().Errorf("did not panic")
} }
func addNonsenseBlock(cb *channelBuilder) error {
lBlock := 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)
if err != nil {
return err
}
txs := []*types.Transaction{types.NewTx(l1InfoTx)}
a := types.NewBlock(&types.Header{
Number: big.NewInt(0),
}, txs, nil, nil, trie.NewStackTrie(nil))
err = cb.AddBlock(a)
return err
}
// TestOutputFrames tests the OutputFrames function
func (testSuite *ChannelBuilderTestSuite) TestOutputFrames() {
// Lower the max frame size so that we can test
testSuite.channelConfig.MaxFrameSize = 2
// Construct the channel builder
cb, err := newChannelBuilder(testSuite.channelConfig)
testSuite.NoError(err)
testSuite.False(cb.IsFull())
testSuite.Equal(0, cb.NumFrames())
// Calling OutputFrames without having called [AddBlock]
// should return `nil`.
testSuite.Nil(cb.OutputFrames())
// There should be no ready bytes yet
readyBytes := cb.co.ReadyBytes()
testSuite.Equal(0, readyBytes)
// Let's add a block
err = addNonsenseBlock(cb)
testSuite.NoError(err)
// Check how many ready bytes
readyBytes = cb.co.ReadyBytes()
testSuite.Equal(2, readyBytes)
testSuite.Equal(0, cb.NumFrames())
// The channel should not be full
// but we want to output the frames for testing anyways
isFull := cb.IsFull()
testSuite.False(isFull)
// Since we manually set the max frame size to 2,
// we should be able to compress the two frames now
err = cb.OutputFrames()
testSuite.NoError(err)
// There should be one frame in the channel builder now
testSuite.Equal(1, cb.NumFrames())
// There should no longer be any ready bytes
readyBytes = cb.co.ReadyBytes()
testSuite.Equal(0, readyBytes)
}
// TestBuilderAddBlock tests the AddBlock function
func (testSuite *ChannelBuilderTestSuite) TestBuilderAddBlock() {
// Lower the max frame size so that we can batch
testSuite.channelConfig.MaxFrameSize = 2
// Configure the Input Threshold params so we observe a full channel
// In reality, we only need the input bytes (74) below to be greater than
// or equal to the input threshold (3 * 2) / 1 = 6
testSuite.channelConfig.TargetFrameSize = 3
testSuite.channelConfig.TargetNumFrames = 2
testSuite.channelConfig.ApproxComprRatio = 1
// Construct the channel builder
cb, err := newChannelBuilder(testSuite.channelConfig)
testSuite.NoError(err)
// Add a nonsense block to the channel builder
err = addNonsenseBlock(cb)
testSuite.NoError(err)
// Check the fields reset in the AddBlock function
testSuite.Equal(74, cb.co.InputBytes())
testSuite.Equal(1, len(cb.blocks))
testSuite.Equal(0, len(cb.frames))
testSuite.True(cb.IsFull())
// Since the channel output is full, the next call to AddBlock
// should return the channel out full error
err = addNonsenseBlock(cb)
testSuite.ErrorIs(err, ErrInputTargetReached)
}
// TestBuilderReset tests the Reset function
func (testSuite *ChannelBuilderTestSuite) TestBuilderReset() {
// Lower the max frame size so that we can batch
testSuite.channelConfig.MaxFrameSize = 2
cb, err := newChannelBuilder(testSuite.channelConfig)
testSuite.NoError(err)
// Add a nonsense block to the channel builder
err = addNonsenseBlock(cb)
testSuite.NoError(err)
// Check the fields reset in the Reset function
testSuite.Equal(1, len(cb.blocks))
testSuite.Equal(0, len(cb.frames))
// Timeout should be updated in the AddBlock internal call to `updateSwTimeout`
timeout := uint64(100) + cb.cfg.SeqWindowSize - cb.cfg.SubSafetyMargin
testSuite.Equal(timeout, cb.timeout)
testSuite.Nil(cb.fullErr)
// Output frames so we can set the channel builder frames
err = cb.OutputFrames()
testSuite.NoError(err)
// Add another block to increment the block count
err = addNonsenseBlock(cb)
testSuite.NoError(err)
// Check the fields reset in the Reset function
testSuite.Equal(2, len(cb.blocks))
testSuite.Equal(1, len(cb.frames))
testSuite.Equal(timeout, cb.timeout)
testSuite.Nil(cb.fullErr)
// Reset the channel builder
err = cb.Reset()
testSuite.NoError(err)
// Check the fields reset in the Reset function
testSuite.Equal(0, len(cb.blocks))
testSuite.Equal(0, len(cb.frames))
testSuite.Equal(uint64(0), cb.timeout)
testSuite.Nil(cb.fullErr)
testSuite.Equal(0, cb.co.InputBytes())
testSuite.Equal(0, cb.co.ReadyBytes())
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment