Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test fixes #4

Merged
merged 1 commit into from
May 8, 2024
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
18 changes: 7 additions & 11 deletions op-node/rollup/derive/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ func BatchReader(r io.Reader) (func() (*BatchData, error), error) {
return nil, err
}

var reader func(io.Reader) (io.Reader, error)
var zr io.Reader
// For zlib, the last 4 bits must be either 8 or 15 (both are reserved value)
if compressionType[0]&0x0F == ZlibCM8 || compressionType[0]&0x0F == ZlibCM15 {
reader = func(r io.Reader) (io.Reader, error) {
return zlib.NewReader(r)
var err error
zr, err = zlib.NewReader(bufReader)
if err != nil {
return nil, err
}
// If the bits equal to 1, then it is a brotli reader
} else if compressionType[0] == ChannelVersionBrotli {
Expand All @@ -174,23 +176,17 @@ func BatchReader(r io.Reader) (func() (*BatchData, error), error) {
if err != nil {
return nil, err
}
reader = func(r io.Reader) (io.Reader, error) {
return brotli.NewReader(r), nil
}
zr = brotli.NewReader(bufReader)
} else {
return nil, fmt.Errorf("cannot distinguish the compression algo used given type byte %v", compressionType[0])
}

// Setup decompressor stage + RLP reader
zr, err := reader(bufReader)
if err != nil {
return nil, err
}
rlpReader := rlp.NewStream(zr, MaxRLPBytesPerChannel)
// Read each batch iteratively
return func() (*BatchData, error) {
var batchData BatchData
if err = rlpReader.Decode(&batchData); err != nil {
if err := rlpReader.Decode(&batchData); err != nil {
return nil, err
}
return &batchData, nil
Expand Down
10 changes: 8 additions & 2 deletions op-node/rollup/derive/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/DataDog/zstd"
"github.com/andybalholm/brotli"
"github.com/ethereum-optimism/optimism/op-service/eth"

"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -114,8 +114,11 @@ func TestBatchReader(t *testing.T) {
batchDataInput := NewBatchData(singularBatch)

encodedBatch := &bytes.Buffer{}
buf := &bytes.Buffer{}
// Get the encoded data of the batch data
batchDataInput.encodeTyped(encodedBatch)
batchDataInput.encodeTyped(buf)
err := rlp.Encode(encodedBatch, buf.Bytes())
require.NoError(t, err)

var testCases = []struct {
name string
Expand All @@ -125,6 +128,7 @@ func TestBatchReader(t *testing.T) {
algo: func(buf *bytes.Buffer) {
writer := zlib.NewWriter(buf)
writer.Write(encodedBatch.Bytes())
writer.Close()
},
},
{
Expand All @@ -133,12 +137,14 @@ func TestBatchReader(t *testing.T) {
buf.WriteByte(ChannelVersionBrotli)
writer := brotli.NewWriterLevel(buf, 10)
writer.Write(encodedBatch.Bytes())
writer.Close()
},
}, {
name: "zstd",
algo: func(buf *bytes.Buffer) {
writer := zstd.NewWriter(buf)
writer.Write(encodedBatch.Bytes())
writer.Close()
},
}}

Expand Down