Skip to content

Commit

Permalink
Merge pull request #4 from roberto-bayardo/tmp
Browse files Browse the repository at this point in the history
test fixes
  • Loading branch information
cody-wang-cb authored May 8, 2024
2 parents 6e09caf + e5f7e6c commit 1730bfc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
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

0 comments on commit 1730bfc

Please sign in to comment.