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

Fjord: Add Brotli channel compression support #10358

Merged
merged 37 commits into from
May 13, 2024

Conversation

cody-wang-cb
Copy link
Contributor

@cody-wang-cb cody-wang-cb commented May 1, 2024

Description

In the batcher

  • Added one config flag for selecting compression algo, it can be one of "zlib", "brotli-9", "brotli-10", "brotli-11".
  • Added ChannelCompressor that abstracts away compression logic, it determines how it compresses based on the algo set. For Brotli, it prepends one byte (0x01) as channel version for Brotli every time it resets. So that the channel will always have the channel version byte.
  • In span_channel_out it uses the ChannelCompressor.
  • For singular batch compressors, RatioCompressor and ShadowCompressor both integrates ChannelCompressor. NonCompressor does not integrate given it's not compressing the data.

For ChannelBank

  • In BatchReader, it determines which compression algorithm by reading the first byte.

Tests

  • Modified all the unit tests to run all the compression algorithms.
  • Added unit tests for the new files created.
  • TODO: adding compression algos for e2e tests.

Additional context

Add any other context about the problem you're solving.

Metadata

Copy link
Contributor

coderabbitai bot commented May 1, 2024

Walkthrough

Walkthrough

The changes introduce a new compression algorithm option for the Optimism batcher and related components. This includes adding a CompressionAlgo field to various configurations, updating methods to handle the new field, and implementing logic to support both zlib and brotli compression algorithms. Test cases and configurations are updated accordingly to ensure the new functionality is properly tested and integrated.

Changes

File Path Change Summary
op-batcher/batcher/config.go Added CompressionAlgo field to CLIConfig and validated it in the Check method.
op-batcher/batcher/config_test.go Updated tests to include CompressionAlgo in the RPC configuration.
op-batcher/batcher/flags/flags.go Added flags for CompressionAlgo and CompressLevel with descriptions and default values.
op-batcher/batcher/channel_builder.go Modified NewChannelBuilder to include CompressionAlgo when BatchType is derive.SpanBatchType.
op-batcher/batcher/channel_config.go Updated methods to accept and use compressionAlgo parameter.
op-batcher/batcher/channel_test.go Added CompressorConfig settings with CompressionAlgo in various test functions.
op-batcher/batcher/channel_builder_test.go Updated test functions to handle CompressionAlgo and added new test cases.
op-batcher/batcher/service.go Updated initChannelConfig to include CompressionAlgo.
op-node/rollup/derive/channel.go Added support for zlib and brotli compression in the BatchReader function.
op-node/rollup/derive/span_channel_out.go Introduced CompressorInterface and updated SpanChannelOut to use it based on CompressionAlgo.
op-node/rollup/derive/channel_test.go Added imports and test cases for different compression algorithms in TestBatchReader.
op-node/rollup/derive/channel_compressor.go Introduced functionality for channel compression using zlib and brotli algorithms.
op-e2e/actions/l2_batcher.go Added CompressionAlgo parameter in compressor.NewShadowCompressor and derive.NewSpanChannelOut calls.
op-e2e/setup.go Added CompressionAlgo field to the SystemConfig struct in the Start method.

Recent Review Details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits Files that changed from the base of the PR and between be6629e and eadc24e.
Files selected for processing (1)
  • op-node/rollup/derive/types.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • op-node/rollup/derive/types.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@cody-wang-cb cody-wang-cb changed the title Support Brotli (DRAFT) Support Brotli May 1, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 10

Out of diff range and nitpick comments (3)
op-batcher/compressor/config.go (1)

20-21: Add documentation for CompressionAlgo and CompressLevel.

It's good practice to document struct fields, especially when they are public. This helps other developers understand the purpose and usage of these fields quickly.

op-node/rollup/derive/channel.go (1)

Line range hint 155-204: Refactor BatchReader to improve readability and error handling.

func BatchReader(r io.Reader) (func() (*BatchData, error), error) {
    var buffer strings.Builder
    if _, err := io.Copy(&buffer, r); err != nil {
        return nil, err
    }

    tmpReader := strings.NewReader(buffer.String())
    compressionType := make([]byte, 1)
    if _, err := tmpReader.Read(compressionType); err != nil {
        return nil, err
    }

    fmt.Println("Compression type")
    fmt.Println(strconv.FormatInt(int64(compressionType[0]), 2))
    fmt.Println(compressionType[0])

    r = strings.NewReader(buffer.String())
    var decompress func(io.Reader) (io.Reader, error)
    if compressionType[0]&0x0F == 8 {
        fmt.Println("Using zlib reader")
        decompress = func(r io.Reader) (io.Reader, error) {
            return zlib.NewReader(r)
        }
    } else if compressionType[0]&0x0F == 2 {
        fmt.Println("Using brotli reader")
        r.Read(make([]byte, 1))  // Skip the compression type byte
        decompress = func(r io.Reader) (io.Reader, error) {
            return brotli.NewReader(r), nil
        }
    }

    zr, err := decompress(r)
    if err != nil {
        return nil, err
    }
    rlpReader := rlp.NewStream(zr, MaxRLPBytesPerChannel)
    return func() (*BatchData, error) {
        var batchData BatchData
        if err = rlpReader.Decode(&batchData); err != nil {
            if err != io.EOF {
                fmt.Println("DECODE ERROR")
                fmt.Println(err)
            }
            return nil, err
        }
        return &batchData, nil
    }, nil
}

