Commit 1316762f authored by refcell's avatar refcell Committed by GitHub

feat(op-dispute-mon): Move types into their own package (#9542)

parent aa3b147c
......@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/ethereum-optimism/optimism/op-challenger/game/types"
monTypes "github.com/ethereum-optimism/optimism/op-dispute-mon/mon/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
......@@ -40,8 +41,8 @@ func newDetector(logger log.Logger, metrics DetectorMetrics, creator GameCallerC
}
func (d *detector) Detect(ctx context.Context, games []types.GameMetadata) {
statBatch := statusBatch{}
detectBatch := detectionBatch{}
statBatch := monTypes.StatusBatch{}
detectBatch := monTypes.DetectionBatch{}
for _, game := range games {
// Fetch the game metadata to ensure the game status is recorded
// regardless of whether the game agreement is checked.
......@@ -58,17 +59,17 @@ func (d *detector) Detect(ctx context.Context, games []types.GameMetadata) {
}
detectBatch.Merge(processed)
}
d.metrics.RecordGamesStatus(statBatch.inProgress, statBatch.defenderWon, statBatch.challengerWon)
d.metrics.RecordGamesStatus(statBatch.InProgress, statBatch.DefenderWon, statBatch.ChallengerWon)
d.recordBatch(detectBatch)
d.logger.Info("Completed updating games", "count", len(games))
}
func (d *detector) recordBatch(batch detectionBatch) {
d.metrics.RecordGameAgreement("in_progress", batch.inProgress)
d.metrics.RecordGameAgreement("agree_defender_wins", batch.agreeDefenderWins)
d.metrics.RecordGameAgreement("disagree_defender_wins", batch.disagreeDefenderWins)
d.metrics.RecordGameAgreement("agree_challenger_wins", batch.agreeChallengerWins)
d.metrics.RecordGameAgreement("disagree_challenger_wins", batch.disagreeChallengerWins)
func (d *detector) recordBatch(batch monTypes.DetectionBatch) {
d.metrics.RecordGameAgreement("in_progress", batch.InProgress)
d.metrics.RecordGameAgreement("agree_defender_wins", batch.AgreeDefenderWins)
d.metrics.RecordGameAgreement("disagree_defender_wins", batch.DisagreeDefenderWins)
d.metrics.RecordGameAgreement("agree_challenger_wins", batch.AgreeChallengerWins)
d.metrics.RecordGameAgreement("disagree_challenger_wins", batch.DisagreeChallengerWins)
}
func (d *detector) fetchGameMetadata(ctx context.Context, game types.GameMetadata) (uint64, common.Hash, types.GameStatus, error) {
......@@ -83,12 +84,12 @@ func (d *detector) fetchGameMetadata(ctx context.Context, game types.GameMetadat
return blockNum, rootClaim, status, nil
}
func (d *detector) checkAgreement(ctx context.Context, addr common.Address, blockNum uint64, rootClaim common.Hash, status types.GameStatus) (detectionBatch, error) {
func (d *detector) checkAgreement(ctx context.Context, addr common.Address, blockNum uint64, rootClaim common.Hash, status types.GameStatus) (monTypes.DetectionBatch, error) {
agree, expectedClaim, err := d.validator.CheckRootAgreement(ctx, blockNum, rootClaim)
if err != nil {
return detectionBatch{}, err
return monTypes.DetectionBatch{}, err
}
batch := detectionBatch{}
batch := monTypes.DetectionBatch{}
batch.Update(status, agree)
if status != types.GameStatusInProgress {
expectedResult := types.GameStatusDefenderWon
......
......@@ -7,6 +7,7 @@ import (
faultTypes "github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
"github.com/ethereum-optimism/optimism/op-challenger/game/types"
monTypes "github.com/ethereum-optimism/optimism/op-dispute-mon/mon/types"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
......@@ -67,45 +68,45 @@ func TestDetector_Detect(t *testing.T) {
func TestDetector_RecordBatch(t *testing.T) {
tests := []struct {
name string
batch detectionBatch
batch monTypes.DetectionBatch
expect func(*testing.T, *mockDetectorMetricer)
}{
{
name: "no games",
batch: detectionBatch{},
batch: monTypes.DetectionBatch{},
expect: func(t *testing.T, metrics *mockDetectorMetricer) {},
},
{
name: "in_progress",
batch: detectionBatch{inProgress: 1},
batch: monTypes.DetectionBatch{InProgress: 1},
expect: func(t *testing.T, metrics *mockDetectorMetricer) {
require.Equal(t, 1, metrics.gameAgreement["in_progress"])
},
},
{
name: "agree_defender_wins",
batch: detectionBatch{agreeDefenderWins: 1},
batch: monTypes.DetectionBatch{AgreeDefenderWins: 1},
expect: func(t *testing.T, metrics *mockDetectorMetricer) {
require.Equal(t, 1, metrics.gameAgreement["agree_defender_wins"])
},
},
{
name: "disagree_defender_wins",
batch: detectionBatch{disagreeDefenderWins: 1},
batch: monTypes.DetectionBatch{DisagreeDefenderWins: 1},
expect: func(t *testing.T, metrics *mockDetectorMetricer) {
require.Equal(t, 1, metrics.gameAgreement["disagree_defender_wins"])
},
},
{
name: "agree_challenger_wins",
batch: detectionBatch{agreeChallengerWins: 1},
batch: monTypes.DetectionBatch{AgreeChallengerWins: 1},
expect: func(t *testing.T, metrics *mockDetectorMetricer) {
require.Equal(t, 1, metrics.gameAgreement["agree_challenger_wins"])
},
},
{
name: "disagree_challenger_wins",
batch: detectionBatch{disagreeChallengerWins: 1},
batch: monTypes.DetectionBatch{DisagreeChallengerWins: 1},
expect: func(t *testing.T, metrics *mockDetectorMetricer) {
require.Equal(t, 1, metrics.gameAgreement["disagree_challenger_wins"])
},
......@@ -163,15 +164,15 @@ func TestDetector_CheckAgreement_Succeeds(t *testing.T) {
name string
rootClaim common.Hash
status types.GameStatus
expectBatch func(*detectionBatch)
expectBatch func(*monTypes.DetectionBatch)
expectErrorLog bool
expectStatus types.GameStatus
err error
}{
{
name: "in_progress",
expectBatch: func(batch *detectionBatch) {
require.Equal(t, 1, batch.inProgress)
expectBatch: func(batch *monTypes.DetectionBatch) {
require.Equal(t, 1, batch.InProgress)
},
},
{
......@@ -179,16 +180,16 @@ func TestDetector_CheckAgreement_Succeeds(t *testing.T) {
rootClaim: mockRootClaim,
status: types.GameStatusDefenderWon,
expectStatus: types.GameStatusDefenderWon,
expectBatch: func(batch *detectionBatch) {
require.Equal(t, 1, batch.agreeDefenderWins)
expectBatch: func(batch *monTypes.DetectionBatch) {
require.Equal(t, 1, batch.AgreeDefenderWins)
},
},
{
name: "disagree_defender_wins",
status: types.GameStatusDefenderWon,
expectStatus: types.GameStatusChallengerWon,
expectBatch: func(batch *detectionBatch) {
require.Equal(t, 1, batch.disagreeDefenderWins)
expectBatch: func(batch *monTypes.DetectionBatch) {
require.Equal(t, 1, batch.DisagreeDefenderWins)
},
expectErrorLog: true,
},
......@@ -197,8 +198,8 @@ func TestDetector_CheckAgreement_Succeeds(t *testing.T) {
rootClaim: mockRootClaim,
status: types.GameStatusChallengerWon,
expectStatus: types.GameStatusDefenderWon,
expectBatch: func(batch *detectionBatch) {
require.Equal(t, 1, batch.agreeChallengerWins)
expectBatch: func(batch *monTypes.DetectionBatch) {
require.Equal(t, 1, batch.AgreeChallengerWins)
},
expectErrorLog: true,
},
......@@ -206,8 +207,8 @@ func TestDetector_CheckAgreement_Succeeds(t *testing.T) {
name: "disagree_challenger_wins",
status: types.GameStatusChallengerWon,
expectStatus: types.GameStatusChallengerWon,
expectBatch: func(batch *detectionBatch) {
require.Equal(t, 1, batch.disagreeChallengerWins)
expectBatch: func(batch *monTypes.DetectionBatch) {
require.Equal(t, 1, batch.DisagreeChallengerWins)
},
},
}
......
......@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/ethereum-optimism/optimism/op-challenger/game/types"
monTypes "github.com/ethereum-optimism/optimism/op-dispute-mon/mon/types"
"github.com/ethereum/go-ethereum/log"
)
......@@ -39,7 +40,7 @@ func newForecast(logger log.Logger, metrics ForecastMetrics, creator GameCallerC
}
func (f *forecast) Forecast(ctx context.Context, games []types.GameMetadata) {
batch := forecastBatch{}
batch := monTypes.ForecastBatch{}
for _, game := range games {
if err := f.forecastGame(ctx, game, &batch); err != nil {
f.logger.Error("Failed to forecast game", "err", err)
......@@ -48,14 +49,14 @@ func (f *forecast) Forecast(ctx context.Context, games []types.GameMetadata) {
f.recordBatch(batch)
}
func (f *forecast) recordBatch(batch forecastBatch) {
func (f *forecast) recordBatch(batch monTypes.ForecastBatch) {
f.metrics.RecordGameAgreement("agree_challenger_ahead", batch.AgreeChallengerAhead)
f.metrics.RecordGameAgreement("disagree_challenger_ahead", batch.DisagreeChallengerAhead)
f.metrics.RecordGameAgreement("agree_defender_ahead", batch.AgreeDefenderAhead)
f.metrics.RecordGameAgreement("disagree_defender_ahead", batch.DisagreeDefenderAhead)
}
func (f *forecast) forecastGame(ctx context.Context, game types.GameMetadata, metrics *forecastBatch) error {
func (f *forecast) forecastGame(ctx context.Context, game types.GameMetadata, metrics *monTypes.ForecastBatch) error {
loader, err := f.creator.CreateContract(game)
if err != nil {
return fmt.Errorf("%w: %w", ErrContractCreation, err)
......
package mon
package types
import (
"github.com/ethereum-optimism/optimism/op-challenger/game/types"
)
type statusBatch struct {
inProgress, defenderWon, challengerWon int
type StatusBatch struct {
InProgress int
DefenderWon int
ChallengerWon int
}
func (s *statusBatch) Add(status types.GameStatus) {
func (s *StatusBatch) Add(status types.GameStatus) {
switch status {
case types.GameStatusInProgress:
s.inProgress++
s.InProgress++
case types.GameStatusDefenderWon:
s.defenderWon++
s.DefenderWon++
case types.GameStatusChallengerWon:
s.challengerWon++
s.ChallengerWon++
}
}
type forecastBatch struct {
type ForecastBatch struct {
AgreeDefenderAhead int
DisagreeDefenderAhead int
AgreeChallengerAhead int
DisagreeChallengerAhead int
}
type detectionBatch struct {
inProgress int
agreeDefenderWins int
disagreeDefenderWins int
agreeChallengerWins int
disagreeChallengerWins int
type DetectionBatch struct {
InProgress int
AgreeDefenderWins int
DisagreeDefenderWins int
AgreeChallengerWins int
DisagreeChallengerWins int
}
func (d *detectionBatch) Update(status types.GameStatus, agree bool) {
func (d *DetectionBatch) Update(status types.GameStatus, agree bool) {
switch status {
case types.GameStatusInProgress:
d.inProgress++
d.InProgress++
case types.GameStatusDefenderWon:
if agree {
d.agreeDefenderWins++
d.AgreeDefenderWins++
} else {
d.disagreeDefenderWins++
d.DisagreeDefenderWins++
}
case types.GameStatusChallengerWon:
if agree {
d.agreeChallengerWins++
d.AgreeChallengerWins++
} else {
d.disagreeChallengerWins++
d.DisagreeChallengerWins++
}
}
}
func (d *detectionBatch) Merge(other detectionBatch) {
d.inProgress += other.inProgress
d.agreeDefenderWins += other.agreeDefenderWins
d.disagreeDefenderWins += other.disagreeDefenderWins
d.agreeChallengerWins += other.agreeChallengerWins
d.disagreeChallengerWins += other.disagreeChallengerWins
func (d *DetectionBatch) Merge(other DetectionBatch) {
d.InProgress += other.InProgress
d.AgreeDefenderWins += other.AgreeDefenderWins
d.DisagreeDefenderWins += other.DisagreeDefenderWins
d.AgreeChallengerWins += other.AgreeChallengerWins
d.DisagreeChallengerWins += other.DisagreeChallengerWins
}
package mon
package types
import (
"fmt"
......@@ -11,24 +11,24 @@ import (
func TestStatusBatch_Add(t *testing.T) {
statusExpectations := []struct {
status types.GameStatus
create func(int) statusBatch
create func(int) StatusBatch
}{
{
status: types.GameStatusInProgress,
create: func(inProgress int) statusBatch {
return statusBatch{inProgress, 0, 0}
create: func(inProgress int) StatusBatch {
return StatusBatch{inProgress, 0, 0}
},
},
{
status: types.GameStatusDefenderWon,
create: func(defenderWon int) statusBatch {
return statusBatch{0, defenderWon, 0}
create: func(defenderWon int) StatusBatch {
return StatusBatch{0, defenderWon, 0}
},
},
{
status: types.GameStatusChallengerWon,
create: func(challengerWon int) statusBatch {
return statusBatch{0, 0, challengerWon}
create: func(challengerWon int) StatusBatch {
return StatusBatch{0, 0, challengerWon}
},
},
}
......@@ -37,7 +37,7 @@ func TestStatusBatch_Add(t *testing.T) {
name string
status types.GameStatus
invocations int
expected statusBatch
expected StatusBatch
}
var tests []test
......@@ -55,7 +55,7 @@ func TestStatusBatch_Add(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
s := statusBatch{}
s := StatusBatch{}
for i := 0; i < test.invocations; i++ {
s.Add(test.status)
}
......@@ -67,30 +67,30 @@ func TestStatusBatch_Add(t *testing.T) {
func TestDetectionBatch_Update(t *testing.T) {
statusExpectations := []struct {
status types.GameStatus
create func(int, bool) detectionBatch
create func(int, bool) DetectionBatch
}{
{
status: types.GameStatusInProgress,
create: func(inProgress int, _ bool) detectionBatch {
return detectionBatch{inProgress, 0, 0, 0, 0}
create: func(inProgress int, _ bool) DetectionBatch {
return DetectionBatch{inProgress, 0, 0, 0, 0}
},
},
{
status: types.GameStatusDefenderWon,
create: func(defenderWon int, agree bool) detectionBatch {
create: func(defenderWon int, agree bool) DetectionBatch {
if agree {
return detectionBatch{0, defenderWon, 0, 0, 0}
return DetectionBatch{0, defenderWon, 0, 0, 0}
}
return detectionBatch{0, 0, defenderWon, 0, 0}
return DetectionBatch{0, 0, defenderWon, 0, 0}
},
},
{
status: types.GameStatusChallengerWon,
create: func(challengerWon int, agree bool) detectionBatch {
create: func(challengerWon int, agree bool) DetectionBatch {
if agree {
return detectionBatch{0, 0, 0, challengerWon, 0}
return DetectionBatch{0, 0, 0, challengerWon, 0}
}
return detectionBatch{0, 0, 0, 0, challengerWon}
return DetectionBatch{0, 0, 0, 0, challengerWon}
},
},
}
......@@ -100,7 +100,7 @@ func TestDetectionBatch_Update(t *testing.T) {
status types.GameStatus
agree bool
invocations int
expected detectionBatch
expected DetectionBatch
}
var tests []test
......@@ -120,7 +120,7 @@ func TestDetectionBatch_Update(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
d := detectionBatch{}
d := DetectionBatch{}
for i := 0; i < test.invocations; i++ {
d.Update(test.status, test.agree)
}
......@@ -132,52 +132,52 @@ func TestDetectionBatch_Update(t *testing.T) {
func TestDetectionBatch_Merge(t *testing.T) {
type test struct {
name string
merge detectionBatch
expected detectionBatch
merge DetectionBatch
expected DetectionBatch
}
tests := []test{
{
name: "Empty",
merge: detectionBatch{},
expected: detectionBatch{},
merge: DetectionBatch{},
expected: DetectionBatch{},
},
{
name: "InProgress",
merge: detectionBatch{1, 0, 0, 0, 0},
expected: detectionBatch{1, 0, 0, 0, 0},
merge: DetectionBatch{1, 0, 0, 0, 0},
expected: DetectionBatch{1, 0, 0, 0, 0},
},
{
name: "AgreeDefenderWins",
merge: detectionBatch{0, 1, 0, 0, 0},
expected: detectionBatch{0, 1, 0, 0, 0},
merge: DetectionBatch{0, 1, 0, 0, 0},
expected: DetectionBatch{0, 1, 0, 0, 0},
},
{
name: "DisagreeDefenderWins",
merge: detectionBatch{0, 0, 1, 0, 0},
expected: detectionBatch{0, 0, 1, 0, 0},
merge: DetectionBatch{0, 0, 1, 0, 0},
expected: DetectionBatch{0, 0, 1, 0, 0},
},
{
name: "AgreeChallengerWins",
merge: detectionBatch{0, 0, 0, 1, 0},
expected: detectionBatch{0, 0, 0, 1, 0},
merge: DetectionBatch{0, 0, 0, 1, 0},
expected: DetectionBatch{0, 0, 0, 1, 0},
},
{
name: "DisagreeChallengerWins",
merge: detectionBatch{0, 0, 0, 0, 1},
expected: detectionBatch{0, 0, 0, 0, 1},
merge: DetectionBatch{0, 0, 0, 0, 1},
expected: DetectionBatch{0, 0, 0, 0, 1},
},
{
name: "All",
merge: detectionBatch{1, 1, 1, 1, 1},
expected: detectionBatch{1, 1, 1, 1, 1},
merge: DetectionBatch{1, 1, 1, 1, 1},
expected: DetectionBatch{1, 1, 1, 1, 1},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
d := detectionBatch{}
d := DetectionBatch{}
d.Merge(test.merge)
require.Equal(t, test.expected, d)
})
......
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