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

fix(batcher): include checksum in zlib-compressed channel encoding #395

Merged
merged 1 commit into from
Nov 29, 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
58 changes: 58 additions & 0 deletions op-batcher/batcher/channel_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package batcher

import (
"bytes"
"encoding/binary"
"errors"
"hash/adler32"
"math"
"math/big"
"math/rand"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -810,3 +813,58 @@ func blockBatchRlpSize(t *testing.T, b *types.Block) int {
require.NoError(t, batch.EncodeRLP(&buf), "RLP-encoding batch")
return buf.Len()
}

// [Kroma: START]
func l2BlockToRlpSpanBatch(rollupCfg *rollup.Config, l2block *types.Block) ([]byte, error) {
sbb := derive.NewSpanBatchBuilder(rollupCfg.Genesis.L2Time, rollupCfg.L2ChainID)
batch, l1Info, err := derive.BlockToSingularBatch(rollupCfg, l2block)
if err != nil {
return nil, err
}
sbb.AppendSingularBatch(batch, l1Info.SequenceNumber)
rawSpanBatch, err := sbb.GetRawSpanBatch()
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = rlp.Encode(&buf, derive.NewBatchData(rawSpanBatch))
return buf.Bytes(), nil
}

func TestChannelBuilder_SpanBatchEncodingIncludesChecksum(t *testing.T) {
require := require.New(t)
cfg := ChannelConfig{
MaxFrameSize: 10000,
TargetNumFrames: 1,
BatchType: derive.SpanBatchType,
}
cfg.InitShadowCompressor()
cb, err := NewChannelBuilder(cfg, defaultTestRollupConfig, latestL1BlockOrigin)
require.NoError(err)

rng := rand.New(rand.NewSource(200))
l2block := dtest.RandomL2BlockWithChainId(rng, 2, defaultTestRollupConfig.L2ChainID)

_, err = cb.AddBlock(l2block)
require.NoError(err)

cb.Close()
require.NoError(cb.OutputFrames())

outputFrames := cb.frames
require.Equal(len(outputFrames), 1)

buf := bytes.NewBuffer(outputFrames[0].data)
f := new(derive.Frame)
err = f.UnmarshalBinary(buf)
require.NoError(err)

rlpBatch, err := l2BlockToRlpSpanBatch(&defaultTestRollupConfig, l2block)
require.NoError(err)
require.Greater(len(f.Data), 4)
actualChecksum := f.Data[len(f.Data)-4:]
expectedChecksum := adler32.Checksum(rlpBatch)
require.Equal(binary.BigEndian.Uint32(actualChecksum), expectedChecksum)
}

// [Kroma: END]
12 changes: 10 additions & 2 deletions op-node/rollup/derive/span_channel_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,18 @@ func (co *SpanChannelOut) Close() error {
return ErrChannelOutAlreadyClosed
}
co.closed = true
if err := co.Flush(); err != nil {
if err := co.compress.Close(); err != nil {
return err
}
return co.compress.Close()
if co.ReadyBytes() == 0 && co.compress.Len() > 0 {
_, err := io.Copy(co.reader, co.compress)
if err != nil {
// Must reset reader to avoid partial output
co.reader.Reset()
return fmt.Errorf("failed to flush compressed data to reader: %w", err)
}
}
return nil
}

// OutputFrame writes a frame to w with a given max size and returns the frame
Expand Down
Loading