-
Notifications
You must be signed in to change notification settings - Fork 3k
/
forecast.go
138 lines (120 loc) · 4.71 KB
/
forecast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package mon
import (
"context"
"errors"
"fmt"
"github.com/ethereum-optimism/optimism/op-challenger/game/types"
"github.com/ethereum-optimism/optimism/op-dispute-mon/metrics"
"github.com/ethereum-optimism/optimism/op-dispute-mon/mon/resolution"
"github.com/ethereum-optimism/optimism/op-dispute-mon/mon/transform"
monTypes "github.com/ethereum-optimism/optimism/op-dispute-mon/mon/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
)
var (
ErrRootAgreement = errors.New("failed to check root agreement")
)
type OutputValidator interface {
CheckRootAgreement(ctx context.Context, l1HeadNum uint64, l2BlockNum uint64, root common.Hash) (bool, common.Hash, error)
}
type ForecastMetrics interface {
RecordClaimResolutionDelayMax(delay float64)
RecordGameAgreement(status metrics.GameAgreementStatus, count int)
}
type forecast struct {
logger log.Logger
metrics ForecastMetrics
validator OutputValidator
}
func newForecast(logger log.Logger, metrics ForecastMetrics, validator OutputValidator) *forecast {
return &forecast{
logger: logger,
metrics: metrics,
validator: validator,
}
}
func (f *forecast) Forecast(ctx context.Context, games []*monTypes.EnrichedGameData) {
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)
}
}
f.recordBatch(batch)
}
func (f *forecast) recordBatch(batch monTypes.ForecastBatch) {
f.metrics.RecordGameAgreement(metrics.AgreeDefenderWins, batch.AgreeDefenderWins)
f.metrics.RecordGameAgreement(metrics.DisagreeDefenderWins, batch.DisagreeDefenderWins)
f.metrics.RecordGameAgreement(metrics.AgreeChallengerWins, batch.AgreeChallengerWins)
f.metrics.RecordGameAgreement(metrics.DisagreeChallengerWins, batch.DisagreeChallengerWins)
f.metrics.RecordGameAgreement(metrics.AgreeChallengerAhead, batch.AgreeChallengerAhead)
f.metrics.RecordGameAgreement(metrics.DisagreeChallengerAhead, batch.DisagreeChallengerAhead)
f.metrics.RecordGameAgreement(metrics.AgreeDefenderAhead, batch.AgreeDefenderAhead)
f.metrics.RecordGameAgreement(metrics.DisagreeDefenderAhead, batch.DisagreeDefenderAhead)
}
func (f *forecast) forecastGame(ctx context.Context, game *monTypes.EnrichedGameData, metrics *monTypes.ForecastBatch) error {
// Check the root agreement.
agreement, expected, err := f.validator.CheckRootAgreement(ctx, game.L1HeadNum, game.L2BlockNumber, game.RootClaim)
if err != nil {
return fmt.Errorf("%w: %w", ErrRootAgreement, err)
}
expectedResult := types.GameStatusDefenderWon
if !agreement {
expectedResult = types.GameStatusChallengerWon
}
if game.Status != types.GameStatusInProgress {
if game.Status != expectedResult {
f.logger.Error("Unexpected game result",
"game", game.Proxy, "blockNum", game.L2BlockNumber,
"expectedResult", expectedResult, "actualResult", game.Status,
"rootClaim", game.RootClaim, "correctClaim", expected)
}
switch game.Status {
case types.GameStatusDefenderWon:
if agreement {
metrics.AgreeDefenderWins++
} else {
metrics.DisagreeDefenderWins++
}
case types.GameStatusChallengerWon:
if agreement {
metrics.AgreeChallengerWins++
} else {
metrics.DisagreeChallengerWins++
}
}
return nil
}
// Create the bidirectional tree of claims.
tree := transform.CreateBidirectionalTree(game.Claims)
// Compute the resolution status of the game.
forecastStatus := resolution.Resolve(tree)
if agreement {
// If we agree with the output root proposal, the Defender should win, defending that claim.
if forecastStatus == types.GameStatusChallengerWon {
metrics.AgreeChallengerAhead++
f.logger.Warn("Forecasting unexpected game result", "status", forecastStatus,
"game", game.Proxy, "blockNum", game.L2BlockNumber,
"rootClaim", game.RootClaim, "expected", expected)
} else {
metrics.AgreeDefenderAhead++
f.logger.Debug("Forecasting expected game result", "status", forecastStatus,
"game", game.Proxy, "blockNum", game.L2BlockNumber,
"rootClaim", game.RootClaim, "expected", expected)
}
} else {
// If we disagree with the output root proposal, the Challenger should win, challenging that claim.
if forecastStatus == types.GameStatusDefenderWon {
metrics.DisagreeDefenderAhead++
f.logger.Warn("Forecasting unexpected game result", "status", forecastStatus,
"game", game.Proxy, "blockNum", game.L2BlockNumber,
"rootClaim", game.RootClaim, "expected", expected)
} else {
metrics.DisagreeChallengerAhead++
f.logger.Debug("Forecasting expected game result", "status", forecastStatus,
"game", game.Proxy, "blockNum", game.L2BlockNumber,
"rootClaim", game.RootClaim, "expected", expected)
}
}
return nil
}