Commit 5199c4a0 authored by Adrian Sutton's avatar Adrian Sutton

op-challenger: Carefully rearrange modules to avoid cycles.

parent c14c1725
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"testing" "testing"
"github.com/ethereum-optimism/optimism/op-challenger/config" "github.com/ethereum-optimism/optimism/op-challenger/config"
"github.com/ethereum-optimism/optimism/op-challenger/fault" "github.com/ethereum-optimism/optimism/op-challenger/flags"
"github.com/ethereum-optimism/optimism/op-service/txmgr" "github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
...@@ -39,12 +39,12 @@ func TestLogLevel(t *testing.T) { ...@@ -39,12 +39,12 @@ func TestLogLevel(t *testing.T) {
func TestDefaultCLIOptionsMatchDefaultConfig(t *testing.T) { func TestDefaultCLIOptionsMatchDefaultConfig(t *testing.T) {
cfg := configForArgs(t, addRequiredArgs()) cfg := configForArgs(t, addRequiredArgs())
defaultCfg := config.NewConfig(l1EthRpc, common.HexToAddress(gameAddressValue), fault.TraceTypeAlphabet, alphabetTrace, cannonDatadir, true, 4) defaultCfg := config.NewConfig(l1EthRpc, common.HexToAddress(gameAddressValue), flags.TraceTypeAlphabet, alphabetTrace, cannonDatadir, true, 4)
require.Equal(t, defaultCfg, cfg) require.Equal(t, defaultCfg, cfg)
} }
func TestDefaultConfigIsValid(t *testing.T) { func TestDefaultConfigIsValid(t *testing.T) {
cfg := config.NewConfig(l1EthRpc, common.HexToAddress(gameAddressValue), fault.TraceTypeAlphabet, alphabetTrace, cannonDatadir, true, 4) cfg := config.NewConfig(l1EthRpc, common.HexToAddress(gameAddressValue), flags.TraceTypeAlphabet, alphabetTrace, cannonDatadir, true, 4)
require.NoError(t, cfg.Check()) require.NoError(t, cfg.Check())
} }
...@@ -66,7 +66,7 @@ func TestTraceType(t *testing.T) { ...@@ -66,7 +66,7 @@ func TestTraceType(t *testing.T) {
verifyArgsInvalid(t, "flag trace-type is required", addRequiredArgsExcept("--trace-type")) verifyArgsInvalid(t, "flag trace-type is required", addRequiredArgsExcept("--trace-type"))
}) })
for _, traceType := range fault.TraceTypes { for _, traceType := range flags.TraceTypes {
traceType := traceType traceType := traceType
t.Run("Valid_"+traceType.String(), func(t *testing.T) { t.Run("Valid_"+traceType.String(), func(t *testing.T) {
cfg := configForArgs(t, addRequiredArgsExcept("--trace-type", "--trace-type", traceType.String())) cfg := configForArgs(t, addRequiredArgsExcept("--trace-type", "--trace-type", traceType.String()))
...@@ -172,7 +172,7 @@ func requiredArgs() map[string]string { ...@@ -172,7 +172,7 @@ func requiredArgs() map[string]string {
"--agree-with-proposed-output": agreeWithProposedOutput, "--agree-with-proposed-output": agreeWithProposedOutput,
"--l1-eth-rpc": l1EthRpc, "--l1-eth-rpc": l1EthRpc,
"--game-address": gameAddressValue, "--game-address": gameAddressValue,
"--trace-type": fault.TraceTypeAlphabet.String(), "--trace-type": flags.TraceTypeAlphabet.String(),
"--alphabet": alphabetTrace, "--alphabet": alphabetTrace,
"--cannon-datadir": cannonDatadir, "--cannon-datadir": cannonDatadir,
} }
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"errors" "errors"
"strings" "strings"
"github.com/ethereum-optimism/optimism/op-challenger/fault"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
...@@ -30,7 +29,7 @@ type Config struct { ...@@ -30,7 +29,7 @@ type Config struct {
AgreeWithProposedOutput bool // Temporary config if we agree or disagree with the posted output AgreeWithProposedOutput bool // Temporary config if we agree or disagree with the posted output
GameDepth int // Depth of the game tree GameDepth int // Depth of the game tree
TraceType fault.TraceType // Type of trace TraceType flags.TraceType // Type of trace
AlphabetTrace string // String for the AlphabetTraceProvider AlphabetTrace string // String for the AlphabetTraceProvider
CannonDatadir string // Cannon Data Directory for the CannonTraceProvider CannonDatadir string // Cannon Data Directory for the CannonTraceProvider
...@@ -40,7 +39,7 @@ type Config struct { ...@@ -40,7 +39,7 @@ type Config struct {
func NewConfig( func NewConfig(
l1EthRpc string, l1EthRpc string,
gameAddress common.Address, gameAddress common.Address,
traceType fault.TraceType, traceType flags.TraceType,
alphabetTrace string, alphabetTrace string,
cannonDatadir string, cannonDatadir string,
agreeWithProposedOutput bool, agreeWithProposedOutput bool,
...@@ -71,10 +70,10 @@ func (c Config) Check() error { ...@@ -71,10 +70,10 @@ func (c Config) Check() error {
if c.TraceType == "" { if c.TraceType == "" {
return ErrMissingTraceType return ErrMissingTraceType
} }
if c.TraceType == fault.TraceTypeCannon && c.CannonDatadir == "" { if c.TraceType == flags.TraceTypeCannon && c.CannonDatadir == "" {
return ErrMissingCannonDatadir return ErrMissingCannonDatadir
} }
if c.TraceType == fault.TraceTypeAlphabet && c.AlphabetTrace == "" { if c.TraceType == flags.TraceTypeAlphabet && c.AlphabetTrace == "" {
return ErrMissingAlphabetTrace return ErrMissingAlphabetTrace
} }
if err := c.TxMgrConfig.Check(); err != nil { if err := c.TxMgrConfig.Check(); err != nil {
...@@ -95,7 +94,7 @@ func NewConfigFromCLI(ctx *cli.Context) (*Config, error) { ...@@ -95,7 +94,7 @@ func NewConfigFromCLI(ctx *cli.Context) (*Config, error) {
txMgrConfig := txmgr.ReadCLIConfig(ctx) txMgrConfig := txmgr.ReadCLIConfig(ctx)
traceTypeFlag := fault.TraceType(strings.ToLower(ctx.String(flags.TraceTypeFlag.Name))) traceTypeFlag := flags.TraceType(strings.ToLower(ctx.String(flags.TraceTypeFlag.Name)))
return &Config{ return &Config{
// Required Flags // Required Flags
......
...@@ -3,7 +3,7 @@ package config ...@@ -3,7 +3,7 @@ package config
import ( import (
"testing" "testing"
"github.com/ethereum-optimism/optimism/op-challenger/fault" "github.com/ethereum-optimism/optimism/op-challenger/flags"
"github.com/ethereum-optimism/optimism/op-service/txmgr" "github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -18,27 +18,27 @@ var ( ...@@ -18,27 +18,27 @@ var (
gameDepth = 4 gameDepth = 4
) )
func validConfig(traceType fault.TraceType) Config { func validConfig(traceType flags.TraceType) Config {
cfg := NewConfig(validL1EthRpc, validGameAddress, traceType, validAlphabetTrace, validCannonDatadir, agreeWithProposedOutput, gameDepth) cfg := NewConfig(validL1EthRpc, validGameAddress, traceType, validAlphabetTrace, validCannonDatadir, agreeWithProposedOutput, gameDepth)
return cfg return cfg
} }
// TestValidConfigIsValid checks that the config provided by validConfig is actually valid // TestValidConfigIsValid checks that the config provided by validConfig is actually valid
func TestValidConfigIsValid(t *testing.T) { func TestValidConfigIsValid(t *testing.T) {
err := validConfig(fault.TraceTypeCannon).Check() err := validConfig(flags.TraceTypeCannon).Check()
require.NoError(t, err) require.NoError(t, err)
} }
func TestTxMgrConfig(t *testing.T) { func TestTxMgrConfig(t *testing.T) {
t.Run("Invalid", func(t *testing.T) { t.Run("Invalid", func(t *testing.T) {
config := validConfig(fault.TraceTypeCannon) config := validConfig(flags.TraceTypeCannon)
config.TxMgrConfig = txmgr.CLIConfig{} config.TxMgrConfig = txmgr.CLIConfig{}
require.Equal(t, config.Check().Error(), "must provide a L1 RPC url") require.Equal(t, config.Check().Error(), "must provide a L1 RPC url")
}) })
} }
func TestL1EthRpcRequired(t *testing.T) { func TestL1EthRpcRequired(t *testing.T) {
config := validConfig(fault.TraceTypeCannon) config := validConfig(flags.TraceTypeCannon)
config.L1EthRpc = "" config.L1EthRpc = ""
require.ErrorIs(t, config.Check(), ErrMissingL1EthRPC) require.ErrorIs(t, config.Check(), ErrMissingL1EthRPC)
config.L1EthRpc = validL1EthRpc config.L1EthRpc = validL1EthRpc
...@@ -46,7 +46,7 @@ func TestL1EthRpcRequired(t *testing.T) { ...@@ -46,7 +46,7 @@ func TestL1EthRpcRequired(t *testing.T) {
} }
func TestGameAddressRequired(t *testing.T) { func TestGameAddressRequired(t *testing.T) {
config := validConfig(fault.TraceTypeCannon) config := validConfig(flags.TraceTypeCannon)
config.GameAddress = common.Address{} config.GameAddress = common.Address{}
require.ErrorIs(t, config.Check(), ErrMissingGameAddress) require.ErrorIs(t, config.Check(), ErrMissingGameAddress)
config.GameAddress = validGameAddress config.GameAddress = validGameAddress
...@@ -54,7 +54,7 @@ func TestGameAddressRequired(t *testing.T) { ...@@ -54,7 +54,7 @@ func TestGameAddressRequired(t *testing.T) {
} }
func TestAlphabetTraceRequired(t *testing.T) { func TestAlphabetTraceRequired(t *testing.T) {
config := validConfig(fault.TraceTypeAlphabet) config := validConfig(flags.TraceTypeAlphabet)
config.AlphabetTrace = "" config.AlphabetTrace = ""
require.ErrorIs(t, config.Check(), ErrMissingAlphabetTrace) require.ErrorIs(t, config.Check(), ErrMissingAlphabetTrace)
config.AlphabetTrace = validAlphabetTrace config.AlphabetTrace = validAlphabetTrace
...@@ -62,7 +62,7 @@ func TestAlphabetTraceRequired(t *testing.T) { ...@@ -62,7 +62,7 @@ func TestAlphabetTraceRequired(t *testing.T) {
} }
func TestCannonTraceRequired(t *testing.T) { func TestCannonTraceRequired(t *testing.T) {
config := validConfig(fault.TraceTypeCannon) config := validConfig(flags.TraceTypeCannon)
config.CannonDatadir = "" config.CannonDatadir = ""
require.ErrorIs(t, config.Check(), ErrMissingCannonDatadir) require.ErrorIs(t, config.Check(), ErrMissingCannonDatadir)
config.CannonDatadir = validCannonDatadir config.CannonDatadir = validCannonDatadir
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/ethereum-optimism/optimism/op-challenger/fault"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
) )
...@@ -77,5 +76,3 @@ func (p *CannonTraceProvider) loadProof(i uint64) (*proofData, error) { ...@@ -77,5 +76,3 @@ func (p *CannonTraceProvider) loadProof(i uint64) (*proofData, error) {
} }
return &proof, nil return &proof, nil
} }
var _ fault.TraceProvider = (*CannonTraceProvider)(nil)
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/ethereum-optimism/optimism/op-bindings/bindings" "github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-challenger/config" "github.com/ethereum-optimism/optimism/op-challenger/config"
"github.com/ethereum-optimism/optimism/op-challenger/fault/cannon" "github.com/ethereum-optimism/optimism/op-challenger/fault/cannon"
"github.com/ethereum-optimism/optimism/op-challenger/flags"
"github.com/ethereum-optimism/optimism/op-service/txmgr" "github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum-optimism/optimism/op-service/txmgr/metrics" "github.com/ethereum-optimism/optimism/op-service/txmgr/metrics"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
...@@ -53,9 +54,9 @@ func NewService(ctx context.Context, logger log.Logger, cfg *config.Config) (*se ...@@ -53,9 +54,9 @@ func NewService(ctx context.Context, logger log.Logger, cfg *config.Config) (*se
var trace TraceProvider var trace TraceProvider
switch cfg.TraceType { switch cfg.TraceType {
case TraceTypeCannon: case flags.TraceTypeCannon:
trace = cannon.NewCannonTraceProvider(cfg.CannonDatadir) trace = cannon.NewCannonTraceProvider(cfg.CannonDatadir)
case TraceTypeAlphabet: case flags.TraceTypeAlphabet:
trace = NewAlphabetProvider(cfg.AlphabetTrace, uint64(cfg.GameDepth)) trace = NewAlphabetProvider(cfg.AlphabetTrace, uint64(cfg.GameDepth))
default: default:
return nil, fmt.Errorf("unsupported trace type: %v", cfg.TraceType) return nil, fmt.Errorf("unsupported trace type: %v", cfg.TraceType)
......
...@@ -3,7 +3,6 @@ package fault ...@@ -3,7 +3,6 @@ package fault
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
...@@ -20,36 +19,6 @@ const ( ...@@ -20,36 +19,6 @@ const (
GameStatusDefenderWon GameStatusDefenderWon
) )
type TraceType string
const (
TraceTypeAlphabet TraceType = "alphabet"
TraceTypeCannon TraceType = "cannon"
)
var TraceTypes = []TraceType{TraceTypeAlphabet, TraceTypeCannon}
func (t TraceType) String() string {
return string(t)
}
func (t *TraceType) Set(value string) error {
if !ValidTraceType(TraceType(value)) {
return fmt.Errorf("unknown trace type: %q", value)
}
*t = TraceType(value)
return nil
}
func ValidTraceType(value TraceType) bool {
for _, t := range TraceTypes {
if t == value {
return true
}
}
return false
}
// StepCallData encapsulates the data needed to perform a step. // StepCallData encapsulates the data needed to perform a step.
type StepCallData struct { type StepCallData struct {
ClaimIndex uint64 ClaimIndex uint64
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/ethereum-optimism/optimism/op-challenger/fault"
openum "github.com/ethereum-optimism/optimism/op-service/enum" openum "github.com/ethereum-optimism/optimism/op-service/enum"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
...@@ -21,6 +20,36 @@ func prefixEnvVars(name string) []string { ...@@ -21,6 +20,36 @@ func prefixEnvVars(name string) []string {
return opservice.PrefixEnvVar(envVarPrefix, name) return opservice.PrefixEnvVar(envVarPrefix, name)
} }
type TraceType string
const (
TraceTypeAlphabet TraceType = "alphabet"
TraceTypeCannon TraceType = "cannon"
)
var TraceTypes = []TraceType{TraceTypeAlphabet, TraceTypeCannon}
func (t TraceType) String() string {
return string(t)
}
func (t *TraceType) Set(value string) error {
if !ValidTraceType(TraceType(value)) {
return fmt.Errorf("unknown trace type: %q", value)
}
*t = TraceType(value)
return nil
}
func ValidTraceType(value TraceType) bool {
for _, t := range TraceTypes {
if t == value {
return true
}
}
return false
}
var ( var (
// Required Flags // Required Flags
L1EthRpcFlag = &cli.StringFlag{ L1EthRpcFlag = &cli.StringFlag{
...@@ -35,10 +64,10 @@ var ( ...@@ -35,10 +64,10 @@ var (
} }
TraceTypeFlag = &cli.GenericFlag{ TraceTypeFlag = &cli.GenericFlag{
Name: "trace-type", Name: "trace-type",
Usage: "The trace type. Valid options: " + openum.EnumString(fault.TraceTypes), Usage: "The trace type. Valid options: " + openum.EnumString(TraceTypes),
EnvVars: prefixEnvVars("TRACE_TYPE"), EnvVars: prefixEnvVars("TRACE_TYPE"),
Value: func() *fault.TraceType { Value: func() *TraceType {
out := fault.TraceType("") // No default value out := TraceType("") // No default value
return &out return &out
}(), }(),
} }
...@@ -96,18 +125,18 @@ func CheckRequired(ctx *cli.Context) error { ...@@ -96,18 +125,18 @@ func CheckRequired(ctx *cli.Context) error {
return fmt.Errorf("flag %s is required", f.Names()[0]) return fmt.Errorf("flag %s is required", f.Names()[0])
} }
} }
gameType := fault.TraceType(strings.ToLower(ctx.String(TraceTypeFlag.Name))) gameType := TraceType(strings.ToLower(ctx.String(TraceTypeFlag.Name)))
switch gameType { switch gameType {
case fault.TraceTypeCannon: case TraceTypeCannon:
if !ctx.IsSet(CannonDatadirFlag.Name) { if !ctx.IsSet(CannonDatadirFlag.Name) {
return fmt.Errorf("flag %s is required", "cannon-datadir") return fmt.Errorf("flag %s is required", "cannon-datadir")
} }
case fault.TraceTypeAlphabet: case TraceTypeAlphabet:
if !ctx.IsSet(AlphabetFlag.Name) { if !ctx.IsSet(AlphabetFlag.Name) {
return fmt.Errorf("flag %s is required", "alphabet") return fmt.Errorf("flag %s is required", "alphabet")
} }
default: default:
return fmt.Errorf("invalid trace type. must be one of %v", fault.TraceTypes) return fmt.Errorf("invalid trace type. must be one of %v", TraceTypes)
} }
return nil return nil
} }
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