Commit 2964e9d5 authored by Sabnock01's avatar Sabnock01

move rest of logic into op-service and update tests

parent 2698fe83
......@@ -109,12 +109,41 @@ func ReadCLIConfigWithPrefix(ctx *cli.Context, flagPrefix string) CLIConfig {
}
}
const (
EndpointFlagName = "signer.endpoint"
AddressFlagName = "signer.address"
)
func SignerCLIFlags(envPrefix string) []cli.Flag {
envPrefix += "_SIGNER"
flags := []cli.Flag{
&cli.StringFlag{
Name: EndpointFlagName,
Usage: "Signer endpoint the client will connect to",
EnvVars: opservice.PrefixEnvVar(envPrefix, "ENDPOINT"),
},
&cli.StringFlag{
Name: AddressFlagName,
Usage: "Address the signer is signing transactions for",
EnvVars: opservice.PrefixEnvVar(envPrefix, "ADDRESS"),
},
}
flags = append(flags, CLIFlagsWithFlagPrefix(envPrefix, "signer")...)
return flags
}
type SignerCLIConfig struct {
Endpoint string
Address string
TLSConfig CLIConfig
}
func NewSignerCLIConfig() SignerCLIConfig {
return SignerCLIConfig{
TLSConfig: NewCLIConfig(),
}
}
func (c SignerCLIConfig) Check() error {
if err := c.TLSConfig.Check(); err != nil {
return err
......@@ -131,3 +160,12 @@ func (c SignerCLIConfig) Enabled() bool {
}
return false
}
func ReadSignerCLIConfig(ctx *cli.Context) SignerCLIConfig {
cfg := SignerCLIConfig{
Endpoint: ctx.String(EndpointFlagName),
Address: ctx.String(AddressFlagName),
TLSConfig: ReadCLIConfigWithPrefix(ctx, "signer"),
}
return cfg
}
......@@ -55,3 +55,65 @@ func configForArgs(args ...string) CLIConfig {
_ = app.Run(args)
return config
}
func TestDefaultSignerCLIOptionsMatchDefaultConfig(t *testing.T) {
cfg := signerConfigForArgs()
defaultCfg := NewSignerCLIConfig()
require.Equal(t, defaultCfg, cfg)
}
func TestDefaultSignerConfigIsValid(t *testing.T) {
err := NewSignerCLIConfig().Check()
require.NoError(t, err)
}
func TestInvalidSignerConfig(t *testing.T) {
tests := []struct {
name string
expected string
configChange func(config *SignerCLIConfig)
}{
{
name: "MissingEndpoint",
expected: "signer endpoint and address must both be set or not set",
configChange: func(config *SignerCLIConfig) {
config.Address = "0x1234"
},
},
{
name: "MissingAddress",
expected: "signer endpoint and address must both be set or not set",
configChange: func(config *SignerCLIConfig) {
config.Endpoint = "http://localhost"
},
},
{
name: "InvalidTLSConfig",
expected: "all tls flags must be set if at least one is set",
configChange: func(config *SignerCLIConfig) {
config.TLSConfig.TLSKey = ""
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg := NewSignerCLIConfig()
test.configChange(&cfg)
err := cfg.Check()
require.ErrorContains(t, err, test.expected)
})
}
}
func signerConfigForArgs(args ...string) SignerCLIConfig {
app := cli.NewApp()
app.Flags = CLIFlags("TEST_")
app.Name = "test"
var config SignerCLIConfig
app.Action = func(ctx *cli.Context) error {
config = ReadSignerCLIConfig(ctx)
return nil
}
_ = app.Run(args)
return config
}
......@@ -9,7 +9,7 @@ import (
opservice "github.com/ethereum-optimism/optimism/op-service"
opcrypto "github.com/ethereum-optimism/optimism/op-service/crypto"
"github.com/ethereum-optimism/optimism/op-signer/client"
optls "github.com/ethereum-optimism/optimism/op-service/tls"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
......@@ -120,7 +120,7 @@ func CLIFlags(envPrefix string) []cli.Flag {
Value: defaultReceiptQueryInterval,
EnvVars: prefixEnvVars("TXMGR_RECEIPT_QUERY_INTERVAL"),
},
}, client.CLIFlags(envPrefix)...)
}, optls.SignerCLIFlags(envPrefix)...)
}
type CLIConfig struct {
......@@ -130,7 +130,7 @@ type CLIConfig struct {
SequencerHDPath string
L2OutputHDPath string
PrivateKey string
SignerCLIConfig client.CLIConfig
SignerCLIConfig optls.SignerCLIConfig
NumConfirmations uint64
SafeAbortNonceTooLowCount uint64
ResubmissionTimeout time.Duration
......@@ -150,7 +150,7 @@ func NewCLIConfig(l1RPCURL string) CLIConfig {
TxSendTimeout: defaultTxSendTimeout,
TxNotInMempoolTimeout: defaultTxNotInMempoolTimeout,
ReceiptQueryInterval: defaultReceiptQueryInterval,
SignerCLIConfig: client.NewCLIConfig(),
SignerCLIConfig: optls.NewSignerCLIConfig(),
}
}
......@@ -190,7 +190,7 @@ func ReadCLIConfig(ctx *cli.Context) CLIConfig {
SequencerHDPath: ctx.String(SequencerHDPathFlag.Name),
L2OutputHDPath: ctx.String(L2OutputHDPathFlag.Name),
PrivateKey: ctx.String(PrivateKeyFlagName),
SignerCLIConfig: client.ReadCLIConfig(ctx),
SignerCLIConfig: optls.ReadSignerCLIConfig(ctx),
NumConfirmations: ctx.Uint64(NumConfirmationsFlagName),
SafeAbortNonceTooLowCount: ctx.Uint64(SafeAbortNonceTooLowCountFlagName),
ResubmissionTimeout: ctx.Duration(ResubmissionTimeoutFlagName),
......
package client
import (
"github.com/urfave/cli/v2"
opservice "github.com/ethereum-optimism/optimism/op-service"
optls "github.com/ethereum-optimism/optimism/op-service/tls"
)
const (
EndpointFlagName = "signer.endpoint"
AddressFlagName = "signer.address"
)
func CLIFlags(envPrefix string) []cli.Flag {
envPrefix += "_SIGNER"
flags := []cli.Flag{
&cli.StringFlag{
Name: EndpointFlagName,
Usage: "Signer endpoint the client will connect to",
EnvVars: opservice.PrefixEnvVar(envPrefix, "ENDPOINT"),
},
&cli.StringFlag{
Name: AddressFlagName,
Usage: "Address the signer is signing transactions for",
EnvVars: opservice.PrefixEnvVar(envPrefix, "ADDRESS"),
},
}
flags = append(flags, optls.CLIFlagsWithFlagPrefix(envPrefix, "signer")...)
return flags
}
func NewCLIConfig() optls.SignerCLIConfig {
return optls.SignerCLIConfig{
TLSConfig: optls.NewCLIConfig(),
}
}
func ReadCLIConfig(ctx *cli.Context) optls.SignerCLIConfig {
cfg := optls.SignerCLIConfig{
Endpoint: ctx.String(EndpointFlagName),
Address: ctx.String(AddressFlagName),
TLSConfig: optls.ReadCLIConfigWithPrefix(ctx, "signer"),
}
return cfg
}
package client
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
optls "github.com/ethereum-optimism/optimism/op-service/tls"
)
func TestDefaultCLIOptionsMatchDefaultConfig(t *testing.T) {
cfg := configForArgs()
defaultCfg := NewCLIConfig()
require.Equal(t, defaultCfg, cfg)
}
func TestDefaultConfigIsValid(t *testing.T) {
err := NewCLIConfig().Check()
require.NoError(t, err)
}
func TestInvalidConfig(t *testing.T) {
tests := []struct {
name string
expected string
configChange func(config *optls.SignerCLIConfig)
}{
{
name: "MissingEndpoint",
expected: "signer endpoint and address must both be set or not set",
configChange: func(config *optls.SignerCLIConfig) {
config.Address = "0x1234"
},
},
{
name: "MissingAddress",
expected: "signer endpoint and address must both be set or not set",
configChange: func(config *optls.SignerCLIConfig) {
config.Endpoint = "http://localhost"
},
},
{
name: "InvalidTLSConfig",
expected: "all tls flags must be set if at least one is set",
configChange: func(config *optls.SignerCLIConfig) {
config.TLSConfig.TLSKey = ""
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg := NewCLIConfig()
test.configChange(&cfg)
err := cfg.Check()
require.ErrorContains(t, err, test.expected)
})
}
}
func configForArgs(args ...string) optls.SignerCLIConfig {
app := cli.NewApp()
app.Flags = CLIFlags("TEST_")
app.Name = "test"
var config optls.SignerCLIConfig
app.Action = func(ctx *cli.Context) error {
config = ReadCLIConfig(ctx)
return nil
}
_ = app.Run(args)
return config
}
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