Refactoring the BatchReader function improves readability and error handling. The proposed changes make the function easier to understand and maintain, especially the handling of different compression types.

op-batcher/batcher/config.go (1)

Line range hint 70-138: Ensure comprehensive validation in CLIConfig.Check.

func (c *CLIConfig) Check() error {
+   if c.CompressionAlgo != "zlib" && c.CompressionAlgo != "brotli" {
+       return errors.New("unsupported compression algorithm")
+   }
+   if (c.CompressionAlgo == "zlib" && (c.CompressLevel < 0 || c.CompressLevel > 9)) || 
+      (c.CompressionAlgo == "brotli" && (c.CompressLevel < 1 || c.CompressLevel > 11)) {
+       return fmt.Errorf("invalid compression level %d for algorithm %s", c.CompressLevel, c.CompressionAlgo)
+   }
    if c.L1EthRpc == "" {
        return errors.New("empty L1 RPC URL")
    }
    if c.L2EthRpc == "" {
        return errors.New("empty L2 RPC URL")
    }
    if c.RollupRpc == "" {
        return errors.New("empty rollup RPC URL")
    }
    if strings.Count(c.RollupRpc, ",") != strings.Count(c.L2EthRpc, ",") {
        return errors.New("number of rollup and eth URLs must match")
    }
    if c.PollInterval == 0 {
        return errors.New("must set PollInterval")
    }
    if c.MaxL1TxSize <= 1 {
        return errors.New("MaxL1TxSize must be greater than 1")
    }
    if c.TargetNumFrames < 1 {
        return errors.New("TargetNumFrames must be at least 1")
    }
    if c.Compressor == compressor.RatioKind && (c.ApproxComprRatio <= 0 || c.ApproxComprRatio > 1) {
        return fmt.Errorf("invalid ApproxComprRatio %v for ratio compressor", c.ApproxComprRatio)
    }
    if c.BatchType > 1 {
        return fmt.Errorf("unknown batch type: %v", c.BatchType)
    }
    if c.CheckRecentTxsDepth > 128 {
        return fmt.Errorf("CheckRecentTxsDepth cannot be set higher than 128: %v", c.CheckRecentTxsDepth)
    }
    if c.DataAvailabilityType == flags.BlobsType && c.TargetNumFrames > 6 {
        return errors.New("too many frames for blob transactions, max 6")
    }
    if !flags.ValidDataAvailabilityType(c.DataAvailabilityType) {
        return fmt.Errorf("unknown data availability type: %q", c.DataAvailabilityType)
    }
    if err := c.MetricsConfig.Check(); err != nil {
        return err
    }
    if err := c.PprofConfig.Check(); err != nil {
        return err
    }
    if err := c.TxMgrConfig.Check(); err != nil {
        return err
    }
    if err := c.RPC.Check(); err != nil {
        return err
    }
    return nil
}

Adding comprehensive validation for the new compression settings in the CLIConfig.Check method ensures that the configuration is always valid. This prevents runtime errors due to incorrect configuration.

op-e2e/actions/sync_test.go Outdated Show resolved Hide resolved
op-e2e/sequencer_failover_setup.go Outdated Show resolved Hide resolved
op-e2e/actions/l2_batcher.go Outdated Show resolved Hide resolved
op-batcher/batcher/channel_builder_test.go Outdated Show resolved Hide resolved
op-batcher/batcher/channel_builder_test.go Outdated Show resolved Hide resolved
op-batcher/batcher/channel_builder_test.go Outdated Show resolved Hide resolved
op-batcher/batcher/channel_builder_test.go Outdated Show resolved Hide resolved
op-batcher/compressor/config.go Outdated Show resolved Hide resolved
op-batcher/batcher/channel_config.go Outdated Show resolved Hide resolved
op-batcher/flags/flags.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 13

Out of diff range and nitpick comments (15)
op-challenger/game/fault/trace/asterisc/provider.go (1)

39-40: Clarify the role of PrestateProvider in the class.

It would be beneficial to add a comment explaining the role and usage of PrestateProvider within AsteriscTraceProvider for better code readability.

op-challenger/game/fault/contracts/gamefactory_test.go (1)

64-64: Ensure consistency in game metadata structure.

It's good practice to ensure that the structure and naming of fields in GameMetadata are consistent across different tests and actual usage in the application.

docs/postmortems/2022-02-02-inflation-vuln.md (7)

Line range hint 8-8: Correct the spelling of "Geth" in the URL.

