Commit e80cfdb2 authored by Andreas Bigger's avatar Andreas Bigger

start channel building tests

parent 35d4a37c
...@@ -79,7 +79,9 @@ type channelBuilder struct { ...@@ -79,7 +79,9 @@ type channelBuilder struct {
frames []taggedData frames []taggedData
} }
func newChannelBuilder(cfg ChannelConfig) (*channelBuilder, error) { // NewChannelBuilder creates a new channel builder or returns an error if the
// channel out could not be created.
func NewChannelBuilder(cfg ChannelConfig) (*channelBuilder, error) {
co, err := derive.NewChannelOut() co, err := derive.NewChannelOut()
if err != nil { if err != nil {
return nil, err return nil, err
......
package batcher
import (
"bytes"
"testing"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/stretchr/testify/require"
)
var defaultTestConfig = ChannelConfig{
SeqWindowSize: 15,
ChannelTimeout: 40,
MaxChannelDuration: 1,
SubSafetyMargin: 4,
MaxFrameSize: 120000,
TargetFrameSize: 100000,
TargetNumFrames: 1,
ApproxComprRatio: 0.4,
}
// TestBuilderNextFrame tests calling NextFrame on a ChannelBuilder with only one frame
func TestBuilderNextFrame(t *testing.T) {
cb, err := NewChannelBuilder(defaultTestConfig)
require.NoError(t, err)
// Mock the internals of `channelBuilder.outputFrame`
// to construct a single frame
co := cb.co
var buf bytes.Buffer
fn, err := co.OutputFrame(&buf, defaultTestConfig.MaxFrameSize)
require.NoError(t, err)
// Push one frame into to the channel builder
expectedTx := txID{chID: co.ID(), frameNumber: fn}
expectedBytes := buf.Bytes()
cb.PushFrame(expectedTx, expectedBytes)
// There should only be 1 frame in the channel builder
require.Equal(t, 1, cb.NumFrames())
// We should be able to increment to the next frame
constructedTx, constructedBytes := cb.NextFrame()
require.Equal(t, expectedTx, constructedTx)
require.Equal(t, expectedBytes, constructedBytes)
require.Equal(t, 0, cb.NumFrames())
// The next call should panic since the length of frames is 0
defer func() { _ = recover() }()
cb.NextFrame()
// If we get here, `NextFrame` did not panic as expected
t.Errorf("did not panic")
}
// TestBuilderInvalidFrameId tests that a panic is thrown when a frame is pushed with an invalid frame id
func TestBuilderWrongFramePanic(t *testing.T) {
cb, err := NewChannelBuilder(defaultTestConfig)
require.NoError(t, err)
// Mock the internals of `channelBuilder.outputFrame`
// to construct a single frame
co, err := derive.NewChannelOut()
require.NoError(t, err)
var buf bytes.Buffer
fn, err := co.OutputFrame(&buf, defaultTestConfig.MaxFrameSize)
require.NoError(t, err)
// The frame push should panic since we constructed a new channel out
// so the channel out id won't match
defer func() { _ = recover() }()
// Push one frame into to the channel builder
tx := txID{chID: co.ID(), frameNumber: fn}
cb.PushFrame(tx, buf.Bytes())
// If we get here, `PushFrame` did not panic as expected
t.Errorf("did not panic")
}
...@@ -229,7 +229,7 @@ func (s *channelManager) ensurePendingChannel(l1Head eth.BlockID) error { ...@@ -229,7 +229,7 @@ func (s *channelManager) ensurePendingChannel(l1Head eth.BlockID) error {
return nil return nil
} }
cb, err := newChannelBuilder(s.cfg) cb, err := NewChannelBuilder(s.cfg)
if err != nil { if err != nil {
return fmt.Errorf("creating new channel: %w", err) return fmt.Errorf("creating new channel: %w", err)
} }
......
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