Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 46 additions & 57 deletions op-batcher/batcher/channel_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ func FuzzDurationTimeoutZeroMaxChannelDuration(f *testing.F) {
})
}

// FuzzDurationZero ensures that when whenever the MaxChannelDuration
// FuzzChannelBuilder_DurationZero ensures that when whenever the MaxChannelDuration
// is not set to 0, the channel builder will always have a duration timeout
// as long as the channel builder's timeout is set to 0.
func FuzzDurationZero(f *testing.F) {
func FuzzChannelBuilder_DurationZero(f *testing.F) {
for i := range [10]int{} {
f.Add(uint64(i), uint64(i))
}
Expand Down Expand Up @@ -313,8 +313,8 @@ func FuzzSeqWindowZeroTimeoutClose(f *testing.F) {
})
}

// TestBuilderNextFrame tests calling NextFrame on a ChannelBuilder with only one frame
func TestBuilderNextFrame(t *testing.T) {
// TestChannelBuilder_NextFrame tests calling NextFrame on a ChannelBuilder with only one frame
func TestChannelBuilder_NextFrame(t *testing.T) {
channelConfig := defaultTestChannelConfig

// Create a new channel builder
Expand Down Expand Up @@ -353,8 +353,8 @@ func TestBuilderNextFrame(t *testing.T) {
require.PanicsWithValue(t, "no next frame", func() { cb.NextFrame() })
}

// TestBuilderInvalidFrameId tests that a panic is thrown when a frame is pushed with an invalid frame id
func TestBuilderWrongFramePanic(t *testing.T) {
// TestChannelBuilder_OutputWrongFramePanic tests that a panic is thrown when a frame is pushed with an invalid frame id
func TestChannelBuilder_OutputWrongFramePanic(t *testing.T) {
channelConfig := defaultTestChannelConfig

// Construct a channel builder
Expand Down Expand Up @@ -383,15 +383,14 @@ func TestBuilderWrongFramePanic(t *testing.T) {
})
}

// TestOutputFrames tests the OutputFrames function
func TestOutputFrames(t *testing.T) {
// TestChannelBuilder_OutputFramesWorks tests the [ChannelBuilder] OutputFrames is successful.
func TestChannelBuilder_OutputFramesWorks(t *testing.T) {
channelConfig := defaultTestChannelConfig
channelConfig.MaxFrameSize = 2
channelConfig.MaxFrameSize = 24

// Construct the channel builder
cb, err := newChannelBuilder(channelConfig)
require.NoError(t, err)

require.False(t, cb.IsFull())
require.Equal(t, 0, cb.NumFrames())

Expand All @@ -400,40 +399,34 @@ func TestOutputFrames(t *testing.T) {
require.NoError(t, cb.OutputFrames())

// There should be no ready bytes yet
readyBytes := cb.co.ReadyBytes()
require.Equal(t, 0, readyBytes)
require.Equal(t, 0, cb.co.ReadyBytes())

// Let's add a block
err = addMiniBlock(cb)
require.NoError(t, err)
require.NoError(t, addMiniBlock(cb))
require.NoError(t, cb.co.Flush())

// Check how many ready bytes
readyBytes = cb.co.ReadyBytes()
require.Equal(t, 2, readyBytes)

// There should be more than the max frame size ready
require.Greater(t, uint64(cb.co.ReadyBytes()), channelConfig.MaxFrameSize)
require.Equal(t, 0, cb.NumFrames())

// The channel should not be full
// but we want to output the frames for testing anyways
isFull := cb.IsFull()
require.False(t, isFull)

// Since we manually set the max frame size to 2,
// we should be able to compress the two frames now
err = cb.OutputFrames()
require.NoError(t, err)
require.False(t, cb.IsFull())

// There should be one frame in the channel builder now
require.Equal(t, 1, cb.NumFrames())
// We should be able to output the frames
require.NoError(t, cb.OutputFrames())

// There should no longer be any ready bytes
readyBytes = cb.co.ReadyBytes()
require.Equal(t, 0, readyBytes)
// There should be many frames in the channel builder now
require.Greater(t, cb.NumFrames(), 1)
for _, frame := range cb.frames {
require.Len(t, frame.data, int(channelConfig.MaxFrameSize))
}
}

// TestMaxRLPBytesPerChannel tests the [channelBuilder.OutputFrames]
// TestChannelBuilder_MaxRLPBytesPerChannel tests the [channelBuilder.OutputFrames]
// function errors when the max RLP bytes per channel is reached.
func TestMaxRLPBytesPerChannel(t *testing.T) {
func TestChannelBuilder_MaxRLPBytesPerChannel(t *testing.T) {
t.Parallel()
channelConfig := defaultTestChannelConfig
channelConfig.MaxFrameSize = derive.MaxRLPBytesPerChannel * 2
Expand All @@ -449,13 +442,13 @@ func TestMaxRLPBytesPerChannel(t *testing.T) {
require.ErrorIs(t, err, derive.ErrTooManyRLPBytes)
}

// TestOutputFramesMaxFrameIndex tests the [channelBuilder.OutputFrames]
// TestChannelBuilder_OutputFramesMaxFrameIndex tests the [ChannelBuilder.OutputFrames]
// function errors when the max frame index is reached.
func TestOutputFramesMaxFrameIndex(t *testing.T) {
func TestChannelBuilder_OutputFramesMaxFrameIndex(t *testing.T) {
channelConfig := defaultTestChannelConfig
channelConfig.MaxFrameSize = 1
channelConfig.MaxFrameSize = 24
channelConfig.TargetNumFrames = math.MaxInt
channelConfig.TargetFrameSize = 1
channelConfig.TargetFrameSize = 24
channelConfig.ApproxComprRatio = 0

// Continuously add blocks until the max frame index is reached
Expand All @@ -477,6 +470,7 @@ func TestOutputFramesMaxFrameIndex(t *testing.T) {
Number: big.NewInt(0),
}, txs, nil, nil, trie.NewStackTrie(nil))
_, err = cb.AddBlock(a)
require.NoError(t, cb.co.Flush())
if cb.IsFull() {
fullErr := cb.FullErr()
require.ErrorIs(t, fullErr, ErrMaxFrameIndex)
Expand All @@ -489,17 +483,15 @@ func TestOutputFramesMaxFrameIndex(t *testing.T) {
}
}

// TestBuilderAddBlock tests the AddBlock function
func TestBuilderAddBlock(t *testing.T) {
// TestChannelBuilder_AddBlock tests the AddBlock function
func TestChannelBuilder_AddBlock(t *testing.T) {
channelConfig := defaultTestChannelConfig

// Lower the max frame size so that we can batch
channelConfig.MaxFrameSize = 2
channelConfig.MaxFrameSize = 30

// 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
channelConfig.TargetFrameSize = 3
channelConfig.TargetFrameSize = 30
channelConfig.TargetNumFrames = 2
channelConfig.ApproxComprRatio = 1

Expand All @@ -508,8 +500,8 @@ func TestBuilderAddBlock(t *testing.T) {
require.NoError(t, err)

// Add a nonsense block to the channel builder
err = addMiniBlock(cb)
require.NoError(t, err)
require.NoError(t, addMiniBlock(cb))
require.NoError(t, cb.co.Flush())

// Check the fields reset in the AddBlock function
require.Equal(t, 74, cb.co.InputBytes())
Expand All @@ -519,23 +511,22 @@ func TestBuilderAddBlock(t *testing.T) {

// Since the channel output is full, the next call to AddBlock
// should return the channel out full error
err = addMiniBlock(cb)
require.ErrorIs(t, err, ErrInputTargetReached)
require.ErrorIs(t, addMiniBlock(cb), ErrInputTargetReached)
}

// TestBuilderReset tests the Reset function
func TestBuilderReset(t *testing.T) {
// TestChannelBuilder_Reset tests the [Reset] function
func TestChannelBuilder_Reset(t *testing.T) {
channelConfig := defaultTestChannelConfig

// Lower the max frame size so that we can batch
channelConfig.MaxFrameSize = 2
channelConfig.MaxFrameSize = 24

cb, err := newChannelBuilder(channelConfig)
require.NoError(t, err)

// Add a nonsense block to the channel builder
err = addMiniBlock(cb)
require.NoError(t, err)
require.NoError(t, addMiniBlock(cb))
require.NoError(t, cb.co.Flush())

// Check the fields reset in the Reset function
require.Equal(t, 1, len(cb.blocks))
Expand All @@ -546,22 +537,20 @@ func TestBuilderReset(t *testing.T) {
require.NoError(t, cb.fullErr)

// Output frames so we can set the channel builder frames
err = cb.OutputFrames()
require.NoError(t, err)
require.NoError(t, cb.OutputFrames())

// Add another block to increment the block count
err = addMiniBlock(cb)
require.NoError(t, err)
require.NoError(t, addMiniBlock(cb))
require.NoError(t, cb.co.Flush())

// Check the fields reset in the Reset function
require.Equal(t, 2, len(cb.blocks))
require.Equal(t, 1, len(cb.frames))
require.Greater(t, len(cb.frames), 1)
require.Equal(t, timeout, cb.timeout)
require.NoError(t, cb.fullErr)

// Reset the channel builder
err = cb.Reset()
require.NoError(t, err)
require.NoError(t, cb.Reset())

// Check the fields reset in the Reset function
require.Equal(t, 0, len(cb.blocks))
Expand Down
Loading