Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
a48ed5b8
Commit
a48ed5b8
authored
May 19, 2023
by
Andreas Bigger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-challenger types and enum string refactor
parent
39aab993
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
233 additions
and
3 deletions
+233
-3
game_type.go
op-challenger/types/game_type.go
+73
-0
game_type_test.go
op-challenger/types/game_type_test.go
+83
-0
flags.go
op-node/flags/flags.go
+2
-1
flags.go
op-program/host/flags/flags.go
+2
-2
enum.go
op-service/enum/enum.go
+36
-0
enum_test.go
op-service/enum/enum_test.go
+37
-0
No files found.
op-challenger/types/game_type.go
0 → 100644
View file @
a48ed5b8
package
types
import
(
"fmt"
"github.com/ethereum-optimism/optimism/op-service/enum"
)
// GameType is the type of dispute game
type
GameType
uint8
// DefaultGameType returns the default dispute game type.
func
DefaultGameType
()
GameType
{
return
AttestationDisputeGameType
}
// String returns the string value of a dispute game type.
func
(
g
GameType
)
String
()
string
{
return
DisputeGameTypes
[
g
]
}
const
(
// AttestationDisputeGameType is the uint8 enum value for the attestation dispute game
AttestationDisputeGameType
GameType
=
iota
// FaultDisputeGameType is the uint8 enum value for the fault dispute game
FaultDisputeGameType
// ValidityDisputeGameType is the uint8 enum value for the validity dispute game
ValidityDisputeGameType
)
// DisputeGameTypes is a list of dispute game types.
var
DisputeGameTypes
=
[]
string
{
"attestation"
,
"fault"
,
"validity"
}
// Valid returns true if the game type is within the valid range.
func
(
g
GameType
)
Valid
()
bool
{
return
g
>=
AttestationDisputeGameType
&&
g
<=
ValidityDisputeGameType
}
// DisputeGameType is a custom flag type for dispute game type.
type
DisputeGameType
struct
{
Enum
[]
enum
.
Stringered
selected
GameType
}
// NewDisputeGameType returns a new dispute game type.
func
NewDisputeGameType
()
*
DisputeGameType
{
return
&
DisputeGameType
{
Enum
:
enum
.
StringeredList
(
DisputeGameTypes
),
selected
:
DefaultGameType
(),
}
}
// Set sets the dispute game type.
func
(
d
*
DisputeGameType
)
Set
(
value
string
)
error
{
for
i
,
enum
:=
range
d
.
Enum
{
if
enum
.
String
()
==
value
{
d
.
selected
=
GameType
(
i
)
return
nil
}
}
return
fmt
.
Errorf
(
"allowed values are %s"
,
enum
.
EnumString
(
d
.
Enum
))
}
// String returns the selected dispute game type.
func
(
d
DisputeGameType
)
String
()
string
{
return
d
.
selected
.
String
()
}
// Type maps the [DisputeGameType] string value to a [GameType] enum value.
func
(
d
DisputeGameType
)
Type
()
GameType
{
return
d
.
selected
}
op-challenger/types/game_type_test.go
0 → 100644
View file @
a48ed5b8
package
types
import
(
"testing"
"github.com/stretchr/testify/require"
)
var
(
disputeGames
=
[]
struct
{
name
string
gameType
GameType
}{
{
"attestation"
,
AttestationDisputeGameType
},
{
"fault"
,
FaultDisputeGameType
},
{
"validity"
,
ValidityDisputeGameType
},
}
)
// TestDefaultGameType returns the default dispute game type.
func
TestDefaultGameType
(
t
*
testing
.
T
)
{
defaultGameType
:=
disputeGames
[
0
]
.
gameType
require
.
Equal
(
t
,
defaultGameType
,
DefaultGameType
())
}
// TestGameType_Valid tests the Valid function with valid inputs.
func
TestGameType_Valid
(
t
*
testing
.
T
)
{
for
_
,
game
:=
range
disputeGames
{
require
.
True
(
t
,
game
.
gameType
.
Valid
())
}
}
// TestGameType_Invalid tests the Valid function with an invalid input.
func
TestGameType_Invalid
(
t
*
testing
.
T
)
{
invalid
:=
disputeGames
[
len
(
disputeGames
)
-
1
]
.
gameType
+
1
require
.
False
(
t
,
GameType
(
invalid
)
.
Valid
())
}
// FuzzGameType_Invalid checks that invalid game types are correctly
// returned as invalid by the validation [Valid] function.
func
FuzzGameType_Invalid
(
f
*
testing
.
F
)
{
maxCount
:=
len
(
DisputeGameTypes
)
f
.
Fuzz
(
func
(
t
*
testing
.
T
,
number
uint8
)
{
if
number
>=
uint8
(
maxCount
)
{
require
.
False
(
t
,
GameType
(
number
)
.
Valid
())
}
else
{
require
.
True
(
t
,
GameType
(
number
)
.
Valid
())
}
})
}
// TestGameType_Default tests the default value of the DisputeGameType.
func
TestGameType_Default
(
t
*
testing
.
T
)
{
d
:=
NewDisputeGameType
()
require
.
Equal
(
t
,
DefaultGameType
(),
d
.
selected
)
require
.
Equal
(
t
,
DefaultGameType
(),
d
.
Type
())
}
// TestGameType_String tests the Set and String function on the DisputeGameType.
func
TestGameType_String
(
t
*
testing
.
T
)
{
for
_
,
dg
:=
range
disputeGames
{
t
.
Run
(
dg
.
name
,
func
(
t
*
testing
.
T
)
{
d
:=
NewDisputeGameType
()
require
.
Equal
(
t
,
dg
.
name
,
dg
.
gameType
.
String
())
require
.
NoError
(
t
,
d
.
Set
(
dg
.
name
))
require
.
Equal
(
t
,
dg
.
name
,
d
.
String
())
require
.
Equal
(
t
,
dg
.
gameType
,
d
.
selected
)
})
}
}
// TestGameType_Type tests the Type function on the DisputeGameType.
func
TestGameType_Type
(
t
*
testing
.
T
)
{
for
_
,
dg
:=
range
disputeGames
{
t
.
Run
(
dg
.
name
,
func
(
t
*
testing
.
T
)
{
d
:=
NewDisputeGameType
()
require
.
Equal
(
t
,
dg
.
name
,
dg
.
gameType
.
String
())
require
.
NoError
(
t
,
d
.
Set
(
dg
.
name
))
require
.
Equal
(
t
,
dg
.
gameType
,
d
.
Type
())
require
.
Equal
(
t
,
dg
.
gameType
,
d
.
selected
)
})
}
}
op-node/flags/flags.go
View file @
a48ed5b8
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-node/sources"
"github.com/ethereum-optimism/optimism/op-node/sources"
openum
"github.com/ethereum-optimism/optimism/op-service/enum"
oplog
"github.com/ethereum-optimism/optimism/op-service/log"
oplog
"github.com/ethereum-optimism/optimism/op-service/log"
"github.com/urfave/cli"
"github.com/urfave/cli"
...
@@ -68,7 +69,7 @@ var (
...
@@ -68,7 +69,7 @@ var (
L1RPCProviderKind
=
cli
.
GenericFlag
{
L1RPCProviderKind
=
cli
.
GenericFlag
{
Name
:
"l1.rpckind"
,
Name
:
"l1.rpckind"
,
Usage
:
"The kind of RPC provider, used to inform optimal transactions receipts fetching, and thus reduce costs. Valid options: "
+
Usage
:
"The kind of RPC provider, used to inform optimal transactions receipts fetching, and thus reduce costs. Valid options: "
+
EnumString
[
sources
.
RPCProviderKind
]
(
sources
.
RPCProviderKinds
),
openum
.
EnumString
(
sources
.
RPCProviderKinds
),
EnvVar
:
prefixEnvVar
(
"L1_RPC_KIND"
),
EnvVar
:
prefixEnvVar
(
"L1_RPC_KIND"
),
Value
:
func
()
*
sources
.
RPCProviderKind
{
Value
:
func
()
*
sources
.
RPCProviderKind
{
out
:=
sources
.
RPCKindBasic
out
:=
sources
.
RPCKindBasic
...
...
op-program/host/flags/flags.go
View file @
a48ed5b8
...
@@ -7,9 +7,9 @@ import (
...
@@ -7,9 +7,9 @@ import (
"github.com/urfave/cli"
"github.com/urfave/cli"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
nodeflags
"github.com/ethereum-optimism/optimism/op-node/flags"
"github.com/ethereum-optimism/optimism/op-node/sources"
"github.com/ethereum-optimism/optimism/op-node/sources"
service
"github.com/ethereum-optimism/optimism/op-service"
service
"github.com/ethereum-optimism/optimism/op-service"
openum
"github.com/ethereum-optimism/optimism/op-service/enum"
oplog
"github.com/ethereum-optimism/optimism/op-service/log"
oplog
"github.com/ethereum-optimism/optimism/op-service/log"
)
)
...
@@ -74,7 +74,7 @@ var (
...
@@ -74,7 +74,7 @@ var (
L1RPCProviderKind
=
cli
.
GenericFlag
{
L1RPCProviderKind
=
cli
.
GenericFlag
{
Name
:
"l1.rpckind"
,
Name
:
"l1.rpckind"
,
Usage
:
"The kind of RPC provider, used to inform optimal transactions receipts fetching, and thus reduce costs. Valid options: "
+
Usage
:
"The kind of RPC provider, used to inform optimal transactions receipts fetching, and thus reduce costs. Valid options: "
+
nodeflags
.
EnumString
[
sources
.
RPCProviderKind
]
(
sources
.
RPCProviderKinds
),
openum
.
EnumString
(
sources
.
RPCProviderKinds
),
EnvVar
:
service
.
PrefixEnvVar
(
EnvVarPrefix
,
"L1_RPC_KIND"
),
EnvVar
:
service
.
PrefixEnvVar
(
EnvVarPrefix
,
"L1_RPC_KIND"
),
Value
:
func
()
*
sources
.
RPCProviderKind
{
Value
:
func
()
*
sources
.
RPCProviderKind
{
out
:=
sources
.
RPCKindBasic
out
:=
sources
.
RPCKindBasic
...
...
op-
node/flags/util
.go
→
op-
service/enum/enum
.go
View file @
a48ed5b8
package
flags
package
enum
import
(
import
(
"fmt"
"fmt"
"strings"
"strings"
)
)
// Stringered wraps the string type to implement the fmt.Stringer interface.
type
Stringered
string
// String returns the string value.
func
(
s
Stringered
)
String
()
string
{
return
string
(
s
)
}
// StringeredList converts a list of strings to a list of Stringered.
func
StringeredList
(
values
[]
string
)
[]
Stringered
{
var
out
[]
Stringered
for
_
,
v
:=
range
values
{
out
=
append
(
out
,
Stringered
(
v
))
}
return
out
}
// EnumString returns a comma-separated string of the enum values.
// This is primarily used to generate a cli flag.
func
EnumString
[
T
fmt
.
Stringer
](
values
[]
T
)
string
{
func
EnumString
[
T
fmt
.
Stringer
](
values
[]
T
)
string
{
var
out
strings
.
Builder
var
out
strings
.
Builder
for
i
,
v
:=
range
values
{
for
i
,
v
:=
range
values
{
...
...
op-service/enum/enum_test.go
0 → 100644
View file @
a48ed5b8
package
enum
import
(
"testing"
"github.com/stretchr/testify/require"
)
// TestEnumString_MultipleInputs tests the EnumString function with multiple inputs.
func
TestEnumString_MultipleInputs
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
"a, b, c"
,
EnumString
([]
Stringered
{
"a"
,
"b"
,
"c"
}))
}
// TestEnumString_SingleString tests the EnumString function with a single input.
func
TestEnumString_SingleString
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
"a"
,
EnumString
([]
Stringered
{
"a"
}))
}
// TestEnumString_EmptyString tests the EnumString function with no inputs.
func
TestEnumString_EmptyString
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
""
,
EnumString
([]
Stringered
{}))
}
// TestStringeredList_MultipleInputs tests the StringeredList function with multiple inputs.
func
TestStringeredList_MultipleInputs
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
[]
Stringered
{
"a"
,
"b"
,
"c"
},
StringeredList
([]
string
{
"a"
,
"b"
,
"c"
}))
}
// TestStringeredList_SingleString tests the StringeredList function with a single input.
func
TestStringeredList_SingleString
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
[]
Stringered
{
"a"
},
StringeredList
([]
string
{
"a"
}))
}
// TestStringeredList_EmptyString tests the StringeredList function with no inputs.
func
TestStringeredList_EmptyString
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
[]
Stringered
(
nil
),
StringeredList
([]
string
{}))
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment