Commit 6b6bc5ff authored by Michael de Hoog's avatar Michael de Hoog

Default to Ratio compressor

parent 6f831fdc
......@@ -58,8 +58,6 @@ type ChannelConfig struct {
// CompressorConfig contains the configuration for creating new compressors.
CompressorConfig compressor.Config
// CompressorFactory creates new compressors.
CompressorFactory compressor.FactoryFunc
}
// Check validates the [ChannelConfig] parameters.
......@@ -128,7 +126,7 @@ type channelBuilder struct {
// newChannelBuilder creates a new channel builder or returns an error if the
// channel out could not be created.
func newChannelBuilder(cfg ChannelConfig) (*channelBuilder, error) {
c, err := cfg.CompressorFactory(cfg.CompressorConfig)
c, err := cfg.CompressorConfig.NewCompressor()
if err != nil {
return nil, err
}
......
......@@ -34,7 +34,6 @@ var defaultTestChannelConfig = ChannelConfig{
TargetNumFrames: 1,
ApproxComprRatio: 0.4,
},
CompressorFactory: compressor.NewRatioCompressor,
}
// TestChannelConfig_Check tests the [ChannelConfig] [Check] function.
......@@ -420,7 +419,7 @@ func TestChannelBuilder_OutputWrongFramePanic(t *testing.T) {
// Mock the internals of `channelBuilder.outputFrame`
// to construct a single frame
c, err := channelConfig.CompressorFactory(channelConfig.CompressorConfig)
c, err := channelConfig.CompressorConfig.NewCompressor()
require.NoError(t, err)
co, err := derive.NewChannelOut(c)
require.NoError(t, err)
......
......@@ -104,7 +104,6 @@ func TestChannelManagerReturnsErrReorgWhenDrained(t *testing.T) {
TargetFrameSize: 0,
ApproxComprRatio: 1.0,
},
CompressorFactory: compressor.NewRatioCompressor,
})
a := newMiniL2Block(0)
......@@ -180,7 +179,6 @@ func TestChannelManager_Clear(t *testing.T) {
TargetNumFrames: 1,
ApproxComprRatio: 1.0,
},
CompressorFactory: compressor.NewRatioCompressor,
})
// Channel Manager state should be empty by default
......@@ -344,7 +342,6 @@ func TestChannelManager_TxResend(t *testing.T) {
TargetFrameSize: 0,
ApproxComprRatio: 1.0,
},
CompressorFactory: compressor.NewRatioCompressor,
})
a, _ := derivetest.RandomL2Block(rng, 4)
......@@ -389,7 +386,6 @@ func TestChannelManagerCloseBeforeFirstUse(t *testing.T) {
TargetFrameSize: 0,
ApproxComprRatio: 1.0,
},
CompressorFactory: compressor.NewRatioCompressor,
})
a, _ := derivetest.RandomL2Block(rng, 4)
......@@ -418,7 +414,6 @@ func TestChannelManagerCloseNoPendingChannel(t *testing.T) {
TargetNumFrames: 1,
ApproxComprRatio: 1.0,
},
CompressorFactory: compressor.NewRatioCompressor,
})
a := newMiniL2Block(0)
b := newMiniL2BlockWithNumberParent(0, big.NewInt(1), a.Hash())
......@@ -458,7 +453,6 @@ func TestChannelManagerClosePendingChannel(t *testing.T) {
TargetFrameSize: 1000,
ApproxComprRatio: 1.0,
},
CompressorFactory: compressor.NewRatioCompressor,
})
a := newMiniL2Block(50_000)
......@@ -504,7 +498,6 @@ func TestChannelManagerCloseAllTxsFailed(t *testing.T) {
TargetFrameSize: 1000,
ApproxComprRatio: 1.0,
},
CompressorFactory: compressor.NewRatioCompressor,
})
a := newMiniL2Block(50_000)
......
......@@ -111,9 +111,6 @@ func (c CLIConfig) Check() error {
if err := c.TxMgrConfig.Check(); err != nil {
return err
}
if err := c.CompressorConfig.Check(); err != nil {
return err
}
return nil
}
......
......@@ -75,11 +75,6 @@ func NewBatchSubmitterFromCLIConfig(cfg CLIConfig, l log.Logger, m metrics.Metri
return nil, err
}
compressorFactory, err := cfg.CompressorConfig.Factory()
if err != nil {
return nil, err
}
batcherCfg := Config{
L1Client: l1Client,
L2Client: l2Client,
......@@ -95,8 +90,7 @@ func NewBatchSubmitterFromCLIConfig(cfg CLIConfig, l log.Logger, m metrics.Metri
MaxChannelDuration: cfg.MaxChannelDuration,
SubSafetyMargin: cfg.SubSafetyMargin,
MaxFrameSize: cfg.MaxL1TxSize - 1, // subtract 1 byte for version
CompressorConfig: cfg.CompressorConfig.Config,
CompressorFactory: compressorFactory,
CompressorConfig: cfg.CompressorConfig.Config(),
},
}
......
package compressor
import (
"fmt"
"strings"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/urfave/cli"
......@@ -11,7 +11,7 @@ const (
TargetL1TxSizeBytesFlagName = "target-l1-tx-size-bytes"
TargetNumFramesFlagName = "target-num-frames"
ApproxComprRatioFlagName = "approx-compr-ratio"
TypeFlagName = "compressor"
KindFlagName = "compressor"
)
func CLIFlags(envPrefix string) []cli.Flag {
......@@ -35,40 +35,44 @@ func CLIFlags(envPrefix string) []cli.Flag {
EnvVar: opservice.PrefixEnvVar(envPrefix, "APPROX_COMPR_RATIO"),
},
cli.StringFlag{
Name: TypeFlagName,
Usage: "The type of compressor. Valid options: " + FactoryFlags(),
Name: KindFlagName,
Usage: "The type of compressor. Valid options: " + strings.Join(KindKeys, ", "),
EnvVar: opservice.PrefixEnvVar(envPrefix, "COMPRESSOR"),
Value: Ratio.FlagValue,
Value: RatioKind,
},
}
}
type CLIConfig struct {
Type string
Config Config
// TargetL1TxSizeBytes to target when creating channel frames. Note that if the
// realized compression ratio is worse than the approximate, more frames may
// actually be created. This also depends on how close the target is to the
// max frame size.
TargetL1TxSizeBytes uint64
// TargetNumFrames to create in this channel. If the realized compression ratio
// is worse than approxComprRatio, additional leftover frame(s) might get created.
TargetNumFrames int
// ApproxComprRatio to assume. Should be slightly smaller than average from
// experiments to avoid the chances of creating a small additional leftover frame.
ApproxComprRatio float64
// Type of compressor to use. Must be one of KindKeys.
Kind string
}
func (c *CLIConfig) Check() error {
_, err := c.Factory()
return err
}
func (c *CLIConfig) Factory() (FactoryFunc, error) {
for _, f := range Factories {
if f.FlagValue == c.Type {
return f.FactoryFunc, nil
func (c *CLIConfig) Config() Config {
return Config{
TargetFrameSize: c.TargetL1TxSizeBytes - 1, // subtract 1 byte for version
TargetNumFrames: c.TargetNumFrames,
ApproxComprRatio: c.ApproxComprRatio,
Kind: c.Kind,
}
}
return nil, fmt.Errorf("unknown compressor kind: %q", c.Type)
}
func ReadCLIConfig(ctx *cli.Context) CLIConfig {
return CLIConfig{
Type: ctx.GlobalString(TypeFlagName),
Config: Config{
TargetFrameSize: ctx.GlobalUint64(TargetL1TxSizeBytesFlagName) - 1, // subtract 1 byte for version,
Kind: ctx.GlobalString(KindFlagName),
TargetL1TxSizeBytes: ctx.GlobalUint64(TargetL1TxSizeBytesFlagName),
TargetNumFrames: ctx.GlobalInt(TargetNumFramesFlagName),
ApproxComprRatio: ctx.GlobalFloat64(ApproxComprRatioFlagName),
},
}
}
package compressor
import (
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
)
type FactoryFunc func(Config) (derive.Compressor, error)
const RatioKind = "ratio"
const ShadowKind = "shadow"
var Kinds = map[string]FactoryFunc{
RatioKind: NewRatioCompressor,
ShadowKind: NewShadowCompressor,
}
var KindKeys []string
func init() {
for k := range Kinds {
KindKeys = append(KindKeys, k)
}
}
package compressor
import (
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
)
type Config struct {
// FrameSizeTarget to target when creating channel frames. Note that if the
// TargetFrameSize to target when creating channel frames. Note that if the
// realized compression ratio is worse than the approximate, more frames may
// actually be created. This also depends on how close the target is to the
// max frame size.
TargetFrameSize uint64
// NumFramesTarget to create in this channel. If the realized compression ratio
// TargetNumFrames to create in this channel. If the realized compression ratio
// is worse than approxComprRatio, additional leftover frame(s) might get created.
TargetNumFrames int
// ApproxCompRatio to assume. Should be slightly smaller than average from
// ApproxComprRatio to assume. Should be slightly smaller than average from
// experiments to avoid the chances of creating a small additional leftover frame.
ApproxComprRatio float64
// Kind of compressor to use. Must
Kind string
}
func (c Config) NewCompressor() (derive.Compressor, error) {
if k, ok := Kinds[c.Kind]; ok {
return k(c)
}
// default to RatioCompressor
return Kinds[RatioKind](c)
}
package compressor
import (
"strings"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
)
type FactoryFunc func(Config) (derive.Compressor, error)
type FactoryFlag struct {
FlagValue string
FactoryFunc FactoryFunc
}
var (
// The Ratio Factory creates new RatioCompressor's (see NewRatioCompressor
// for a description).
Ratio = FactoryFlag{
FlagValue: "ratio",
FactoryFunc: NewRatioCompressor,
}
// The Shadow Factory creates new ShadowCompressor's (see NewShadowCompressor
// for a description).
Shadow = FactoryFlag{
FlagValue: "shadow",
FactoryFunc: NewShadowCompressor,
}
)
var Factories = []FactoryFlag{
Ratio,
Shadow,
}
func FactoryFlags() string {
var out strings.Builder
for i, v := range Factories {
out.WriteString(v.FlagValue)
if i+1 < len(Factories) {
out.WriteString(", ")
}
}
return out.String()
}
......@@ -337,13 +337,10 @@ func TestMigration(t *testing.T) {
MaxChannelDuration: 1,
MaxL1TxSize: 120_000,
CompressorConfig: compressor.CLIConfig{
Type: compressor.Ratio.FlagValue,
Config: compressor.Config{
TargetFrameSize: 100_000 - 1,
TargetL1TxSizeBytes: 100_000,
TargetNumFrames: 1,
ApproxComprRatio: 0.4,
},
},
SubSafetyMargin: 4,
PollInterval: 50 * time.Millisecond,
TxMgrConfig: newTxMgrConfig(forkedL1URL, secrets.Batcher),
......
......@@ -601,13 +601,10 @@ func (cfg SystemConfig) Start(_opts ...SystemConfigOption) (*System, error) {
MaxChannelDuration: 1,
MaxL1TxSize: 120_000,
CompressorConfig: compressor.CLIConfig{
Type: compressor.Ratio.FlagValue,
Config: compressor.Config{
TargetFrameSize: 100_000 - 1,
TargetL1TxSizeBytes: 100_000,
TargetNumFrames: 1,
ApproxComprRatio: 0.4,
},
},
SubSafetyMargin: 4,
PollInterval: 50 * time.Millisecond,
TxMgrConfig: newTxMgrConfig(sys.Nodes["l1"].WSEndpoint(), cfg.Secrets.Batcher),
......
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