- [Geth](https://github.com/ethereum/go-ethereu)
+ [Geth](https://github.com/ethereum/go-ethereum)

Line range hint 19-19: Start the section title "Lead up" with an uppercase letter.

- ## Lead up
+ ## Lead Up

Line range hint 22-22: Consider replacing "for several months prior to" with "for several months before".

- for several months prior to identifying this bug.
+ for several months before identifying this bug.

Line range hint 41-41: Replace "to" with "on" for correct preposition usage.

- no impact to ordinary users.
+ no impact on ordinary users.

Line range hint 53-53: Capitalize "Slack" to refer to the communication tool properly.

- private slack channel.
+ private Slack channel.

Line range hint 58-58: Capitalize "GitHub" to refer to the platform properly.

- Using github handles as identifiers
+ Using GitHub handles as identifiers

Line range hint 72-72: Replace "infra" with a more formal term like "infrastructure".

- instructions to infra providers on how to upgrade.
+ instructions to infrastructure providers on how to upgrade.
bedrock-devnet/devnet/__init__.py (5)

Line range hint 7-7: Consider removing the unused import calendar.

- import calendar

Line range hint 12-12: Consider removing the unused import gzip.

- import gzip

Line range hint 18-18: Consider removing the unused import devnet.log_setup.

- import devnet.log_setup

Line range hint 219-219: Simplify the boolean checks by using direct truthy or falsy evaluations.

- if os.path.exists(l2_allocs_path) == False or DEVNET_FPAC == True:
+ if not os.path.exists(l2_allocs_path) or DEVNET_FPAC:

Line range hint 308-308: The local variable e is assigned but never used. Consider logging the exception or removing it if not needed.

- except Exception as e:
+ except Exception:
packages/contracts-bedrock/src/L1/OptimismPortal2.sol (1)

97-99: Consider adding a comment explaining the purpose of the spacer _balance.

While the code is functionally correct, adding a comment explaining the purpose of the spacer _balance would improve code readability and maintainability.

op-challenger/flags/flags.go Outdated Show resolved Hide resolved
op-challenger/flags/flags.go Outdated Show resolved Hide resolved
op-challenger/flags/flags.go Outdated Show resolved Hide resolved
docs/postmortems/2022-02-02-inflation-vuln.md Outdated Show resolved Hide resolved
op-challenger/config/config_test.go Outdated Show resolved Hide resolved
op-node/rollup/derive/span_channel_out.go Outdated Show resolved Hide resolved
op-challenger/game/fault/contracts/gamefactory.go Outdated Show resolved Hide resolved
op-challenger/game/fault/contracts/gamefactory.go Outdated Show resolved Hide resolved
op-challenger/game/monitor_test.go Outdated Show resolved Hide resolved
op-challenger/game/monitor_test.go Outdated Show resolved Hide resolved
@cody-wang-cb cody-wang-cb marked this pull request as draft May 1, 2024 21:36
op-batcher/batcher/channel_manager_test.go Outdated Show resolved Hide resolved
op-e2e/setup.go Outdated Show resolved Hide resolved
op-batcher/batcher/channel_test.go Show resolved Hide resolved
op-batcher/batcher/channel_test.go Show resolved Hide resolved
op-batcher/batcher/channel_test.go Show resolved Hide resolved
op-batcher/batcher/channel_test.go Show resolved Hide resolved
op-batcher/batcher/channel_test.go Show resolved Hide resolved
op-node/benchmarks/batchbuilding_test.go Show resolved Hide resolved
op-node/rollup/derive/channel_compressor.go Outdated Show resolved Hide resolved
packages/contracts-bedrock/lib/forge-std Outdated Show resolved Hide resolved
op-node/rollup/derive/channel.go Outdated Show resolved Hide resolved
op-node/rollup/derive/channel.go Outdated Show resolved Hide resolved
@sebastianst sebastianst marked this pull request as ready for review May 10, 2024 19:36
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

op-e2e/sequencer_failover_setup.go Show resolved Hide resolved
op-e2e/actions/sync_test.go Show resolved Hide resolved
op-batcher/batcher/channel_manager_test.go Show resolved Hide resolved
op-batcher/compressor/config.go Show resolved Hide resolved
op-node/rollup/derive/types.go Show resolved Hide resolved
op-batcher/batcher/channel_config_test.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Out of diff range and nitpick comments (2)
op-node/rollup/derive/channel_test.go (2)

Line range hint 36-36: Replace require.Nil with require.NoError for better error formatting.

- require.Nil(t, err)
+ require.NoError(t, err)

Line range hint 36-36: Replace require.NotNil with require.Error to explicitly check for errors.

- require.NotNil(t, err)
+ require.Error(t, err)

op-node/rollup/derive/channel.go Outdated Show resolved Hide resolved
Copy link
Member

@sebastianst sebastianst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, nice work! ✨ LGTM now.

op-node/rollup/derive/channel_test.go Outdated Show resolved Hide resolved
op-node/rollup/derive/channel.go Show resolved Hide resolved
@sebastianst sebastianst dismissed axelKingsley’s stale review May 13, 2024 19:11

Axel's concerns have been addressed.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

op-node/rollup/derive/channel.go Show resolved Hide resolved
op-node/rollup/derive/channel.go Show resolved Hide resolved
@sebastianst sebastianst added this pull request to the merge queue May 13, 2024
Merged via the queue into ethereum-optimism:develop with commit 4b8f6f4 May 13, 2024
69 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants