Commit 404cdd79 authored by Andreas Bigger's avatar Andreas Bigger

revert channel builder test suite back to bare metal testing

parent 80377f72
...@@ -7,49 +7,59 @@ import ( ...@@ -7,49 +7,59 @@ import (
"github.com/ethereum-optimism/optimism/op-node/eth" "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/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/require"
) )
// ChannelBuilderTestSuite encapsulates testing on the ChannelBuilder. var defaultTestChannelConfig = ChannelConfig{
type ChannelBuilderTestSuite struct { SeqWindowSize: 15,
suite.Suite ChannelTimeout: 40,
channelConfig ChannelConfig MaxChannelDuration: 1,
SubSafetyMargin: 4,
MaxFrameSize: 120000,
TargetFrameSize: 100000,
TargetNumFrames: 1,
ApproxComprRatio: 0.4,
} }
// SetupTest sets up the test suite. // addNonsenseBlock is a helper function that adds a nonsense block
func (testSuite *ChannelBuilderTestSuite) SetupTest() { // to the channel builder using the [channelBuilder.AddBlock] method.
testSuite.channelConfig = ChannelConfig{ func addNonsenseBlock(cb *channelBuilder) error {
SeqWindowSize: 15, lBlock := types.NewBlock(&types.Header{
ChannelTimeout: 40, BaseFee: big.NewInt(10),
MaxChannelDuration: 1, Difficulty: common.Big0,
SubSafetyMargin: 4, Number: big.NewInt(100),
MaxFrameSize: 120000, }, nil, nil, nil, trie.NewStackTrie(nil))
TargetFrameSize: 100000, l1InfoTx, err := derive.L1InfoDeposit(0, lBlock, eth.SystemConfig{}, false)
TargetNumFrames: 1, if err != nil {
ApproxComprRatio: 0.4, return err
} }
} txs := []*types.Transaction{types.NewTx(l1InfoTx)}
a := types.NewBlock(&types.Header{
// TestChannelBuilder runs the ChannelBuilderTestSuite. Number: big.NewInt(0),
func TestChannelBuilder(t *testing.T) { }, txs, nil, nil, trie.NewStackTrie(nil))
suite.Run(t, new(ChannelBuilderTestSuite)) err = cb.AddBlock(a)
return err
} }
// TestBuilderNextFrame tests calling NextFrame on a ChannelBuilder with only one frame // TestBuilderNextFrame tests calling NextFrame on a ChannelBuilder with only one frame
func (testSuite *ChannelBuilderTestSuite) TestBuilderNextFrame() { func TestBuilderNextFrame(t *testing.T) {
cb, err := newChannelBuilder(testSuite.channelConfig) channelConfig := defaultTestChannelConfig
testSuite.NoError(err)
// Create a new channel builder
cb, err := newChannelBuilder(channelConfig)
require.NoError(t, err)
// Mock the internals of `channelBuilder.outputFrame` // Mock the internals of `channelBuilder.outputFrame`
// to construct a single frame // to construct a single frame
co := cb.co co := cb.co
var buf bytes.Buffer var buf bytes.Buffer
fn, err := co.OutputFrame(&buf, testSuite.channelConfig.MaxFrameSize) fn, err := co.OutputFrame(&buf, channelConfig.MaxFrameSize)
testSuite.NoError(err) require.NoError(t, err)
// Push one frame into to the channel builder // Push one frame into to the channel builder
expectedTx := txID{chID: co.ID(), frameNumber: fn} expectedTx := txID{chID: co.ID(), frameNumber: fn}
...@@ -57,237 +67,226 @@ func (testSuite *ChannelBuilderTestSuite) TestBuilderNextFrame() { ...@@ -57,237 +67,226 @@ func (testSuite *ChannelBuilderTestSuite) TestBuilderNextFrame() {
cb.PushFrame(expectedTx, expectedBytes) cb.PushFrame(expectedTx, expectedBytes)
// There should only be 1 frame in the channel builder // There should only be 1 frame in the channel builder
testSuite.Equal(1, cb.NumFrames()) require.Equal(t, 1, cb.NumFrames())
// We should be able to increment to the next frame // We should be able to increment to the next frame
constructedTx, constructedBytes := cb.NextFrame() constructedTx, constructedBytes := cb.NextFrame()
testSuite.Equal(expectedTx, constructedTx) require.Equal(t, expectedTx, constructedTx)
testSuite.Equal(expectedBytes, constructedBytes) require.Equal(t, expectedBytes, constructedBytes)
testSuite.Equal(0, cb.NumFrames()) require.Equal(t, 0, cb.NumFrames())
// The next call should panic since the length of frames is 0 // The next call should panic since the length of frames is 0
defer func() { _ = recover() }() require.PanicsWithValue(t, "no next frame", func() { cb.NextFrame() })
cb.NextFrame()
// If we get here, `NextFrame` did not panic as expected
testSuite.T().Errorf("did not panic")
} }
// TestBuilderInvalidFrameId tests that a panic is thrown when a frame is pushed with an invalid frame id // TestBuilderInvalidFrameId tests that a panic is thrown when a frame is pushed with an invalid frame id
func (testSuite *ChannelBuilderTestSuite) TestBuilderWrongFramePanic() { func TestBuilderWrongFramePanic(t *testing.T) {
cb, err := newChannelBuilder(testSuite.channelConfig) channelConfig := defaultTestChannelConfig
testSuite.NoError(err)
// Construct a channel builder
cb, err := newChannelBuilder(channelConfig)
require.NoError(t, err)
// Mock the internals of `channelBuilder.outputFrame` // Mock the internals of `channelBuilder.outputFrame`
// to construct a single frame // to construct a single frame
co, err := derive.NewChannelOut() co, err := derive.NewChannelOut()
testSuite.NoError(err) require.NoError(t, err)
var buf bytes.Buffer var buf bytes.Buffer
fn, err := co.OutputFrame(&buf, testSuite.channelConfig.MaxFrameSize) fn, err := co.OutputFrame(&buf, channelConfig.MaxFrameSize)
testSuite.NoError(err) require.NoError(t, err)
// The frame push should panic since we constructed a new channel out // The frame push should panic since we constructed a new channel out
// so the channel out id won't match // so the channel out id won't match
defer func() { _ = recover() }() require.PanicsWithValue(t, "wrong channel", func() {
tx := txID{chID: co.ID(), frameNumber: fn}
// Push one frame into to the channel builder cb.PushFrame(tx, buf.Bytes())
tx := txID{chID: co.ID(), frameNumber: fn} })
cb.PushFrame(tx, buf.Bytes())
// If we get here, `PushFrame` did not panic as expected
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 // TestOutputFrames tests the OutputFrames function
func (testSuite *ChannelBuilderTestSuite) TestOutputFrames() { func TestOutputFrames(t *testing.T) {
channelConfig := defaultTestChannelConfig
// Lower the max frame size so that we can test // Lower the max frame size so that we can test
testSuite.channelConfig.MaxFrameSize = 2 channelConfig.MaxFrameSize = 2
// Construct the channel builder // Construct the channel builder
cb, err := newChannelBuilder(testSuite.channelConfig) cb, err := newChannelBuilder(channelConfig)
testSuite.NoError(err) require.NoError(t, err)
testSuite.False(cb.IsFull()) require.False(t, cb.IsFull())
testSuite.Equal(0, cb.NumFrames()) require.Equal(t, 0, cb.NumFrames())
// Calling OutputFrames without having called [AddBlock] // Calling OutputFrames without having called [AddBlock]
// should return `nil`. // should return no error
testSuite.Nil(cb.OutputFrames()) require.NoError(t, cb.OutputFrames())
// There should be no ready bytes yet // There should be no ready bytes yet
readyBytes := cb.co.ReadyBytes() readyBytes := cb.co.ReadyBytes()
testSuite.Equal(0, readyBytes) require.Equal(t, 0, readyBytes)
// Let's add a block // Let's add a block
err = addNonsenseBlock(cb) err = addNonsenseBlock(cb)
testSuite.NoError(err) require.NoError(t, err)
// Check how many ready bytes // Check how many ready bytes
readyBytes = cb.co.ReadyBytes() readyBytes = cb.co.ReadyBytes()
testSuite.Equal(2, readyBytes) require.Equal(t, 2, readyBytes)
testSuite.Equal(0, cb.NumFrames()) require.Equal(t, 0, cb.NumFrames())
// The channel should not be full // The channel should not be full
// but we want to output the frames for testing anyways // but we want to output the frames for testing anyways
isFull := cb.IsFull() isFull := cb.IsFull()
testSuite.False(isFull) require.False(t, isFull)
// Since we manually set the max frame size to 2, // Since we manually set the max frame size to 2,
// we should be able to compress the two frames now // we should be able to compress the two frames now
err = cb.OutputFrames() err = cb.OutputFrames()
testSuite.NoError(err) require.NoError(t, err)
// There should be one frame in the channel builder now // There should be one frame in the channel builder now
testSuite.Equal(1, cb.NumFrames()) require.Equal(t, 1, cb.NumFrames())
// There should no longer be any ready bytes // There should no longer be any ready bytes
readyBytes = cb.co.ReadyBytes() readyBytes = cb.co.ReadyBytes()
testSuite.Equal(0, readyBytes) require.Equal(t, 0, readyBytes)
} }
// TestBuilderAddBlock tests the AddBlock function // TestBuilderAddBlock tests the AddBlock function
func (testSuite *ChannelBuilderTestSuite) TestBuilderAddBlock() { func TestBuilderAddBlock(t *testing.T) {
channelConfig := defaultTestChannelConfig
// Lower the max frame size so that we can batch // Lower the max frame size so that we can batch
testSuite.channelConfig.MaxFrameSize = 2 channelConfig.MaxFrameSize = 2
// Configure the Input Threshold params so we observe a full channel // 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 // In reality, we only need the input bytes (74) below to be greater than
// or equal to the input threshold (3 * 2) / 1 = 6 // or equal to the input threshold (3 * 2) / 1 = 6
testSuite.channelConfig.TargetFrameSize = 3 channelConfig.TargetFrameSize = 3
testSuite.channelConfig.TargetNumFrames = 2 channelConfig.TargetNumFrames = 2
testSuite.channelConfig.ApproxComprRatio = 1 channelConfig.ApproxComprRatio = 1
// Construct the channel builder // Construct the channel builder
cb, err := newChannelBuilder(testSuite.channelConfig) cb, err := newChannelBuilder(channelConfig)
testSuite.NoError(err) require.NoError(t, err)
// Add a nonsense block to the channel builder // Add a nonsense block to the channel builder
err = addNonsenseBlock(cb) err = addNonsenseBlock(cb)
testSuite.NoError(err) require.NoError(t, err)
// Check the fields reset in the AddBlock function // Check the fields reset in the AddBlock function
testSuite.Equal(74, cb.co.InputBytes()) require.Equal(t, 74, cb.co.InputBytes())
testSuite.Equal(1, len(cb.blocks)) require.Equal(t, 1, len(cb.blocks))
testSuite.Equal(0, len(cb.frames)) require.Equal(t, 0, len(cb.frames))
testSuite.True(cb.IsFull()) require.True(t, cb.IsFull())
// Since the channel output is full, the next call to AddBlock // Since the channel output is full, the next call to AddBlock
// should return the channel out full error // should return the channel out full error
err = addNonsenseBlock(cb) err = addNonsenseBlock(cb)
testSuite.ErrorIs(err, ErrInputTargetReached) require.ErrorIs(t, err, ErrInputTargetReached)
} }
// TestBuilderReset tests the Reset function // TestBuilderReset tests the Reset function
func (testSuite *ChannelBuilderTestSuite) TestBuilderReset() { func TestBuilderReset(t *testing.T) {
channelConfig := defaultTestChannelConfig
// Lower the max frame size so that we can batch // Lower the max frame size so that we can batch
testSuite.channelConfig.MaxFrameSize = 2 channelConfig.MaxFrameSize = 2
cb, err := newChannelBuilder(testSuite.channelConfig) cb, err := newChannelBuilder(channelConfig)
testSuite.NoError(err) require.NoError(t, err)
// Add a nonsense block to the channel builder // Add a nonsense block to the channel builder
err = addNonsenseBlock(cb) err = addNonsenseBlock(cb)
testSuite.NoError(err) require.NoError(t, err)
// Check the fields reset in the Reset function // Check the fields reset in the Reset function
testSuite.Equal(1, len(cb.blocks)) require.Equal(t, 1, len(cb.blocks))
testSuite.Equal(0, len(cb.frames)) require.Equal(t, 0, len(cb.frames))
// Timeout should be updated in the AddBlock internal call to `updateSwTimeout` // Timeout should be updated in the AddBlock internal call to `updateSwTimeout`
timeout := uint64(100) + cb.cfg.SeqWindowSize - cb.cfg.SubSafetyMargin timeout := uint64(100) + cb.cfg.SeqWindowSize - cb.cfg.SubSafetyMargin
testSuite.Equal(timeout, cb.timeout) require.Equal(t, timeout, cb.timeout)
testSuite.Nil(cb.fullErr) require.NoError(t, cb.fullErr)
// Output frames so we can set the channel builder frames // Output frames so we can set the channel builder frames
err = cb.OutputFrames() err = cb.OutputFrames()
testSuite.NoError(err) require.NoError(t, err)
// Add another block to increment the block count // Add another block to increment the block count
err = addNonsenseBlock(cb) err = addNonsenseBlock(cb)
testSuite.NoError(err) require.NoError(t, err)
// Check the fields reset in the Reset function // Check the fields reset in the Reset function
testSuite.Equal(2, len(cb.blocks)) require.Equal(t, 2, len(cb.blocks))
testSuite.Equal(1, len(cb.frames)) require.Equal(t, 1, len(cb.frames))
testSuite.Equal(timeout, cb.timeout) require.Equal(t, timeout, cb.timeout)
testSuite.Nil(cb.fullErr) require.NoError(t, cb.fullErr)
// Reset the channel builder // Reset the channel builder
err = cb.Reset() err = cb.Reset()
testSuite.NoError(err) require.NoError(t, err)
// Check the fields reset in the Reset function // Check the fields reset in the Reset function
testSuite.Equal(0, len(cb.blocks)) require.Equal(t, 0, len(cb.blocks))
testSuite.Equal(0, len(cb.frames)) require.Equal(t, 0, len(cb.frames))
testSuite.Equal(uint64(0), cb.timeout) require.Equal(t, uint64(0), cb.timeout)
testSuite.Nil(cb.fullErr) require.NoError(t, cb.fullErr)
testSuite.Equal(0, cb.co.InputBytes()) require.Equal(t, 0, cb.co.InputBytes())
testSuite.Equal(0, cb.co.ReadyBytes()) require.Equal(t, 0, cb.co.ReadyBytes())
} }
// TestBuilderRegisterL1Block tests the RegisterL1Block function // TestBuilderRegisterL1Block tests the RegisterL1Block function
func (testSuite *ChannelBuilderTestSuite) TestBuilderRegisterL1Block() { func TestBuilderRegisterL1Block(t *testing.T) {
channelConfig := defaultTestChannelConfig
// Construct the channel builder // Construct the channel builder
cb, err := newChannelBuilder(testSuite.channelConfig) cb, err := newChannelBuilder(channelConfig)
testSuite.NoError(err) require.NoError(t, err)
// Assert params modified in RegisterL1Block // Assert params modified in RegisterL1Block
testSuite.Equal(uint64(1), testSuite.channelConfig.MaxChannelDuration) require.Equal(t, uint64(1), channelConfig.MaxChannelDuration)
testSuite.Equal(uint64(0), cb.timeout) require.Equal(t, uint64(0), cb.timeout)
// Register a new L1 block // Register a new L1 block
cb.RegisterL1Block(uint64(100)) cb.RegisterL1Block(uint64(100))
// Assert params modified in RegisterL1Block // Assert params modified in RegisterL1Block
testSuite.Equal(uint64(1), testSuite.channelConfig.MaxChannelDuration) require.Equal(t, uint64(1), channelConfig.MaxChannelDuration)
testSuite.Equal(uint64(101), cb.timeout) require.Equal(t, uint64(101), cb.timeout)
} }
// TestBuilderRegisterL1BlockZeroMaxChannelDuration tests the RegisterL1Block function // TestBuilderRegisterL1BlockZeroMaxChannelDuration tests the RegisterL1Block function
func (testSuite *ChannelBuilderTestSuite) TestBuilderRegisterL1BlockZeroMaxChannelDuration() { func TestBuilderRegisterL1BlockZeroMaxChannelDuration(t *testing.T) {
channelConfig := defaultTestChannelConfig
// Set the max channel duration to 0 // Set the max channel duration to 0
testSuite.channelConfig.MaxChannelDuration = 0 channelConfig.MaxChannelDuration = 0
// Construct the channel builder // Construct the channel builder
cb, err := newChannelBuilder(testSuite.channelConfig) cb, err := newChannelBuilder(channelConfig)
testSuite.NoError(err) require.NoError(t, err)
// Assert params modified in RegisterL1Block // Assert params modified in RegisterL1Block
testSuite.Equal(uint64(0), testSuite.channelConfig.MaxChannelDuration) require.Equal(t, uint64(0), channelConfig.MaxChannelDuration)
testSuite.Equal(uint64(0), cb.timeout) require.Equal(t, uint64(0), cb.timeout)
// Register a new L1 block // Register a new L1 block
cb.RegisterL1Block(uint64(100)) cb.RegisterL1Block(uint64(100))
// Since the max channel duration is set to 0, // Since the max channel duration is set to 0,
// the L1 block register should not update the timeout // the L1 block register should not update the timeout
testSuite.Equal(uint64(0), testSuite.channelConfig.MaxChannelDuration) require.Equal(t, uint64(0), channelConfig.MaxChannelDuration)
testSuite.Equal(uint64(0), cb.timeout) require.Equal(t, uint64(0), cb.timeout)
} }
// TestFramePublished tests the FramePublished function // TestFramePublished tests the FramePublished function
func (testSuite *ChannelBuilderTestSuite) TestFramePublished() { func TestFramePublished(t *testing.T) {
channelConfig := defaultTestChannelConfig
// Construct the channel builder // Construct the channel builder
cb, err := newChannelBuilder(testSuite.channelConfig) cb, err := newChannelBuilder(channelConfig)
testSuite.NoError(err) require.NoError(t, err)
// Let's say the block number is fed in as 100 // Let's say the block number is fed in as 100
// and the channel timeout is 1000 // and the channel timeout is 1000
...@@ -299,28 +298,5 @@ func (testSuite *ChannelBuilderTestSuite) TestFramePublished() { ...@@ -299,28 +298,5 @@ func (testSuite *ChannelBuilderTestSuite) TestFramePublished() {
cb.FramePublished(l1BlockNum) cb.FramePublished(l1BlockNum)
// Now the timeout will be 1000 // Now the timeout will be 1000
testSuite.Equal(uint64(1000), cb.timeout) require.Equal(t, uint64(1000), cb.timeout)
}
// TestFramePublishedUnderflows tests the FramePublished function
//
// Realistically, this will not happen because the [ChannelTimeout] is _known_
// to be greater than the [SubSafetyMargin] when the ChannelConfig is configured
// via cli flags / environment variables.
func (testSuite *ChannelBuilderTestSuite) TestFramePublishedUnderflows() {
// Construct the channel builder
cb, err := newChannelBuilder(testSuite.channelConfig)
testSuite.NoError(err)
// Let's say the block number is fed in as 0
// and the channel timeout is < the sub safety margin
l1BlockNum := uint64(0)
cb.cfg.ChannelTimeout = uint64(1)
cb.cfg.SubSafetyMargin = uint64(2)
// Then the frame published will underflow the timeout
cb.FramePublished(l1BlockNum)
// Now the timeout will be the max uint64
testSuite.Equal(uint64(0xffffffffffffffff), cb.timeout)
} }
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