Commit 6483bb21 authored by Adrian Sutton's avatar Adrian Sutton Committed by GitHub

op-challenger: Improve formatting of list-claims and list-games (#9923)

* op-challenger: Format list-claims output better

* op-challenger: Format list-games output better
parent 99a53381
...@@ -3,14 +3,18 @@ package main ...@@ -3,14 +3,18 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"math/big"
"strconv"
"github.com/ethereum-optimism/optimism/op-challenger/flags" "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"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
opservice "github.com/ethereum-optimism/optimism/op-service" opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/dial" "github.com/ethereum-optimism/optimism/op-service/dial"
oplog "github.com/ethereum-optimism/optimism/op-service/log" 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"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock" "github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
"github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
...@@ -63,7 +67,7 @@ func listClaims(ctx context.Context, game *contracts.FaultDisputeGameContract) e ...@@ -63,7 +67,7 @@ func listClaims(ctx context.Context, game *contracts.FaultDisputeGameContract) e
if err != nil { if err != nil {
return fmt.Errorf("failed to retrieve status: %w", err) return fmt.Errorf("failed to retrieve status: %w", err)
} }
_, l2BlockNum, err := game.GetBlockRange(ctx) l2StartBlockNum, l2BlockNum, err := game.GetBlockRange(ctx)
if err != nil { if err != nil {
return fmt.Errorf("failed to retrieve status: %w", err) return fmt.Errorf("failed to retrieve status: %w", err)
} }
...@@ -73,14 +77,44 @@ func listClaims(ctx context.Context, game *contracts.FaultDisputeGameContract) e ...@@ -73,14 +77,44 @@ func listClaims(ctx context.Context, game *contracts.FaultDisputeGameContract) e
return fmt.Errorf("failed to retrieve claims: %w", err) return fmt.Errorf("failed to retrieve claims: %w", err)
} }
info := fmt.Sprintf("Claim count: %v\n", len(claims)) // 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
gameState := types.NewGameState(claims, maxDepth)
lineFormat := "%3v %-7v %6v %5v %14v %-66v %-42v %-42v\n"
info := fmt.Sprintf(lineFormat, "Idx", "Move", "Parent", "Depth", "Index", "Value", "Claimant", "Countered By")
for i, claim := range claims { for i, claim := range claims {
pos := claim.Position pos := claim.Position
info = info + fmt.Sprintf("%v - Position: %v, Depth: %v, IndexAtDepth: %v Trace Index: %v, Value: %v, Countered: %v, ParentIndex: %v\n", parent := strconv.Itoa(claim.ParentContractIndex)
i, pos.ToGIndex(), pos.Depth(), pos.IndexAtDepth(), pos.TraceIndex(maxDepth), claim.Value.Hex(), claim.CounteredBy, claim.ParentContractIndex) if claim.IsRoot() {
parent = ""
}
countered := claim.CounteredBy.Hex()
if claim.CounteredBy == (common.Address{}) {
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)
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)
}
}
info = info + fmt.Sprintf(lineFormat,
i, move, parent, pos.Depth(), traceIdx, claim.Value.Hex(), claim.Claimant, countered)
} }
fmt.Printf("Status: %v - L2 Block: %v - Split Depth: %v - Max Depth: %v:\n%v\n", fmt.Printf("Status: %v • L2 Blocks: %v to %v • Split Depth: %v • Max Depth: %v • Claim Count: %v\n%v\n",
status, l2BlockNum, splitDepth, maxDepth, info) status, l2StartBlockNum, l2BlockNum, splitDepth, maxDepth, len(claims), info)
return nil return nil
} }
......
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"github.com/ethereum-optimism/optimism/op-service/dial" "github.com/ethereum-optimism/optimism/op-service/dial"
oplog "github.com/ethereum-optimism/optimism/op-service/log" 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"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
...@@ -52,6 +53,8 @@ func ListGames(ctx *cli.Context) error { ...@@ -52,6 +53,8 @@ func ListGames(ctx *cli.Context) error {
type gameInfo struct { type gameInfo struct {
types.GameMetadata types.GameMetadata
claimCount uint64 claimCount uint64
l2BlockNum uint64
rootClaim common.Hash
status types.GameStatus status types.GameStatus
err error err error
} }
...@@ -75,27 +78,32 @@ func listGames(ctx context.Context, caller *batching.MultiCaller, factory *contr ...@@ -75,27 +78,32 @@ func listGames(ctx context.Context, caller *batching.MultiCaller, factory *contr
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
claimCount, err := gameContract.GetClaimCount(ctx) _, l2BlockNum, rootClaim, status, _, err := gameContract.GetGameMetadata(ctx, rpcblock.ByHash(block))
if err != nil { if err != nil {
info.err = fmt.Errorf("failed to retrieve claim count for game %v: %w", gameProxy, err) info.err = fmt.Errorf("failed to retrieve metadata for game %v: %w", gameProxy, err)
return return
} }
info.claimCount = claimCount info.status = status
status, err := gameContract.GetStatus(ctx) info.l2BlockNum = l2BlockNum
info.rootClaim = rootClaim
claimCount, err := gameContract.GetClaimCount(ctx)
if err != nil { if err != nil {
info.err = fmt.Errorf("failed to retrieve status for game %v: %w", gameProxy, err) info.err = fmt.Errorf("failed to retrieve claim count for game %v: %w", gameProxy, err)
return return
} }
info.status = status info.claimCount = claimCount
}() }()
} }
wg.Wait() wg.Wait()
lineFormat := "%3v %-42v %4v %-21v %14v %-66v %6v %-14v\n"
fmt.Printf(lineFormat, "Idx", "Game", "Type", "Created (Local)", "L2 Block", "Output Root", "Claims", "Status")
for idx, game := range infos { for idx, game := range infos {
if game.err != nil { if game.err != nil {
return err return err
} }
fmt.Printf("%v Game: %v Type: %v Created: %v Claims: %v Status: %v\n", created := time.Unix(int64(game.Timestamp), 0).Format(time.DateTime)
idx, game.Proxy, game.GameType, time.Unix(int64(game.Timestamp), 0), game.claimCount, game.status) fmt.Printf(lineFormat,
idx, game.Proxy, game.GameType, created, game.l2BlockNum, game.rootClaim, game.claimCount, game.status)
} }
return nil return nil
} }
......
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