-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathlist_claims.go
203 lines (189 loc) · 6.85 KB
/
list_claims.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"context"
"fmt"
"math/big"
"strconv"
"time"
"github.com/ethereum-optimism/optimism/op-challenger/flags"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/contracts"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/contracts/metrics"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
gameTypes "github.com/ethereum-optimism/optimism/op-challenger/game/types"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/dial"
"github.com/ethereum-optimism/optimism/op-service/eth"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
"github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli/v2"
)
var (
GameAddressFlag = &cli.StringFlag{
Name: "game-address",
Usage: "Address of the fault game contract.",
EnvVars: opservice.PrefixEnvVar(flags.EnvVarPrefix, "GAME_ADDRESS"),
}
VerboseFlag = &cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Verbose output",
EnvVars: opservice.PrefixEnvVar(flags.EnvVarPrefix, "VERBOSE"),
}
)
func ListClaims(ctx *cli.Context) error {
logger, err := setupLogging(ctx)
if err != nil {
return err
}
rpcUrl := ctx.String(flags.L1EthRpcFlag.Name)
if rpcUrl == "" {
return fmt.Errorf("missing %v", flags.L1EthRpcFlag.Name)
}
gameAddr, err := opservice.ParseAddress(ctx.String(GameAddressFlag.Name))
if err != nil {
return err
}
l1Client, err := dial.DialEthClientWithTimeout(ctx.Context, dial.DefaultDialTimeout, logger, rpcUrl)
if err != nil {
return fmt.Errorf("failed to dial L1: %w", err)
}
defer l1Client.Close()
caller := batching.NewMultiCaller(l1Client.Client(), batching.DefaultBatchSize)
contract, err := contracts.NewFaultDisputeGameContract(ctx.Context, metrics.NoopContractMetrics, gameAddr, caller)
if err != nil {
return err
}
return listClaims(ctx.Context, contract, ctx.Bool(VerboseFlag.Name))
}
func listClaims(ctx context.Context, game contracts.FaultDisputeGameContract, verbose bool) error {
metadata, err := game.GetGameMetadata(ctx, rpcblock.Latest)
if err != nil {
return fmt.Errorf("failed to retrieve metadata: %w", err)
}
maxDepth, err := game.GetMaxGameDepth(ctx)
if err != nil {
return fmt.Errorf("failed to retrieve max depth: %w", err)
}
maxClockDuration, err := game.GetMaxClockDuration(ctx)
if err != nil {
return fmt.Errorf("failed to retrieve max clock duration: %w", err)
}
splitDepth, err := game.GetSplitDepth(ctx)
if err != nil {
return fmt.Errorf("failed to retrieve split depth: %w", err)
}
status := metadata.Status
l2StartBlockNum, l2BlockNum, err := game.GetBlockRange(ctx)
if err != nil {
return fmt.Errorf("failed to retrieve status: %w", err)
}
claims, err := game.GetAllClaims(ctx, rpcblock.Latest)
if err != nil {
return fmt.Errorf("failed to retrieve claims: %w", err)
}
var resolutionTime time.Time
if status != gameTypes.GameStatusInProgress {
resolutionTime, err = game.GetResolvedAt(ctx, rpcblock.Latest)
if err != nil {
return fmt.Errorf("failed to retrieve resolved at: %w", err)
}
}
// The top game runs from depth 0 to split depth *inclusive*.
// The - 1 here accounts for the fact that the split depth is included in the top game.
bottomDepth := maxDepth - splitDepth - 1
resolved, err := game.IsResolved(ctx, rpcblock.Latest, claims...)
if err != nil {
return fmt.Errorf("failed to retrieve claim resolution: %w", err)
}
gameState := types.NewGameState(claims, maxDepth)
valueFormat := "%-14v"
if verbose {
valueFormat = "%-66v"
}
now := time.Now()
lineFormat := "%3v %-7v %6v %5v %14v " + valueFormat + " %-42v %12v %-19v %10v %v\n"
info := fmt.Sprintf(lineFormat, "Idx", "Move", "Parent", "Depth", "Index", "Value", "Claimant", "Bond (ETH)", "Time", "Clock Used", "Resolution")
for i, claim := range claims {
pos := claim.Position
parent := strconv.Itoa(claim.ParentContractIndex)
var elapsed time.Duration // Root claim does not accumulate any time on its team's chess clock
if claim.IsRoot() {
parent = ""
} else {
parentClaim, err := gameState.GetParent(claim)
if err != nil {
return fmt.Errorf("failed to retrieve parent claim: %w", err)
}
// Get the total chess clock time accumulated by the team that posted this claim at the time of the claim.
elapsed = gameState.ChessClock(claim.Clock.Timestamp, parentClaim)
}
var countered string
if !resolved[i] {
clock := gameState.ChessClock(now, claim)
resolvableAt := now.Add(maxClockDuration - clock).Format(time.DateTime)
countered = fmt.Sprintf("⏱️ %v", resolvableAt)
} else if claim.IsRoot() && metadata.L2BlockNumberChallenged {
countered = "❌ " + metadata.L2BlockNumberChallenger.Hex()
} else if claim.CounteredBy != (common.Address{}) {
countered = "❌ " + claim.CounteredBy.Hex()
} else {
countered = "✅"
}
move := "Attack"
if gameState.DefendsParent(claim) {
move = "Defend"
}
var traceIdx *big.Int
if claim.Depth() <= splitDepth {
traceIdx = claim.TraceIndex(splitDepth)
} else {
relativePos, err := claim.Position.RelativeToAncestorAtDepth(splitDepth + 1)
if err != nil {
fmt.Printf("Error calculating relative position for claim %v: %v", claim.ContractIndex, err)
traceIdx = big.NewInt(-1)
} else {
traceIdx = relativePos.TraceIndex(bottomDepth)
}
}
value := claim.Value.TerminalString()
if verbose {
value = claim.Value.Hex()
}
timestamp := claim.Clock.Timestamp.Format(time.DateTime)
bond := fmt.Sprintf("%12.8f", eth.WeiToEther(claim.Bond))
if verbose {
bond = fmt.Sprintf("%f", eth.WeiToEther(claim.Bond))
}
info = info + fmt.Sprintf(lineFormat,
i, move, parent, pos.Depth(), traceIdx, value, claim.Claimant, bond, timestamp, elapsed, countered)
}
blockNumChallenger := "Unchallenged"
if metadata.L2BlockNumberChallenged {
blockNumChallenger = "❌ " + metadata.L2BlockNumberChallenger.Hex()
}
statusStr := status.String()
if status != gameTypes.GameStatusInProgress {
statusStr = fmt.Sprintf("%v • Resolution Time: %v", statusStr, resolutionTime.Format(time.DateTime))
}
fmt.Printf("Status: %v • L2 Blocks: %v to %v (%v) • Split Depth: %v • Max Depth: %v • Claim Count: %v\n%v\n",
statusStr, l2StartBlockNum, l2BlockNum, blockNumChallenger, splitDepth, maxDepth, len(claims), info)
return nil
}
func listClaimsFlags() []cli.Flag {
cliFlags := []cli.Flag{
flags.L1EthRpcFlag,
GameAddressFlag,
VerboseFlag,
}
cliFlags = append(cliFlags, oplog.CLIFlags(flags.EnvVarPrefix)...)
return cliFlags
}
var ListClaimsCommand = &cli.Command{
Name: "list-claims",
Usage: "List the claims in a dispute game",
Description: "Lists the claims in a dispute game",
Action: Interruptible(ListClaims),
Flags: listClaimsFlags(),
}