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